package com.nanometer.smartlab.service; import java.math.BigInteger; import java.sql.Timestamp; import java.util.*; import javax.annotation.Resource; 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; import com.nanometer.smartlab.entity.enumtype.ArrivalStatus; import com.nanometer.smartlab.entity.enumtype.OperateStatus; import com.nanometer.smartlab.entity.enumtype.SeeFlag; import com.nanometer.smartlab.exception.AlarmCode; import com.nanometer.smartlab.exception.AlarmException; import com.nanometer.smartlab.exception.BusinessException; import com.nanometer.smartlab.exception.ExceptionEnumCode; import com.nanometer.smartlab.util.IDUtils; import com.nanometer.smartlab.util.MessageUtil; import org.springframework.util.Assert; /** * Created by maweiqing on 17/12/12. */ @Service("opeReagentStatusService") public class OpeReagentStatusServiceImpl implements OpeReagentStatusService { private static Logger logger = Logger.getLogger(OpeReagentStatusService.class); @Resource(name = "opeReagentStatusDao") OpeReagentStatusDao opeReagentStatusDao; @Lazy @Resource private OpeWarehouseReserveService opeWarehouseReserveService; @Lazy @Resource private OpeUseFlowService opeUseFlowService; @Resource private SysUserService sysUserService; @Resource private SysLaboratoryService sysLaboratoryService; @Resource private SysLaboratoryContainerDao sysLaboratoryContainerDao; @Resource private SysWarehouseContainerDao sysWarehouseContainerDao; @Resource private SysLaboratoryContainerService sysLaboratoryContainerService; @Resource private OpeUseFlowDao opeUseFlowDao; @Resource private OpeWarehouseReserveDao opeWarehouseReserveDao; @Resource private BaseRoleService baseRoleService; @Resource(name="baseMetaDao") BaseMetaDao baseMetaDao; /* * (non-Javadoc) * * @see com.nanometer.smartlab.service.OpeReagentStatusService# * getOpeReagentStatusList(java.lang.String, java.lang.String, * java.lang.Integer, java.lang.String, java.lang.String, java.lang.Integer, * java.lang.Integer) */ @Transactional(propagation = Propagation.REQUIRED) public List getOpeReagentStatusList(String reagentId, String articleNumber, Integer status, String reagentCode, String userId, Integer first, Integer pageSize) { try { Map params = new HashMap(); params.put("reagentId", reagentId); params.put("articleNumber", articleNumber); params.put("status", status); addParamByUserId(userId, params); if (StringUtils.isNotBlank(reagentCode)) { params.put("reagentCode", "%" + reagentCode + "%"); } params.put("first", first); params.put("pageSize", pageSize); return this.opeReagentStatusDao.getOpeReagentStatusList(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public List getOpeReagentStatusList22(String reagentId, String articleNumber, Integer status, String reagentCode, String userId, Integer first, Integer pageSize) { try { Map params = new HashMap(); params.put("reagentId", reagentId); params.put("articleNumber", articleNumber); params.put("status", status); addParamByUserId(userId, params); if (StringUtils.isNotBlank(reagentCode)) { params.put("reagentCode", "%" + reagentCode + "%"); } params.put("first", first); params.put("pageSize", pageSize); return this.opeReagentStatusDao.getOpeReagentStatusList22(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public List getOpeReagentStatusListForLab(String name, String articleNumber, Integer status, String reagentCode, String userId,String labName, Integer first, Integer pageSize) { try { Map params = new HashMap(); params.put("name", "%" + name + "%"); params.put("articleNumber", articleNumber); params.put("status", status); 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 + "%"); } params.put("first", first); params.put("pageSize", pageSize); return this.opeReagentStatusDao.getOpeReagentStatusListForLab(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public int getOpeReagentStatusTotalCountForLab(String name, String articleNumber, Integer status, String reagentCode, String userId,String labName) { try { Map params = new HashMap(); params.put("name", "%" + name + "%"); // 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); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public int getOpeReagentStatusTotalCount(String reagentId, String articleNumber, Integer status, String reagentCode, String userId) { try { Map params = new HashMap(); params.put("reagentId", reagentId); addParamByUserId(userId, params); params.put("articleNumber", articleNumber); if (StringUtils.isNotBlank(reagentCode)) { params.put("reagentCode", "%" + reagentCode + "%"); } params.put("status", status); return this.opeReagentStatusDao.getOpeReagentStatusTotalCount(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public List getOpeReagentStatusListByName(String reagentId, String articleNumber, Integer status, String reagentCode, String userId, Integer first, Integer pageSize) { try { Map params = new HashMap(); params.put("reagentName", reagentId); params.put("articleNumber", articleNumber); params.put("status", status); addParamByUserId(userId, params); if (StringUtils.isNotBlank(reagentCode)) { params.put("reagentCode", "%" + reagentCode + "%"); } params.put("first", first); params.put("pageSize", pageSize); return this.opeReagentStatusDao.getOpeReagentStatusList(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public int getOpeReagentStatusTotalCountByName(String reagentId, String articleNumber, Integer status, String reagentCode, String userId) { try { Map params = new HashMap(); params.put("reagentName", reagentId); addParamByUserId(userId, params); params.put("articleNumber", articleNumber); if (StringUtils.isNotBlank(reagentCode)) { params.put("reagentCode", "%" + reagentCode + "%"); } params.put("status", status); return this.opeReagentStatusDao.getOpeReagentStatusTotalCount(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Override public int getPersonReagentStatusTotalCountByName(String reagentId, String articleNumber, String reagentCode, String userId,String applyPerson,Date startTime,Date endTime) { try { Map params = new HashMap(); params.put("reagentName", reagentId); addParamByUserId(userId, params); params.put("articleNumber", articleNumber); if (StringUtils.isNotBlank(reagentCode)) { params.put("reagentCode", "%" + reagentCode + "%"); } params.put("applyPerson", applyPerson); params.put("startTime", startTime); params.put("endTime", endTime); return this.opeReagentStatusDao.getPersonReagentStatusTotalCount(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Override public List getPersonReagentStatusListByName(String reagentId, String articleNumber, String reagentCode, String userId,String applyPerson,Date startTime,Date endTime, Integer first, Integer pageSize) { try { Map params = new HashMap(); params.put("reagentName", reagentId); params.put("articleNumber", articleNumber); addParamByUserId(userId, params); if (StringUtils.isNotBlank(reagentCode)) { params.put("reagentCode", "%" + reagentCode + "%"); } params.put("applyPerson", applyPerson); params.put("startTime", startTime); params.put("endTime", endTime); params.put("first", first); params.put("pageSize", pageSize); return this.opeReagentStatusDao.getPersonReagentStatusList(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public List getLogOpeReagentStatusList(String reagentId, Integer status, String userId, String loginId, Integer first, Integer pageSize) { try { Map params = new HashMap(); params.put("reagentId", reagentId); addParamByLoginId(userId, loginId, params); params.put("status", status); params.put("first", first); params.put("pageSize", pageSize); return this.opeReagentStatusDao.getOpeReagentStatusList(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public int getLogOpeReagentStatusTotalCount(String reagentId, Integer status, String userId, String loginId) { try { Map params = new HashMap(); params.put("reagentId", reagentId); addParamByLoginId(userId, loginId, params); params.put("status", status); return this.opeReagentStatusDao.getOpeReagentStatusTotalCount(params); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } /** * 实验室库存管理用 * * @param userId * @param params */ private void addParamByUserId(String userId, Map params) { // 领用操作对话框的userid为空 // 只有实验室管理时userid才不为空 if (StringUtils.isNotBlank(userId)) { SysUser sysUser = sysUserService.getSysUser(userId); // // 不是管理员时,加入部门 // if (sysUser.getSeeFlag().getKey() != SeeFlag.MANAGE.getKey()) { // params.put("department", sysUser.getDepartment()); // } // 不是管理员时,加入userid //if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()||sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) { if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey() && sysUser.getSeeFlag().getKey() != SeeFlag.LEADING.getKey()) { } else { params.put("userId", userId); } } } /** * 个人管理、仓库库存管理用 * * @param userId * @param loginId * @param params */ private void addParamByLoginId(String userId, String loginId, Map params) { if (StringUtils.isNotBlank(loginId)) { SysUser sysUser = sysUserService.getSysUser(loginId); // 负责人,需要把部门的userid传递到SQL if (sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) { List departmentUserIds = new ArrayList(); List departmentUsers = sysUserService.getSeeUserList(sysUser.getDepartment()); for (SysUser departmentUser : departmentUsers) { departmentUserIds.add(departmentUser.getId()); } if (departmentUserIds.size() > 0) { params.put("departmentUserIds", departmentUserIds); } if (StringUtils.isNotBlank(userId)) { params.put("userId", userId); } } else if (sysUser.getSeeFlag().getKey() == SeeFlag.NORMAL.getKey()) {// 管理员,部门的userid和个人id都不需要 params.put("userId", userId); } else { if (StringUtils.isNotBlank(userId)) { params.put("userId", userId); } } } } @Transactional(propagation = Propagation.REQUIRED) public OpeReagentStatus insertOpeReagentStatus(OpeReagentStatus opeReagentStatus) { try { opeReagentStatus.setId(IDUtils.uuid()); this.opeReagentStatusDao.insertOpeReagentStatus(opeReagentStatus); return opeReagentStatus; } 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); } } @Override @Transactional(propagation = Propagation.REQUIRED) public boolean isOpeReagentStatusExist(String reagentCode) { try { OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(reagentCode); return status != null; } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Override @Transactional(propagation = Propagation.REQUIRED) public OpeReagentStatus getOpeReagentStatusByCode(String reagentCode){ try { return this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(reagentCode); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Override public boolean isAllowWarehouseUse(OpeReagentStatus opeReagentStatus) { return opeReagentStatus != null && opeReagentStatus.getStatus() != null && opeReagentStatus.getStatus().getKey() == ArrivalStatus.WAREHOUSE.getKey(); } public boolean isAllowWarehouseUseByIds(List list) { boolean flag = true; if (list != null && list.size() > 0) { for (String opeReagentStatusId : list) { OpeReagentStatus opeReagentStatus = getOpeReagentStatus(opeReagentStatusId); if (!isAllowWarehouseUse(opeReagentStatus)) { flag = false; break; } } } else { flag = false; } return flag; } @Transactional(propagation = Propagation.REQUIRED) public boolean updateReagentStatus2(OpeReagentStatus opeReagentStatus){ try { if (isAllowWarehouseUse(opeReagentStatus)) { // 减少库存 OpeWarehouseReserve opeWarehouseReserve = this.opeWarehouseReserveService.getOpeWarehouseReserve( opeReagentStatus.getReagent().getId(), opeReagentStatus.getArticleNumber()); 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()); opeUseFlow.setHouseId(opeReagentStatus.getHouseId()); opeUseFlow.setContainerId(opeReagentStatus.getContainerId()); opeUseFlow.setUserId(opeReagentStatus.getUserId()); opeUseFlow.setPlace(opeReagentStatus.getPlace()); opeUseFlow.setRemainder(opeReagentStatus.getRemainder()); Map metaMap = new HashMap<>(); metaMap.put("groupId", "operate_status"); metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey())); List 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); } } @Override public boolean updateReagentStatus3(List reagentCodes,String userId,String projectNum,String laboratoryId,String laboratoryContainerId,String receiptNumber) { try { for (String opeReagentStatusId : reagentCodes) { OpeReagentStatus opeReagentStatus = this.getOpeReagentStatus(opeReagentStatusId); opeReagentStatus.setUserId(userId); opeReagentStatus.setProjectNum(projectNum); //1.判断->试剂状态是否为在仓库 if (isAllowWarehouseUse(opeReagentStatus)) { //1.1获得 试剂的库存选择批次 List 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()); opeUseFlow.setReceiptNumber(receiptNumber); Map metaMap = new HashMap<>(); metaMap.put("groupId", "operate_status"); metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey())); List 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,String receiptNumber) { try { if(StringUtils.isBlank(receiptNumber)){ throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "领用单号为空")); } if (isAllowWarehouseUse(opeReagentStatus)) { // 减少库存 OpeWarehouseReserve opeWarehouseReserve = this.opeWarehouseReserveService.getOpeWarehouseReserve( opeReagentStatus.getReagent().getId(), opeReagentStatus.getArticleNumber()); opeWarehouseReserve.setReserve(opeWarehouseReserve.getReserve() - 1); this.opeWarehouseReserveService.updateOpeWarehouseReserve(opeWarehouseReserve); } opeReagentStatus.setStatus(ArrivalStatus.NOREGISTER); int row = this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus); if (row == 0) { return false; } OpeUseFlow opeUseFlow = new OpeUseFlow(); opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode()); opeUseFlow.setStatus(opeReagentStatus.getStatus()); opeUseFlow.setHouseId(opeReagentStatus.getHouseId()); opeUseFlow.setContainerId(opeReagentStatus.getContainerId()); opeUseFlow.setUserId(opeReagentStatus.getUserId()); opeUseFlow.setPlace(opeReagentStatus.getPlace()); opeUseFlow.setRemainder(opeReagentStatus.getRemainder()); opeUseFlow.setReceiptNumber(receiptNumber); Map metaMap = new HashMap<>(); metaMap.put("groupId", "operate_status"); metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey())); List 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); } } public OpeReagentStatus getOpeReagentStatus(String id) { try { return this.opeReagentStatusDao.getOpeReagentStatus(id); } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } public OpeReagentStatus getOpeReagentStatusByReagentCode2(OpeReagentStatus opeReagentStatus) { try { OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(opeReagentStatus.getReagentCode()); return status; } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } public OpeReagentStatus getOpeReagentStatusByReagentCode(String reagentCode) { try { OpeReagentStatus status = this.opeReagentStatusDao.getOpeReagentStatusByReagentCode(reagentCode); return status; } catch (DataAccessException e) { logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } } @Transactional(propagation = Propagation.REQUIRED) public void putReagentInLaboratory(String reagentCode, String barCode, String containerCode, SysUser sysUser) { try { // 检查试剂条码是否存在 OpeReagentStatus opeReagentStatus = this.getOpeReagentStatusByReagentCode(reagentCode); if (opeReagentStatus == null) { throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "试剂条码", reagentCode)); } // 检查地点条码是否存在 SysLaboratory sysLaboratory = this.sysLaboratoryService.getSysLaboratoryByBarCode(barCode); if (sysLaboratory == null) { throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "地点条码", barCode)); } // 检查货柜条码是否存在 SysLaboratoryContainer sysLaboratoryContainer = this.sysLaboratoryContainerService .getSysLaboratoryContainerByContainerCode(containerCode); if (sysLaboratoryContainer == null) { throw new BusinessException(ExceptionEnumCode.PARAM_NO_EXIST, MessageUtil .getMessageByCode(ExceptionEnumCode.PARAM_NO_EXIST.getCode(), "货柜条码", containerCode)); } // 检查货柜是否属于该地点 if (!sysLaboratory.getId().equals(sysLaboratoryContainer.getLaboratoryId())) { throw new BusinessException(ExceptionEnumCode.PARAM_ERR, "货柜不属于该地点"); } opeReagentStatus.setStatus(ArrivalStatus.LABORATORY); opeReagentStatus.setHouseId(sysLaboratory.getId()); opeReagentStatus.setContainerId(sysLaboratoryContainer.getId()); opeReagentStatus.setUserId(sysUser.getId()); opeReagentStatus.setPlace(null); this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus); OpeUseFlow opeUseFlow = new OpeUseFlow(); opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode()); opeUseFlow.setStatus(opeReagentStatus.getStatus()); opeUseFlow.setHouseId(opeReagentStatus.getHouseId()); opeUseFlow.setContainerId(opeReagentStatus.getContainerId()); opeUseFlow.setUserId(opeReagentStatus.getUserId()); opeUseFlow.setPlace(opeReagentStatus.getPlace()); opeUseFlow.setRemainder(opeReagentStatus.getRemainder()); // 接口应新追加操作状态和试剂状态两个参数。 this.opeUseFlowService.insertOpeUseFlow(opeUseFlow); } 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); } } @Transactional(propagation = Propagation.REQUIRED) public boolean store(List selectedStoreList, String laboratoryId, String laboratoryContainerId) { try { for (OpeReagentStatus opeReagentStatus : selectedStoreList) { opeReagentStatus.setStatus(ArrivalStatus.LABORATORY); opeReagentStatus.setHouseId(laboratoryId); opeReagentStatus.setContainerId(laboratoryContainerId); int row = this.opeReagentStatusDao.updateOpeReagentStatusDao(opeReagentStatus); if (row == 0) { return false; } OpeUseFlow opeUseFlow = new OpeUseFlow(); opeUseFlow.setReagentCode(opeReagentStatus.getReagentCode()); opeUseFlow.setStatus(opeReagentStatus.getStatus()); opeUseFlow.setHouseId(opeReagentStatus.getHouseId()); opeUseFlow.setContainerId(opeReagentStatus.getContainerId()); opeUseFlow.setUserId(opeReagentStatus.getUserId()); opeUseFlow.setPlace(opeReagentStatus.getPlace()); opeUseFlow.setRemainder(opeReagentStatus.getRemainder()); Map metaMap = new HashMap<>(); metaMap.put("groupId", "operate_status"); metaMap.put("metaKey", String.valueOf(OperateStatus.LABORATORYIN.getKey())); List 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); } } @Override @Transactional(readOnly = true) public List getOpeReagentStatusInfoList(String startTime,String endTime, String containerid, String status){ Map params = ImmutableMap.of("startTime",startTime,"endTime",endTime,"containerid",containerid,"status",status); return opeReagentStatusDao.getOpeReagentStatusInfoList(params); } @Override @Transactional(propagation = Propagation.REQUIRED) public void syncOpeReagentStatus(List reagentStatusList){ Assert.notNull(reagentStatusList,"请求参数不能为空!"); for (Map params : reagentStatusList) { // if(params.get("idcard") != null){ SysUser sysUser = sysUserService.getSysUserByIdCard((String)params.get("idcard")); if(Objects.isNull(sysUser)){ logger.error("The idcard:" + (String) params.get("idcard") + " has not been exists!"); continue; } params.put("userId",sysUser.getId()); } else { logger.error("The idcard is empty!"); continue; } //0:个人领用 1:在库 3:报废 if(params.get("status").toString().equals("1")){ SysWarehouseContainer container = sysWarehouseContainerDao.getSysWarehouseContainerByContainerCode((String)params.get("containerCode")); if(Objects.isNull(container)==false){ params.put("status",1); params.put("containerId",container.getId()); params.put("houseId",container.getWarehouseId()); }else{ SysLaboratoryContainer sysLaboratoryContainer = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode((String)params.get("containerCode")); if(Objects.isNull(sysLaboratoryContainer)){ logger.error("The containerCode has not been exists!"); continue; } params.put("status",2); params.put("containerId",sysLaboratoryContainer.getId()); params.put("houseId",sysLaboratoryContainer.getLaboratoryId()); } } opeReagentStatusDao.syncOpeReagentStatus(params); } } @Override public void deleteByReagentCode(String reagentCode) { this.opeReagentStatusDao.deleteByReagentCode(reagentCode); } @Override public List getReagentStatusByContainerCode(String containerCode) { try{ SysLaboratoryContainer sysLaboratoryContainer = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode(containerCode); if(sysLaboratoryContainer != null){ List list = opeReagentStatusDao.getReagentStatusByContainerId(sysLaboratoryContainer.getId()); return list; } SysWarehouseContainer sysWarehouseContainer = sysWarehouseContainerDao.getSysWarehouseContainerByContainerCode(containerCode); if(sysWarehouseContainer != null){ List list = opeReagentStatusDao.getReagentStatusByContainerId(sysWarehouseContainer.getId()); return list; } }catch (Exception e){ logger.error(e.getMessage(), e); throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e); } return null; } @Override public void updateTimeByCode(String code) { Map params = new HashMap<>(); params.put("code", code); opeReagentStatusDao.updateTimeByCode(params); } /** *@description -1为入库,1为认领 */ @Override @Transactional public void batchUpdateStatusByCode(List applyList) throws Exception { for (PersonUseDetail pu : applyList) { //变更试剂状态为入库 Map params1 = new HashMap<>(); String reagentCode = pu.getReagentCode(); params1.put("code", reagentCode); params1.put("status",ArrivalStatus.WAREHOUSE); opeReagentStatusDao.updateStatusByCode(params1); //删除试剂流向记录 Map params2 = new HashMap<>(); params2.put("validFlag", ValidFlag.INVALID); params2.put("reagentCode", reagentCode); params2.put("status", ArrivalStatus.NOREGISTER); opeUseFlowDao.deleteByReagentCodeAndStatus(params2); //增加数据库存 Map params3 = new HashMap<>(); String reagentId = pu.getReagentId(); String articleNumber = pu.getArticleNumber(); params3.put("reagentId", reagentId); params3.put("articleNumber", articleNumber); opeWarehouseReserveDao.updateCount(params3); } } @Override public List selectByReId(String id) { return this.opeReagentStatusDao.selectByReId(id); } @Override public void updateByReId(String newReId, String oldReId) { Map params=new HashMap(); params.put("newReId",newReId); params.put("oldReId",oldReId); this.opeReagentStatusDao.updateByReId(params); } @Override public List getReagentCodes(String reagentId) { return opeReagentStatusDao.selectReagentCodesByReId(reagentId); } @Override /** * @Description: 订单入库时 的更新试剂状态和更新试剂流向 */ @Transactional public void orderInputWarehouseReagentStatusAndUseFlow (OpeApplyReserve opeApplyReserve,String userId,List reagentCodeList,OpeOrder opeOrder) { for (String reagentCode : reagentCodeList) { //1.新增试剂状态:在仓库有 OpeReagentStatus ors = new OpeReagentStatus(); ors.setId(IDUtils.uuid()); //状态和用户 ors.setStatus(ArrivalStatus.WAREHOUSE); ors.setUserId(userId); //入库场所 ors.setHouseId(opeApplyReserve.getHouseId()); ors.setContainerId(opeApplyReserve.getContainerId()); //试剂编码 id和批号 ors.setReagentCode(reagentCode); ors.setReagentId(opeApplyReserve.getReagent().getId()); ors.setArticleNumber(opeApplyReserve.getArticleNumber()); //订单和申购单 ors.setApplyCode(opeApplyReserve.getApplyCode()); ors.setOrderCode(opeOrder.getOrderCode()); opeReagentStatusDao.insertOpeReagentStatus2(ors); //2.更新试剂流向 OpeUseFlow ouf = new OpeUseFlow(); ouf.setId(IDUtils.uuid()); //持有者 ouf.setUserId(userId); //在仓库状态 ouf.setStatus(ArrivalStatus.WAREHOUSE); //操作状态->仓库入库 Map metaMap = new HashMap<>(); metaMap.put("groupId", "operate_status"); metaMap.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEIN.getKey())); List baseMetaList = baseMetaDao.getBaseMetaList(metaMap); ouf.setOperateState(baseMetaList.get(0).getId()); //地点 ouf.setHouseId(opeApplyReserve.getHouseId()); ouf.setContainerId(opeApplyReserve.getContainerId()); //试剂条码 ouf.setReagentCode(reagentCode); //创建时间 ouf.setCreateTime(new Timestamp(new Date().getTime())); opeUseFlowDao.insertOpeUseFlow(ouf); } } /** * @Description: 校验条码在试剂状态中是否存在 */ @Override public List checkReagentCode(String startReagentCode2, String endReagentCode2,Integer arrivalNum) throws BusinessException { if (startReagentCode2 == null || startReagentCode2.length() < 1) { return null; } if (endReagentCode2 == null || endReagentCode2.length() < 1) { return null; } List 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); String regentPrefix2 = endReagentCode2.substring(0, endReagentCode2.length() - 5); //如果距离不是 到货数量或者非后5位得前缀有问题,则说明输入有问题 if (randomEnd - randomStart + 1 != arrivalNum||!regentPrefix2.equals(regentPrefix)) { throw new BusinessException(ExceptionEnumCode.PARAM_EXIST,"试剂的开始结束条码有问题"); } for (int random = randomStart; random <= randomEnd; random++) { String random5 = String.format("%0" + 5 + "d", random); String reagentCode = regentPrefix + random5; OpeReagentStatus opeReagentStatus = this.getOpeReagentStatusByReagentCode(reagentCode); if (opeReagentStatus != null) { throw new BusinessException(ExceptionEnumCode.PARAM_EXIST, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_EXIST.getCode(), "试剂条码", reagentCode)); }else{ codeList.add(reagentCode); } } return codeList; }else{ throw new BusinessException(ExceptionEnumCode.REAGENT_CODE_INVALID,"输入的试剂条码不合法"); } } //2.纯数字编码列表生成 BigInteger reagentCode= new BigInteger(startReagentCode2); BigInteger endReagentCode = new BigInteger(endReagentCode2); //条码得距离 如果和到货数量不等 则输入有问题 if (!endReagentCode.subtract(reagentCode).add(BigInteger.ONE).equals(new BigInteger(String.valueOf(arrivalNum)))) { throw new BusinessException(ExceptionEnumCode.REAGENT_CODE_INVALID,"试剂的开始结束条码有问题"); } while (reagentCode.compareTo(endReagentCode) <= 0) { String reagentCodeStr = String.format("%0" + startReagentCode2.length() + "d", reagentCode); OpeReagentStatus opeReagentStatus = this.getOpeReagentStatusByReagentCode(reagentCodeStr); if (opeReagentStatus != null) { throw new BusinessException(ExceptionEnumCode.PARAM_EXIST, MessageUtil.getMessageByCode(ExceptionEnumCode.PARAM_EXIST.getCode(), "试剂条码", reagentCodeStr)); }else{ codeList.add(reagentCodeStr); } reagentCode = reagentCode.add(BigInteger.ONE); } return codeList; } @Override public List 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 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 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 selectExportList(String name, String articleNumber, Integer status, String reagentCode, String userId,String labName) { Map 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 list) throws Exception { Map 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); } }