package thyyxxk.webserver.service.medicine;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.taobao.api.response.AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.medicine.MzDrugTracCodgDao;
import thyyxxk.webserver.dao.his.medicine.YpCodgLineDao;
import thyyxxk.webserver.dao.his.medicine.YpCodgMatchDao;
import thyyxxk.webserver.dao.his.medicine.YpInDetlDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.medicine.MzDrugTracCodg;
import thyyxxk.webserver.entity.medicine.YpCodgLine;
import thyyxxk.webserver.entity.medicine.YpInDetlVo;
import thyyxxk.webserver.entity.medicine.YpSelinfoSold;
import thyyxxk.webserver.entity.medicine.vo.YpCodgVo;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.ResultVoUtil;

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

/**
 * @ClassName YpCodgMatchService
 * @Author hsh
 * @Date 2025/6/20 0020 11:25
 * @Version 1.0
 * @Description 药品追溯码匹配Service
 **/
@Service
@Slf4j
public class YpCodgMatchService {

    private final YpCodgMatchDao matchDao;

    private final YpCodgLineDao lineDao;

    private final YpTaoBaoService taoBaoService;

    private final YpInDetlDao inDao;

    private final MzDrugTracCodgDao mzDrugTracCodgDao;

    private final UserCache userCache;

    public YpCodgMatchService(YpCodgMatchDao matchDao, YpCodgLineDao lineDao, YpTaoBaoService taoBaoService, YpInDetlDao inDao, MzDrugTracCodgDao mzDrugTracCodgDao, UserCache userCache) {
        this.matchDao = matchDao;
        this.lineDao = lineDao;
        this.taoBaoService = taoBaoService;
        this.inDao = inDao;
        this.mzDrugTracCodgDao = mzDrugTracCodgDao;
        this.userCache = userCache;
    }

    /**
     * @Description 查询已缴费的待发药处方
     * @Author hsh
     * @param vo 参数
     * @return list 待发药处方明细
     * @Date 2025/6/25 0025 11:11
     */
    public ResultVo<List<Map<String, Object>>> selectMzChargePrescription(YpCodgVo vo){
        // 查询血透室的科室编码（特殊需求： 血透室的发药是批量发的，不再这里显示）
        // String deptCode = matchDao.selectXtsDeptCode();
        return ResultVoUtil.success(matchDao.selectMzChargePrescription(vo));
    }

    /**
     * @Description 根据处方信息查询处方明细
     * @Author hsh
     * @param vo 处方信息
     * @return list 处方明细
     * @Date 2025/7/4 0004 11:34
     */
    public ResultVo<List<Map<String, Object>>> selectMzCfDetail(YpCodgVo vo){
        return ResultVoUtil.success(matchDao.selectMzCfDetail(vo));
    }

