package thyyxxk.webserver.controller.zhuyuanyizheng.emr;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.envionment.ApiUrl;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.fluorescenceTest.FluorescenceSpecimenResult;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.ZyPatientInfo;
import thyyxxk.webserver.entity.outpatient.thmz.MzEmrPatientData;
import thyyxxk.webserver.entity.zhuyuanyisheng.ZyZkList;
import thyyxxk.webserver.entity.zhuyuanyisheng.emr.*;
import thyyxxk.webserver.entity.zhuyuanyisheng.jianyanjiancha.YshYjReq;
import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.OpRecord;
import thyyxxk.webserver.service.zhuyuanyisheng.emr.EmrServer;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;
import java.util.Map;

/**
 * 肖蟾
 */
@RequestMapping("/Emr")
@RestController
public class EmrController {
    private final EmrServer server;
    private final ApiUrl apiUrl;

    public EmrController(EmrServer server, ApiUrl apiUrl) {
        this.server = server;
        this.apiUrl = apiUrl;
    }

    @GetMapping("/getPatientDataTree")
    @PassToken
    public ResultVo<List<EmrPatientData>> getPatientDataTree(@RequestParam("patNo") String patNo,
                                                             @RequestParam("times") Integer times) {
        return server.getPatientDataTree(patNo, times);
    }

    @GetMapping("/getSnippetTree")
    public ResultVo<List<JSONObject>> getSnippetTree() {
        return server.getSnippetTree();
    }

    @GetMapping("/getEmrTree")
    public ResultVo<Map<String, List<JSONObject>>> getEmrTree(@RequestParam("type") String type) {
        return server.getEmrTree(type);
    }

    @PostMapping("/insertEmrData")
    public ResultVo<Map<String, Object>> insertEmrData(@RequestBody @Validated EmrPatientData param) {
        return server.insertEmrData(param);
    }

    @GetMapping("/deletePatientEmrByDocumentId")
    public ResultVo<List<String>> deletePatientEmrByDocumentId(@RequestParam("documentId") String documentId) {
        return server.deletePatientEmrByDocumentId(documentId);
    }

    @PostMapping("/queryWhetherThePatientHasASpecifiedMedicalRecord")
    public ResultVo<Boolean> queryWhetherThePatientHasASpecifiedMedicalRecord(@RequestBody EmrPatientData param) {
        return server.queryWhetherThePatientHasASpecifiedMedicalRecord(param);
    }

    @PassToken
    @GetMapping("/hotSearchSorting")
    public void hotSearchSorting(@RequestParam("userCode") String userCode,
                                 @RequestParam("code") String code,
                                 @RequestParam("tableName") String tableName) {
        server.hotSearchSorting(userCode, code, tableName);
    }


    @GetMapping("/submitMedicalRecord")
    public ResultVo<String> submitMedicalRecord(@RequestParam("documentId") String documentId) {
        return server.submitMedicalRecord(documentId);
    }

    @GetMapping("/audit")
    public ResultVo<String> audit(@RequestParam("documentId") String documentId) {
        return server.audit(documentId);
    }

    @GetMapping("/getDrgIntelligentGrouping")
    public ResultVo<String> getDrgIntelligentGrouping(@RequestParam("patNo") String patNo,
                                                      @RequestParam("times") Integer times) {
        return server.getDrgIntelligentGrouping(patNo, times);
    }

    @GetMapping("/getDischargeTimes")
    public ResultVo<Integer> getDischargeTimes(String patNo) {
        return server.getDischargeTimes(patNo);
    }


    @GetMapping("/getExtractDataElement")
    public ResultVo<JSONObject> getExtractDataElement(@RequestParam("patNo") String patNo,
                                                      @RequestParam("times") Integer times) {
        return server.getExtractDataElement(patNo, times);
    }

    @GetMapping("/getAllWards")
    public ResultVo<List<CodeName>> getAllWards() {
        return server.getAllWards();
    }

    @GetMapping("/getDeleteMedicalRecord")
    public ResultVo<List<EmrPatientData>> getDeleteMedicalRecord(@RequestParam("patNo") String patNo) {
        return server.getDeleteMedicalRecord(patNo);
    }

    @GetMapping("/getInvalidByDocumentId")
    public ResultVo<JSONObject> getInvalidByDocumentId(@RequestParam("id") String id) {
        return server.getInvalidByDocumentId(id);
    }

    @GetMapping("/resumeMedicalRecords")
    public ResultVo<String> resumeMedicalRecords(@RequestParam("documentId") String documentId) {
        return server.resumeMedicalRecords(documentId);
    }


    @GetMapping("/getDisReqEmr")
    public ResultVo<List<DischargedEmrModifyApply>> getDisReqEmr(@RequestParam("flag") Integer flag) {
        return server.getDisReqEmr(flag);
    }

    @GetMapping("/downloadTheDischargeEdit")
    public ResultVo<List<DischargedEmrModifyApply>> downloadTheDischargeEdit(@RequestParam("start") String start,
                                                                             @RequestParam("end") String end) {
        return server.downloadTheDischargeEdit(start, end);
    }

    @PostMapping("/saveDisEmrReq")
    public ResultVo<String> saveDisEmrReq(@RequestBody DischargedEmrModifyApply param) {
        return server.saveDisEmrReq(param);
    }

    @GetMapping("/deleteDisEmrDis")
    public ResultVo<String> deleteDisEmrDis(@RequestParam("patNo") String patNo) {
        return server.deleteDisEmrDis(patNo);
    }

