package thyyxxk.webserver.service.medicalinsurance;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.medicalinsurance.HospitalApprovalDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.hospitalapproval.PatientFeeItem;
import thyyxxk.webserver.service.hutoolcache.DeptCache;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

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

@Slf4j
@Service
public class HospitalApprovalService {
    private final HospitalApprovalDao dao;
    private final SiZyService zyService;
    private final UserCache userCache;
    private final DeptCache deptCache;

    @Autowired
    public HospitalApprovalService(HospitalApprovalDao dao, SiZyService zyService, UserCache userCache, DeptCache deptCache) {
        this.dao = dao;
        this.zyService = zyService;
        this.userCache = userCache;
        this.deptCache = deptCache;
    }

    public ResultVo<List<PatientFeeItem>> getPatientFeeList(String patNo, Integer times, Integer ledgerSn) {
        return ResultVoUtil.success(dao.getPatientFeeList(patNo, times, ledgerSn));
    }

    public ResultVo<List<PatientFeeItem>> getAppliedItems(String patNo, Integer times, Integer ledgerSn) {
        List<PatientFeeItem> applies = dao.selectPatientApplies(patNo, times, ledgerSn);
        if (applies.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "患者没有已申请医院审批的费用。");
        }
        for (PatientFeeItem item : applies) {
            item.setStaffName(userCache.getEmployeeName(item.getStaffId()));
            item.setHandlerName(userCache.getEmployeeName(item.getApplyHandler()));
        }
        return ResultVoUtil.success(applies);
    }

    public ResultVo<String> applyHospitalApproval(PatientFeeItem feeItem) {
        feeItem.setStaffId(TokenUtil.getInstance().getTokenUserId());
        log.info("申请医院审批：{}", feeItem);
        PatientFeeItem existItem = dao.selectAccurateApply(feeItem.getPatNo(), feeItem.getTimes(),
                feeItem.getLedgerSn(), feeItem.getDetailSn());
        if (null == existItem) {
            dao.insertNewApply(feeItem);
            return ResultVoUtil.success("已发起申请，请等待医保科审核。");
        }
        if (existItem.getApplyStatus() == 2) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此条费用的申请已被医保科驳回："
                    + existItem.getHandleRemark());
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "此条费用已申请过，请勿重复申请。");
    }

    public ResultVo<List<PatientFeeItem>> getAllApplies(String month) {
        String begntime = month + "-01 00:00:00.000";
        String endtime = DateUtil.getMonthEndtime(month);
        List<PatientFeeItem> applies = dao.selectAllApplies(begntime, endtime);
        if (applies.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有已申请医院审批的费用。");
        }
        for (PatientFeeItem item : applies) {
            item.setStaffName(userCache.getEmployeeName(item.getStaffId()));
            item.setHandlerName(userCache.getEmployeeName(item.getApplyHandler()));
            item.setWardName(deptCache.getDeptName(item.getWardName()));
        }
        return ResultVoUtil.success(applies);
    }

    public ResultVo<String> confirmHandleApply(PatientFeeItem feeItem) {
        feeItem.setApplyHandler(TokenUtil.getInstance().getTokenUserId());
        log.info("申审核医院审批：{}", feeItem);
        dao.handleApply(feeItem);
        if (feeItem.getApplyStatus() == 1) {
            String transFlag = dao.selectFeeTransFlag(feeItem.getPatNo(), feeItem.getTimes(),
                    feeItem.getLedgerSn(), feeItem.getDetailSn());
            String medType = dao.selectMedType(feeItem.getPatNo(), feeItem.getTimes(), feeItem.getLedgerSn());
            if (Objects.equals(transFlag, "1") && !Objects.equals(medType, "42")) {
                return zyService.revokeSingleCharge(feeItem.getPatNo(), feeItem.getTimes(),
                        feeItem.getLedgerSn(), feeItem.getDetailSn());
            }
        }
        return ResultVoUtil.success("处理成功。");
    }
}