    /**
     * @Description 查询获取的追溯码匹配信息
     * @Author hsh
     * @param vo 处方信息
     * @return map
     * @Date 2025/7/7 0007 14:48
     */
    public ResultVo<Map<String, Object>> selectMatchCodgInfo(YpCodgVo vo){
        String codgLine = vo.getCodeLine();
        if(StringUtils.isBlank(codgLine)){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品追溯码信息不存在，请检查！");
        }
        // 查询处方明细
        List<Map<String, Object>> cfDetail = matchDao.selectMzCfDetail(vo);
        // 查询是否开放码上放心平台核对校验功能（0：否； 1：是）
        String isOpenTb = matchDao.selectIsOpenTb();
        if(cfDetail.isEmpty()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人处方信息不存在，请检查！");
        }
        Map<String, Object> resultMap = new HashMap<>();
        List<MzDrugTracCodg> resultList = new ArrayList<>();
        UserInfo us = userCache.getUserInfoByToken();
        // 根据处方信息查询企业追溯下载信息并进行追溯码匹配工作
        List<String> strArr = Arrays.stream(codgLine.split("\n")).collect(Collectors.toList());
        // 查询匹配的未上传使用的追溯码信息
        List<YpCodgLine> lines = lineDao.selectYpCodgLineByIds(strArr);
        if(!lines.isEmpty()){
            // 匹配后直接生成门诊销售待上传数据(这个后续在考虑是否有必要)
            // List<YpSelinfoSold> solds = callYpSelinfoSoldData(vo, lines, cfDetail);

            // 根据匹配信息生成门诊处方药品追溯码信息
            List<MzDrugTracCodg> ss = callMzDrugTracCodgDataByYb(us, vo, cfDetail, lines);
            if(!ss.isEmpty()) {
                resultList.addAll(ss);
            }
        }
        // 未匹配企业追溯下载的追溯码并且走淘宝接口获取药品信息
        List<String> unMatchCodgList;
        if(!lines.isEmpty()) {
            unMatchCodgList = strArr.stream().filter(s ->
                    lines.stream().noneMatch(line -> s.equals(line.getSmlPacTracCodg()))).collect(Collectors.toList());
        } else {
            unMatchCodgList = new ArrayList<>(strArr);
        }

        // 生成门诊药品追溯码信息
        if(!unMatchCodgList.isEmpty() && "1".equals(isOpenTb)){
            // 调用淘宝接口核对药品信息
            String str = String.join(",", unMatchCodgList);
            ResultVo<Map<String, Object>> resultVo = taoBaoService.queryCodeDetail(str);
            Map<String, Object> map = resultVo.getData();
            List<AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse.CodeFullInfoDto> codeFullInfoDto =
                    (List<AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse.CodeFullInfoDto>) map.get("data");

            // 根据淘宝接口返回追溯码信息匹配并生成门诊处方药品追溯码信息
            List<MzDrugTracCodg> tb = callMzDrugTracCodgDataByTaoBao(us, vo, cfDetail, codeFullInfoDto);
            if(!tb.isEmpty()) {
                resultList.addAll(tb);
            }
        } else {
            for(String str : unMatchCodgList){
                MzDrugTracCodg codg = new MzDrugTracCodg();
                codg.setDrugTracCodg(str);
                codg.setPatientId(vo.getPatNo());
                codg.setTimes(vo.getTimes());
                codg.setReceiptNo(vo.getReceiptNo());
                codg.setOrderNo(vo.getOrderNo());
                codg.setItemNo(vo.getItemNo());
                codg.setRealNo(vo.getRealNo());
                codg.setGroupNo(vo.getGroupNo());
                codg.setConfirmId(StringUtils.trim(us.getCode()));
                codg.setConfirmName(StringUtils.trim(us.getName()));
                codg.setConfirmTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                codg.setFlag("-1");
                codg.setMatchFlag("-1");
                codg.setMatchMessage("未匹配到药品信息，请检查！");
            }
        }
        resultMap.put("data", resultList);
        return ResultVoUtil.success(resultMap);
    }

