package thyyxxk.webserver.service.executeItem;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.datamodify.ZyDetailChargeDao;
import thyyxxk.webserver.dao.his.executeItem.StoreProcedureConvertDao;
import thyyxxk.webserver.dao.his.executeItem.YzZyPatientFeeDao;
import thyyxxk.webserver.entity.datamodify.YzActOrder;
import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
import thyyxxk.webserver.entity.executeItem.NumberEnum;
import thyyxxk.webserver.entity.executeItem.ZdChargeItem;
import thyyxxk.webserver.entity.executeItem.ZyLedgerFile;
import thyyxxk.webserver.entity.executeItem.ZyTmpReceiveData;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.inpatient.dismiss.ZyInit;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.CommonUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 存储过程转换接口
 * @Author:lihong
 * @Date: 2024/8/21
 */
@Service
@Slf4j
public class StoreProcedureConvertService {
    @Resource
    private StoreProcedureConvertDao dao;
    @Resource
    private YzZyPatientFeeDao yzZyPatientFeeDao;
    @Resource
    private ZyDetailChargeDao zyDetailChargeDao;

    private static final String ZY_DETAIL_CHARGE_TABLE = "zy_detail_charge";
    private static final String ZY_DETAIL_CHARGE_YB_TABLE = "zy_detail_charge_yb";
    private static final String YZ_ACT_ORDER_TABLE = "yz_act_order";
    private static final String YZ_ERASE_ORDER_TABLE = "yz_erase_order";

