package com.xinelu.applet.controller.careplan;


import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xinelu.applet.vo.careplan.PerRecordVo;
import com.xinelu.applet.vo.careplan.PerformanceRecordVo;
import com.xinelu.applet.vo.careplan.PerformanceSchemaVo;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.utils.http.HttpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "一体化照护方案控制器")
@RestController
@RequestMapping("/nurseApp/chronic/careplan")
public class SchemaController {
    @Resource
    private HttpService httpService;

    @Value("${th.url}")
    private String thUrl;

    /**
     * @return
     * @Author mengkuiliang
     * @Description 履约计划
     * @Date 2023-01-09 16:41
     * @Param
     **/
    @ApiOperation("照护方案列表")
    @GetMapping("/schema/list")
    public AjaxResult schemaList(String identity, String year, Integer pageNum, Integer pageSize) {
        StringBuffer params = new StringBuffer();
        params.append("?identity=").append(identity);
        if (!StringUtils.isBlank(year)) {
            params.append("&year=").append(year);
        }
        params.append("&pageSize=").append(pageSize != null && pageSize > 0 ? pageSize : 15);
        params.append("&pageNum=").append(pageNum != null && pageNum > 0 ? pageNum : 1);
        String result = (String) httpService.get(thUrl + "/fdmp/schema/list" + params.toString(), null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!"200".equals(jsonObject.get("code").toString())) {
            return AjaxResult.error(jsonObject.get("msg").toString());
        }
        PageInfo pageInfo = new PageInfo();
        if (jsonObject.get("rows") != null) {
            pageInfo.setTotal(jsonObject.getInteger("total"));
            pageInfo.setList(JSONArray.parseArray(jsonObject.getJSONArray("rows").toJSONString()).toJavaList(PerformanceSchemaVo.class));
        }
        return AjaxResult.success(pageInfo);
    }

    /**
     * @return
     * @Author mengkuiliang
     * @Description 照护记录列表
     * @Date 2023-01-09 16:41
     * @Param
     **/
    @ApiOperation("照护记录列表")
    @GetMapping("/per/list")
    public AjaxResult perList(String identity, String year, Integer pageNum, Integer pageSize) {
        StringBuffer params = new StringBuffer();
        params.append("?identity=").append(identity);
        if (!StringUtils.isBlank(year)) {
            params.append("&year=").append(year);
        }
        params.append("&pageSize=").append(pageSize != null && pageSize > 0 ? pageSize : 15);
        params.append("&pageNum=").append(pageNum != null && pageNum > 0 ? pageNum : 1);
        String result = (String) httpService.get(thUrl + "/fdmp/per/list" + params.toString(), null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!"200".equals(jsonObject.get("code").toString())) {
            return AjaxResult.error(jsonObject.get("msg").toString());
        }
        PageInfo pageInfo = new PageInfo();
        if (jsonObject.get("rows") != null) {
            pageInfo.setTotal(jsonObject.getInteger("total"));
            pageInfo.setList(jsonObject.getJSONArray("rows").toJavaList(PerRecordVo.class));
        }
        return AjaxResult.success(pageInfo);
    }

    /**
     * @return
     * @Author mengkuiliang
     * @Description 获取照护记录详情
     * @Date 2023-01-09 16:41
     * @Param
     **/
    @ApiOperation("照护记录详情")
    @GetMapping("/per/detail/{perRecordId}")
    public AjaxResult detail(@PathVariable String perRecordId) {
        String result = (String) httpService.get(thUrl + "/fdmp/per/detail/" + perRecordId, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!"200".equals(jsonObject.get("code").toString())) {
            return AjaxResult.error(jsonObject.get("msg").toString());
        }
        if (jsonObject.get("data") != null) {
            return AjaxResult.success(JSON.parseObject(jsonObject.getJSONObject("data").toJSONString(), PerformanceRecordVo.class));
        }
        return AjaxResult.error();
    }
}