    private List<MzDrugTracCodg> callMzDrugTracCodgDataByTaoBao(UserInfo us, YpCodgVo vo, List<Map<String, Object>> cfDetail,
        List<AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse.CodeFullInfoDto> codeFullInfoDto) {
        List<MzDrugTracCodg> tracCodgList = new ArrayList<>();
        if(null == codeFullInfoDto || codeFullInfoDto.isEmpty()){
            return tracCodgList;
        }
        Map<String, List<Map<String, Object>>> m =
                cfDetail.stream().collect(Collectors.groupingBy(f -> String.valueOf(f.get("chargeCode")), Collectors.toList()));

        for (AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse.CodeFullInfoDto dto : codeFullInfoDto){
            MzDrugTracCodg codg = new MzDrugTracCodg();
            // 药品生产信息
            List<AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse.ProduceInfoDto> produceInfoDto =
                    dto.getCodeProduceInfoDTO().getProduceInfoList();
            // 药品基本信息
            AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse.DrugEntBaseDto drugEntBaseDto = dto.getDrugEntBaseDTO();
            // 生产企业信息
            AlibabaAlihealthDrugtraceTopYljgQueryCodedetailResponse.PUserEntDto pUserEntDto = dto.getpUserEntDTO();
            // 追溯码
            codg.setDrugTracCodg(dto.getCode());
            codg.setConfirmId(StringUtils.trim(us.getCode()));
            codg.setConfirmName(StringUtils.trim(us.getName()));
            codg.setConfirmTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            codg.setPatientId(vo.getPatNo());
            codg.setTimes(vo.getTimes());
            codg.setReceiptNo(vo.getReceiptNo());
            codg.setOrderNo(vo.getOrderNo());
            codg.setItemNo(vo.getItemNo());
            codg.setRealNo(vo.getRealNo());
            codg.setGroupNo(vo.getGroupNo());

            // 生产批号
            String manuNo = produceInfoDto.get(0).getBatchNo();
            // 生产日期
            String manuDate = produceInfoDto.get(0).getProduceDateStr();
            // 有效期止
            String expyEnd = DateUtil.format(DateUtil.parse(produceInfoDto.get(0).getExpireDate(), "yyyyMMdd"), "yyyy-MM-dd");
            // 药品名称
            String chargeName = drugEntBaseDto.getPhysicName();
            // 批准文号
            String licenceNo = drugEntBaseDto.getApprovalLicenceNo();
            // 包装规格
            String specification = drugEntBaseDto.getPkgSpecCrit();
            // 制剂规格
            String spec = drugEntBaseDto.getPrepnSpec();
            // 生产企业名称
            String factory = pUserEntDto.getEntName();
            // 模糊查询入库信息（根据追溯码只能模糊匹配） 2025.07.25 暂时不根据批准文号匹配，因院内维护原因会有很多不一致
            List<YpInDetlVo> detlVoList = inDao.selectYpInDetlListByName(manuNo, chargeName);
            if(null == detlVoList || detlVoList.isEmpty()){
                codg.setFlag("-1");
                codg.setMatchFlag("-1");
                codg.setDrugName(chargeName);
                codg.setSpecification(specification);
                codg.setManuNo(manuNo);
                codg.setManuDate(manuDate);
                codg.setAbbrName(factory);
                codg.setExpyEnd(expyEnd);
                codg.setMatchMessage("未匹配到入库信息：扫码药品【" + chargeName + "】，请核对！");
            } else {
                List<Map<String, Object>> l = m.get(detlVoList.get(0).getChargeCode());
                if(null != l && !l.isEmpty()){
                    String drugName = Convert.toStr(l.get(0).get("chargeName"));
                    String serial = Convert.toStr(l.get(0).get("serial"));
                    if(serial.equals(detlVoList.get(0).getSerial())){
                        codg.setFlag("0");
                        codg.setMatchFlag("1");
                        codg.setMatchMessage("匹配码上放心平台药品信息成功");
                        // 补全处方信息
                        codg.setItemNo(Convert.toInt(l.get(0).get("itemNo")));
                        codg.setDrugName(drugName);
                        codg.setChargeItemCode(Convert.toStr(l.get(0).get("chargeCode")));
                        codg.setSerial(Convert.toStr(l.get(0).get("serial")));
                        codg.setChargeDate(Convert.toStr(l.get(0).get("chargeDate")));
                        codg.setWarnDeptName(Convert.toStr(l.get(0).get("deptName")));
                        codg.setSpecification(Convert.toStr(l.get(0).get("specification")));
                        codg.setAbbrName(Convert.toStr(l.get(0).get("factory")));
                        codg.setUnitPrice(Convert.toBigDecimal(l.get(0).get("retprice")));
                        codg.setManuNo(manuNo);
                        codg.setManuDate(manuDate);
                        codg.setExpyEnd(expyEnd);
                    } else {
                        codg.setFlag("-1");
                        codg.setMatchFlag("-1");
                        codg.setDrugName(chargeName);
                        codg.setSpecification(specification);
                        codg.setManuNo(manuNo);
                        codg.setManuDate(manuDate);
                        codg.setAbbrName(factory);
                        codg.setExpyEnd(expyEnd);
                        codg.setMatchMessage("未匹配到药品信息，药品大小包装规格不对，请核对是否拆零");
                    }
                } else {
                    codg.setFlag("-1");
                    codg.setMatchFlag("-1");
                    codg.setDrugName(chargeName);
                    codg.setSpecification(specification);
                    codg.setManuNo(manuNo);
                    codg.setManuDate(manuDate);
                    codg.setAbbrName(factory);
                    codg.setExpyEnd(expyEnd);
                    codg.setMatchMessage("未匹配到处方药品信息: 扫码药品是【" + chargeName + "】，请核对！");
                }
            }
            tracCodgList.add(codg);
        }
        return tracCodgList;
    }

