package thyyxxk.webserver.service.medicalinsurance.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.Gender;
import thyyxxk.webserver.dao.his.medicalinsurance.SiZyDao;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.*;
import thyyxxk.webserver.service.medicalinsurance.ZyOrJzYbData;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.IdCardUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ZyYbData implements ZyOrJzYbData {
    private final SiZyDao dao;

    @Autowired
    public ZyYbData(SiZyDao dao) {
        this.dao = dao;
    }

    @Override
    public AdmMdtrtinfo getAdmMdtrtinfo(ZyPatientInfo zyptnt) {
        AdmMdtrtinfo admMdtrtinfo = dao.selectAdmMdtrtinfo(zyptnt.getInpatientNo(), zyptnt.getLedgerSn());
        if (null == admMdtrtinfo) {
            throw new BizException("没有查询到此患者的参保信息，请按正确流程操作。");
        }
        if (admMdtrtinfo.getMdtrtCertType().equals("01")) {
            admMdtrtinfo.setMdtrtCertType("02");
        }
        Gender idGender = IdCardUtil.getGenderByIdCard(zyptnt.getSocialNo());
        Gender inputGender = Gender.getGenderByCode(zyptnt.getSex());
        if (idGender != Gender.UNKNOWN && idGender != inputGender) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者HIS性别与证件不符，请检查！");
        }
        Date setlEndTime = dao.selectSetlEndTime(zyptnt.getInpatientNo(), zyptnt.getAdmissTimes());
        if (null != setlEndTime && setlEndTime.after(admMdtrtinfo.getBegntime())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有 " +
                    DateUtil.formatDatetime(setlEndTime, "yyyy-MM-dd") + " 的医保结算条目，不可重复登记。");
        }
        if (StringUtil.isBlank(admMdtrtinfo.getChfpdrName())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "管床医生不能为空！");
        }
        return admMdtrtinfo;
    }

    @Override
    public List<AdmDiseinfo> getAdmDiseinfo(String patNo, int times) {
        List<AdmDiseinfo> admDiseinfos = dao.selectAdmDiags(patNo, times);
        if (null == admDiseinfos || admDiseinfos.isEmpty()) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "请填写医保入院诊断！");
        }
        return admDiseinfos;
    }

    @Override
    public PreSetlmt getPreSetlmt(ZyPatientInfo zyptnt) {
        PreSetlmt preSetlmt = dao.selectPreSetlmt(zyptnt.getInpatientNo(), zyptnt.getAdmissTimes(),
                zyptnt.getLedgerSn(), DateUtil.getTodayEndTime());
        if (null == preSetlmt || StringUtil.isBlank(preSetlmt.getMdtrtId())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息！");
        }
        return preSetlmt;
    }

    @Override
    public Dscginfo getDscginfo(ZyPatientInfo zyptnt) {
        Dscginfo dscginfo;
        if (zyptnt.getMidSetl()) {
            dscginfo = dao.selectDscginfoForMidSetl(zyptnt.getInpatientNo(), zyptnt.getAdmissTimes(), zyptnt.getLedgerSn());
            dscginfo.setEndtime(zyptnt.getZjdzDatetime());
        } else {
            dscginfo = dao.selectDscginfo(zyptnt.getInpatientNo(), zyptnt.getAdmissTimes(), zyptnt.getLedgerSn());
        }
        if (null == dscginfo || StringUtil.isBlank(dscginfo.getMdtrtId())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息！");
        }
        if (StringUtil.isBlank(dscginfo.getDscgDeptCodg())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "出院科室【" +
                    dscginfo.getDscgDeptName() + "】编码不符合医保字典规范，请联系医保科匹配。");
        }
        return dscginfo;
    }

    @Override
    public List<SetlDiseinfo> getSetlDiseinfo(ZyPatientInfo zyptnt) {
        List<SetlDiseinfo> setlDises = dao.selectSetlDises(zyptnt.getInpatientNo(), zyptnt.getAdmissTimes(), zyptnt.getLedgerSn());
        if (null == setlDises || setlDises.isEmpty()) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者医保出院诊断不能为空，请联系医生在病案首页填写。");
        }
        return setlDises;
    }

    @Override
    public PreSetlmt getSetlmt(ZyPatientInfo zyptnt) {
        PreSetlmt setlmt;
        if (zyptnt.getMidSetl()) {
            setlmt = dao.selectPreSetlmtForMidSetl(zyptnt.getInpatientNo(), zyptnt.getAdmissTimes(),
                    zyptnt.getLedgerSn(), zyptnt.getYbRegisterDate(), zyptnt.getZjdzDatetime());
        } else {
            setlmt = dao.selectPreSetlmt(zyptnt.getInpatientNo(), zyptnt.getAdmissTimes(),
                    zyptnt.getLedgerSn(), "2999-12-31 23:59:59.999");
        }
        if (null == setlmt || StringUtil.isBlank(setlmt.getMdtrtId())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息！");
        }
        return setlmt;
    }
}
