package thyyxxk.webserver.controller.medicalinsurance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.medicalinsurance.inpatient.InHspPsnInfo;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.SpcChrDiseAcct;
import thyyxxk.webserver.entity.medicalinsurance.query.*;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.TimesList;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.service.medicalinsurance.SiQueryService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

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

/**
 * @description: 医保查询入口
 * @author: DingJie
 * @create: 2021-06-23 16:39:13
 **/
@RestController
@RequestMapping("/siQuery")
public class SiQueryController {
    private final SiQueryService service;

    @Autowired
    public SiQueryController(SiQueryService service) {
        this.service = service;
    }

    @PostMapping("/getSiPatInfo")
    public ResultVo<SiPatInfo> getSiPatInfo(@RequestBody QryPsnBsInfo qryPsnBsInfo) {
        return service.getSiPatInfo(qryPsnBsInfo);
    }

    @PassToken
    @PostMapping("/fetchPsnNo")
    public ResultVo<String> fetchPsnNo(@RequestBody QryPsnBsInfo qryPsnBsInfo) {
        if (StringUtil.isBlank(qryPsnBsInfo.getReadCardResult())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "读卡内容不能为空，请先读卡！");
        }
        ResultVo<PsnBaseInfo> psnRes = service.obtainBasicPersonInfo(qryPsnBsInfo);
        if (null == psnRes) {
            return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
        }
        if (!Objects.equals(psnRes.getCode(), ExceptionEnum.SUCCESS.getCode())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, psnRes.getMessage());
        }
        PsnBaseInfo psnBaseInfo = psnRes.getData();
        return ResultVoUtil.success(psnBaseInfo.getPsnNo());
    }

    @PassToken
    @PostMapping("/obtainBasicPersonInfo")
    public ResultVo<PsnBaseInfo> obtainBasicPersonInfo(@RequestBody QryPsnBsInfo qryPsnBsInfo) {
        return service.obtainBasicPersonInfo(qryPsnBsInfo);
    }

    @GetMapping("/obtainMedicalInstitutionInfo")
    public ResultVo<List<MedInsInfo>> obtainMedicalInstitutionInfo(@RequestParam("type") String type,
                                                                   @RequestParam("fuzzyName") String fuzzyName) {
        return service.obtainMedicalInstitutionInfo(type, fuzzyName);
    }

    @PostMapping("/queryVisitInfo")
    public ResultVo<List<MdtrtInfo>> queryVisitInfo(@RequestBody BscQryPrm q) {
        return service.queryVisitInfo(q);
    }

    @PostMapping("/queryDiagnosisInfo")
    public ResultVo<List<DiseInfo>> queryDiagnosisInfo(@RequestBody BscQryPrm q) {
        return service.queryDiagnosisInfo(q);
    }

    @PassToken
    @PostMapping("/querySettlementInfo")
    public ResultVo<SetlInfo> querySettlementInfo(@RequestBody BscQryPrm q) {
        return service.querySettlementInfo(q);
    }

    @PostMapping("/queryFeeDetails")
    public ResultVo<Map<String, Object>> queryFeeDetails(@RequestBody QryFeeDetl q) {
        return service.queryFeeDetails(q);
    }

    @PostMapping("/checkPersonTreatment")
    public ResultVo<List<TrtInfo>> checkPersonTreatment(@RequestBody BscQryPrm q) {
        return service.checkPersonTreatment(q);
    }

    @PostMapping("/queryInHospitalPatientsInfo")
    public ResultVo<List<InHspPsnInfo>> queryInHospitalPatientsInfo(@RequestBody BscQryPrm q) {
        return service.queryInHospitalPatientsInfo(q);
    }

    @PostMapping("/queryPersonnelCumulativeInfo")
    public ResultVo<List<CumInfo>> queryPersonnelCumulativeInfo(@RequestBody BscQryPrm q) {
        return service.queryPersonnelCumulativeInfo(q);
    }

    @PostMapping("/queryPersonnelTransferHospitalInfo")
    public ResultVo<List<RefMedin>> queryPersonnelTransferHospitalInfo(@RequestBody BscQryPrm q) {
        return service.queryPersonnelTransferHospitalInfo(q);
    }

    @PostMapping("/querySpecialChronicDiseasesMedicalRecord")
    public ResultVo<List<MedUseRcd>> querySpecialChronicDiseasesMedicalRecord(@RequestBody BscQryPrm q) {
        return service.querySpecialChronicDiseasesMedicalRecord(q);
    }

    @PostMapping("/querySpecialChronicDiseasesAccreditation")
    public ResultVo<List<SpcChrDiseAcct>> querySpecialChronicDiseasesAccreditation(@RequestBody BscQryPrm q) {
        return service.querySpecialChronicDiseasesAccreditation(q);
    }

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

    @PostMapping("/queryPersonnelAssignmentInfo")
    public ResultVo<List<PsnFixMedin>> queryPersonnelAssignmentInfo(@RequestBody BscQryPrm q) {
        return service.queryPersonnelAssignmentInfo(q);
    }

    @PassToken
    @PostMapping("/querySiSetlList")
    public ResultVo querySiSetlList(@RequestBody @Validated SetlIndex index) {
        if (index.getType() == 11) {
            return service.queryOutpatientSetlList(index);
        }
        return service.queryInpatientSetlList(index);
    }

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

    @GetMapping("/fetchSiTimesList")
    public ResultVo<List<TimesList>> fetchSiTimesList(@RequestParam("patNo") String patNo, @RequestParam("type") Integer type) {
        return service.fetchSiTimesList(patNo, type);
    }

    @PostMapping("/selectSetlinfoStatistics")
    public ResultVo<List<SetlStatisticResult>> selectSetlinfoStatistics(@RequestBody @Validated SetlCondition condition) {
        return service.selectSetlinfoStatistics(condition);
    }

    @PostMapping("/selectSetldetailStatistics")
    public ResultVo<List<SiSetlinfo>> selectSetldetailStatistics(@RequestBody @Validated SetlCondition condition) {
        return service.selectSetldetailStatistics(condition);
    }

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

    @GetMapping("/weiJieSuanZaiYuanHuanZhe")
    public ResultVo<List<ZyActpatient>> weiJieSuanZaiYuanHuanZhe(@RequestParam("patNo") String patNo,
                                                                 @RequestParam("startTime") String startTime,
                                                                 @RequestParam("endTime") String endTime,
                                                                 @RequestParam("name") String name,
                                                                 @RequestParam("certno") String certno) {
        return service.weiJieSuanZaiYuanHuanZhe(patNo, startTime, endTime, name, certno);
    }

    @PassToken
    @PostMapping("/saveCumInfo")
    public void saveCumInfo(@RequestBody SiSetlinfo setlinfo) {
        service.saveCumInfo(setlinfo);
    }
}
