package com.yihu.figure.controller;

import com.yihu.figure.model.disease.Inspection;
import com.yihu.figure.model.disease.Visit;
import com.yihu.figure.service.DiseaseService;
import com.yihu.figure.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by chenweida on 2017/3/6.
 * 近期疾病
 */
@RestController
@RequestMapping(value = "/disease", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(description = "近期疾病")
public class DiseaseController extends BaseController {
    @Autowired
    private DiseaseService diseaseService;


    /**
     * 根据健康卡号抓取数据某个病人的全部就诊事件列表接口
     * /smjk/GetResidentEventListJson
     *
     * @param strSSID 健康卡号
     * @return
     */
    @ApiOperation(value = "根据健康卡号生成数据某个病人的全部就诊事件")
    @RequestMapping(value = "GetResidentEventListJson", method = RequestMethod.GET)
    public String getResidentEventListJson(
            @ApiParam(name = "strSSID", value = "健康卡号", required = true) @RequestParam(value = "strSSID", required = true) String strSSID,
            @ApiParam(name = "patientCode", value = "患者code", required = true) @RequestParam(value = "patientCode", required = true) String patientCode) {
        try {
            diseaseService.getResidentEventListJson(strSSID,patientCode);
            return success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }

    /**
     * 根据健康卡号抓取数据某个病人的全部检查检验事件
     * /smjk/GetResidentEventListJson
     *
     * @param strSSID 健康卡号
     * @return
     */
    @ApiOperation(value = "根据健康卡号抓取数据某个病人的全部检查检验事件")
    @RequestMapping(value = "GetRecordListByCatalogcodesJson", method = RequestMethod.GET)
    public String GetRecordListByCatalogcodesJson(
            @ApiParam(name = "strSSID", value = "健康卡号", required = true) @RequestParam(value = "strSSID", required = true) String strSSID,
            @ApiParam(name = "patientCode", value = "患者code", required = true) @RequestParam(value = "patientCode", required = true) String patientCode) {
        try {
            diseaseService.GetRecordListByCatalogcodesJson(strSSID,patientCode);
            return success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }


    @ApiOperation(value = "根据code查询患者的就诊事件")
    @RequestMapping(value = "getVisits", method = RequestMethod.GET)
    public String getVisits(
            @ApiParam(name = "patientCode", value = "患者code", required = true) @RequestParam(value = "patientCode", required = true) String patientCode,
            @ApiParam(name = "icdName", value = "主诊断(高血压或者糖尿病)", required = false) @RequestParam(value = "icdName", required = false) String icdName,
            @ApiParam(name = "time", value = "时间", required = false) @RequestParam(value = "time", required = false) String time) {
        try {
            List<Visit> visitList = diseaseService.getVisits(patientCode,time,icdName);
            JSONArray ja=new JSONArray();
            visitList.stream().forEach( v->{
                JSONObject jo=new JSONObject();
                jo.put("orgName",v.getOrgName());
                jo.put("id",v.getId());
                jo.put("diagnosis",v.getDiagnosis());//主诊断结果
                jo.put("typeName",v.getTypeName());//住院或者门诊
                jo.put("time", DateUtil.dateToStr(v.getEndTime(),"yyyy-MM-dd"));
                ja.put(jo);
            });
            return write(200, "获取列表成功！", "list", ja);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }

    @ApiOperation(value = "根据code查询患者的检查检验")
    @RequestMapping(value = "getInspections", method = RequestMethod.GET)
    public String getInspections(
            @ApiParam(name = "patientCode", value = "患者code", required = true) @RequestParam(value = "patientCode", required = true) String patientCode,
            @ApiParam(name = "type", value = "类型(1高血压 2糖尿病)", required = false) @RequestParam(value = "type", required = false) String type,
            @ApiParam(name = "time", value = "时间", required = false) @RequestParam(value = "time", required = false) String time) {
        try {
            List<Inspection> inspections = diseaseService.getInspections(patientCode,time,type);
            JSONArray ja=new JSONArray();
            inspections.stream().forEach( v->{
                JSONObject jo=new JSONObject();
                jo.put("id",v.getId());
                jo.put("orgName",v.getOrgName());
                jo.put("item",v.getItem());
                jo.put("cataLogCodeName",v.getCataLogCodeName());//检查或者检验
                jo.put("time", DateUtil.dateToStr(v.getEndTime(),"yyyy-MM-dd"));
                ja.put(jo);
            });
            return write(200, "获取列表成功！", "list", ja);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }

    @ApiOperation(value = "删除检查检验")
    @RequestMapping(value = "deleteInspections", method = RequestMethod.POST)
    public String deleteInspections(
            @ApiParam(name = "id", value = "检查检验id", required = true) @RequestParam(value = "id", required = true) Long id) {
        try {
            diseaseService.deleteInspections(id);
            return success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }

    @ApiOperation(value = "删除就诊事件")
    @RequestMapping(value = "deleteVisit", method = RequestMethod.POST)
    public String deleteVisit(
            @ApiParam(name = "id", value = "就诊事件id", required = true) @RequestParam(value = "id", required = true) Long id) {
        try {
            diseaseService.deleteVisit(id);
            return success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, e.getMessage());
        }
    }
}