    /**
     * @description: 药品费用接收
     * @author: lihong
     * @date: 2024/8/21 17:24
     * @param: patNo 住院号
     * @param: times 住院次数
     **/
    public void zyReceiveDrug(String patNo, Integer times) {
        AssertUtil.isnotBlank(patNo, "住院号不能为空");
        AssertUtil.isnotBlank(times, "住院次数不能为空");
        //大人住院号
        String inpatientNo = dealPatNo(patNo);
        ZyInit zyInit = dao.selectZyInit();
        String westCode = zyInit.getWestDrugCode();
        String chnCode = zyInit.getChnDrugCode();
        //在院标识 1 在院 0出院
        int bAct = dao.selectCountZyActpatient(inpatientNo, times) > 0 ? 1 : 0;
        ZyActpatient zyActpatient = getZyActpatient(inpatientNo, times);
        //结账次数
        Integer sn = zyActpatient.getTimesBilled();
        //结账日期
        Date bAccounted = dao.selectAccountDate(inpatientNo, times, sn);
        //结账状态
        String chgStatus = getChgStatus(bAccounted);
        // 费用接收中间表
        List<ZyTmpReceiveData> zyTmpReceiveDataList = dao.selectDrugZyTmpReceiveData(patNo, times, sn, westCode, chnCode);
        if (CollUtil.isEmpty(zyTmpReceiveDataList)) return;
        //结账标识未结账的改为住院接收状态
        dao.updateYpZyPatientAcctSign(patNo, times);
        List<String> codeList = zyTmpReceiveDataList.stream().map(item -> item.getChargeCode()).collect(Collectors.toList());
        List<ZdChargeItem> zdChargeItemList = dao.selectZdChargeItem(CommonUtil.toInStringSql(codeList));
        //设置状态
        for (ZyTmpReceiveData zyTmpReceiveData : zyTmpReceiveDataList) {
            zyTmpReceiveData.setChargeStatus(chgStatus);
            zyTmpReceiveData.setChargeFee(Convert.toBigDecimal(zyTmpReceiveData.getChargeFee(), new BigDecimal("0.01")).multiply(zyTmpReceiveData.getOrderCount()).setScale(2, BigDecimal.ROUND_HALF_UP));
            if (zdChargeItemList != null) {
                Map<String, ZdChargeItem> zdChargeItemMap = zdChargeItemList.stream().collect(Collectors.toMap(ZdChargeItem::getCode, a -> a, (k1, k2) -> k1));
                if (zdChargeItemMap != null) {
                    ZdChargeItem zdChargeItem = zdChargeItemMap.get(Convert.toStr(zyTmpReceiveData.getChargeCode(),"").trim());
                    if (zdChargeItem != null) {
                        zyTmpReceiveData.setBillItemCode(zdChargeItem.getBillItemZy());
                        if (StrUtil.isBlank(zdChargeItem.getBillItemZy())) {
                            zyTmpReceiveData.setChargeStatus(NumberEnum.ONE.getCode());
                        }
                    }
                }
            }
        }
        List<BigDecimal> actOrderNoList = zyTmpReceiveDataList.stream().filter(item -> Convert.toBigDecimal(item.getActOrderNo(), BigDecimal.ZERO).compareTo(BigDecimal.ZERO) != 0).map(o -> o.getActOrderNo()).distinct().collect(Collectors.toList());
        Map<BigDecimal, YzActOrder> groupActOrderMap = getGroupActOrderMap(actOrderNoList, YZ_ACT_ORDER_TABLE);
        ZyActpatient zyActpatient1 = getZyActpatient(patNo, times);
        List<String> deptList = dao.selectZdDeptAll();
        setOrderMapVlaue(zyTmpReceiveDataList, groupActOrderMap, deptList, zyActpatient1.getZkWard());
        //撤销表
        Map<BigDecimal, YzActOrder> groupEraseOrderMap = getGroupActOrderMap(actOrderNoList, YZ_ERASE_ORDER_TABLE);
        setOrderMapVlaue(zyTmpReceiveDataList, groupEraseOrderMap, null, null);
        List<ZyTmpReceiveData> collect = zyTmpReceiveDataList.stream().filter(item -> Convert.toBigDecimal(item.getActOrderNo(), BigDecimal.ZERO).compareTo(new BigDecimal("100")) > 0 && Convert.toBigDecimal(item.getChargeFee(), BigDecimal.ZERO).compareTo(BigDecimal.ZERO) < 0).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            List<BigDecimal> actOrderNos = collect.stream().map(o -> o.getActOrderNo()).distinct().collect(Collectors.toList());
            if (CollUtil.isNotEmpty(actOrderNos)) {
                List<ZyDetailCharge> zyDetailChargeList = dao.selectZyDetailChargeExecUnitAndWardCode(actOrderNos, patNo, times);
                if (CollUtil.isNotEmpty(zyDetailChargeList)) {
                    Map<String, List<ZyDetailCharge>> groupMap = zyDetailChargeList.stream().collect(Collectors.groupingBy(o -> StrUtil.format("{}_{}_{}", o.getInpatientNo(), o.getAdmissTimes(), Convert.toStr(o.getOrderNo(), "0"))));
                    for (ZyTmpReceiveData zyTmpReceiveData : zyTmpReceiveDataList) {
                        String format = StrUtil.format("{}_{}_{}", patNo, times, Convert.toStr(zyTmpReceiveData.getActOrderNo(), "0"));
                        if (groupMap != null && CollUtil.isNotEmpty(groupMap.get(format))) {
                            zyTmpReceiveData.setExecUnit(groupMap.get(format).get(0).getExecUnit());
                            zyTmpReceiveData.setWardCode(groupMap.get(format).get(0).getWardCode());
                        }
                    }
                }
            }
        }
        // 处理数据
        SpringUtil.getBean(StoreProcedureConvertService.class).dealData(patNo, times, inpatientNo, bAct, sn, zyTmpReceiveDataList, NumberEnum.TWO);
    }
    @Transactional(rollbackFor = Exception.class)
    public void dealData(String patNo, Integer times, String inpatientNo, int bAct, Integer sn, List<ZyTmpReceiveData> zyTmpReceiveDataList, NumberEnum two) {
        Integer maxDetailSn = dao.selectMaxDetailSn(inpatientNo, times, sn, ZY_DETAIL_CHARGE_TABLE);
        List<ZyDetailCharge> zyDetailChargeList = convert2ZyDetailCharge(zyTmpReceiveDataList, maxDetailSn, inpatientNo, times, sn, two.getCode());
        SpringUtil.getBean(StoreProcedureConvertService.class).batchSaveZyDetailCharge(zyDetailChargeList);
        List<ZyTmpReceiveData> collect = zyTmpReceiveDataList.stream().filter(item -> NumberEnum.TWO.getCode().equals(item.getChargeStatus()) && !"XXX".equals(item.getBillItemCode())).collect(Collectors.toList());
        SpringUtil.getBean(StoreProcedureConvertService.class).afterDetailFee(patNo, times, inpatientNo, bAct, sn, collect);
    }

    /**
     * @description: 关联医嘱表 给一些值赋值
     * @author: lihong
     * @date: 2024/8/23 15:38
     * @param: zyTmpReceiveDataList
     * @param: groupActOrderMap
     **/
    private void setOrderMapVlaue(List<ZyTmpReceiveData> zyTmpReceiveDataList, Map<BigDecimal, YzActOrder> groupActOrderMap, List<String> deptList, String zkWard) {
        if (CollUtil.isEmpty(groupActOrderMap) && StrUtil.isBlank(zkWard)) return;
        for (ZyTmpReceiveData zyTmpReceiveData : zyTmpReceiveDataList) {
            if (CollUtil.isNotEmpty(groupActOrderMap)) {
                YzActOrder yzActOrder = groupActOrderMap.get(zyTmpReceiveData.getActOrderNo());
                if (yzActOrder != null) {
                    if (StrUtil.isBlank(yzActOrder.getExecUnit())) {
                        zyTmpReceiveData.setExecUnit(yzActOrder.getExecUnit());
                    }
                    zyTmpReceiveData.setYbSelfFlag(yzActOrder.getYbSelfFlag());
                    zyTmpReceiveData.setDoctorCode(yzActOrder.getPhysician());
                    zyTmpReceiveData.setReferPhysician(yzActOrder.getReferPhysician());
                    zyTmpReceiveData.setConsultPhysician(yzActOrder.getConsultPhysician());
                }
                if (CollUtil.isNotEmpty(deptList) && deptList.contains(zyTmpReceiveData.getExecUnit()) && StrUtil.isNotBlank(zkWard)) {
                    zyTmpReceiveData.setExecUnit(zkWard);
                }
                if (StrUtil.isNotBlank(zkWard)) {
                    zyTmpReceiveData.setWardCode(zkWard);
                }
            }
        }
    }

    /**
     * @description: 医嘱费用接收
     * @author: lihong
     * @date: 2024/8/21 17:25
     * @param: patNo 住院号
     * @param: times 住院次数
     **/
    @Transactional(rollbackFor = Exception.class)
    public void zyReceiveOne(String patNo, Integer times) {
        AssertUtil.isnotBlank(patNo, "住院号不能为空");
        AssertUtil.isnotBlank(times, "住院次数不能为空");
        //大人住院号
        String inpatientNo = dealPatNo(patNo);
        ZyActpatient zyActpatient = getZyActpatient(inpatientNo, times);
        //在院标识 1 在院 0出院
        int bAct = dao.selectCountZyActpatient(inpatientNo, times) > 0 ? 1 : 0;
        //结账次数
        Integer sn = zyActpatient.getTimesBilled();
        if (sn == null) return;
        //结账类型
        String chargeType = zyActpatient.getChargeType();
        List<String> chargeTypes = CollUtil.newArrayList(new String[]{"1", "2", "3", "4", "5"});
        if (!chargeTypes.contains(chargeType)) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "住院号:[" + patNo + "]病人身份错误");
        }
        //结账日期
        Date bAccounted = dao.selectAccountDate(inpatientNo, times, sn);
        //结账状态
        String chgStatus = getChgStatus(bAccounted);
        // 费用接收中间表
        List<ZyTmpReceiveData> zyTmpReceiveDataList = dao.selectZyTmpReceiveData(patNo, times);
        if (CollUtil.isEmpty(zyTmpReceiveDataList)) return;
        //赋值项目 住院码
        setBillItemCode(chargeType, zyTmpReceiveDataList);
        String zkWard = zyActpatient.getZkWard();
        if (!patNo.equals(inpatientNo)) {
            ZyActpatient tempZyPatient = getZyActpatient(patNo, times);
            zkWard = tempZyPatient.getZkWard();
        }
        //更新状态为接收
        yzZyPatientFeeDao.updateChargeStatusReceived(patNo, times);
        BigDecimal bedFee = dao.selectBedFee(inpatientNo, times);
        List<String> bedCodeList = dao.selectZySpecialBedCode();
        List<BigDecimal> actOrderNoList = zyTmpReceiveDataList.stream().filter(item -> Convert.toBigDecimal(item.getActOrderNo(), BigDecimal.ZERO).compareTo(BigDecimal.ZERO) != 0).map(o -> o.getActOrderNo().abs()).distinct().collect(Collectors.toList());
        Map<BigDecimal, YzActOrder> actOrderMap = getGroupActOrderMap(actOrderNoList, YZ_ACT_ORDER_TABLE);
        List<String> deptList = dao.selectZdDeptAll();
        for (ZyTmpReceiveData zyTmpReceiveData : zyTmpReceiveDataList) {
            zyTmpReceiveData.setChargeFee(Convert.toBigDecimal(zyTmpReceiveData.getChargeFee(), BigDecimal.ZERO).multiply(Convert.toBigDecimal(zyTmpReceiveData.getOrderCount(), BigDecimal.ZERO)).setScale(2, BigDecimal.ROUND_HALF_UP));
            if (CollUtil.isNotEmpty(bedCodeList)) {
                if (bedCodeList.contains(zyTmpReceiveData.getChargeCode())) {
                    zyTmpReceiveData.setChargeFee(zyTmpReceiveData.getChargeFee().subtract(Convert.toBigDecimal(bedFee, BigDecimal.ZERO)).setScale(2, BigDecimal.ROUND_HALF_UP));
                }
            }
            if (zyTmpReceiveData.getActOrderNo() == null || zyTmpReceiveData.getActOrderNo().compareTo(BigDecimal.ZERO) == 0) {
                zyTmpReceiveData.setActOrderNo(BigDecimal.ONE);
            }
            //设置执行科室
            if (MapUtil.isNotEmpty(actOrderMap)) {
                YzActOrder yzActOrder = actOrderMap.get(zyTmpReceiveData.getActOrderNo().abs());
                if (yzActOrder != null) {
                    if (StrUtil.isNotBlank(yzActOrder.getExecUnit())) {
                        zyTmpReceiveData.setExecUnit(yzActOrder.getExecUnit());
                    }
                    zyTmpReceiveData.setYbSelfFlag(yzActOrder.getYbSelfFlag());
                    zyTmpReceiveData.setDoctorCode(yzActOrder.getPhysician());
                    zyTmpReceiveData.setDeptCode(yzActOrder.getDeptCode());
                    zyTmpReceiveData.setWardCode(yzActOrder.getWardCode());
                    zyTmpReceiveData.setReferPhysician(yzActOrder.getReferPhysician());
                    zyTmpReceiveData.setConsultPhysician(yzActOrder.getConsultPhysician());
                }
            }
            if (StrUtil.isNotBlank(zkWard)) {
                zyTmpReceiveData.setWardCode(zkWard);
            }
            if (StrUtil.isNotBlank(zkWard) && CollUtil.isNotEmpty(deptList) && deptList.contains(zyTmpReceiveData.getExecUnit())) {
                zyTmpReceiveData.setExecUnit(zkWard);
            }
            //设置费用状态
            zyTmpReceiveData.setChargeStatus(chgStatus);
            if (StrUtil.isBlank(zyTmpReceiveData.getBillItemCode())) {
                zyTmpReceiveData.setChargeStatus(NumberEnum.ONE.getCode());
            }
        }
        // 最大流水号
        dealData(patNo, times, inpatientNo, bAct, sn, zyTmpReceiveDataList, NumberEnum.ONE);
    }

    /**
     * @description: 赋值项目 住院码
     * @author: lihong
     * @date: 2024/8/23 17:09
     * @param: chargeType
     * @param: zyTmpReceiveDataList
     **/
    private void setBillItemCode(String chargeType, List<ZyTmpReceiveData> zyTmpReceiveDataList) {
        List<String> codeList = zyTmpReceiveDataList.stream().map(item -> item.getChargeCode()).collect(Collectors.toList());
        List<ZdChargeItem> zdChargeItemList = dao.selectZdChargeItem(CommonUtil.toInStringSql(codeList));
        //给住院发票码和折扣率赋值
        if (CollUtil.isNotEmpty(zdChargeItemList)) {
            Map<String, ZdChargeItem> zdChargeItemMap = zdChargeItemList.stream().collect(Collectors.toMap(ZdChargeItem::getCode, a -> a, (k1, k2) -> k1));
            for (ZyTmpReceiveData zyTmpReceiveData : zyTmpReceiveDataList) {
                ZdChargeItem zdChargeItem = zdChargeItemMap.get(Convert.toStr(zyTmpReceiveData.getChargeCode(), "").trim());
                if (zdChargeItem != null) {
                    zyTmpReceiveData.setBillItemCode(zdChargeItem.getBillItemZy());
                    if (NumberEnum.ONE.getCode().equals(chargeType)) {
                        zyTmpReceiveData.setP1(zdChargeItem.getPercentag1());
                    } else if (NumberEnum.TWO.getCode().equals(chargeType)) {
                        zyTmpReceiveData.setP1(zdChargeItem.getPercentag2());
                    } else if (NumberEnum.ThREE.getCode().equals(chargeType)) {
                        zyTmpReceiveData.setP1(zdChargeItem.getPercentag3());
                    } else if (NumberEnum.FOUR.getCode().equals(chargeType)) {
                        zyTmpReceiveData.setP1(zdChargeItem.getPercentag4());
                    } else if (NumberEnum.FIVE.getCode().equals(chargeType)) {
                        zyTmpReceiveData.setP1(zdChargeItem.getPercentag5());
                    } else {
                        zyTmpReceiveData.setP1(BigDecimal.ONE);
                    }
                }
            }
        }
    }

    /**
     * @description: 最后更新 账页,住院表等 费用明细
     * @author: lihong
     * @date: 2024/8/23 17:01
     * @param: patNo
     * @param: times
     * @param: inpatientNo
     * @param: bAct
     * @param: sn
     * @param: chgStatus
     * @param: zyTmpReceiveDataList
     **/
    @Transactional(rollbackFor = Exception.class)
    public void afterDetailFee(String patNo, Integer times, String inpatientNo, int bAct, Integer sn, List<ZyTmpReceiveData> zyTmpReceiveDataList) {
        if (CollUtil.isEmpty(zyTmpReceiveDataList)) return;
        ZyLedgerFile zyLedgerFile = new ZyLedgerFile();
        zyLedgerFile.setInpatientNo(inpatientNo);
        zyLedgerFile.setAdmissTimes(times);
        zyLedgerFile.setLedgerSn(sn);
        BigDecimal totalChargeFee = zyTmpReceiveDataList.stream().map(ZyTmpReceiveData::getChargeFee).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal totalChargeFeeP = zyTmpReceiveDataList.stream().map(item -> item.getChargeFee().multiply(item.getP1())).reduce(BigDecimal.ZERO, BigDecimal::add);
        zyLedgerFile.setTotalCharge(totalChargeFee.setScale(2, BigDecimal.ROUND_HALF_UP));
        zyLedgerFile.setTotalChargeP(totalChargeFeeP.setScale(2, BigDecimal.ROUND_HALF_UP));
        //项目费用序号集合
        for (int i = 1; i <= 30; i++) {
            String code = i < 10 ? "00" + i : "0" + i;
            List<ZyTmpReceiveData> itemCodesList = zyTmpReceiveDataList.stream().filter(item -> code.equals(item.getBillItemCode())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(itemCodesList)) {
                BigDecimal chargeFee = itemCodesList.stream().map(ZyTmpReceiveData::getChargeFee).reduce(BigDecimal.ZERO, BigDecimal::add);
                ReflectUtil.setFieldValue(zyLedgerFile, "charge" + i, chargeFee.setScale(2, BigDecimal.ROUND_HALF_UP));
            } else {
                ReflectUtil.setFieldValue(zyLedgerFile, "charge" + i, BigDecimal.ZERO);
            }
        }
        //更新账页费用
        dao.updateZyLedgerFilechargeFee(zyLedgerFile);
        if (bAct == 1) {
            zyLedgerFile.setTableName(ZyActpatient.ZY_ACTPATIENT);
            dao.updateZyActpatient(zyLedgerFile);
            //    查询欠费下限
            BigDecimal oweLimit = dao.selectoweLimit(patNo, times);
            if (Convert.toBigDecimal(oweLimit, BigDecimal.ZERO).compareTo(BigDecimal.ZERO) <= 0) {
                dao.updateInternCode(patNo, times, "9", "and isnull(intern_code, '0') != '1'");
            } else {
                dao.updateInternCode(patNo, times, "0", "and intern_code = '9'");
            }
        } else {
            zyLedgerFile.setTableName(ZyActpatient.ZY_INACTPATIENT);
            dao.updateZyActpatient(zyLedgerFile);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void batchSaveZyDetailCharge(List<ZyDetailCharge> zyDetailChargeList) {
        List<List<ZyDetailCharge>> splitList = CollUtil.split(zyDetailChargeList, 50);
        for (List<ZyDetailCharge> list : splitList) {
            zyDetailChargeDao.insertZyDetailCharge(list);
        }
    }

    /**
     * @description: 获取医嘱号分组Map
     * @author: lihong
     * @date: 2024/8/23 15:19
     * @param: actOrderNoList
     * @return: java.util.Map<java.math.BigDecimal, thyyxxk.webserver.entity.datamodify.YzActOrder>
     **/
    private Map<BigDecimal, YzActOrder> getGroupActOrderMap(List<BigDecimal> actOrderNoList, String tableName) {
        List<YzActOrder> yzActOrderList = null;
        if (CollUtil.isNotEmpty(actOrderNoList)) {
            yzActOrderList = dao.selectYzActOrderNo(actOrderNoList, tableName);
        }
        Map<BigDecimal, YzActOrder> actOrderMap = null;
        if (CollUtil.isNotEmpty(yzActOrderList)) {
            actOrderMap = yzActOrderList.stream().collect(Collectors.toMap(YzActOrder::getActOrderNo, a -> a, (k1, k2) -> k1));
        }
        return actOrderMap;
    }

    private String getChgStatus(Date bAccounted) {
        String chgStatus;
        if (bAccounted == null) {
            //无效状态
            chgStatus = "2";
        } else {
            //未接收
            chgStatus = "1";
        }
        return chgStatus;
    }

    /**
     * @description: 获取病人住院信息
     * @author: lihong
     * @date: 2024/8/23 17:20
     * @param: inpatientNo
     * @param: times
     * @return: thyyxxk.webserver.entity.inpatient.ZyActpatient
     **/
    public ZyActpatient getZyActpatient(String inpatientNo, Integer times) {
        ZyActpatient zyActpatient = dao.selectZyActpatient(inpatientNo, times, ZyActpatient.ZY_ACTPATIENT);
        if (zyActpatient == null) {
            zyActpatient = dao.selectZyActpatient(inpatientNo, times, ZyActpatient.ZY_INACTPATIENT);
        }
        if (zyActpatient == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "住院号:[" + inpatientNo + "]没有该住院病人信息");
        }
        return zyActpatient;
    }


    /**
     * @description: 转换为费用明细
     * @author: lihong
     * @date: 2024/8/23 10:38
     * @param: zyTmpReceiveDataList
     * @param: maxDetailSn
     * @param: inpatientNo
     * @param: times
     * @param: sn
     * @param: flag 1 医嘱  2 药品
     * @return: java.util.List<thyyxxk.webserver.entity.datamodify.ZyDetailCharge>
     **/
    private List<ZyDetailCharge> convert2ZyDetailCharge(List<ZyTmpReceiveData> zyTmpReceiveDataList, Integer maxDetailSn, String inpatientNo, Integer times, Integer sn, String flag) {
        List<ZyDetailCharge> zyDetailChargeList = new ArrayList<>();
        if (CollUtil.isEmpty(zyTmpReceiveDataList)) return zyDetailChargeList;
        int index = 0;
        for (ZyTmpReceiveData zyTmpReceiveData : zyTmpReceiveDataList) {
            ZyDetailCharge zyDetailCharge = new ZyDetailCharge();
            zyDetailCharge.setInpatientNo(inpatientNo);
            zyDetailCharge.setAdmissTimes(times);
            zyDetailCharge.setLedgerSn(sn);
            zyDetailCharge.setDetailSn(Convert.toInt(maxDetailSn, 0) + index);
            zyDetailCharge.setChargeDate(zyTmpReceiveData.getChargeDate());
            zyDetailCharge.setOpIdCode(zyTmpReceiveData.getOpIdCode());
            zyDetailCharge.setChargeCode(zyTmpReceiveData.getChargeCode());
            zyDetailCharge.setInfantFlag(Convert.toStr(zyTmpReceiveData.getInfantFlag(), "0"));
            zyDetailCharge.setChargeStatus(zyTmpReceiveData.getChargeStatus());
            zyDetailCharge.setChargeFee(Convert.toBigDecimal(zyTmpReceiveData.getChargeFee(), BigDecimal.ZERO));
            zyDetailCharge.setSelfFlag(Convert.toStr(zyTmpReceiveData.getSelfFlag(), "0"));
            zyDetailCharge.setSeparateFlag(Convert.toStr(zyTmpReceiveData.getSeparateFlag(), "0"));
            zyDetailCharge.setSuppressFlag(Convert.toStr(zyTmpReceiveData.getSuppressFlag(), "0"));
            zyDetailCharge.setWbCode(zyTmpReceiveData.getWardCode());
            zyDetailCharge.setDeptCode(zyTmpReceiveData.getDeptCode());
            zyDetailCharge.setOrderNo(zyTmpReceiveData.getActOrderNo());
            zyDetailCharge.setOpeFlag("0");
            zyDetailCharge.setExecUnit(NumberEnum.ONE.getCode().equals(flag) ? StrUtil.isNotBlank(zyTmpReceiveData.getExecUnit()) ? zyTmpReceiveData.getExecUnit() : zyTmpReceiveData.getWardCode() : zyTmpReceiveData.getExecUnit());
            zyDetailCharge.setChargeAmount(zyTmpReceiveData.getOrderCount());
            zyDetailCharge.setChargeCodeMx(NumberEnum.ONE.getCode().equals(flag) ? "00" : zyTmpReceiveData.getChargeItemCode());
            zyDetailCharge.setDoctorCode(zyTmpReceiveData.getDoctorCode());
            zyDetailCharge.setReferPhysician(zyTmpReceiveData.getReferPhysician());
            zyDetailCharge.setConsultPhysician(zyTmpReceiveData.getConsultPhysician());
            if (NumberEnum.ONE.getCode().equals(flag)) {
                zyDetailCharge.setGenTime(zyTmpReceiveData.getOpDate() != null ? zyTmpReceiveData.getOpDate() : zyTmpReceiveData.getChargeDate());
            }
            zyDetailCharge.setYbSelfFlag(zyTmpReceiveData.getYbSelfFlag());
            ++index;
        }
        return zyDetailChargeList;
    }

    /**
     * @description: 小孩住院号转为大人住院号
     * @author: lihong
     * @date: 2024/8/22 9:11
     * @param: patNo
     **/
    private static String dealPatNo(String patNo) {
        if (StrUtil.contains(patNo, "$")) {
            return StrUtil.split(patNo, "$").get(0);
        }
        return patNo;
    }

    public static void main(String[] args) {

    }

}
