package com.xinelu.web.controller.applet;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.enums.FollowupType;
import com.xinelu.common.utils.http.HttpService;
import com.xinelu.common.utils.spring.SpringUtils;
import com.xinelu.familydoctor.applet.pojo.dto.MedicalRecord;
import com.xinelu.familydoctor.applet.pojo.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author mengkuiliang
 * @Description 随访控制器
 * @Date 2023-09-28 028 9:10
 * @Param
 * @return
 **/
@Api(tags = "随访控制器")
@RestController
@RequestMapping("/applet/followup")
public class FollowupController {
    @Resource
    private HttpService httpService;

    @ApiOperation("获取单个居民随访日期列表")
    @GetMapping("/dateList/{phType}/{identity}")
    public R<List<FollowUpRecordDetailVo>> dateList(@PathVariable("phType") String phType, @PathVariable("identity") String identity, @RequestHeader("region") String region) {
        if (phType == null || phType.isEmpty()) {
            return R.fail("请输入正确的phType");
        }
        if (identity == null || identity.isEmpty()) {
            return R.fail("请输入正确的identity");
        }
        String result = (String) httpService.get(SpringUtils.getFdUrl(region) + "/followup/dateList/" + phType + "/" + identity, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("0".equals(jsonObject.get("code"))) {
            return R.fail(jsonObject.get("msg").toString());
        }
        if (jsonObject.get("data") == null) {
            return R.fail();
        }
        JSONArray array = jsonObject.getJSONArray("data");
        List<FollowUpRecordDetailVo> followUpRecordDetailVoList = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            FollowUpRecordDetailVo vo = JSONObject.parseObject(object.toJSONString(), FollowUpRecordDetailVo.class);
            followUpRecordDetailVoList.add(vo);
        }
        return R.ok(followUpRecordDetailVoList);
    }

    @ApiOperation(value = "获取随访详情")
    @GetMapping("/detail/{phType}/{identity}/{id}")
    public R<?> detail(@PathVariable("phType") String phType, @PathVariable("identity") String identity, @PathVariable("id") String objParam, @RequestHeader("region") String region) {
        if (phType == null || phType.isEmpty()) {
            return R.fail("请输入正确的phType");
        }
        if (identity == null || identity.isEmpty()) {
            return R.fail("请输入正确的identity");
        }
        if (objParam == null || objParam.isEmpty()) {
            return R.fail("请输入正确的id");
        }
        String result = (String) httpService.get(SpringUtils.getFdUrl(region) + "/followup/detail/" + phType + "/" + identity + "/" + objParam, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if ("0".equals(jsonObject.get("code"))) {
            return R.fail(jsonObject.get("msg").toString());
        }
        if(!jsonObject.containsKey("data") || jsonObject.getJSONObject("data") == null) {
            return R.ok();
        }
        return R.ok(jsonObject.getJSONObject("data").getJSONObject("record"));

    }

    @ApiOperation("获取公卫体检记录")
    @GetMapping("fetch/record/{identity}")
    public R<List<MedicalRecord>> fetchMedicalRecord(@PathVariable String identity, @RequestHeader("region") String region) {
        if (identity == null || identity.isEmpty()) {
            return R.fail("请输入正确的identity");
        }
        String result = (String) httpService.get(SpringUtils.getFdUrl(region) + "/followup/fetch/record/" + identity, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!"1".equals(jsonObject.get("code"))) {
            return R.fail(jsonObject.get("msg").toString());
        }
        JSONArray array = (JSONArray) jsonObject.get("data");
        if (array == null || array.isEmpty()) {
            return R.ok();
        }
        List<MedicalRecord> medicalRecordVoList = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject object = array.getJSONObject(i);
            MedicalRecord medicalRecordVo = JSONObject.parseObject(object.toJSONString(), MedicalRecord.class);
            medicalRecordVoList.add(medicalRecordVo);
        }
        return R.ok(medicalRecordVoList);
    }

    @ApiOperation("获取体检详情")
    @GetMapping("fetch/detail/{identity}/{id}")
    public R<MedicalDetailVo> fetchMedicalDetail(@PathVariable String identity, @PathVariable String id, @RequestHeader("region") String region) {
        if (identity == null || identity.isEmpty()) {
            return R.fail("请输入正确的identity");
        }
        String result = (String) httpService.get(SpringUtils.getFdUrl(region) + "/followup/fetch/detail/" + identity + "/" + id, null, String.class);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (!"1".equals(jsonObject.get("code"))) {
            return R.fail(jsonObject.get("msg").toString());
        }
        if (!jsonObject.containsKey("data")) {
            return R.ok();
        }
        JSONObject data = jsonObject.getJSONObject("data");
        if (data == null) {
            return R.ok();
        }
        MedicalDetailVo medicalDetailVo = JSONObject.parseObject(data.toJSONString(), MedicalDetailVo.class);
        return R.ok(medicalDetailVo);
    }

}