    private List<MzDrugTracCodg> callMzDrugTracCodgDataByYb(UserInfo us, YpCodgVo vo, List<Map<String, Object>> cfDetail, List<YpCodgLine> lines) {
        List<MzDrugTracCodg> tracCodgList = new ArrayList<>();
        if(null == lines || lines.isEmpty()){
            return tracCodgList;
        }
        Map<String, List<Map<String, Object>>> m =
                cfDetail.stream().collect(Collectors.groupingBy(f -> String.valueOf(f.get("chargeCode")), Collectors.toList()));

        for(YpCodgLine line : lines){
            MzDrugTracCodg codg = new MzDrugTracCodg();
            codg.setDrugTracCodg(line.getSmlPacTracCodg());
            codg.setPatientId(vo.getPatNo());
            codg.setTimes(vo.getTimes());
            codg.setReceiptNo(vo.getReceiptNo());
            codg.setOrderNo(vo.getOrderNo());
            codg.setItemNo(vo.getItemNo());
            codg.setRealNo(vo.getRealNo());
            codg.setGroupNo(vo.getGroupNo());
            codg.setConfirmId(StringUtils.trim(us.getCode()));
            codg.setConfirmName(StringUtils.trim(us.getName()));
            codg.setConfirmTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            // 根据传票号和生产批号查询入库记录
            YpInDetlVo detlVo = inDao.selectYpInDetlListByNo(line.getDelvBchno(), line.getManuBchno());
            if(null == detlVo){
                codg.setFlag("-1");
                codg.setMatchFlag("-1");
                codg.setDrugName(line.getDrugProdname());
                codg.setSpecification(line.getDrugSpec());
                codg.setManuNo(line.getManuBchno());
                codg.setManuDate(line.getManuDate());
                codg.setAbbrName(line.getProdentpName());
                codg.setExpyEnd(line.getExpyEnd());
                codg.setMatchMessage("未匹配到入库信息：扫码药品【" + line.getDrugProdname() + "】，请核对！");
            } else {
                List<Map<String, Object>> l = m.get(StringUtils.trim(detlVo.getChargeCode()));
                if(null != l && !l.isEmpty()){
                    String chargeName = Convert.toStr(l.get(0).get("chargeName"));
                    String serial = Convert.toStr(l.get(0).get("serial"));
                    if(serial.equals(detlVo.getSerial())){
                        codg.setFlag("0");
                        codg.setMatchFlag("0");
                        codg.setMatchMessage("匹配企业追溯下载信息成功");
                        // 补全处方信息
                        codg.setItemNo(Convert.toInt(l.get(0).get("itemNo")));
                        codg.setDrugName(chargeName);
                        codg.setChargeItemCode(Convert.toStr(l.get(0).get("chargeCode")));
                        codg.setSerial(Convert.toStr(l.get(0).get("serial")));
                        codg.setChargeDate(Convert.toStr(l.get(0).get("chargeDate")));
                        codg.setWarnDeptName(Convert.toStr(l.get(0).get("deptName")));
                        codg.setSpecification(Convert.toStr(l.get(0).get("specification")));
                        codg.setAbbrName(Convert.toStr(l.get(0).get("factory")));
                        codg.setUnitPrice(Convert.toBigDecimal(l.get(0).get("retprice")));
                        codg.setManuNo(line.getManuBchno());
                        codg.setManuDate(line.getManuDate());
                        codg.setExpyEnd(line.getExpyEnd());
                    } else {
                        codg.setFlag("-1");
                        codg.setMatchFlag("-1");
                        codg.setDrugName(line.getDrugProdname());
                        codg.setSpecification(line.getDrugSpec());
                        codg.setManuNo(line.getManuBchno());
                        codg.setManuDate(line.getManuDate());
                        codg.setAbbrName(line.getProdentpName());
                        codg.setExpyEnd(line.getExpyEnd());
                        codg.setMatchMessage("未匹配到药品信息，药品大小包装规格不对，请核对是否拆零");
                    }
                } else {
                    codg.setFlag("-1");
                    codg.setMatchFlag("-1");
                    codg.setDrugName(line.getDrugProdname());
                    codg.setSpecification(line.getDrugSpec());
                    codg.setManuNo(line.getManuBchno());
                    codg.setManuDate(DateUtil.format(DateUtil.parse(line.getManuDate()), "yyyy-MM-dd"));
                    codg.setAbbrName(line.getProdentpName());
                    codg.setExpyEnd(DateUtil.format(DateUtil.parse(line.getExpyEnd()), "yyyy-MM-dd"));
                    codg.setMatchMessage("未匹配到处方药品信息: 扫码药品是【" + line.getDrugProdname() + "】，请核对！");
                }
            }
            tracCodgList.add(codg);
        }
        return tracCodgList;
    }

