package thyyxxk.webserver.service.medicine;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.dictionary.EmployeeMiDao;
import thyyxxk.webserver.dao.his.medicine.YpDictDao;
import thyyxxk.webserver.dao.his.medicine.YpZdChargeGroupDao;
import thyyxxk.webserver.dao.his.medicine.YpZdClassDao;
import thyyxxk.webserver.dao.his.medicine.YpZdDosageDao;
import thyyxxk.webserver.dao.his.medicine.YpZdDrugKindDao;
import thyyxxk.webserver.dao.his.medicine.YpZdGroupNameDao;
import thyyxxk.webserver.dao.his.medicine.YpZdManuFactoryDao;
import thyyxxk.webserver.dao.his.medicine.YpZdSupplyDao;
import thyyxxk.webserver.dao.his.medicine.YpZdUnitDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.inpatient.YpZdDict;
import thyyxxk.webserver.entity.medicine.YpDictConstant;
import thyyxxk.webserver.entity.medicine.YpPrintName;
import thyyxxk.webserver.entity.medicine.YpTypeNumberEnum;
import thyyxxk.webserver.entity.medicine.YpZdChargeGroup;
import thyyxxk.webserver.entity.medicine.YpZdClass;
import thyyxxk.webserver.entity.medicine.YpZdDictVo;
import thyyxxk.webserver.entity.medicine.YpZdDosage;
import thyyxxk.webserver.entity.medicine.YpZdDrugKind;
import thyyxxk.webserver.entity.medicine.YpZdManuFactory;
import thyyxxk.webserver.entity.medicine.YpZdSupply;
import thyyxxk.webserver.entity.medicine.YpZdUnit;
import thyyxxk.webserver.utils.DecimalUtil;
import thyyxxk.webserver.utils.PingYinUtils;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

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;

/**
 * @ClassName YpDictService
 * @Author Administrator
 * @Date 2023/12/27 16:26
 * @Version 1.0
 * @Description 药品字典
 **/
@Service
@Slf4j
public class YpDictService {

    private final YpDictDao dao;

    private final YpZdClassDao ypClassDao;

    private final YpZdDrugKindDao drugKindDao;

    private final YpZdDosageDao ypDosageDao;

    private final YpZdUnitDao ypUnitDao;

    private final YpZdChargeGroupDao ypChargeGroupDao;

    private final YpZdSupplyDao ypSupplyDao;

    private final YpZdManuFactoryDao ypManuFactoryDao;

    private final EmployeeMiDao employeeMiDao;

    private final YpZdGroupNameDao ypZdGroupNameDao;

    public YpDictService(YpDictDao dao, YpZdClassDao ypClassDao, YpZdDrugKindDao drugKindDao,
                         YpZdDosageDao ypDosageDao, YpZdUnitDao ypUnitDao, YpZdChargeGroupDao ypChargeGroupDao,
                         YpZdSupplyDao ypSupplyDao, YpZdManuFactoryDao ypManuFactoryDao, EmployeeMiDao employeeMiDao,
                         YpZdGroupNameDao ypZdGroupNameDao) {
        this.dao = dao;
        this.ypClassDao = ypClassDao;
        this.drugKindDao = drugKindDao;
        this.ypDosageDao = ypDosageDao;
        this.ypUnitDao = ypUnitDao;
        this.ypChargeGroupDao = ypChargeGroupDao;
        this.ypSupplyDao = ypSupplyDao;
        this.ypManuFactoryDao = ypManuFactoryDao;
        this.employeeMiDao = employeeMiDao;
        this.ypZdGroupNameDao = ypZdGroupNameDao;
    }

    /**
     * @Description 查询药性字典信息
     * @Author hsh
     * @param text 关键字
     * @return List<YpZdClass>
     * @Date 2023/12/28 10:10
     */
    public ResultVo<List<YpZdClass>> selectYpClass(String text){
        return ResultVoUtil.success(ypClassDao.selectYpClass(text));
    }

