package cn.hnthyy.thmz.service.impl.his.mz;

import cn.hnthyy.thmz.Utils.SignUtil;
import cn.hnthyy.thmz.entity.his.mz.JcJyItemCharge;
import cn.hnthyy.thmz.entity.his.mz.JyZdItem;
import cn.hnthyy.thmz.entity.his.zd.ZdChargeItem;
import cn.hnthyy.thmz.enums.ReqTypeEnum;
import cn.hnthyy.thmz.mapper.his.mz.JcJyItemChargeMapper;
import cn.hnthyy.thmz.mapper.his.zd.ZdChargeItemMapper;
import cn.hnthyy.thmz.service.his.mz.JcJyItemChargeService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class JcJyItemChargeServiceImpl implements JcJyItemChargeService {
    @SuppressWarnings("all")
    @Autowired
    private JcJyItemChargeMapper jcJyItemChargeMapper;
    @SuppressWarnings("all")
    @Autowired
    private ZdChargeItemMapper zdChargeItemMapper;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;

    @Override
    public JcJyItemCharge queryJcItemChargeByCode(String code, String reqType) {
        return jcJyItemChargeMapper.selectJcItemChargeByCode(code, reqType, ReqTypeEnum.JIAN_CHA.code.equals(reqType) ? "jc_item_charge" : "jy_item_charge");
    }

    @Override
    public List<JcJyItemCharge> queryJcItemChargeByCodeList(List<String> codes, String reqType) {
        return jcJyItemChargeMapper.selectJcItemChargeByCodeList(codes, reqType, ReqTypeEnum.JIAN_CHA.code.equals(reqType) ? "jc_item_charge" : "jy_item_charge");
    }

    @Override
    public List<JyZdItem> queryJcJyItemByCommonParams(String commonParams) {
        if (StringUtils.isBlank(commonParams)) {
            commonParams = null;
        }
        if (SignUtil.isAlpha(commonParams)) {
            commonParams = commonParams.toUpperCase();
        }
        if (StringUtils.isNotBlank(commonParams)) {
            commonParams = "%" + commonParams.toUpperCase() + "%";
        }
        List<JyZdItem> jyZdItemList = jcJyItemChargeMapper.selectJcJyItemByCommonParams(commonParams);
        List<JyZdItem> zlList = zdChargeItemMapper.selectZlByCommonParams(commonParams, null, null,20);
        if(jyZdItemList==null){
            jyZdItemList=zlList;
        }else {
            jyZdItemList.addAll(zlList);
        }
        if (jyZdItemList != null && jyZdItemList.size() > 0) {
            jyZdItemList.stream().forEach(j -> {
                if (StringUtils.isNotBlank(j.getExecUnit())) {
                    j.setExecUnitName(zdUnitCodeService.queryDeptNameByIdInCache(j.getExecUnit()));
                }
            });
        }
        return jyZdItemList;
    }

    @Override
    public List<ZdChargeItem> queryJcJyItemChargeByCode(String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            throw new Exception("诊疗与医技编码为空！");
        }
        List<JcJyItemCharge> jcJyItemChargeList = jcJyItemChargeMapper.selectJcJyItemChargeByCode(code);
        if (jcJyItemChargeList == null || jcJyItemChargeList.size() == 0) {
            return null;
        }
        List<String> chargeCodeLists = jcJyItemChargeList.stream().map(j -> j.getChargeCode()).collect(Collectors.toList());
        Map<String, BigDecimal> map = jcJyItemChargeList.stream().collect(Collectors.toMap(JcJyItemCharge::getChargeCode, JcJyItemCharge::getAmount));
        if (chargeCodeLists == null || chargeCodeLists.size() == 0) {
            return null;
        }
        List<ZdChargeItem> zdChargeItemList = zdChargeItemMapper.selectZdChargeItemByCodeList(chargeCodeLists);
        if (map != null && map.size() > 0) {
            for (ZdChargeItem zd : zdChargeItemList) {
                zd.setNum(map.get(zd.getCode()));
                zd.setTotalAmount((zd.getChargeAmount() == null ? BigDecimal.ZERO : zd.getChargeAmount()).multiply(zd.getNum() == null ? BigDecimal.ZERO : zd.getNum()));
            }
        }
        return zdChargeItemList;
    }

    @Override
    public List<JyZdItem> queryZlByCommonParams(String commonParams) {
        if (StringUtils.isBlank(commonParams)) {
            commonParams = null;
        }
        if (SignUtil.isAlpha(commonParams)) {
            commonParams = commonParams.toUpperCase();
        }
        if (StringUtils.isNotBlank(commonParams)) {
            commonParams = "%" + commonParams.toUpperCase() + "%";
        }
        return jcJyItemChargeMapper.selectZlByCommonParams(commonParams);
    }


}
