kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
src/main/java/com/nanometer/smartlab/service/SysWarehouseServiceImpl.java
@@ -1,7 +1,13 @@
package com.nanometer.smartlab.service;
import com.nanometer.smartlab.dao.BaseMetaDao;
import com.nanometer.smartlab.dao.OpeReagentStatusDao;
import com.nanometer.smartlab.dao.SysWarehouseDao;
import com.nanometer.smartlab.entity.SysWarehouse;
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.entity.dto.InWarehouseInfoDto;
import com.nanometer.smartlab.entity.dto.ReagentReceivingDto;
import com.nanometer.smartlab.entity.dto.SysWarehouseDto;
import com.nanometer.smartlab.entity.enumtype.*;
import com.nanometer.smartlab.exception.AlarmCode;
import com.nanometer.smartlab.exception.AlarmException;
import com.nanometer.smartlab.exception.BusinessException;
@@ -18,10 +24,8 @@
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * Created by cmower on 17/11/20.
@@ -33,6 +37,24 @@
    @Resource(name = "sysWarehouseDao")
    SysWarehouseDao sysWarehouseDao;
    @Resource
    DangerousEncodeService dangerousEncodeService;
    @Resource
    BaseMetaService baseMetaService;
    @Resource
    BaseMetaDao baseMetaDao;
    @Resource
    SysReagentService sysReagentService;
    @Resource
    OpeReagentStatusService opeReagentStatusService;
    @Resource
    OpeUseFlowService opeUseFlowService;
    @Resource
    OpeWarehouseReserveService opeWarehouseReserveService;
    @Resource
    private SysLaboratoryService sysLaboratoryService;
    @Resource
    private OpeReagentStatusDao opeReagentStatusDao;
    @Transactional(propagation = Propagation.REQUIRED)
    public List<SysWarehouse> getSysWarehouseList(String type, String name, Integer first, Integer pageSize) {
@@ -70,7 +92,7 @@
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public SysWarehouse getSysWarehouse(String id) {
    public SysWarehouse getSysWarehouse(Long id) {
        try {
            return this.sysWarehouseDao.getSysWarehouse(id);
        } catch (DataAccessException e) {
@@ -82,9 +104,9 @@
    @Transactional(propagation = Propagation.REQUIRED)
    public SysWarehouse insertSysWarehouse(SysWarehouse sysWarehouse) {
        try {
            if (sysWarehouse.getId() == null) {
                sysWarehouse.setId(IDUtils.uuid());
            }
//            if (sysWarehouse.getId() == null) {
//                sysWarehouse.setId(IDUtils.uuid());
//            }
            this.sysWarehouseDao.insertSysWarehouse(sysWarehouse);
            return sysWarehouse;
        } catch (DuplicateKeyException ex) {
@@ -127,7 +149,7 @@
                return false;
            }
            List<String> ids = new ArrayList<String>();
            List<Long> ids = new ArrayList<Long>();
            for (SysWarehouse sysWarehouse : sysWarehouseList) {
                ids.add(sysWarehouse.getId());
            }
@@ -149,9 +171,9 @@
   public List<SysWarehouse> getAllSysWarehouseList() {
      return this.sysWarehouseDao.getAllSysWarehouseList();
   }
   @Transactional(propagation = Propagation.REQUIRED)
    public boolean isSysWarehouseExist(String barCode, String editId) {
    public boolean isSysWarehouseExist(String barCode, Long editId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("barCode", barCode);
@@ -164,4 +186,203 @@
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
    @Override
    public List<SysWarehouseDto> getWarehouseList() {
        return sysWarehouseDao.selectWarehouse();
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void putInWarehouse(InWarehouseInfoDto inWarehouseInfo) throws Exception {
        List<String> codes = inWarehouseInfo.getReagentCode();
        if (codes !=null && codes.size()>0){
            //
            codes.forEach(code->{
             //获取试剂名称码 5到8位
                String reagentCode = code.substring(4, 8);
                //获取厂商编码 第3位
                String factoryCode = code.substring(2, 3);
                //获取规格  第4位
                String specificationsCode = code.substring(3, 4);
                //获取包装  第19位
                String packingCode = code.substring(18, 19);
                //厂商
                BaseMeta factory = baseMetaService
                        .getBaseMeta("encode_replace_"+ReplaceDictType.factory.getKey(), factoryCode,null);
                //规格
                BaseMeta specifications = baseMetaService
                        .getBaseMeta("encode_replace_"+ReplaceDictType.specifications.getKey(), specificationsCode,null);
                //厂商信息
                BaseMeta packing= baseMetaService
                        .getBaseMeta("encode_replace_"+ReplaceDictType.packing.getKey(), packingCode,null);
                //Todo 校验
                DangerousEncode dangerousEncode = new DangerousEncode();
                //数据库存储的前缀00空着
                dangerousEncode.setReagentCode("00"+reagentCode);
                List<DangerousEncode> dangerousEncodes =
                        dangerousEncodeService.selectAll(dangerousEncode,null,null);
                String reagentName = dangerousEncodes.get(0).getReagentName();
                String cas = dangerousEncodes.get(0).getCas();
                //危险性质 reagent_character
                String dangerousInfo = dangerousEncodes.get(0).getMemo();
                BaseMeta meta = baseMetaService.getBaseMeta("reagent_character", null, dangerousInfo);
                BaseMeta baseMeta = new BaseMeta();
                if (meta == null){
                 //   baseMeta.setId(IDUtils.uuid());
                    baseMeta.setGroupCode("reagent_character");
                    baseMeta.setValidFlag(ValidFlag.VALID);
                    baseMeta.setMetaValue(dangerousInfo);
                    baseMeta.setMetaKey(dangerousInfo);
                    baseMetaService.insertBaseMeta(baseMeta);
                }else{
                    baseMeta = meta;
                }
                //查找是否存在试剂
                List<SysReagent> reagent = sysReagentService
                        .getReagent(reagentName, cas, factory.getId(), specifications.getId(), packing.getId());
                    OpeReagentStatus ors = new OpeReagentStatus();
                if (reagent != null &&reagent.size()>0){
                    //存在试剂
                    Long reagentId = reagent.get(0).getId();
                    ors.setReagentId(reagentId);
                }else{
                    //试剂不存在
                    SysReagent sr = new SysReagent();
                 //   sr.setId(IDUtils.uuid());
                    sr.setCas(cas);
                    sr.setName(reagentName);
                //    sr.setProductHome(factory.getId());
//                    sr.setReagentFormat(specifications.getId());
//                    sr.setReagentUnit(packing.getId());
//                    //危险性质
//                    sr.setReagentCharacter(baseMeta.getId());
                    //设备生成试剂类型 为3
                    sr.setType(3);
                    sr.setDangerousFlag(DangerousFlag.NORMAL);
                    SysReagent sysReagent = sysReagentService.insertSysReagent(sr);
                    Long reagentId = sysReagent.getId();
                    ors.setReagentId(reagentId);
                }
              //  ors.setId(IDUtils.uuid());
                //入仓库
                ors.setStatus(ArrivalStatus.WAREHOUSE.getKey());
                //批号
                ors.setArticleNumber(inWarehouseInfo.getArticleNumber());
                ors.setValidFlag(ValidFlag.VALID.getKey());
                ors.setContainerId(inWarehouseInfo.getContainerId());
                ors.setHouseId(inWarehouseInfo.getWarehouseId());
                ors.setUserId(inWarehouseInfo.getUser());
                ors.setReagentCode(code);
                opeReagentStatusService.insertOpeReagentStatus(ors);
                //更新试剂流向
                OpeUseFlow opeUseFlow = new OpeUseFlow();
                //入仓库
                opeUseFlow.setStatus(ArrivalStatus.WAREHOUSE.getKey());
                opeUseFlow.setContainerId(inWarehouseInfo.getContainerId());
                opeUseFlow.setHouseId(inWarehouseInfo.getWarehouseId());
                opeUseFlow.setStoreType(StoreType.DIRECTSTORE.getKey());
                opeUseFlow.setReagentCode(code);
                //持有者
                opeUseFlow.setUserId(inWarehouseInfo.getUser());
                opeUseFlow.setOperateState(OperateStatus.WAREHOUSEIN.getKey());
                opeUseFlowService.insertOpeUseFlow(opeUseFlow);
                //更新仓库库存
                // 仓库库存update
                OpeWarehouseReserve opeWarehouseReserve = opeWarehouseReserveService
                        .getOpeWarehouseReserve2(ors.getReagentId(), inWarehouseInfo.getArticleNumber(), inWarehouseInfo.getWarehouseId());
                if (opeWarehouseReserve == null) {
                    opeWarehouseReserve = new OpeWarehouseReserve();
                    opeWarehouseReserve.setReagentId(ors.getReagentId());
                    opeWarehouseReserve.setArticleNumber(inWarehouseInfo.getArticleNumber());
                    opeWarehouseReserve.setReserve(0);
                    opeWarehouseReserve.setWarehouseId(inWarehouseInfo.getWarehouseId());
                    opeWarehouseReserve.setUserId(1L);
                }
                //库存加1
                opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() + 1);
                if (opeWarehouseReserve.getId()!=null) {
                    this.opeWarehouseReserveService.insertOpeWarehouseReserve(opeWarehouseReserve);
                } else {
                    this.opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve);
                }
            });
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void reagentReceiving(ReagentReceivingDto reagentReceiving) {
        String number = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String receiptNum = "BS" + number;
        reagentReceiving.getReagentCode().forEach(code->{
                //
            OpeReagentStatus ors = opeReagentStatusService.getOpeReagentStatusByReagentCode(code);
            //1.更新试剂状态  在仓库-》领用
//            ors.setStatus(ArrivalStatus.PERSONAL);
            ors.setContainerId(reagentReceiving.getLabContainerId());
            ors.setHouseId(reagentReceiving.getLabId());
            ors.setUserId(reagentReceiving.getApplyUserId());
            if (opeReagentStatusService.isAllowWarehouseUse(ors)) {
                // 减少库存
                OpeWarehouseReserve opeWarehouseReserve = opeWarehouseReserveService.getOpeWarehouseReserve(
                        ors.getReagent().getId(), ors.getArticleNumber());
                opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() - 1);
                opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve);
            }
            ors.setStatus(ArrivalStatus.NOREGISTER.getKey());
            opeReagentStatusDao.updateOpeReagentStatusDao(ors);
            OpeUseFlow opeUseFlow = new OpeUseFlow();
            opeUseFlow.setReagentCode(ors.getReagentCode());
            opeUseFlow.setStatus(ors.getStatus());
            opeUseFlow.setHouseId(ors.getHouseId());
            opeUseFlow.setContainerId(ors.getContainerId());
            opeUseFlow.setUserId(ors.getUserId());
            opeUseFlow.setPlace(ors.getPlace());
            opeUseFlow.setRemainder(ors.getRemainder());
            //新增 领用单号
            opeUseFlow.setReceiptNumber(receiptNum);
            opeUseFlow.setOperateState(OperateStatus.WAREHOUSEOUT.getKey());
            this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
        });
    }
    @Override
    @Transactional
    public void updateSysWarehouse2(SysWarehouse sysWarehouse, SysLaboratory sysLaboratory) {
        //1.0更新自己
        this.updateSysWarehouse(sysWarehouse);
        //2.更新实验室
        sysLaboratory.setInfoCode(sysWarehouse.getInfoCode());
        sysLaboratory.setBarCode(sysWarehouse.getBarCode());
        sysLaboratory.setLocation1(sysWarehouse.getLocation1());
        sysLaboratory.setLocation2(sysWarehouse.getLocation2());
        sysLaboratory.setDepartment(sysWarehouse.getDepartment());
        sysLaboratoryService.updateSysLaboratory(sysLaboratory);
    }
    @Override
    public List<Map> getAllWarehouse() {
        return sysWarehouseDao.selectAllWarehouse();
    }
}