    private List<YpSelinfoSold> callYpSelinfoSoldData(YpCodgVo vo, List<YpCodgLine> lines, List<Map<String, Object>> cfDetail) {
        List<YpSelinfoSold> solds = new ArrayList<>();
        if(null == lines || lines.isEmpty()){
            return solds;
        }
        for(YpCodgLine line : lines){
            YpSelinfoSold sold = new YpSelinfoSold();
            sold.setMedListCodg(line.getMedListCodg());
            sold.setFixmedinsHilistId(line.getFixmedinsCode());
            sold.setFixmedinsHilistName(line.getFixmedinsName());
            sold.setFixmedinsBchno(line.getDelvBchno());
            sold.setManuLotnum(line.getManuBchno());
            sold.setManuDate(DateUtil.format(DateUtil.parse(line.getManuDate(), "yyyy-MM-dd"), "yyyy-MM-dd"));
            sold.setExpyEnd(DateUtil.format(DateUtil.parse(line.getExpyEnd(), "yyyy-MM-dd"), "yyyy-MM-dd"));
            sold.setRxFlag(line.getRxFlag());
            sold.setTrdnFlag("0");
            sold.setRxno(vo.getPatNo() + "_" + vo.getTimes());
            sold.setRtalDocno(String.valueOf(vo.getRealNo()));
            sold.setSelRetnCnt(BigDecimal.ONE);
        }

        return solds;
    }

