李宇
2021-08-18 c3510da29a7974c6af7ff3de1ade3db429d6f848
src/main/java/com/nanometer/smartlab/service/OpeReagentStatusServiceImpl.java
@@ -9,12 +9,16 @@
import com.google.common.collect.ImmutableMap;
import com.nanometer.smartlab.dao.*;
import com.nanometer.smartlab.entity.*;
import com.nanometer.smartlab.entity.dto.PersonUseDetail;
import com.nanometer.smartlab.entity.enumtype.ValidFlag;
import com.nanometer.smartlab.util.ExcelUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@@ -40,9 +44,10 @@
   @Resource(name = "opeReagentStatusDao")
   OpeReagentStatusDao opeReagentStatusDao;
   @Lazy
   @Resource
   private OpeWarehouseReserveService opeWarehouseReserveService;
   @Lazy
   @Resource
   private OpeUseFlowService opeUseFlowService;
   @Resource
@@ -59,6 +64,8 @@
   private OpeUseFlowDao opeUseFlowDao;
   @Resource
   private OpeWarehouseReserveDao opeWarehouseReserveDao;
   @Resource
   private BaseRoleService baseRoleService;
    @Resource(name="baseMetaDao")
    BaseMetaDao baseMetaDao;
@@ -96,14 +103,26 @@
   @Transactional(propagation = Propagation.REQUIRED)
   public List<OpeReagentStatus> getOpeReagentStatusListForLab(String name, String articleNumber, Integer status,
                                            String reagentCode, String userId, Integer first, Integer pageSize) {
                                            String reagentCode, String userId,String labName, Integer first, Integer pageSize) {
      try {
         Map<String, Object> params = new HashMap<String, Object>();
         params.put("name", "%" + name + "%");
         params.put("articleNumber", articleNumber);
         params.put("status", status);
         addParamByUserId(userId, params);
         params.put("labName", labName);
//         addParamByUserId(userId, params);
         if (StringUtils.isNotBlank(userId)) {
            SysUser sysUser = sysUserService.getSysUser(userId);
            BaseRole baseRole = baseRoleService.getBaseRole(sysUser.getRoleId());
            //不是系统管理员 根据用户的课题组判断可视(用户所在课题组是否 在实验室的课题组下)
            if (!"系统管理员".equals(baseRole.getName())) {
               if (StringUtils.isBlank(sysUser.getProject())) {
                  return null;
               }
               params.put("project", sysUser.getProject());
            }
         }
         if (StringUtils.isNotBlank(reagentCode)) {
            params.put("reagentCode", "%" + reagentCode + "%");
         }
@@ -119,17 +138,29 @@
   @Transactional(propagation = Propagation.REQUIRED)
   public int getOpeReagentStatusTotalCountForLab(String name, String articleNumber, Integer status, String reagentCode,
                                  String userId) {
                                  String userId,String labName) {
      try {
         Map<String, Object> params = new HashMap<String, Object>();
         params.put("name", "%" + name + "%");
         addParamByUserId(userId, params);
//         addParamByUserId(userId, params);
         params.put("articleNumber", articleNumber);
         if (StringUtils.isNotBlank(userId)) {
            SysUser sysUser = sysUserService.getSysUser(userId);
            BaseRole baseRole = baseRoleService.getBaseRole(sysUser.getRoleId());
            //不是系统管理员 根据用户的课题组判断可视(用户所在课题组是否 在实验室的课题组下)
            if (!"系统管理员".equals(baseRole.getName())) {
               if (StringUtils.isBlank(sysUser.getProject())) {
                  return 0;
               }
               params.put("project", sysUser.getProject());
            }
         }
         if (StringUtils.isNotBlank(reagentCode)) {
            params.put("reagentCode", "%" + reagentCode + "%");
         }
         params.put("status", status);
         params.put("labName", labName);
         return this.opeReagentStatusDao.getOpeReagentStatusTotalCountForLab(params);
      } catch (DataAccessException e) {
         logger.error(e.getMessage(), e);
@@ -304,7 +335,8 @@
         // 不是管理员时,加入userid
         //if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()||sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) {
         if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()) {
         if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()
               && sysUser.getSeeFlag().getKey() != SeeFlag.LEADING.getKey()) {
         } else {
            params.put("userId", userId);
@@ -472,6 +504,67 @@
      }
   }
   @Override
   public boolean updateReagentStatus3(List<String> reagentCodes,String userId,String projectNum,String laboratoryId,String laboratoryContainerId) {
      try {
         for (String opeReagentStatusId : reagentCodes) {
            OpeReagentStatus opeReagentStatus = this.getOpeReagentStatus(opeReagentStatusId);
            opeReagentStatus.setUserId(userId);
            opeReagentStatus.setProjectNum(projectNum);
            //1.判断->试剂状态是否为在仓库
            if (isAllowWarehouseUse(opeReagentStatus)) {
               //1.1获得 试剂的库存选择批次
               List<OpeWarehouseReserve> owrList = this.opeWarehouseReserveService
                     .getOpeWarehouseReserveList(opeReagentStatus.getReagent().getId(), opeReagentStatus.getArticleNumber(), opeReagentStatus.getHouseId());
               //在同一个仓库有相同批次的试剂,根据时间早的,个数少的先扣除库存
               owrList.get(0).setReserve(owrList.get(0).getReserve() - 1);
               this.opeWarehouseReserveService.updateOpeWarehouseReserve(owrList.get(0));
            }
            //设置 试剂状态->领用待入库
            opeReagentStatus.setStatus(ArrivalStatus.NOREGISTER);
            opeReagentStatus.setHouseId(laboratoryId);
            opeReagentStatus.setContainerId(laboratoryContainerId);
            int row = this.updateOpeReagentStatus(opeReagentStatus);
            if (row == 0) {
               return false;
            }
            OpeUseFlow opeUseFlow = new OpeUseFlow();
            opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode());
            opeUseFlow.setStatus(opeReagentStatus.getStatus());
            opeUseFlow.setHouseId(laboratoryId);
            opeUseFlow.setContainerId(laboratoryContainerId);
            opeUseFlow.setUserId(opeReagentStatus.getUserId());
            opeUseFlow.setPlace(opeReagentStatus.getPlace());
            opeUseFlow.setRemainder(opeReagentStatus.getRemainder());
            Map<String, String> metaMap = new HashMap<>();
            metaMap.put("groupId", "operate_status");
            metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
            List<BaseMeta> baseMetaList = baseMetaDao.getBaseMetaList(metaMap);
            opeUseFlow.setOperateState(baseMetaList.get(0).getId());
            this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
         }
         return true;
      } catch (DuplicateKeyException ex) {
         logger.warn(ex.getMessage(), ex);
         throw new AlarmException(AlarmCode.DATA_DUPLICATE,
               MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
      } catch (DataIntegrityViolationException ex) {
         logger.warn(ex.getMessage(), ex);
         throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
      } catch (DataAccessException ex) {
         logger.error(ex.getMessage(), ex);
         throw new BusinessException(ExceptionEnumCode.DB_ERR,
               MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
      }catch(Exception e){
         e.printStackTrace();
         throw new RuntimeException(e);
      }
   }
   @Transactional(propagation = Propagation.REQUIRED)
   public boolean updateReagentStatus(OpeReagentStatus opeReagentStatus) {
      try {
@@ -483,17 +576,11 @@
            opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() - 1);
            this.opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve);
         }
         opeReagentStatus.setStatus(ArrivalStatus.NOREGISTER);
//         opeReagentStatus.setHouseId(null);
//         opeReagentStatus.setContainerId(null);
         int row = this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus);
         if (row == 0) {
            return false;
         }
         OpeUseFlow opeUseFlow = new OpeUseFlow();
         opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode());
         opeUseFlow.setStatus(opeReagentStatus.getStatus());
