package thyyxxk.webserver.service.medicaladvice.drugmanage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.medicaladvice.drugmanage.DrugSubmitDao;
import thyyxxk.webserver.dao.his.medicaladvice.drugmanage.YzYpPageNoDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.executeItem.NumberEnum;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzSequence;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzYpPageNo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.CyDetailVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DispenseDrugTotalVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailTimeVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugSubmitParam;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugTotalVo;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.PageClassEnum;
import thyyxxk.webserver.entity.zhuyuanyisheng.caoyaoyizhu.YzActOrderCy;
import thyyxxk.webserver.service.medicaladvice.common.MedicalCommonService;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.CommonUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2023/9/22
 */
@Service
@Slf4j
public class DrugSubmitService {
    @Autowired
    private DrugSubmitDao dao;
    @Autowired
    private MedicalCommonService medicalCommonService;
    @Autowired
    private YzYpPageNoDao yzYpPageNoDao;
    /**
     * @description:查询提交药品单信息
     * @author: lihong
     * @date: 2023/9/25 9:54
     * @return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     **/
    public Map<String,Object> queryDrugSubmitInfo(DrugSubmitParam param){
        dealParam(param);
        Map<String, Object> result = new HashMap(5);
        if(PageClassEnum.ZCYYD.getCode().equals(param.getPageClass())){
            param.setStatusFlag(" in ('2','3') ");
            List<CyDetailVo> cyDetailVos = dao.selectCyDetail(param);
            CommonUtil.BeanTrim(cyDetailVos);
            result.put("cyDetailVo", dealCyDetailVos(cyDetailVos));
        }else {
            List<DrugTotalVo> drugTotalVos = dao.selectDrugSumitTotal(param);
            List<DrugDetailTimeVo> drugDetailTimeVos = dao.selectDrugDetailTime(param);
            List<DrugDetailVo> drugDetailVos = dao.selectDrugDetail(param);
            CommonUtil.BeanTrim(drugTotalVos);
            CommonUtil.BeanTrim(drugDetailTimeVos);
            CommonUtil.BeanTrim(drugDetailVos);
            if(CollUtil.isNotEmpty(drugTotalVos)){
                BigDecimal  sumDrugTotal = drugTotalVos.stream().map(DrugTotalVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
                DrugTotalVo sum = new DrugTotalVo();
                sum.setTotalFeeFlag("1");
                sum.setTotalFee(sumDrugTotal);
                drugTotalVos.add(sum);
            }
            if(CollUtil.isNotEmpty(drugDetailTimeVos)){
                BigDecimal  sumDrugDetail = drugDetailTimeVos.stream().map(DrugDetailTimeVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
                DrugDetailTimeVo sum = new DrugDetailTimeVo();
                sum.setTotalFeeFlag("1");
                sum.setTotalFee(sumDrugDetail);
                drugDetailTimeVos.add(sum);
            }
            result.put("drugTotalVos",drugTotalVos);
            result.put("drugDetailTimeVos",drugDetailTimeVos);
            result.put("drugDetailVos",dealDrugDetail(drugDetailVos));
        }
        return result;
    }

    private void dealParam(DrugSubmitParam param) {
        AssertUtil.isnotBlank(param.getWardCode(),"病室不能为空,请选择病室");
        AssertUtil.isnotBlank(param.getGroupNo(),"药房不能为空,请选择药房");
        AssertUtil.isnotBlank(param.getPageNo(),"药单号不能为空");
        AssertUtil.isnotBlank(param.getStartDate(),"开始时间不能为空");
        AssertUtil.isnotBlank(param.getEndDate(),"结束时间不能为空");
        CommonUtil.objectEmpty2Null(param,DrugSubmitParam.class);
        if (param.getPageNo().compareTo(BigDecimal.ZERO) == 0 && PageClassEnum.CQKF.getCode().equals(param.getPageClass())) {
            if(DateUtil.between(DateUtil.parseDate(param.getStartDate()),DateUtil.parseDate(param.getEndDate()), DateUnit.DAY) > 1){
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "长期口服药一次只能提交一天的数据");
            }
        }
        String startDate ;
        String endDate;
        if("2".equals(param.getTimeType())){
             startDate = StrUtil.format("{} 07:59:30",param.getStartDate());
             endDate = StrUtil.format("{} 08:00:30", DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parseDate(param.getEndDate()),1)));
        }else {
            startDate = StrUtil.format("{} 07:59:30",param.getStartDate());
            endDate = StrUtil.format("{} 11:59:30",DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parseDate(param.getEndDate()),1)));
        }
        param.setStartDate(startDate);
        param.setEndDate(endDate);
        if(!PageClassEnum.JSY.getCode().equals(param.getPageClass())){
            param.setJsyFlag(null);
        }
        if(PageClassEnum.ZCYYD.getCode().equals(param.getPageClass())){
            if(param.getPageNo().compareTo(BigDecimal.ZERO) == 0){
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "中草药单请选要择确认的药单号");
            }
        }
    }

    private CyDetailVo dealCyDetailVos(List<CyDetailVo> cyDetailVos) {
        CyDetailVo cyDetailVo = new CyDetailVo();
        List<CyDetailVo> list = new ArrayList();
        cyDetailVo.setTableData(list);
        if(CollUtil.isEmpty(cyDetailVos)) return cyDetailVo;
        BeanUtil.copyProperties(cyDetailVos.get(0),cyDetailVo);
        for (int i = 0; i < cyDetailVos.size(); i+=2) {
            CyDetailVo temp= new CyDetailVo();
            temp.setLchargeName(cyDetailVos.get(i).getChargeName());
            temp.setLquantityStr(cyDetailVos.get(i).getQuantityStr());
            temp.setLinstruction(cyDetailVos.get(i).getInstruction());
            if ((i + 1) < cyDetailVos.size()) {
                temp.setRchargeName(cyDetailVos.get(i+1).getChargeName());
                temp.setRquantityStr(cyDetailVos.get(i+1).getQuantityStr());
                temp.setRinstruction(cyDetailVos.get(i+1).getInstruction());
            }
            list.add(temp);
        }
        cyDetailVo.setTableData(list);
        return cyDetailVo;
    }

    private List<DrugDetailVo> dealDrugDetail(List<DrugDetailVo> drugDetailVos) {
        List<DrugDetailVo> result = new ArrayList();
        if(CollUtil.isEmpty(drugDetailVos)) return result;
        Map<String, List<DrugDetailVo>> groupMap = drugDetailVos.stream().collect(Collectors.groupingBy(DrugDetailVo::getChargeCode));
        //数量求和 增加小计
        for(String key : groupMap.keySet()){
            List<DrugDetailVo> temp = groupMap.get(key);
            BigDecimal sum = temp.stream().map(DrugDetailVo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            DrugDetailVo vo = new DrugDetailVo();
            vo.setXjFlag("1");
            vo.setTotalAmount(sum);
            vo.setDrugName(temp.get(0).getDrugName());
            vo.setChargeCode(temp.get(0).getChargeCode());
            result.addAll(temp);
            result.add(vo);
        }
        return result;
    }

    /**
     * @description:保存提交的药品单
     * @author: lihong
     * @date: 2023/9/27 11:37
     * @param: param
     * @return: thyyxxk.webserver.entity.ResultVo<java.lang.String>
     **/
    @Transactional(timeout = 36000,rollbackFor = Exception.class)
    public Map<String,Object> saveSubmitDrug(DrugSubmitParam param) {
        String tempStartDate = param.getStartDate();
        String tempEndDate = param.getEndDate();
        dealParam(param);
        if (!PageClassEnum.ZCYYD.getCode().equals(param.getPageClass())) {
            if(param.getPageNo().compareTo(BigDecimal.ZERO) != 0){
                throw new BizException(ExceptionEnum.LOGICAL_ERROR,"只有药单号为0的才能保存,请操作取数后,保存");
            }
        }
        Map<String, Object> result = new HashMap(1);
        Integer newPageNo = medicalCommonService.getPageNo();
        param.setNewPageNo(newPageNo);
        if (PageClassEnum.ZCYYD.getCode().equals(param.getPageClass())) {
            param.setStatusFlag(" in ('2','3') ");
            List<CyDetailVo> cyDetailVos = dao.selectCyDetail(param);
            if (CollUtil.isEmpty(cyDetailVos)) {
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有需要提交的中草药品单数据,请重新取数");
            }
            YzActOrderCy yzActOrderCy = new YzActOrderCy();
            CyDetailVo cyDetailVo = cyDetailVos.get(0);
            yzActOrderCy.setStatusFlag(NumberEnum.ThREE.getCode());
            yzActOrderCy.setSubmitDate(new Date());
            yzActOrderCy.setSubmitId(TokenUtil.getInstance().getTokenUserId());
            yzActOrderCy.setOrderNo(BigDecimal.valueOf(cyDetailVo.getOrderNo()));
            yzActOrderCy.setSerialNo(cyDetailVo.getSerialNo());
            yzActOrderCy.setChargeDate(new Date());
            String statusFlag = dao.selctCyStatusFlag(yzActOrderCy);
            if ("3".equals(statusFlag)) {
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "该中草药品单已提交,请不要重复提交");
            } else if ("4".equals(statusFlag)) {
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "该中草药品单已发药,不能提交");
            }
            dao.updateYzActOrderCy(yzActOrderCy);
        } else {
            List<DrugTotalVo> drugTotalVos = dao.selectDrugSumitTotal(param);
            if (CollUtil.isEmpty(drugTotalVos)) {
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有需要提交的" + PageClassEnum.get(param.getPageClass()).getName() + "数据,请重新取数");
            }
            YzYpPageNo yzYpPageNo = new YzYpPageNo();
            yzYpPageNo.setPageNo(newPageNo);
            yzYpPageNo.setPageClass(param.getPageClass());
            yzYpPageNo.setPageName(PageClassEnum.get(param.getPageClass()).getName());
            yzYpPageNo.setStartDate(DateUtil.parseDate(tempStartDate));
            yzYpPageNo.setEndDate(DateUtil.parseDate(tempEndDate));
            yzYpPageNo.setOccTimeF(DateUtil.parseDateTime(param.getStartDate()));
            yzYpPageNo.setOccTimeT(DateUtil.parseDateTime(param.getEndDate()));
            yzYpPageNo.setSubmitFlag("1");
            yzYpPageNo.setSubmitName(TokenUtil.getInstance().getTokenUserId());
            yzYpPageNo.setSubmitTime(new Date());
            yzYpPageNo.setPaySelf("1");
            yzYpPageNo.setDeptCode(param.getWardCode());
            yzYpPageNo.setWardCode(param.getWardCode());
            yzYpPageNo.setGroupNo(param.getGroupNo());
            param.setOpCode(TokenUtil.getInstance().getTokenUserId());
            yzYpPageNoDao.insert(yzYpPageNo);
            dao.updateYzYpZyOrderPageNo(param);
            dao.updateYzActOccPageNo(param);
        }
        result.put("newPageNo", newPageNo);
        return result;
    }

    public Map<String,Object> validBeforeDrug(DrugSubmitParam param) {
        Map<String, Object> map = new HashMap<>(1);
        dealParam(param);
        if (param.getPageNo().compareTo(BigDecimal.ZERO) == 0) {
            //查询之前是否还有未提交的药品单
            Date occTime =  dao.selectMinOccTime(param);
            String startDate = param.getStartDate();
            if(occTime != null && occTime.before(DateUtil.parseDateTime(startDate))) {
                map.put("occTime", DateUtil.formatDateTime(occTime));
            }
        }
        return map;
    }
    @Transactional(timeout = 36000,rollbackFor = Exception.class)
    public ResultVo<String> delSubmitDrug(DrugSubmitParam param) {
        dealParam(param);
        if (param.getPageNo().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "不能删除,请选择已确认的药单号");
        }
        if(PageClassEnum.ZCYYD.getCode().equals(param.getPageClass())){
        //    中草药
            param.setStatusFlag(" in ('2','3') ");
            List<CyDetailVo> cyDetailVos = dao.selectCyDetail(param);
            if (CollUtil.isEmpty(cyDetailVos)) {
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有需要删除的中草药品单数据");
            }
            YzActOrderCy yzActOrderCy = new YzActOrderCy();
            CyDetailVo cyDetailVo = cyDetailVos.get(0);
            yzActOrderCy.setStatusFlag(NumberEnum.TWO.getCode());
            yzActOrderCy.setOrderNo(BigDecimal.valueOf(cyDetailVo.getOrderNo()));
            yzActOrderCy.setSerialNo(cyDetailVo.getSerialNo());
            String statusFlag = dao.selctCyStatusFlag(yzActOrderCy);
            if (!"3".equals(statusFlag)) {
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "不是提交状态,不能删除");
            }
            dao.updateYzActOrderCy(yzActOrderCy);
        }else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("page_no", param.getPageNo());
            YzYpPageNo yzYpPageNo = yzYpPageNoDao.selectOne(queryWrapper);
            if (yzYpPageNo == null) {
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "已删除,请不要重复操作");
            }
            if (!"1" .equals(yzYpPageNo.getSubmitFlag())){
                throw new BizException(ExceptionEnum.LOGICAL_ERROR, "不是已提交状态不能删除");
            }
            yzYpPageNoDao.delete(queryWrapper);
            dao.delYzYpZyOrderPageNo(param);
            dao.delYzActOccPageNo(param);
        }
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE,"删除成功");
    }

    /**
     * @description:查询退药信息
     * @author: lihong
     * @date: 2023/10/8 10:30
     * @param: param
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String,Object> queryTcInfo(DrugSubmitParam param){
        Map<String, Object> result = new HashMap<>();
        CommonUtil.objectEmpty2Null(param, DrugSubmitParam.class);
        dealTime(param);
        param.setPageNo(param.getPageNo()==null ? BigDecimal.ZERO:param.getPageNo());
        List<DrugTotalVo> drugTotalVos = dao.selectTcSumTotal(param);
        List<DrugDetailTimeVo> drugDetailTimeVos = dao.selectTcTimeDetail(param);
        List<DrugDetailVo> drugDetailVos = dao.selectTcDetail(param);
        CommonUtil.BeanTrim(drugTotalVos);
        CommonUtil.BeanTrim(drugDetailTimeVos);
        CommonUtil.BeanTrim(drugDetailVos);
        if (CollUtil.isNotEmpty(drugTotalVos)) {
            BigDecimal totalSumFee = drugTotalVos.stream().map(DrugTotalVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
            result.put("totalSumFee", totalSumFee);
        }
        if (CollUtil.isNotEmpty(drugDetailTimeVos)) {
            BigDecimal detailSumFee = drugDetailTimeVos.stream().map(DrugDetailTimeVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
            result.put("detailSumFee", detailSumFee);
        }
        result.put("drugTotalVos", drugTotalVos);
        result.put("drugDetailTimeVos", drugDetailTimeVos);
        result.put("drugDetailVos", dealTcDrugDetail(drugDetailVos));
        return result;
    }

    private List<DrugDetailVo> dealTcDrugDetail(List<DrugDetailVo> drugDetailVos) {
        List<DrugDetailVo>  result = new ArrayList<>();
        if(CollUtil.isEmpty(drugDetailVos)) return result;
        Map<String, List<DrugDetailVo>> groupMap = drugDetailVos.stream().collect(Collectors.groupingBy(o -> o.getInpatientNo() + o.getAdmissTimes()));
        for (String key : groupMap.keySet()) {
            List<DrugDetailVo> temp = groupMap.get(key);
            for (int i = 0; i <temp.size() ; i++) {
                if(i > 0){
                    DrugDetailVo dt = new DrugDetailVo();
                    BeanUtil.copyProperties(temp.get(i), dt);
                    dt.setInpatientNo("");
                    dt.setBedNo("");
                    dt.setName("");
                    result.add(dt);
                }else {
                    result.add(temp.get(i));
                }
            }
        }
        return result;
    }
    @Transactional(timeout = 36000,rollbackFor = Exception.class)
    public Map<String,Object> saveTcData(DrugSubmitParam param) {
        AssertUtil.isnotBlank(param.getWardCode(),"病室不能为空,请选择病室");
        AssertUtil.isnotBlank(param.getGroupNo(),"药房不能为空,请选择药房");
        AssertUtil.isnotBlank(param.getPageNo(),"药单号不能为空");
        AssertUtil.isnotBlank(param.getStartDate(),"开始时间不能为空");
        AssertUtil.isnotBlank(param.getEndDate(),"结束时间不能为空");
        if(param.getPageNo().compareTo(BigDecimal.ZERO) != 0){
            throw new BizException(ExceptionEnum.LOGICAL_ERROR,"只有药单号为0的才能保存");
        }
        String tempStartDate = param.getStartDate();
        String tempEndDate = param.getEndDate();
        Map<String,Object> result = new HashMap<>(1);
        CommonUtil.objectEmpty2Null(param, DrugSubmitParam.class);
        dealTime(param);
        List<DrugTotalVo> drugTotalVos = dao.selectTcSumTotal(param);
        if (CollUtil.isEmpty(drugTotalVos)) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有需要提交的数据");
        }
        Integer tcPageNo = medicalCommonService.getPageNo();
        YzYpPageNo yzYpPageNo = new YzYpPageNo();
        yzYpPageNo.setPageNo(tcPageNo);
        yzYpPageNo.setPageClass("T");
        yzYpPageNo.setPageName("退药单");
        yzYpPageNo.setStartDate(DateUtil.parseDate(tempStartDate));
        yzYpPageNo.setEndDate(DateUtil.parseDate(tempEndDate));
        yzYpPageNo.setOccTimeF(DateUtil.parseDateTime(param.getStartDate()));
        yzYpPageNo.setOccTimeT(DateUtil.parseDateTime(param.getEndDate()));
        yzYpPageNo.setSubmitFlag("1");
        yzYpPageNo.setSubmitName(TokenUtil.getInstance().getTokenUserId());
        yzYpPageNo.setSubmitTime(new Date());
        yzYpPageNo.setPaySelf("0");
        yzYpPageNo.setDeptCode(param.getWardCode());
        yzYpPageNo.setWardCode(param.getWardCode());
        yzYpPageNo.setGroupNo(param.getGroupNo());
        yzYpPageNoDao.insert(yzYpPageNo);
        param.setNewPageNo(tcPageNo);
        dao.updateYzYpZyOrderTcPageNo(param);
        result.put("newPageNo", tcPageNo);
        return result;
    }
    @Transactional(timeout = 36000,rollbackFor = Exception.class)
    public ResultVo<String> delTcData(DrugSubmitParam param) {
        AssertUtil.isnotBlank(param.getWardCode(),"病室不能为空,请选择病室");
        AssertUtil.isnotBlank(param.getGroupNo(),"药房不能为空,请选择药房");
        AssertUtil.isnotBlank(param.getPageNo(),"药单号不能为空");
        AssertUtil.isnotBlank(param.getStartDate(),"开始时间不能为空");
        AssertUtil.isnotBlank(param.getEndDate(),"结束时间不能为空");
        if (param.getPageNo().compareTo(BigDecimal.ZERO) <= 0) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "不能删除,请选择已确认的退药单号");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("page_no", param.getPageNo());
        YzYpPageNo yzYpPageNo = yzYpPageNoDao.selectOne(queryWrapper);
        if (yzYpPageNo == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "已删除,请不要重复操作");
        }
        if (!"1" .equals(yzYpPageNo.getSubmitFlag())){
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "不是已提交状态不能删除");
        }
        yzYpPageNoDao.delete(queryWrapper);
        dao.delYzYpZyOrderTcPageNo(param);
        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE,"删除成功");
    }


    public Map<String,Object> queryQsTime(DrugSubmitParam param){
        Map<String,Object> result = new HashMap<>();
        dealTime(param);
        result.put("startDayTime", param.getStartDate());
        result.put("endDayTime", param.getEndDate());
        return result;
    }


    public void dealTime(DrugSubmitParam param){
        YzSequence yzSequence = medicalCommonService.queryYzSequence();
        String startDate = param.getStartDate();
        String endDate = param.getEndDate();
        DateTime dateTime = DateUtil.offsetDay(DateUtil.parseDate(endDate), 1);
        String startDayTime = yzSequence.getStartDayTime();
        String endDayTime = yzSequence.getEndDayTime();
        param.setStartDate(startDate +" " +startDayTime +":00");
        param.setEndDate(DateUtil.formatDate(dateTime) +" " +endDayTime+":00");
    }

    /**
     * @description:查询药单列表
     * @author: lihong
     * @date: 2023/10/9 15:17
     * @param: param
     * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.common.YzYpPageNo>
     **/
     public List<YzYpPageNo> queryYpPageNoList(DrugSubmitParam param){
         setDateTime(param);
         setSupplyCode(param);
        return  dao.selectYpPageNo(param);
     }

    private void setSupplyCode(DrugSubmitParam param) {
         if(StrUtil.isNotBlank(param.getPageClass())){
            if(PageClassEnum.CYDY.getCode().equals(param.getPageClass())){
                param.setSupplyCode("c");
            }else if(PageClassEnum.JSY.getCode().equals(param.getPageClass())){
                param.setSupplyCode("j");
            }else if(PageClassEnum.DSY.getCode().equals(param.getPageClass())){
                param.setSupplyCode("i");
            }else if(PageClassEnum.CQKF.getCode().equals(param.getPageClass())){
                param.setSupplyCode("b");
            }else if(PageClassEnum.YJKSLY.getCode().equals(param.getPageClass())){
                param.setSupplyCode("A");
            }else{
                param.setSupplyCode("");
            }
         }

    }

    /**
     * @description:查询发药汇总 长期和临时 明细
     * @author: lihong
     * @date: 2023/10/9 15:22
     * @param: param
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String, Object> queryDispenseDrugTotalVo(DrugSubmitParam param) {
        Map<String, Object> result = new HashMap<>();
        setDateTime(param);
        param.setAlwaysFlag("a");
        List<DispenseDrugTotalVo> cqDetail = dao.selectDispenseDrugTotalVo(param);
        param.setAlwaysFlag("t");
        List<DispenseDrugTotalVo> lsDetail = dao.selectDispenseDrugTotalVo(param);
        result.put("cqDetail",cqDetail);
        result.put("lsDetail",lsDetail);
        return result;
    }
    /**
     * @description:查询发药明细
     * @author: lihong
     * @date: 2023/10/9 15:33
     * @param: param
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String, Object> queryDispenseDrugDetail(DrugSubmitParam param) {
        Map<String, Object> result = new HashMap<>();
        setDateTime(param);
        setSupplyCode(param);
        boolean pageNoFlag = false;
        if (param.getPageNo() == null) {
            param.setPageNo(BigDecimal.ZERO);
            pageNoFlag = true;
        }
        List<DrugTotalVo> drugTotalVos = dao.selectDispenseDrugTotalDetail(param);
        result.put("drugTotalVos", drugTotalVos);
        if(CollUtil.isNotEmpty(drugTotalVos)){
            BigDecimal drugTotalFee = drugTotalVos.stream().map(DrugTotalVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
            result.put("drugTotalFee", drugTotalFee);
        }
        if(pageNoFlag){
            param.setPageNo(null);
        }
        if (PageClassEnum.JYYP.getCode().equals(param.getPageClass())) {
            List<DrugDetailVo> jyDetail = dao.selectJyDetail(param);
            result.put("jyDetail", jyDetail);
        }else if (PageClassEnum.TYD.getCode().equals(param.getPageClass())){
            List<DrugDetailVo> tyDetail = dao.selectDispenseTyDetail(param);
            result.put("tyDetail", tyDetail);
            if(CollUtil.isNotEmpty(tyDetail)){
                BigDecimal drugDetailTotalFee = tyDetail.stream().map(DrugDetailVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
                result.put("drugDetailTotalFee", drugDetailTotalFee);
            }
        }
        else {
            List<DrugDetailVo> drugDetailVos = dao.selectDispenseDrugDetail(param);
            result.put("drugDetailVos", drugDetailVos);
            if(CollUtil.isNotEmpty(drugDetailVos)){
                BigDecimal drugDetailTotalFee = drugDetailVos.stream().map(DrugDetailVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
                result.put("drugDetailTotalFee", drugDetailTotalFee);
            }
        }
        return result;
    }
    /**
     * @description:查询发药草药明细
     * @author: lihong
     * @date: 2023/10/9 15:39
     * @param: param
     * @return: java.util.Map<java.lang.String,java.lang.Object>
     **/
    public Map<String, Object> queryDispenseCyDetail(DrugSubmitParam param) {
        Map<String, Object> result = new HashMap<>();
        setDateTime(param);
        param.setStatusFlag(" ='4' ");
        param.setGroupNo("81");
        List<CyDetailVo> cyDetailVos = dao.selectCyDetail(param);
        result.put("cyDetailVo", dealCyDetailVos(cyDetailVos));
        return result;
    }

    private void setDateTime(DrugSubmitParam param){
        if(StrUtil.isNotBlank(param.getStartDate())){
            param.setStartDate(param.getStartDate() + " " +"00:00:00");
        }
        if(StrUtil.isNotBlank(param.getEndDate())){
            param.setEndDate(param.getEndDate() + " " + "23:59:59");
        }

    }
    /**
     * @description:查询大输液消耗统计
     * @author: lihong
     * @date: 2023/10/10 16:15
     * @param: param
     * @return: java.util.List<thyyxxk.webserver.entity.medicaladvice.medicamanage.drugmanage.DrugDetailVo>
     **/
    public Map<String,Object> queryDsyDetail(DrugSubmitParam param) {
        setDateTime(param);
        Map<String, Object> res = new HashMap<>(3);
        List<DrugDetailVo> drugDetailVos = dao.selectDsyTotal(param);
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalFee = BigDecimal.ZERO;
        if (CollUtil.isNotEmpty(drugDetailVos)) {
             totalAmount = drugDetailVos.stream().map(DrugDetailVo::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
             totalFee = drugDetailVos.stream().map(DrugDetailVo::getFee).reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        res.put("drugDetailVos",drugDetailVos);
        res.put("totalAmount",totalAmount);
        res.put("totalFee",totalFee);
        return res;
    }
}
