package thyyxxk.webserver.controller.outpatient.triage;

import cn.hutool.core.convert.Convert;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.inpatient.chargeinputinquiry.InquiryParams;
import thyyxxk.webserver.entity.outpatient.triage.*;
import thyyxxk.webserver.service.outpatient.triage.TriageService;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

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

@RestController
@RequestMapping("/triage")
public class TriageController {
    private final TriageService service;

    public TriageController(TriageService service) {
        this.service = service;
    }

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

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

    @GetMapping("/getUnTriagedPatients")
    public ResultVo<Map<String, Object>> getUnTriagedPatients() {
        return service.getUnTriagedPatients();
    }

    @GetMapping("/getPatientIdByIdNo")
    public ResultVo<List<String>> getPatientIdByIdNo(@RequestParam("idNo") String idNo) {
        return service.getPatientIdByIdNo(idNo);
    }

    @GetMapping("/getTriagedPatients")
    public ResultVo<List<MzfzPatientOrder>> getTriagedPatients() {
        return service.getTriagedPatients();
    }

    @GetMapping("/getTreatedPatients")
    public ResultVo<List<MzfzPatientOrder>> getTreatedPatients() {
        return service.getTreatedPatients();
    }

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

    @GetMapping("/getRooms")
    public ResultVo<List<MzfzZdDeptRoom>> getDeptRoom(@RequestParam("deptCode") String deptCode) {
        return service.getRooms(deptCode);
    }

    @GetMapping("/hasDoneCovidAssessment")
    public ResultVo<String> hasDoneCovidAssessment(@RequestParam("patientId") String patientId) {
        return service.hasDoneCovidAssessment(patientId);
    }

    @PostMapping("/fenZhen")
    public ResultVo<String> fenZhen(@RequestBody MzfzZdDeptRoom room) {
        return service.fenZhen(room);
    }

    @GetMapping("/fuZhen")
    public ResultVo<Integer> fuZhen(@RequestParam("serialNo") Integer serialNo) {
        return service.fuZhen(serialNo);
    }

    @PassToken
    @GetMapping("/notifyComplete")
    public void notifyComplete(@RequestParam("serialNo") Integer serialNo) {
        service.notifyComplete(serialNo);
    }

    @GetMapping("/cancelTriage")
    public ResultVo<Integer> cancelTriage(@RequestParam("serialNo") Integer serialNo) {
        return service.cancelTriage(serialNo);
    }

    @GetMapping("/refreshBigScreenData")
    public ResultVo<String> refreshBigScreenData() {
        return ResultVoUtil.success(service.refreshBigScreenData());
    }

    @GetMapping("/getBigScreenData")
    public ResultVo<List<FloorScreen>> getNotifiedPatients() {
        return service.getBigScreenData();
    }

    @GetMapping("/getAllPatients")
    public ResultVo<List<MzfzPatientOrder>> getAllPatients(@RequestParam("searchContent") String searchContent) {
        return service.getAllPatients(searchContent);
    }

    @GetMapping("/getMzVitalSigns")
    public ResultVo<MzYshTzxx> getMzVitalSigns(@RequestParam("patientId") String patientId,
                                               @RequestParam("times") Integer times) {
        return service.getMzVitalSigns(patientId, times);
    }

    @PostMapping("/saveMzVitalSigns")
    public ResultVo<String> saveMzVitalSigns(@RequestBody MzYshTzxx param) {
        return service.saveMzVitalSigns(param);
    }

    @PassToken
    @PostMapping("/notify")
    public ResultVo<String> notifyMessage(@RequestBody MessageForPush param) {
        return service.notifyMessage(param);
    }

    @PostMapping("/saveNewCard")
    public ResultVo<String> saveNewCard(@RequestBody Map<String,Object> param) {
        String oldPatientId = Convert.toStr(param.get("oldPatientId"));
        String newPatientId = Convert.toStr(param.get("newPatientId"));
        AssertUtil.isnotBlank(oldPatientId,"旧门诊号不能为空");
        AssertUtil.isnotBlank(newPatientId,"新门诊号不能为空");
        return service.saveNewCard(oldPatientId,newPatientId);
    }

    @PostMapping("/queryCardInfo")
    public ResultVo<Map<String,Object>> queryCardInfo(@RequestBody Map<String,Object> param) {
        String oldPatientId = Convert.toStr(param.get("oldPatientId"));
        String newPatientId = Convert.toStr(param.get("newPatientId"));
        return service.queryCardInfo(oldPatientId,newPatientId);
    }

    @GetMapping("/getEmergencyRegInfo")
    public ResultVo<EmergencyRegInfo> getEmergencyRegInfo(
            @RequestParam("patientId") String patientId, @RequestParam("times") Integer times) {
        EmergencyRegInfo info = service.getEmergencyRegInfo(patientId,times);
        if (null == info) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有找到患者的挂号信息。");
        }
        return ResultVoUtil.success(info);
    }

    @PostMapping("/saveEmergencyRegInfo")
    public ResultVo<Integer> saveEmergencyRegInfo(@RequestBody EmergencyRegInfo info) {
        int res = service.saveEmergencyRegInfo(info);
        if (res > 0) {
            return ResultVoUtil.success(res);
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "保存失败。");
    }

    @GetMapping("/getEmrRegExportData")
    public ResultVo<List<EmergencyRegInfo>> getEmrRegExportData(
            @RequestParam("start") String start, @RequestParam("end") String end) {
        List<EmergencyRegInfo> list = service.getEmrRegExportData(start, end);
        if (list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有可以导出的数据。");
        }
        return ResultVoUtil.success(list);
    }
}