@@ -508,9 +595,7 @@
         metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
         List<BaseMeta> baseMetaList = baseMetaDao.getBaseMetaList(metaMap);
         opeUseFlow.setOperateState(baseMetaList.get(0).getId());
         this.opeUseFlowService.insertOpeUseFlow(opeUseFlow);
         return true;
      } catch (DuplicateKeyException ex) {
         logger.warn(ex.getMessage(), ex);
@@ -526,7 +611,6 @@
      }
   }
   @Transactional(propagation = Propagation.REQUIRED)
   public OpeReagentStatus getOpeReagentStatus(String id) {
      try {
         return this.opeReagentStatusDao.getOpeReagentStatus(id);
@@ -537,7 +621,6 @@
      }
   }
   @Transactional(propagation = Propagation.REQUIRED)
   public OpeReagentStatus getOpeReagentStatusByReagentCode2(OpeReagentStatus opeReagentStatus) {
      try {
         OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(opeReagentStatus.getReagentCode());
@@ -550,7 +633,6 @@
      }
   }
   @Transactional(propagation = Propagation.REQUIRED)
   public OpeReagentStatus getOpeReagentStatusByReagentCode(String reagentCode) {
      try {
         OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(reagentCode);
@@ -757,15 +839,15 @@
   */
   @Override
   @Transactional
   public void batchUpdateStatusByCode(List<OpeReagentStatus> list) throws Exception {
      if(list == null || list.size() <1){
         throw new Exception("没有选择试剂");
      }
   public void batchUpdateStatusByCode(List<PersonUseDetail> applyList) throws Exception {
      for (OpeReagentStatus opeReagentStatus : list) {
      for (PersonUseDetail pu : applyList) {
         //变更试剂状态为入库
         Map<String, Object> params1 = new HashMap<>();
         String reagentCode = opeReagentStatus.getReagentCode();
         String reagentCode = pu.getReagentCode();
         params1.put("code", reagentCode);
         params1.put("status",ArrivalStatus.WAREHOUSE);
         opeReagentStatusDao.updateStatusByCode(params1);
@@ -779,8 +861,8 @@
         //增加数据库存
         Map<String, Object> params3 = new HashMap<>();
         String reagentId = opeReagentStatus.getReagentId();
         String articleNumber = opeReagentStatus.getArticleNumber();
         String reagentId = pu.getReagentId();
         String articleNumber = pu.getArticleNumber();
         params3.put("reagentId", reagentId);
         params3.put("articleNumber", articleNumber);
         opeWarehouseReserveDao.updateCount(params3);
@@ -884,11 +966,12 @@
      List<String> codeList= new ArrayList<>();
      //1.做24位的字母判断否则就是纯数字
      if (startReagentCode2.matches(".*[A-F]+.*")) {
      if (startReagentCode2.matches(".*\\D+.*")) {
         //长度为24并且后5位随机码得是数字
         if (startReagentCode2.length() == 24 &&
               endReagentCode2.length() == 24 &&
               startReagentCode2.substring(startReagentCode2.length()-5).matches("/d{5}")) {
               startReagentCode2.matches(".*([A-F]|\\d)+.*") &&
               startReagentCode2.substring(startReagentCode2.length()-5).matches("\\d{5}")) {
            //24位指定编码生成
            Integer randomStart = Integer.valueOf(startReagentCode2.substring(startReagentCode2.length() - 5));
            Integer randomEnd = Integer.valueOf(endReagentCode2.substring(endReagentCode2.length() - 5));
@@ -898,7 +981,7 @@
            if (randomEnd - randomStart + 1 != arrivalNum||!regentPrefix2.equals(regentPrefix)) {
               throw new BusinessException(ExceptionEnumCode.PARAM_EXIST,"试剂的开始结束条码有问题");
            }
            for (int random = randomStart; random < randomEnd; random++) {
            for (int random = randomStart; random <= randomEnd; random++) {
               String random5 = String.format("%0" + 5 + "d", random);
               String reagentCode = regentPrefix + random5;
               OpeReagentStatus opeReagentStatus = this.getOpeReagentStatusByReagentCode(reagentCode);
@@ -941,4 +1024,118 @@
   }
   @Override
   public List<String> generateReagentCode(String startReagentCode2, String endReagentCode2) {
      if (startReagentCode2 == null || startReagentCode2.length() < 1) {
         throw new BusinessException(ExceptionEnumCode.REAGENT_CODE_INVALID,"试剂条码不合法");
      }
      if (endReagentCode2 == null || endReagentCode2.length() < 1) {
         throw new BusinessException(ExceptionEnumCode.REAGENT_CODE_INVALID,"试剂条码不合法");
      }
      List<String> codeList= new ArrayList<>();
      //1.做24位的字母判断否则就是纯数字
      if (startReagentCode2.matches(".*\\D+.*")) {
         //长度为24并且后5位随机码得是数字
         if (startReagentCode2.length() == 24 &&
               endReagentCode2.length() == 24 &&
               startReagentCode2.matches(".*([A-F]|\\d)+.*") &&
               startReagentCode2.substring(startReagentCode2.length()-5).matches("\\d{5}")) {
            //24位指定编码生成
            Integer randomStart = Integer.valueOf(startReagentCode2.substring(startReagentCode2.length() - 5));
            Integer randomEnd = Integer.valueOf(endReagentCode2.substring(endReagentCode2.length() - 5));
            String regentPrefix = startReagentCode2.substring(0, startReagentCode2.length() - 5);
            for (int random = randomStart; random <= randomEnd; random++) {
               String random5 = String.format("%0" + 5 + "d", random);
               String reagentCode = regentPrefix + random5;
               codeList.add(reagentCode);
            }
            return codeList;
         }else{
            throw new BusinessException(ExceptionEnumCode.REAGENT_CODE_INVALID,"试剂条码不合法");
         }
      }
      //2.纯数字编码列表生成
      BigInteger reagentCode= new BigInteger(startReagentCode2);
      BigInteger endReagentCode = new BigInteger(endReagentCode2);
      while (reagentCode.compareTo(endReagentCode) <= 0) {
         String reagentCodeStr =  String.format("%0" + startReagentCode2.length() + "d", reagentCode);
         codeList.add(reagentCodeStr);
         reagentCode = reagentCode.add(BigInteger.ONE);
      }
      return codeList;
   }
   @Override
   public int updateOpeReagentStatus(OpeReagentStatus opeReagentStatus) {
      return opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus);
   }
   @Override
   public int getReagentNumInWarehouse(String id, String articleNumber, String warehouseId) {
      Map<String, Object> params = new HashMap<>();
      params.put("reagentId", id);
      params.put("articleNumber", articleNumber);
      params.put("warehouseId", warehouseId);
      return opeReagentStatusDao.countReagentByArticleAndWarehouse(params);
   }
   /**
    * @Description: 导出
    * @date 2021/4/22 10:33
    */
   @Override
   public List<Map> selectExportList(String name, String articleNumber, Integer status, String reagentCode, String userId,String labName) {
      Map<String, Object> params = new HashMap<>();
      params.put("name", name);
      params.put("articleNumber", articleNumber);
      params.put("status", status);
      params.put("labName", labName);
      if (StringUtils.isNotBlank(userId)) {
         SysUser sysUser = sysUserService.getSysUser(userId);
         BaseRole baseRole = baseRoleService.getBaseRole(sysUser.getRoleId());
         //不是系统管理员 根据用户的课题组判断可视(用户所在课题组是否 在实验室的课题组下)
         if (!"系统管理员".equals(baseRole.getName())) {
            if (StringUtils.isBlank(sysUser.getProject())) {
               return null;
            }
            params.put("project", sysUser.getProject());
         }
      }
      params.put("reagentCode", reagentCode);
      return opeReagentStatusDao.selectExportList(params);
   }
   @Override
   public void exportLabStock2Excel(List<Map> list) throws Exception {
      Map<String, String> map = new LinkedHashMap<>();
      map.put("houseName", "实验室名称");
      map.put("containerCode", "临时存储库条码");
      map.put("reagentName", "试剂名称");
      map.put("reagentCode", "试剂条形码");
      map.put("articleNumber", "批号");
      map.put("cas", "cas");
      map.put("productHome", "厂家");
      map.put("supplierName", "供应商");
      map.put("remainder", "残存量");
      ExcelUtils.export2Excel(list,"实验室库存",map);
   }
    @Override
    public OpeReagentStatus getStatus(String reagentId, String reagentCode) {
      return opeReagentStatusDao.getStatus(reagentId,reagentCode);
    }
}