    /**
     * @Description 更新门诊处方药品追溯码匹配信息
     * @Author hsh
     * @param list 门诊处方药品追溯码匹配信息
     * @return map
     * @Date 2025/7/11 0011 17:15
     */
    public ResultVo<Map<String, Object>> updateMzDrugCodgData(List<MzDrugTracCodg> list){
        Map<String, Object> resultMap = new HashMap<>();
        if(null == list || list.isEmpty()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品扫码的追溯码信息不存在，请检查！");
        }
        String patNo = list.get(0).getPatientId();
        Integer times = list.get(0).getTimes();
        Integer orderNo = list.get(0).getOrderNo();
        String groupNo = list.get(0).getGroupNo();
        if(StringUtils.isBlank(patNo)){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人门诊号信息不存在，请检查！");
        }
        if(null == times){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人门诊次数信息不存在，请检查！");
        }
        if(null == orderNo){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人门诊处方号信息不存在，请检查！");
        }
        if(StringUtils.isBlank(groupNo)){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人药房信息不存在，请检查！");
        }
        List<MzDrugTracCodg> mzCodgList = mzDrugTracCodgDao.selectMzDrugTracCodgByCode(list.get(0));
        int c = 0;
        if(null == mzCodgList || mzCodgList.isEmpty()){
            List<List<MzDrugTracCodg>> rl = ListUtils.partition(list, 30);
            for (List<MzDrugTracCodg> al : rl) {
                c = c + mzDrugTracCodgDao.insertMzDrugTracCodgBatch(al);
            }
        } else {
            mzDrugTracCodgDao.deleteMzDrugTracCodgByCode(list.get(0));
            List<List<MzDrugTracCodg>> rl = ListUtils.partition(list, 30);
            for (List<MzDrugTracCodg> al : rl) {
                c = c + mzDrugTracCodgDao.insertMzDrugTracCodgBatch(al);
            }
        }
        // 更新发药状态为已配药
        mzDrugTracCodgDao.updateMzChargeDetailConfirmFlag(patNo, times, orderNo, groupNo);

        if(c > 0){
            resultMap.put("code", 0);
            resultMap.put("message", "保存追溯码信息成功！");
        } else {
            resultMap.put("code", -1);
            resultMap.put("message", "保存追溯码信息失败！");
        }
        resultMap.put("data", list);
        return ResultVoUtil.success(resultMap);
    }

    /**
     * @Description 根据条件删除门诊处方匹配的追溯码信息
     * @Author hsh
     * @param vo 条件
     * @return map
     * @Date 2025/7/14 0014 15:52
     */
    public ResultVo<Map<String, Object>> delMzDrugCodgByCode(YpCodgVo vo){
        Map<String, Object> map = new HashMap<>();
        MzDrugTracCodg codg = new MzDrugTracCodg();
        codg.setPatientId(vo.getPatNo());
        codg.setTimes(vo.getTimes());
        codg.setReceiptNo(vo.getReceiptNo());
        codg.setOrderNo(vo.getOrderNo());

        List<MzDrugTracCodg> l = mzDrugTracCodgDao.selectMzDrugTracCodgByCode(codg);
        if(null == l || l.isEmpty()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "门诊处方匹配的追溯码信息不存在，请检查！");
        }

        int c = mzDrugTracCodgDao.deleteMzDrugTracCodgByCode(codg);
        if(c > 0){
            map.put("code", 0);
            map.put("message", "删除门诊处方匹配的追溯码信息成功！");
        } else {
            map.put("code", -1);
            map.put("message", "删除门诊处方匹配的追溯码信息失败！");
        }
        return ResultVoUtil.success(map);
    }

    /**
     * @Description 查询门诊处方药品追溯码匹配情况
     * @Author hsh
     * @param vo 查询条件
     * @return map
     * @Date 2025/7/15 0015 16:47
     */
    public ResultVo<Map<String, Object>> selectMzDrugTracCodgData(YpCodgVo vo){
        Map<String, Object> map = new HashMap<>();
        if(StringUtils.isBlank(vo.getPatNo())){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人门诊号信息不存在，请检查！");
        }
        if(null == vo.getTimes()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人门诊次数信息不存在，请检查！");
        }
        if(null == vo.getReceiptNo()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人门诊结帐次数信息不存在，请检查！");
        }
        if(null == vo.getOrderNo()){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "病人门诊处方号信息不存在，请检查！");
        }
        MzDrugTracCodg codg = new MzDrugTracCodg();
        codg.setPatientId(vo.getPatNo());
        codg.setTimes(vo.getTimes());
        codg.setReceiptNo(vo.getReceiptNo());
        codg.setOrderNo(vo.getOrderNo());
        List<MzDrugTracCodg> mzCodgList = mzDrugTracCodgDao.selectMzDrugTracCodgByCode(codg);
        map.put("data", mzCodgList);
        return ResultVoUtil.success(map);
    }

}