    @GetMapping("/adoptEmrDisReq")
    public ResultVo<String> adoptEmrDisReq(@RequestParam("patNo") String patNo) {
        return server.adoptEmrDisReq(patNo);
    }

    @GetMapping("/refuseEmrDisReq")
    public ResultVo<String> refuseEmrDisReq(@RequestParam("patNo") String patNo,
                                            @RequestParam("reviewNotes") String reviewNotes) {
        return server.refuseEmrDisReq(patNo, reviewNotes);
    }

    @GetMapping("/isDisReqEdit")
    public ResultVo<Boolean> isDisReqEdit(String patNo) {
        return server.isDisReqEdit(patNo);
    }

    @GetMapping("/getHistory")
    public ResultVo<Object> getHistory(@RequestParam("documentId") String documentId) {
        return server.getHistory(documentId);
    }

    @GetMapping("/getEmrTips")
    public ResultVo<List<MedicalHistoryPrompts>> getEmrTips() {
        return server.getEmrTips();
    }

    @PostMapping("/addEmrTips")
    public ResultVo<String> addEmrTips(@RequestBody MedicalHistoryPrompts param) {
        return server.addEmrTips(param);
    }

    @PostMapping("/updateEmrTips")
    public ResultVo<String> updateEmrTips(@RequestBody MedicalHistoryPrompts param) {
        return server.updateEmrTips(param);
    }

    @GetMapping("/deleteEmrTips")
    public ResultVo<String> deleteEmrTips(@RequestParam("id") Integer id) {
        return server.deleteEmrTips(id);
    }

    @GetMapping("/getEmrTipsData")
    public ResultVo<List<MedicalHistoryPrompts>> getEmrTipsData(@RequestParam("name") String name) {
        int typeCode = 1;
        if (name.length() > 2) {
            typeCode = getTypeCode(name.substring(0, 2).toUpperCase());
            if (typeCode == 0) {
                typeCode = 1;
            } else {
                name = name.substring(2);
                if (StringUtil.isBlank(name)) {
                    name = "%";
                }
            }
        }
        return server.getEmrTipsData(name, typeCode);
    }

    private int getTypeCode(String name) {
        switch (name) {
            case "G ":
            case "1 ":
                return 1;
            case "K ":
            case "2 ":
                return 2;
            case "Q ":
            case "3 ":
                return 3;
            default:
                return 0;
        }
    }

    @GetMapping("/getListOfDischargedPatients")
    public ResultVo<List<ZyPatientInfo>> getListOfDischargedPatients(@RequestParam("patNo") String patNo) {
        return server.getListOfDischargedPatients(patNo);
    }

    @PostMapping("/getYzTemperature")
    public ResultVo<JSONObject> getYzTemperature(@RequestBody YzTemperatureParam param) {
        return server.getYzTemperature(param);
    }

    @GetMapping("/getExamine")
    public ResultVo<List<YshYjReq>> getExamine(@RequestParam("patNo") String patNo,
                                               @RequestParam("times") Integer times) {
        return server.getExamine(patNo, times);
    }

    @GetMapping("/whetherItExistsInTheDepartment")
    public ResultVo<ZyZkList> whetherItExistsInTheDepartment(@RequestParam("patNo") String patNo,
                                                             @RequestParam("times") Integer times) {
        return server.whetherItExistsInTheDepartment(patNo, times);
    }

    @PostMapping("/electronicMedicalRecordSequencing")
    public ResultVo<String> electronicMedicalRecordSequencing(@RequestBody List<EmrPatientData> data) {
        return server.electronicMedicalRecordSequencing(data);
    }

    @GetMapping("/getPastHistory")
    public ResultVo<Map<String, List<EmrPatientData>>> getPastHistory(@RequestParam("patNo") String patNo,
                                                                      @RequestParam("times") Integer times) {
        return server.getPastHistory(patNo, times);
    }

    @GetMapping("/getMzPatientDataByPatNo")
    public ResultVo<Map<Integer, List<MzEmrPatientData>>> getMzPatientDataByPatNo(String patNo) {
        return server.getMzPatientDataByPatNo(patNo);
    }

    @GetMapping("/getFluorescenceSpecimenResult")
    @PassToken
    public ResultVo<List<FluorescenceSpecimenResult>> getFluorescenceSpecimenResult(@RequestParam("patNo") String patNo,
                                                                                    @RequestParam("times") Integer times) {
        return server.getFluorescenceSpecimenResult(patNo, times);
    }


    @GetMapping("/getOpRecordList")
    public ResultVo<List<OpRecord>> getOpRecordList(@RequestParam("patNo") String patNo,
                                                    @RequestParam("times") Integer times) {
        return server.getOpRecordList(patNo, times);
    }

    @GetMapping("/getEmrToken")
    @PassToken
    public ResultVo<String> getEmrToken() {
        return ResultVoUtil.success(server.getEmrToken());
    }

    @PostMapping("/newDir")
    public ResultVo<String> newDir(@RequestBody EmrPatientData folder) {
        return server.newDir(folder);
    }

    @GetMapping("/delDir")
    public ResultVo<String> delDir(@RequestParam("id") String id) {
        return server.delDir(id);
    }

    @GetMapping("/rename")
    public ResultVo<String> rename(@RequestParam("name") String name,
                                   @RequestParam("documentId") String documentId) {
        return server.rename(name, documentId);
    }

    @GetMapping("/getEmrTemplateUrl")
    public ResultVo<String> getEmrTemplateUrl() {
        String url = apiUrl.getEmrEditorTemplateUrl();
        if (StrUtil.isBlank(url)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未配置电子病历模板编辑地址");
        }
        return ResultVoUtil.success(url);
    }
}