    /**
     * @Description 保存药性字典信息
     * @Author hsh
     * @param ypZdClass 药性字典信息
     * @return map
     * @Date 2023/12/28 11:10
     */
    public ResultVo<Map<String, Object>> saveYpClass(YpZdClass ypZdClass){

        if(null == ypZdClass || ypZdClass.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药性字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = ypZdClass.getCode();
        YpZdClass ypClass = ypClassDao.selectYpClassByCode(code);

        // 根据名称生成新的拼音码和五笔码
        ypZdClass.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdClass.getName()));
        ypZdClass.setDCode(PingYinUtils.getWBCode(ypZdClass.getName()));
        int num;
        try{
            if(null != ypClass){
                num = ypClassDao.updateYpClassByCode(ypZdClass);
            } else {
                num = ypClassDao.insert(ypZdClass);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药性字典失败！");
            }
            resultMap.put("cg", "保存药性字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药性字典失败！");
        }
    }

    /**
     * @Description 删除药性字典信息
     * @Author hsh
     * @param code 药性字典编码
     * @return map
     * @Date 2023/12/28 15:06
     */
    public ResultVo<Map<String, Object>> delYpClassByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药性字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = ypClassDao.deleteYpClassByCode(code);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药性字典失败！");
            }
            resultMap.put("cg", "删除药性字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药性字典失败！");
        }
    }

    /**
     * @Description 查询药品分类字典信息
     * @Author hsh
     * @param text 关键字
     * @return YpZdDrugKind 药品分类字典信息
     * @Date 2023/12/28 17:03
     */
    public ResultVo<List<YpZdDrugKind>> selectDrugKind(String text){
        return ResultVoUtil.success(drugKindDao.selectDrugKind(text));
    }

    /**
     * @Description 保存药品分类字典信息
     * @Author hsh
     * @param drugKind 药品分类字典信息
     * @return map
     * @Date 2023/12/28 17:14
     */
    public ResultVo<Map<String, Object>> saveDrugKind(YpZdDrugKind drugKind){

        if(null == drugKind || drugKind.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品分类字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = drugKind.getCode();
        YpZdDrugKind kind = drugKindDao.selectDrugKindByCode(code);

        // 根据名称生成新的拼音码和五笔码
        drugKind.setPyCode(PingYinUtils.pyShouZiMuDaXie(drugKind.getName()));
        drugKind.setDCode(PingYinUtils.getWBCode(drugKind.getName()));
        int num;
        try{
            if(null != kind){
                num = drugKindDao.updateDrugKindByCode(drugKind);
            } else {
                num = drugKindDao.insert(drugKind);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品分类字典失败！");
            }
            resultMap.put("cg", "保存药品分类字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品分类字典失败！");
        }
    }

    /**
     * @Description 删除药品分类字典信息
     * @Author hsh
     * @param code 药品分类字典编码
     * @return map
     * @Date 2023/12/28 15:06
     */
    public ResultVo<Map<String, Object>> delDrugKindByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品分类字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = drugKindDao.deleteDrugKindByCode(code);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品分类字典失败！");
            }
            resultMap.put("cg", "删除药品分类字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品分类字典失败！");
        }
    }

    /**
     * @Description 查询药品剂型字典
     * @Author hsh
     * @param text 药品剂型字典编码
     * @return YpZdDosage 药品剂型字典信息
     * @Date 2023/12/29 11:18
     */
    public ResultVo<List<YpZdDosage>> selectYpDosage(String text){
        return ResultVoUtil.success(ypDosageDao.selectYpDosage(text));
    }

    /**
     * @Description 保存药品剂型字典
     * @Author hsh
     * @param ypZdDosage 药品剂型字典信息
     * @return map
     * @Date 2023/12/29 11:18
     */
    public ResultVo<Map<String, Object>> saveYpDosage(YpZdDosage ypZdDosage){

        if(null == ypZdDosage || ypZdDosage.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品剂型字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = ypZdDosage.getCode();
        YpZdDosage ypDosage = ypDosageDao.selectYpDosageByCode(code);

        // 根据名称生成新的拼音码和五笔码
        ypZdDosage.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdDosage.getName()));
        ypZdDosage.setDCode(PingYinUtils.getWBCode(ypZdDosage.getName()));
        int num;
        try{
            if(null != ypDosage){
                num = ypDosageDao.updateYpDosageByCode(ypZdDosage);
            } else {
                num = ypDosageDao.insert(ypZdDosage);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品剂型字典失败！");
            }
            resultMap.put("cg", "保存药品剂型字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品剂型字典失败！");
        }
    }

    /**
     * @Description 删除药品剂型字典信息
     * @Author hsh
     * @param code 药品剂型字典编码
     * @return map
     * @Date 2023/12/29 11:19
     */
    public ResultVo<Map<String, Object>> delYpDosageByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品剂型字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = ypDosageDao.deleteYpDosageByCode(code);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品剂型字典失败！");
            }
            resultMap.put("cg", "删除药品剂型字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品剂型字典失败！");
        }
    }

    /**
     * @Description 查询药品单位字典
     * @Author hsh
     * @param text 关键字
     * @return YpZdUnit 药品单位字典
     * @Date 2023/12/29 16:38
     */
    public ResultVo<List<YpZdUnit>> selectYpUnit(String text){
        return ResultVoUtil.success(ypUnitDao.selectYpUnit(text));
    }

    /**
     * @Description 保存药品单位字典
     * @Author hsh
     * @param ypZdUnit 药品单位字典
     * @return map
     * @Date 2023/12/29 16:39
     */
    public ResultVo<Map<String, Object>> saveYpUnit(YpZdUnit ypZdUnit){

        if(null == ypZdUnit || ypZdUnit.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品剂型字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = ypZdUnit.getCode();
        YpZdUnit ypUnit = ypUnitDao.selectYpUnitByCode(code);

        // 根据名称生成新的拼音码和五笔码
        ypZdUnit.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdUnit.getName()));
        ypZdUnit.setDCode(PingYinUtils.getWBCode(ypZdUnit.getName()));
        int num;
        try{
            if(null != ypUnit){
                num = ypUnitDao.updateYpUnitByCode(ypZdUnit);
            } else {
                num = ypUnitDao.insert(ypZdUnit);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品剂型字典失败！");
            }
            resultMap.put("cg", "保存药品剂型字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品剂型字典失败！");
        }
    }

    /**
     * @Description 删除药品单位字典
     * @Author hsh
     * @param code 药品单位字典编码
     * @return map
     * @Date 2023/12/29 16:40
     */
    public ResultVo<Map<String, Object>> delYpUnitByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品剂型字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = ypUnitDao.deleteYpUnitByCode(code);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品剂型字典失败！");
            }
            resultMap.put("cg", "删除药品剂型字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品剂型字典失败！");
        }
    }

    /**
     * @Description 查询药品费别字典信息
     * @Author hsh
     * @param text 关键字
     * @return YpZdChargeGroup 药品费别字典信息
     * @Date 2023/12/30 10:38
     */
    public ResultVo<List<YpZdChargeGroup>> selectYpChargeGroup(String text){
        return ResultVoUtil.success(ypChargeGroupDao.selectYpChargeGroup(text));
    }

    /**
     * @Description 保存药品费别字典信息
     * @Author hsh
     * @param ypZdChargeGroup 药品费别字典信息
     * @return map
     * @Date 2023/12/30 10:39
     */
    public ResultVo<Map<String, Object>> saveYpChargeGroup(YpZdChargeGroup ypZdChargeGroup){

        if(null == ypZdChargeGroup || ypZdChargeGroup.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品费别字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = ypZdChargeGroup.getCode();
        YpZdChargeGroup chargeGroup = ypChargeGroupDao.selectYpChargeGroupByCode(code);

        // 根据名称生成新的拼音码和五笔码
        ypZdChargeGroup.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdChargeGroup.getName()));
        ypZdChargeGroup.setDCode(PingYinUtils.getWBCode(ypZdChargeGroup.getName()));
        int num;
        try{
            if(null != chargeGroup){
                num = ypChargeGroupDao.updateYpChargeGroupByCode(ypZdChargeGroup);
            } else {
                num = ypChargeGroupDao.insert(ypZdChargeGroup);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品费别字典失败！");
            }
            resultMap.put("cg", "保存药品费别字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品费别字典失败！");
        }
    }

    /**
     * @Description 删除药品费别字典
     * @Author hsh
     * @param code 药品费别字典编码
     * @return map
     * @Date 2023/12/30 10:41
     */
    public ResultVo<Map<String, Object>> delYpChargeGroupByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品费别字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = ypChargeGroupDao.deleteYpChargeGroupByCode(code);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品费别字典失败！");
            }
            resultMap.put("cg", "删除药品费别字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品费别字典失败！");
        }
    }

    /**
     * @Description 查询药房药品是否停用信息
     * @Author hsh
     * @param text 关键字
     * @return map
     * @Date 2024/1/3 9:08
     */
    public ResultVo<List<Map<String, Object>>> selectYpVisibleFlag(String text){
        return ResultVoUtil.success(dao.selectYpVisibleFlag(text));
    }

    /**
     * @Description 更新药房药品是否停用
     * @Author hsh
     * @param map 药房药品信息
     * @return map
     * @Date 2024/1/3 9:09
     */
    public ResultVo<Map<String, Object>> updateYpVisibleFlag(Map<String, Object> map){
        Map<String, Object> resultMap = new HashMap<>();
        String chargeCode = String.valueOf(map.get("chargeCode"));
        String serial = String.valueOf(map.get("serial"));
        String groupNo = String.valueOf(map.get("groupNo"));
        // 门诊停用
        String visibleFlagMz = String.valueOf(map.get("visibleFlagMz"));
        // 住院停用
        String visibleFlagZy = String.valueOf(map.get("visibleFlagZy"));
        if(StringUtil.isBlank(chargeCode) || StringUtil.isBlank(serial) || StringUtil.isBlank(groupNo)){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码,包装类型或者药房编码不存在，请检查！");
        }
        int num = dao.updateYpVisibleFlag(chargeCode, serial, groupNo, visibleFlagMz, visibleFlagZy);
        if(num == 0){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药房药品是否停用失败！");
        }
        resultMap.put("cg", "更新药房药品是否停用成功！");
        return ResultVoUtil.success(resultMap);
    };

    /**
     * @Description 查询药品供应商字典信息
     * @Author hsh
     * @param text 关键字
     * @return YpZdSupply 药品供应商字典信息
     * @Date 2024/1/3 9:31
     */
    public ResultVo<List<YpZdSupply>> selectYpSupply(String text){
        return ResultVoUtil.success(ypSupplyDao.selectYpSupply(text));
    }

    /**
     * @Description 保存药品供应商字典信息
     * @Author hsh
     * @param ypZdSupply 药品供应商字典信息
     * @return map
     * @Date 2024/1/3 9:33
     */
    public ResultVo<Map<String, Object>> saveYpSupply(YpZdSupply ypZdSupply){

        if(null == ypZdSupply || ypZdSupply.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品供应商字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = ypZdSupply.getCode();
        YpZdSupply ypSupply = ypSupplyDao.selectYpSupplyByCode(code);

        // 根据名称生成新的拼音码和五笔码
        ypZdSupply.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdSupply.getName()).substring(0,9));
        ypZdSupply.setDCode(PingYinUtils.getWBCode(ypZdSupply.getName()).substring(0,9));
        int num;
        try{
            if(null != ypSupply){
                num = ypSupplyDao.updateYpSupplyByCode(ypZdSupply);
            } else {
                num = ypSupplyDao.insert(ypZdSupply);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品供应商字典失败！");
            }
            resultMap.put("cg", "保存药品供应商字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品供应商字典失败: " + e.getMessage());
        }
    }

    /**
     * @Description 删除药品供应商字典
     * @Author hsh
     * @param code 药品供应商字典编码
     * @return map
     * @Date 2024/1/3 9:34
     */
    public ResultVo<Map<String, Object>> delYpSupplyByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品供应商字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = ypSupplyDao.deleteYpSupplyByCode(code);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品供应商字典失败！");
            }
            resultMap.put("cg", "删除药品供应商字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品供应商字典失败: " + e.getMessage());
        }
    }

    /**
     * @Description 查询生产厂家字典信息
     * @Author hsh
     * @param text 关键字
     * @return YpZdManuFactory 药品生产厂家字典信息
     * @Date 2024/1/3 16:30
     */
    public ResultVo<List<YpZdManuFactory>> selectYpManuFactory(String text){
        return ResultVoUtil.success(ypManuFactoryDao.selectYpManuFactory(text));
    }

    /**
     * @Description 保存药品生产厂家字典信息
     * @Author hsh
     * @param ypZdManuFactory 药品生产厂家字典信息
     * @return map
     * @Date 2024/1/3 16:31
     */
    public ResultVo<Map<String, Object>> saveYpManuFactory(YpZdManuFactory ypZdManuFactory){

        if(null == ypZdManuFactory || ypZdManuFactory.getCode() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品生产厂家字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = ypZdManuFactory.getCode();
        YpZdManuFactory ypManuFactory = ypManuFactoryDao.selectYpManuFactoryByCode(code);

        // 根据名称生成新的拼音码和五笔码
        ypZdManuFactory.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdManuFactory.getName()));
        ypZdManuFactory.setDCode(PingYinUtils.getWBCode(ypZdManuFactory.getName()));
        int num;
        try{
            if(null != ypManuFactory){
                num = ypManuFactoryDao.updateYpManuFactoryByCode(ypZdManuFactory);
            } else {
                num = ypManuFactoryDao.insert(ypZdManuFactory);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品生产厂家字典失败！");
            }
            resultMap.put("cg", "保存药品生产厂家字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品生产厂家字典失败: " + e.getMessage());
        }
    }

    /**
     * @Description 删除药品生产厂家字典
     * @Author hsh
     * @param code 药品生产厂家字典编码
     * @return map
     * @Date 2024/1/3 16:32
     */
    public ResultVo<Map<String, Object>> delYpManuFactoryByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品生产厂家字典编码，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = ypManuFactoryDao.deleteYpManuFactoryByCode(code);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品生产厂家字典失败！");
            }
            resultMap.put("cg", "删除药品生产厂家字典成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品生产厂家字典失败: " + e.getMessage());
        }
    }

    /**
     * @Description 查询药品账页字典
     * @Author hsh
     * @param text 关键字
     * @return map 药品账页字典信息
     * @Date 2024/1/9 15:37
     */
    public ResultVo<List<Map<String, String>>> selectYpDict(String text, String groupNo, String isSee){
        List<Map<String, String>> list = dao.selectYpDict(text, groupNo, isSee);
        if(StringUtil.notBlank(groupNo) && list.isEmpty()){
            List<Map<String, String>> list1 = dao.selectYpDict(text, "", isSee);
            if(list1.size() == 1){
                // 查询所有药房是否存在检索的药品
                int count = dao.selectYpBaseCountByGroupNo(list1.get(0).get("code"));
                if(count == 0){
                    return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药库检索不到的此药品明细！");
                }
            } else if(list1.size() == 2){
                if(list1.get(0).get("code").equals(list1.get(1).get("code"))){
                    // 查询所有药房是否存在检索的药品
                    int count = dao.selectYpBaseCountByGroupNo(list1.get(0).get("code"));
                    if(count == 0){
                        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药库检索不到的此药品明细！");
                    }
                }
            }
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此药库不包含检索的药品明细！");
        }
        return ResultVoUtil.success(list);
    }

    /**
     * @Description 药品账页字典更新delFlag（是否停用）标志
     * @Author hsh
     * @param code 药品编码，delFlag 停用状态（1：停用）
     * @return map
     * @Date 2024/1/9 16:07
     */
    public ResultVo<Map<String, Object>> updateYpStopOrUsed(String code, String serial, String delFlag){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品编码，请检查！");
        }

        YpZdDict yp = dao.selectYpDictByCode(code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码(" + code + ")的药品账页字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpDictDelFlagByCode(code, delFlag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药品账页字典停用或启用失败！");
            }
            // 更新药房对应药品的状态
            dao.updateYpBaseYfDelFlagByCode(code, delFlag);
            resultMap.put("cg", "更新药品账页字典停用或启用成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药品账页字典停用或启用失败: " + e.getMessage());
        }

    }

    /**
     * @Description 根据药品编码和包装规格查询药品账页信息
     * @Author hsh
     * @param code 药品编码, serial 药品包装规格
     * @return YpZdDict 药品账页信息
     * @Date 2024/1/12 15:34
     */
    public ResultVo<YpZdDict> selectYpDictByCode(String code, String serial){
        return ResultVoUtil.success(dao.selectYpDictByCode(code, serial));
    }

    /**
     * @Description 根据药品编码查询药品账页信息
     * @Author hsh
     * @param code 药品编码
     * @return list 药品账页信息
     * @Date 2025/8/3 0003 15:25
     */
    public ResultVo<List<YpZdDict>> selectYpDictByCodeNoSerial(String code){
        return ResultVoUtil.success(dao.selectYpDictByCodeNoSerial(code));
    }

    /**
     * @Description 查询需要的药品字典集合
     * @Author hsh
     * @return map 药品字典集合
     * @Date 2024/1/17 17:48
     */
    public ResultVo<Map<String, List<CodeName>>> selectYpDictData(){
        Map<String, List<CodeName>> map = new HashMap<>();
        // 药性字典
        map.put("getYpClassCode", ypClassDao.selectYpClassCode());
        // 药品类别
        map.put("getYpKindCode", drugKindDao.selectDrugKindCode());
        // 剂型
        map.put("getYpDosageCode", ypDosageDao.selectYpDosageCode());
        // 单位
        map.put("getYpUnitCode", ypUnitDao.selectYpUnitCode());
        // 药品费别
        map.put("getYpChargeGroupCode", ypChargeGroupDao.selectYpChargeGroupCode());
        // 生产厂家字典
        map.put("getYpSupplyCode", ypSupplyDao.selectYpSupplyCode());
        // 药品供应商字典
        map.put("getYpManuFactoryCode", ypManuFactoryDao.selectYpManuFactoryCode());
        // 药品大类
        map.put("getYpCategoriesCode", dao.selectYpCategories());
        // 门诊收费码
        map.put("getMzBillItemCode", dao.selectMzBillItem());
        // 住院收费码
        map.put("getZyBillItemCode", dao.selectZyBillItem());
        // 医保类别
        map.put("getYpYbTypeCode", dao.selectYpYbType());
        // 给药方式(门诊)
        map.put("getMzSupplyCode", dao.selectMzSupply());
        // 执行频率(门诊)
        map.put("getMzFrequencyCode", dao.selectMzFrequency());
        // 核算码(住院)
        map.put("getZyAuditItemCode", dao.selectZyAuditItem());
        // 所有医生
        map.put("getEmployeeCode", employeeMiDao.selectEmployee());
        // 药品来源字典
        map.put("getYpFromCode", dao.selectYpFrom());
        // 药品抗菌药物字典
        map.put("getYpKjywCode", dao.selectYpKjywCode());
        return ResultVoUtil.success(map);
    }

    /**
     * @Description 药品账页信息保存
     * @Author hsh
     * @param ypZdDictVo 药品账页信息
     * @return map
     * @Date 2024/2/17 9:33
     */
    public ResultVo<Map<String, Object>> saveYpDict(YpZdDictVo ypZdDictVo){

        if(null == ypZdDictVo){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品账页字典信息不存在，请检查！");
        }
        int num = 0;
        Map<String, Object> resultMap = new HashMap<>();
        // 编辑
        if(StringUtils.isNotBlank(ypZdDictVo.getCode())){
            String code = ypZdDictVo.getCode();
            String serial = ypZdDictVo.getSerial();
            YpZdDict ypDict = dao.selectYpDictByCode(code, serial);
            if(null == ypDict){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, code + "没有需要更新的药品账页字典信息！");
            }
            YpZdDict zdDict = callYpVoChargeDict(ypZdDictVo);
            UpdateWrapper<YpZdDict> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("code",zdDict.getCode());
            updateWrapper.eq("serial",zdDict.getSerial());
            num = dao.update(zdDict, updateWrapper);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药品账页字典失败！");
            }
            // 更新其他序号的药品字典
            dao.updateYpZdDictSyncElseSerial(ypZdDictVo.getCode(), ypZdDictVo.getSerial());
            dao.updateYpBaseSync(ypZdDictVo.getCode(), ypZdDictVo.getSerial());
            // 更新药房药品价格
            // dao.updateYpBaseYfSync(ypZdDictVo.getCode(), ypZdDictVo.getSerial());
        } else {
            if(StringUtil.isBlank(ypZdDictVo.getGroupNo())){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "新增药品账页字典信息请选择药库！");
            }
            YpZdDrugKind ypKind = drugKindDao.selectDrugKindByCode(ypZdDictVo.getDrugKind());
            String ypType = ypKind.getYpType();
            YpTypeNumberEnum ypTypeNumber = YpTypeNumberEnum.getYpTypeNumberByCode(Integer.parseInt(ypType));
            assert ypTypeNumber != null;
            String maxCode = dao.selectMaxCode(ypTypeNumber.minNum, ypTypeNumber.maxNum);
            int initNum;
            if("1".equals(ypType)){
                initNum = 0;
            } else {
                initNum = Convert.toInt(ypTypeNumber.minNum, 0);
            }
            int code = Convert.toInt(maxCode,initNum) + 1;
            maxCode = callYpCodeFormat(code);
            // 药品编码与医嘱编码冲突重置编码
            int cs = dao.selectYzOrderItemByCode(maxCode);
            while (cs > 0) {
                code ++;
                maxCode = callYpCodeFormat(code);
                cs = dao.selectYzOrderItemByCode(maxCode);
            }
            ypZdDictVo.setCode(maxCode);
            String serial = ypZdDictVo.getMiniUnit().equals(ypZdDictVo.getPackUnit()) ? YpDictConstant.SERIAL_01 : YpDictConstant.SERIAL_99;
            ypZdDictVo.setSerial(serial);
            ypZdDictVo.setOperId("");
            YpZdDict copyVo = callYpVoChargeDict(ypZdDictVo);
            copyVo.setDrugId(copyVo.getCode());
            num = dao.insert(copyVo);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品账页字典失败！");
            }

            if(copyVo.getSerial().equals(YpDictConstant.SERIAL_99)){
                YpZdDict minYp = new YpZdDict();
                BeanUtils.copyProperties(copyVo, minYp, YpZdDict.class);
                minYp.setSerial(YpDictConstant.SERIAL_01);
                BigDecimal minPackPrice = DecimalUtil.divide(ypZdDictVo.getPackRetprice(), ypZdDictVo.getPackSize(), 3);
                minYp.setPackRetprice(minPackPrice.doubleValue());
                BigDecimal minBuyPrice = DecimalUtil.divide(ypZdDictVo.getBuyPrice(), ypZdDictVo.getPackSize(), 3);
                minYp.setBuyPrice(minBuyPrice.doubleValue());
                BigDecimal minYbxjPrice = DecimalUtil.divide(ypZdDictVo.getYbxjPrice(), ypZdDictVo.getPackSize(), 3);
                minYp.setYbxjPrice(minYbxjPrice.doubleValue());
                minYp.setPackSize(1d);
                int d2 = dao.insert(minYp);
            }
            YpPrintName ypPrintName = callYpPrintName(ypZdDictVo, "T");
            // 保存药品通用名
            dao.insertYpPrintName(ypPrintName);
            // 保存药品库房记录
            num = dao.insertYpBaseSync(ypZdDictVo.getCode(), serial, ypZdDictVo.getGroupNo());
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品账页字典初始化药库信息失败！");
            }
        }
        if(num > 0){
            resultMap.put("cg", "保存药品账页字典成功！");
        }
        return ResultVoUtil.success(resultMap);
    }

    private YpZdDict callYpVoChargeDict(YpZdDictVo ypZdDictVo) {
        YpZdDict copyVo = new YpZdDict();
        BeanUtils.copyProperties(ypZdDictVo, copyVo, YpZdDict.class);
        copyVo.setOperDate(new Date());
        // 根据名称生成新的拼音码和五笔码
        copyVo.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdDictVo.getName()));
        copyVo.setDCode(PingYinUtils.getWBCode(ypZdDictVo.getName()));
        copyVo.setNCode(ypZdDictVo.getNcode());
        copyVo.setPackSize(Convert.toDouble(ypZdDictVo.getPackSize()));
        copyVo.setPackRetprice(Convert.toDouble(ypZdDictVo.getPackRetprice()));
        copyVo.setRetprice(Convert.toDouble(ypZdDictVo.getRetprice()));
        copyVo.setBuyPrice(Convert.toDouble(ypZdDictVo.getBuyPrice()));
        copyVo.setYbxjPrice(Convert.toDouble(ypZdDictVo.getBuyPrice()));
        return copyVo;
    }

    private YpPrintName callYpPrintName(YpZdDictVo ypZdDictVo, String ypFlag) {
        YpPrintName name = new YpPrintName();
        name.setChargeCode(ypZdDictVo.getCode());
        name.setPrintName(ypZdDictVo.getName());
        name.setPyCode(PingYinUtils.pyShouZiMuDaXie(ypZdDictVo.getName()));
        name.setDCode(PingYinUtils.getWBCode(ypZdDictVo.getName()));
        // 1商品名 2别名 T通用名 d停用
        name.setYpFlag(ypFlag);
        return name;
    }

    private String callYpCodeFormat(int code) {
        String ypCode;
        if(code > 9999){
            ypCode = ""+code;
        }else if(code > 999){
            ypCode = "0"+code;
        }else if(code > 99){
            ypCode = "00"+code;
        }else if(code > 9){
            ypCode = "000"+code;
        }else{
            ypCode = "0000"+code;
        }
        return ypCode;
    }

    /**
     * @Description 根据药品编码(chargeCode)查询药品别名
     * @Author hsh
     * @param chargeCode 药品编码
     * @return YpPrintName 药品别名
     * @Date 2024/2/19 14:38
     */
    public ResultVo<List<YpPrintName>> selectYpPrintNameData(String chargeCode){
        return ResultVoUtil.success(dao.selectYpPrintNameByChargeCode(chargeCode));
    }

    public ResultVo<Map<String, Object>> savePrintName(YpPrintName ypPrintName){

        if(null == ypPrintName || ypPrintName.getPrintName() == null){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品别名信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        String code = ypPrintName.getChargeCode();
        String name = ypPrintName.getPrintName();
        YpPrintName printName = dao.selectYpPrintNameByPrintName(code, name);

        // 根据名称生成新的拼音码和五笔码
        String py = PingYinUtils.pyShouZiMuDaXie(ypPrintName.getPrintName());
        String wb = PingYinUtils.getWBCode(ypPrintName.getPrintName());
        ypPrintName.setPyCode(py.length() > 8 ? py.substring(0,8) : py);
        ypPrintName.setDCode(wb.length() > 8 ? wb.substring(0,8) : wb);
        int num;
        try{
            if(null != printName){
                num = dao.updateYpPrintNameByPrintName(ypPrintName);
            } else {
                num = dao.insertYpPrintName(ypPrintName);
            }
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品别名信息失败！");
            }
            resultMap.put("cg", "保存药品别名信息成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "保存药品别名信息失败: " + e.getMessage());
        }
    }

    /**
     * @Description 根据药品编码和别名删除药品别名信息
     * @Author hsh
     * @param chargeCode 药品编码
     * @param printName 药品别名
     * @return map
     * @Date 2024/2/19 16:55
     */
    public ResultVo<Map<String, Object>> delYpPrintNameByPrintName(String chargeCode, String printName) {
        if (StringUtil.isBlank(chargeCode) || StringUtil.isBlank(printName)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品编码或者别名，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.delYpPrintNameByPrintName(chargeCode, printName);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品别名信息失败！");
            }
            resultMap.put("cg", "删除药品别名信息成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "删除药品别名信息失败: " + e.getMessage());
        }
    }

    /**
     * @Description 查询药库或者药房
     * @Author hsh
     * @param text 关键字
     * @param yfFlag 药库药房标志(0：药库；1：药房)
     * @return list 药库或者药房
     * @Date 2024/2/21 14:56
     */
    public ResultVo<List<CodeName>> selectYpGroupNameByYfFlag(String text, String yfFlag){
        List<CodeName> list = ypZdGroupNameDao.selectYpGroupNameByYfFlag(text, yfFlag);
        return ResultVoUtil.success(list);
    }

    /**
     * @Description 查询药库以及药房
     * @Author hsh
     * @param text 关键字
     * @return list 药库以及药房
     * @Date 2024/2/29 15:19
     */
    public ResultVo<List<Map<String, Object>>> selectYpGroupNameAll(String text){
        List<CodeName> list = ypZdGroupNameDao.selectYpGroupNameAll(text);
        Map<Integer, List<CodeName>> map = list.stream().collect(Collectors.groupingBy(CodeName::getStatusFlag));
        List<Map<String, Object>> ll = new ArrayList<>();
        for(Map.Entry<Integer, List<CodeName>> entry: map.entrySet()){
            Integer key = entry.getKey();
            List<CodeName> value = entry.getValue();
            Map<String, Object> m = new HashMap<>();
            if(key == 0){
                m.put("label", "库房");
            } else if(key == 1){
                m.put("label", "药房");
            }
            m.put("key", key);
            m.put("options", value);
            ll.add(m);
        }
        return ResultVoUtil.success(ll);
    }

    /**
     * @Description 更新临购药品标志(1：是)
     * @Author hsh
     * @param code 药品编码 serial 序号 tempPurchaseFlag 临购药品标志
     * @return map
     * @Date 2024/4/23 9:57
     */
    public ResultVo<Map<String, Object>> updateYpTempPurchase(String code, String serial, String tempPurchaseFlag){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到药品编码，请检查！");
        }

        YpZdDict yp = dao.selectYpDictByCode(code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码(" + code + ")的药品账页字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpTempPurchaseByCode(code, tempPurchaseFlag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药品临购标志失败！");
            }
            resultMap.put("cg", "更新药品临购标志成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药品临购标志失败: " + e.getMessage());
        }

    }

    /**
     * @Description 药库药品更新delFlag（是否停用）标志
     * @Author hsh
     * @param groupNo 药库, code 药品编码, delFlag 停用状态（1：停用）
     * @return map
     * @Date 2024/4/23 15:46
     */
    public ResultVo<Map<String, Object>> updateYkYpStopOrUsed(String groupNo, String code, String serial, String delFlag){
        if(StringUtil.isBlank(groupNo)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药库编码，请检查！");
        }

        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有药品编码，请检查！");
        }

        Map<String, Object> yp = dao.selectYpBaseByCode(groupNo, code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药库药品编码(" + code + ")的药品信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpBaseDelFlagByCode(groupNo, code, delFlag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药库药品停用或启用失败！");
            }
            // 更新药库药品的状态
            dao.updateYpBaseNoDelFlagByCode(code, delFlag, groupNo);
            resultMap.put("cg", "更新药库药品停用或启用成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药库药品停用或启用失败: " + e.getMessage());
        }

    }

    /**
     * @Description 更新国家基本药物标志
     * @Author hsh
     * @param code 药品编码 serial 序号 jbFlag 国家基本药物标志
     * @return map
     * @Date 2024/5/11 9:31
     */
    public ResultVo<Map<String, Object>> updateYpJbFlag(String code, String serial, String jbFlag){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到药品编码，请检查！");
        }

        YpZdDict yp = dao.selectYpDictByCode(code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码(" + code + ")的药品账页字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpJbFlagByCode(code, jbFlag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药品国家基本药物标志失败！");
            }
            resultMap.put("cg", "更新药品国家基本药物标志成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新药品国家基本药物标志失败: " + e.getMessage());
        }

    }

    public ResultVo<Map<String, Object>> selectYpStorageInfo(String code, String groupNo){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到药品编码，请检查！");
        }
        Map<String, Object> resultMap = new HashMap<>();
        StringBuilder msg = new StringBuilder();
        if(StringUtil.isBlank(groupNo)){
            List<Map<String, Object>> msgYf = dao.selectYpStorageBaseYf(code);
            if(!msgYf.isEmpty()){
                for(Map<String, Object> yfMap : msgYf){
                    String groupName = (String) yfMap.get("groupName");
                    Double stockAmount = (Double) yfMap.get("stockAmount");
                    msg.append(groupName).append("剩余库存：").append(stockAmount).append("。 ");
                }
            }
            if(msg.length() > 0){
                resultMap.put("msg", msg);
            } else {
                resultMap.put("msg", "各药房无库存。");
            }
        } else {
            List<Map<String, Object>> msgYk = dao.selectYpStorageBase(code, groupNo);
            if(!msgYk.isEmpty()){
                for(Map<String, Object> ykMap : msgYk){
                    String groupName = (String) ykMap.get("groupName");
                    Double stockAmount = (Double) ykMap.get("stockAmount");
                    msg.append(groupName).append("剩余库存：").append(stockAmount).append("。 ");
                }
            }
            if(msg.length() > 0){
                resultMap.put("msg", msg);
            } else {
                resultMap.put("msg", "各药库无库存。");
            }
        }
        return ResultVoUtil.success(resultMap);
    }

    /**
     * @Description 更新高警示药品标志
     * @Author hsh
     * @param code 药品编码 serial 序号 flag 高警示药品标志
     * @return map
     * @Date 2024/9/2 16:32
     */
    public ResultVo<Map<String, Object>> updateYpHighWarning(String code, String serial, String flag){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到药品编码，请检查！");
        }

        YpZdDict yp = dao.selectYpDictByCode(code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码(" + code + ")的药品账页字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpHighWarningByCode(code, flag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新高警示药品标志失败！");
            }
            resultMap.put("cg", "更新高警示药品标志成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新高警示药品标志失败: " + e.getMessage());
        }

    }

    /**
     * @Description 更新国家组织药品集中采购中标药品标志
     * @Author hsh
     * @param code 药品编码 serial 序号 flag 国家组织药品集中采购中标药品标志
     * @return map
     * @Date 2024/9/2 17:15
     */
    public ResultVo<Map<String, Object>> updateYpWinningBidder(String code, String serial, String flag){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到药品编码，请检查！");
        }

        YpZdDict yp = dao.selectYpDictByCode(code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码(" + code + ")的药品账页字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpWinningBidderByCode(code, flag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新国家组织药品集中采购中标药品标志失败！");
            }
            resultMap.put("cg", "更新国家组织药品集中采购中标药品标志成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新国家组织药品集中采购中标药品标志失败: " + e.getMessage());
        }

    }

    /**
     * @Description 更新重点监控药品标志(1：是)
     * @Author hsh
     * @param code 药品编码 serial 序号 flag 重点监控药品标志
     * @return map
     * @Date 2024/9/10 10:45
     */
    public ResultVo<Map<String, Object>> updateYpFocusMonitor(String code, String serial, String flag){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到药品编码，请检查！");
        }

        YpZdDict yp = dao.selectYpDictByCode(code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码(" + code + ")的药品账页字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpFocusMonitorByCode(code, flag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新重点监控药品标志失败！");
            }
            resultMap.put("cg", "更新重点监控药品标志成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新重点监控药品标志失败: " + e.getMessage());
        }

    }

    /**
     * @Description 更新口服药品是否允许退药标志(1：允许退药)
     * @Author hsh
     * @param code 药品编码 serial 序号 flag 口服药品是否允许退药标志
     * @return map
     * @Date 2024/12/24 15:41
     */
    public ResultVo<Map<String, Object>> updateYpReturnFlag(String code, String serial, String flag){
        if(StringUtil.isBlank(code)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到药品编码，请检查！");
        }

        YpZdDict yp = dao.selectYpDictByCode(code, serial);
        if(null == yp){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "药品编码(" + code + ")的药品账页字典信息不存在，请检查！");
        }

        Map<String, Object> resultMap = new HashMap<>();
        try{
            int num = dao.updateYpReturnFlagByCode(code, flag);
            if(num == 0){
                return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新口服药品是否允许退药标志失败！");
            }
            resultMap.put("cg", "更新口服药品是否允许退药标志成功！");
            return ResultVoUtil.success(resultMap);
        } catch(Exception e){
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "更新口服药品是否允许退药标志失败: " + e.getMessage());
        }

    }

}
