package thyyxxk.webserver.service.zhuyuanyiji;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.zhuyuanyiji.ChargeInputInquiryDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.inpatient.FeeSelect;
import thyyxxk.webserver.entity.inpatient.chargeinputinquiry.AnalyzedInputItems;
import thyyxxk.webserver.entity.inpatient.chargeinputinquiry.ChargeInputItem;
import thyyxxk.webserver.entity.inpatient.chargeinputinquiry.InquiryParams;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChargeInputInquiryService {
    private final ChargeInputInquiryDao dao;

    @Autowired
    public ChargeInputInquiryService(ChargeInputInquiryDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<FeeSelect>> selectCharges(Integer type, String content) {
        content = "%" + content.toUpperCase() + "%";
        List<FeeSelect> list = type == 1 ? dao.selectMedicineCharges(content) : dao.selectProjectCharges(content);
        return ResultVoUtil.success(list);
    }

    public ResultVo<AnalyzedInputItems> selectInputItems(InquiryParams params) {
        if (StringUtil.isBlank(params.getDept())) {
            String dept = dao.selectUserDept(TokenUtil.getInstance().getTokenUserId());
            if (StringUtil.isBlank(dept)) {
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
                        "没有找到您的科室编码，请联系人力资源部确认您的科室信息是否有效。");
            }
        }
        List<ChargeInputItem> raws = params.getType() == 1 ?
                dao.selectMedicineInputItems(params) : dao.selectProjectInputItems(params);
        if (raws.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        AnalyzedInputItems result = new AnalyzedInputItems();
        BigDecimal sumChargeAmount = BigDecimal.ZERO;
        BigDecimal sumChargeFee = BigDecimal.ZERO;
        Map<String, List<ChargeInputItem>> chargeItems = new HashMap<>();
        for (ChargeInputItem raw : raws) {
            sumChargeAmount = sumChargeAmount.add(raw.getChargeAmount());
            sumChargeFee = sumChargeFee.add(raw.getChargeFee());
            String mapKey = raw.getPatNo() + "^" + raw.getTimes() + "^" + raw.getPatName();
            if (chargeItems.containsKey(mapKey)) {
                chargeItems.get(mapKey).add(raw);
                continue;
            }
            List<ChargeInputItem> tempList = new ArrayList<>();
            tempList.add(raw);
            chargeItems.put(mapKey, tempList);
        }
        result.setSumChargeAmount(sumChargeAmount);
        result.setSumChargeFee(sumChargeFee);
        result.setChargeItems(chargeItems);
        return ResultVoUtil.success(result);
    }
}
