package thyyxxk.webserver.service.medicalinsurance;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.sidicts.MedType;
import thyyxxk.webserver.dao.his.medicalinsurance.SiAdmVerifyDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.SiAdmissApply;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.entity.inpatient.patient.ZyInYbDiag;
import thyyxxk.webserver.service.hutoolcache.RegionCache;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.service.inpatient.PatientService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

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

/**
 * @author dj
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SiAdmVerifyService {
    private final SiAdmVerifyDao dao;
    private final PatientService ptntSrvc;
    private final UserCache userCache;
    private final RegionCache regionCache;


    public ResultVo<SiAdmissApply> selectAdmissApply(Patient p) {
        MedType medType = MedType.get(p.getMedType());
        if (null == medType) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医疗类别不能为空！");
        }
        if (medType == MedType.GENERAL_HOSPITALIZATION || p.getInpatientNo().startsWith("JT")) {
            SiAdmissApply apply = new SiAdmissApply();
            apply.setStatus(1);
            return ResultVoUtil.success(apply);
        }
        SiAdmissApply apply = dao.selectAdmissApply(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
        if (null == apply) {
            apply = new SiAdmissApply();
            apply.setAdmissDatetime(p.getYbRegisterDate());
        }
        return ResultVoUtil.success(apply);
    }

    public ResultVo<String> submitAdmissApply(SiAdmissApply apply) {
        if (StringUtil.isBlank(apply.getInputComment())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "申请理由不能为空！");
        }
        apply.setStatus(0);
        apply.setInputStaff(TokenUtil.getInstance().getTokenUserId());
        dao.deleteAdmissApply(apply.getPatNo(), apply.getTimes(), apply.getLedgerSn());
        dao.insertAdmissApply(apply);
        return ResultVoUtil.success("提交成功。");
    }

    public ResultVo<List<SiAdmissApply>> selectUnhandledApplies() {
        List<SiAdmissApply> list = dao.selectUnhandledApplies();
        for (SiAdmissApply apply : list) {
            apply.setInputName(userCache.getEmployeeName(apply.getInputStaff()));
            apply.setHandleStaffName(userCache.getEmployeeName(apply.getHandleStaff()));
            apply.setAdmdvsName(regionCache.get(apply.getAdmdvs()));
            apply.setMedTypeName(MedType.getName(apply.getMedType()));
        }
        return ResultVoUtil.success(list);
    }

    public ResultVo<Map<String, Object>> selectPatientInfo(SiAdmissApply apply) {
        ResultVo<Patient> ptntRsvo = ptntSrvc.getPatientInfo(apply.getPatNo());
        if (!Objects.equals(ptntRsvo.getCode(), ExceptionEnum.SUCCESS.getCode())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, ptntRsvo.getMessage());
        }
        Patient patient = ptntRsvo.getData();
        List<ZyInYbDiag> inYbDiags = ptntSrvc.getZyInYbDiags(apply.getPatNo(), apply.getTimes()).getData();
        for (ZyInYbDiag itm : inYbDiags) {
            itm.setOperName(userCache.getEmployeeName(itm.getOperId()));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("apply", apply);
        map.put("patient", patient);
        map.put("indiags", inYbDiags);
        return ResultVoUtil.success(map);
    }

    public ResultVo<String> handleApply(SiAdmissApply apply) {
        if (StringUtil.isBlank(apply.getHandleComment())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "处理意见不能为空！");
        }
        apply.setHandleStaff(TokenUtil.getInstance().getTokenUserId());
        dao.handleApply(apply);
        return ResultVoUtil.success("处理成功。");
    }
}
