package com.nanometer.smartlab.service;
|
|
import java.math.BigDecimal;
|
import java.sql.Timestamp;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
import javax.annotation.Resource;
|
|
import com.nanometer.smartlab.dao.BaseMetaDao;
|
import com.nanometer.smartlab.dao.SysLaboratoryContainerDao;
|
import com.nanometer.smartlab.dao.SysWarehouseContainerDao;
|
import com.nanometer.smartlab.entity.*;
|
import com.nanometer.smartlab.entity.dto.PersonUseDetail;
|
import com.nanometer.smartlab.entity.enumtype.OperateStatus;
|
import com.nanometer.smartlab.util.Constants;
|
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang3.time.DateUtils;
|
import org.apache.log4j.Logger;
|
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataIntegrityViolationException;
|
import org.springframework.dao.DuplicateKeyException;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Propagation;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import com.nanometer.smartlab.dao.OpeUseFlowDao;
|
import com.nanometer.smartlab.entity.enumtype.ArrivalStatus;
|
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 johnny on 17/12/16.
|
*/
|
@Service("OpeUseFlowService")
|
public class OpeUseFlowServiceImpl implements OpeUseFlowService {
|
|
private static Logger logger = Logger.getLogger(OpeUseFlowService.class);
|
|
@Resource(name = "opeUseFlowDao")
|
OpeUseFlowDao opeUseFlowDao;
|
@Resource
|
private SysLaboratoryContainerDao sysLaboratoryContainerDao;
|
@Resource
|
private SysWarehouseContainerDao sysWarehouseContainerDao;
|
|
@Resource
|
private SysUserService sysUserService;
|
@Resource
|
private OpeReagentStatusService opeReagentStatusService;
|
@Resource
|
private SysReagentService sysReagentService;
|
@Resource
|
private BaseMetaDao baseMetaDao;
|
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public OpeUseFlow insertOpeUseFlow(OpeUseFlow opeUseFlow) {
|
try {
|
if (opeUseFlow.getId() == null) {
|
opeUseFlow.setId(IDUtils.uuid());
|
}
|
|
if(opeUseFlow.getCreateTime()==null){
|
opeUseFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
}
|
|
this.opeUseFlowDao.insertOpeUseFlow(opeUseFlow);
|
return 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 List<OpeUseFlow> getOpeUseFlowList(String reagentId, String reagentCode, Integer status, String userId,
|
Integer first, Integer pageSize) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
|
params.put("reagentId", reagentId);
|
if (StringUtils.isNotBlank(reagentCode)) {
|
params.put("reagentCode", "%" + reagentCode + "%");
|
}
|
params.put("status", status);
|
|
addParamByUserId(userId, params);
|
|
params.put("first", first);
|
params.put("pageSize", pageSize);
|
return this.opeUseFlowDao.getOpeUseFlowList(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 getOpeUseFlowTotalCount(String reagentId, String reagentCode, Integer status, String userId) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
params.put("reagentId", reagentId);
|
|
if (StringUtils.isNotBlank(reagentCode)) {
|
params.put("reagentCode", "%" + reagentCode + "%");
|
}
|
|
addParamByUserId(userId, params);
|
|
params.put("status", status);
|
return this.opeUseFlowDao.getOpeUseFlowTotalCount(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<OpeUseFlow> getOpeUseFlowListByName(String houseName,String reagentId, String reagentCode,String containerCode, Integer status, String userId,
|
Date startDate,Date endDate,Integer first, Integer pageSize) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
|
params.put("reagentName", reagentId);
|
if (StringUtils.isNotBlank(reagentCode)) {
|
params.put("reagentCode", "%" + reagentCode + "%");
|
}
|
|
SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
Date now = new Date();
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(now);
|
cal.add(Calendar.DATE, -7);
|
Date startTime = cal.getTime();
|
|
if (null != startDate){
|
try {
|
params.put("startDate",sdfstart.format(startDate));
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}else {
|
params.put("startDate",sdfstart.format(startTime));
|
}
|
|
if (null != endDate){
|
try {
|
params.put("endDate",sdfend.format(endDate));
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}else {
|
params.put("endDate",sdfend.format(now));
|
}
|
|
params.put("status", status);
|
params.put("houseName", houseName);
|
params.put("containerCode", containerCode);
|
addParamByUserId(userId, params);
|
|
params.put("first", first);
|
params.put("pageSize", pageSize);
|
return this.opeUseFlowDao.getOpeUseFlowList(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 getOpeUseFlowTotalCountByName(String houseName,String reagentId, String reagentCode,String containerCode, Integer status, String userId,Date startDate,Date endDate) {
|
try {
|
Map<String, Object> params = new HashMap<String, Object>();
|
params.put("reagentName", reagentId);
|
|
if (StringUtils.isNotBlank(reagentCode)) {
|
params.put("reagentCode", "%" + reagentCode + "%");
|
}
|
|
SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
|
SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
|
Date now = new Date();
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(now);
|
cal.add(Calendar.DATE, -7);
|
Date startTime = cal.getTime();
|
|
if (null != startDate){
|
try {
|
params.put("startDate",sdfstart.format(startDate));
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}else {
|
params.put("startDate",sdfstart.format(startTime));
|
}
|
|
if (null != endDate){
|
try {
|
params.put("endDate",sdfend.format(endDate));
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}else {
|
params.put("endDate",sdfend.format(now));
|
}
|
|
params.put("houseName", houseName);
|
params.put("containerCode", containerCode);
|
addParamByUserId(userId, params);
|
|
params.put("status", status);
|
return this.opeUseFlowDao.getOpeUseFlowTotalCount(params);
|
} catch (DataAccessException e) {
|
logger.error(e.getMessage(), e);
|
throw new BusinessException(ExceptionEnumCode.DB_ERR,
|
MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
|
}
|
}
|
|
private void addParamByUserId(String userId, Map<String, Object> params) {
|
if (StringUtils.isNotBlank(userId)) {
|
SysUser sysUser = sysUserService.getSysUser(userId);
|
|
// 不是管理员时,加入userid
|
if (sysUser.getSeeFlag().getKey() == SeeFlag.MANAGE.getKey()||sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) {
|
|
} else {
|
params.put("userId", userId);
|
}
|
|
// // 负责人,需要把部门的userid传递到SQL
|
// if (sysUser.getSeeFlag().getKey() == SeeFlag.LEADING.getKey()) {
|
// List<String> departmentUserIds = new ArrayList<String>();
|
// List<SysUser> departmentUsers = sysUserService.getSeeUserList(sysUser.getDepartment());
|
// for (SysUser departmentUser : departmentUsers) {
|
// departmentUserIds.add(departmentUser.getId());
|
// }
|
//
|
// if (departmentUserIds.size() > 0) {
|
// params.put("departmentUserIds", departmentUserIds);
|
// }
|
//
|
// } else if (sysUser.getSeeFlag().getKey() == SeeFlag.NORMAL.getKey()) {
|
// params.put("userId", userId);
|
// }
|
}
|
}
|
|
@Transactional(propagation = Propagation.REQUIRED)
|
public boolean update(OpeUseFlow opeUseFlow) {
|
try {
|
int row = this.opeUseFlowDao.update(opeUseFlow);
|
return row != 0;
|
} 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 void syncReagentFlow(List<LinkedHashMap> reagentFlowList) throws ParseException {
|
Assert.notNull(reagentFlowList, "请求参数不能为空!");
|
for (Map params : reagentFlowList) {
|
//0:个人领用 1:在库 3:报废
|
//<!--0:个人领用 1:在仓库sys_warehouse_container 2:在实验室sys_laboratory_container 3:报废-->
|
OpeUseFlow opeUseFlow = new OpeUseFlow();
|
opeUseFlow.setId(IDUtils.uuid());
|
opeUseFlow.setCreateTime(new Timestamp(DateUtils.parseDate(params.get("updateTime").toString(), Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS).getTime()));
|
opeUseFlow.setStatus(ArrivalStatus.parse(Integer.valueOf(String.valueOf(params.get("status")))));
|
opeUseFlow.setReagentCode((String) params.get("reagentCode"));
|
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;
|
}
|
opeUseFlow.setUserId(sysUser.getId());
|
opeUseFlow.setContainerId("0");
|
opeUseFlow.setHouseId("0");
|
} else {
|
logger.error("The idcard is empty!");
|
continue;
|
}
|
// if (params.get("status").toString().equals("1")) {
|
SysWarehouseContainer container = sysWarehouseContainerDao.getSysWarehouseContainerByContainerCode((String) params.get("containerCode"));
|
if (Objects.isNull(container)==false) {
|
if (params.get("status").toString().equals("1")) {
|
opeUseFlow.setStatus(ArrivalStatus.WAREHOUSE);
|
} else {
|
opeUseFlow.setStatus(ArrivalStatus.PERSONAL);
|
}
|
opeUseFlow.setContainerId(container.getId());
|
opeUseFlow.setHouseId(container.getWarehouseId());
|
opeUseFlow.setPlace(container.getStructure());
|
}else{
|
SysLaboratoryContainer sysLaboratoryContainer = sysLaboratoryContainerDao.getSysLaboratoryContainerByContainerCode((String) params.get("containerCode"));
|
if (Objects.isNull(sysLaboratoryContainer)) {
|
logger.error("The containerCode["+(String) params.get("containerCode")+"] has not been exists!");
|
continue;
|
}
|
if (params.get("status").toString().equals("1")) {
|
opeUseFlow.setStatus(ArrivalStatus.LABORATORY);
|
} else {
|
opeUseFlow.setStatus(ArrivalStatus.PERSONAL);
|
}
|
opeUseFlow.setContainerId(sysLaboratoryContainer.getId());
|
opeUseFlow.setHouseId(sysLaboratoryContainer.getLaboratoryId());
|
opeUseFlow.setPlace(sysLaboratoryContainer.getStructure());
|
}
|
// }
|
|
if(params.get("remainder") != null && !params.get("remainder").toString().equals("")) {
|
|
opeUseFlow.setRemainder(new BigDecimal(params.get("remainder").toString()).setScale(2, BigDecimal.ROUND_HALF_UP));
|
}
|
|
if(opeUseFlow.getCreateTime()==null){
|
opeUseFlow.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
}
|
|
|
opeUseFlowDao.insertOpeUseFlow(opeUseFlow);
|
}
|
}
|
|
@Override
|
public void deleteByReagentCode(String reagentCode) {
|
opeUseFlowDao.deleteByReagentCode(reagentCode);
|
}
|
|
@Override
|
public void updateTimeByCode(String code) {
|
Map<String, Object> params = new HashMap<String, Object>();
|
params.put("code", code);
|
params.put("status",-1);
|
opeUseFlowDao.updateTimeByCode(params);
|
}
|
|
@Override
|
public OpeUseFlow getOpeUseFlowByCode(String reagentCode) {
|
return opeUseFlowDao.getOpeUseFlowByCode(reagentCode);
|
}
|
|
@Override
|
public void updateReceiptNumber(String code, String receiptNumber) {
|
opeUseFlowDao.updateReceiptNumber(code, receiptNumber);
|
}
|
|
@Transactional
|
public void updateReceiptNumberByCode(List<OpeApplyReserve> selectedTmpOrderList,String receiptNumber) {
|
|
for (OpeApplyReserve oar : selectedTmpOrderList) {
|
//根据id或者试剂的条码直接查找 状态表单 查询订单所有的流向
|
List<String> codeTmp = opeReagentStatusService
|
.generateReagentCode(oar.getStartReagentCode2(), oar.getEndReagentCode2());
|
assert codeTmp.size() > 0;
|
|
if (oar.getFlag() == 1) {
|
//库中领取 code存的是 试剂状态的id
|
for (String statusId : codeTmp) {
|
String code = opeReagentStatusService.getOpeReagentStatus(statusId).getReagentCode();
|
//此时状态为领用待入库
|
this.updateReceiptNumber(code, receiptNumber);
|
}
|
}else{
|
for (String code : codeTmp) {
|
this.updateReceiptNumber(code, receiptNumber);
|
}
|
}
|
|
}
|
|
|
}
|
|
@Override
|
public Map<String, Integer> getRegentInfoFromReceiptNumber(String receiptNumber) {
|
List<Map> maps = opeUseFlowDao.getRegentInfoFromReceiptNumber(receiptNumber);
|
if (maps.size() > 0) {
|
Map<String, Integer> printInfo = new HashMap<>();
|
for (Map map : maps) {
|
Integer count =Integer.parseInt(String.valueOf(map.get("count")));;
|
String reagentId = (String) map.get("reagentId");
|
SysReagent reagent = sysReagentService.getSysReagent(reagentId);
|
printInfo.put(reagent.getId(), count);
|
}
|
return printInfo;
|
}
|
|
return null;
|
}
|
|
@Override
|
public Map getApplyUserByReagentCode(String reagentCode) {
|
|
Map<String, String> metaMap2 = new HashMap<>();
|
metaMap2.put("groupId", "operate_status");
|
metaMap2.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
|
List<BaseMeta> baseMetaList2 = baseMetaDao.getBaseMetaList(metaMap2);
|
String id = baseMetaList2.get(0).getId();
|
return opeUseFlowDao.getUserIdByReagentCode(reagentCode, id);
|
}
|
|
@Override
|
public void updateReceiptNumberByCode2(List<OpeWarehouseReserve> selectTmpList, String receiptNumber) {
|
for (OpeWarehouseReserve owr : selectTmpList) {
|
//根据id或者试剂的条码直接查找 状态表单 查询订单所有的流向
|
List<String> codeTmp = opeReagentStatusService
|
.generateReagentCode(owr.getStartReagentCode2(), owr.getEndReagentCode2());
|
assert codeTmp.size() > 0;
|
|
if (owr.getFlag() == 1) {
|
//库中领取 code存的是 试剂状态的id
|
for (String statusId : codeTmp) {
|
String code = opeReagentStatusService.getOpeReagentStatus(statusId).getReagentCode();
|
//此时状态为领用待入库
|
this.updateReceiptNumber(code, receiptNumber);
|
}
|
}else{
|
for (String code : codeTmp) {
|
this.updateReceiptNumber(code, receiptNumber);
|
}
|
}
|
|
}
|
|
}
|
|
@Override
|
public int getPersonalUseInfoCount(String reagentCode,String userId, Date startTime, Date endTime, String receiptNumber,
|
String department, String project,String applyPerson,String reagentName) {
|
Map<String,Object> params = new HashMap<>();
|
params.put("userId", userId);
|
params.put("startTime", startTime);
|
params.put("endTime", endTime);
|
params.put("receiptNumber", receiptNumber);
|
params.put("department", department);
|
params.put("project", project);
|
params.put("reagentCode", reagentCode);
|
params.put("applyPerson", applyPerson);
|
if (StringUtils.isNotBlank(reagentName)) {
|
//根据试剂名获取单号
|
|
}
|
params.put("reagentName", reagentName);
|
//操作状态位仓库领取
|
Map<String, String> metaMap2 = new HashMap<>();
|
metaMap2.put("groupId", "operate_status");
|
metaMap2.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
|
List<BaseMeta> baseMetas = baseMetaDao.getBaseMetaList(metaMap2);
|
params.put("operatestate", baseMetas.get(0).getId());
|
|
addParamByUserId(userId, params);
|
return opeUseFlowDao.countPersonalUseInfo(params);
|
}
|
|
@Override
|
public List<OpeUseFlow> getPersonalUseInfoList(String reagentCode,String userId, Date startTime, Date endTime, String receiptNumber,
|
String department, String project,String applyPerson,String reagentName, int first, int pageSize) {
|
Map<String, Object> params = new HashMap<>();
|
params.put("userId", userId);
|
params.put("startTime", startTime);
|
params.put("endTime", endTime);
|
params.put("receiptNumber", receiptNumber);
|
params.put("department", department);
|
params.put("project", project);
|
params.put("reagentCode", reagentCode);
|
params.put("applyPerson", applyPerson);
|
params.put("reagentName", reagentName);
|
//操作状态位仓库领取
|
Map<String, String> metaMap2 = new HashMap<>();
|
metaMap2.put("groupId", "operate_status");
|
metaMap2.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
|
List<BaseMeta> baseMetas = baseMetaDao.getBaseMetaList(metaMap2);
|
params.put("operatestate", baseMetas.get(0).getId());
|
params.put("first", first);
|
params.put("pageSize", pageSize);
|
|
addParamByUserId(userId, params);
|
return opeUseFlowDao.selectPersonalUseInfo(params);
|
}
|
|
@Override
|
public List<PersonUseDetail> getApplyInfo(String receiptNumber) {
|
Map<String,Object> params = new HashMap<>();
|
Map<String, String> metaMap2 = new HashMap<>();
|
metaMap2.put("groupId", "operate_status");
|
metaMap2.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
|
List<BaseMeta> baseMetas = baseMetaDao.getBaseMetaList(metaMap2);
|
params.put("operatestate", baseMetas.get(0).getId());
|
params.put("receiptNumber", receiptNumber);
|
|
return opeUseFlowDao.getApplyInfo(params);
|
|
}
|
|
|
@Override
|
public int getApplyInfoSize(String receiptNumber) {
|
Map<String,Object> params = new HashMap<>();
|
Map<String, String> metaMap2 = new HashMap<>();
|
metaMap2.put("groupId", "operate_status");
|
metaMap2.put("metaKey", String.valueOf(OperateStatus.WAREHOUSEOUT.getKey()));
|
List<BaseMeta> baseMetas = baseMetaDao.getBaseMetaList(metaMap2);
|
params.put("operatestate", baseMetas.get(0).getId());
|
params.put("receiptNumber", receiptNumber);
|
|
return opeUseFlowDao.getApplyInfoSize(params);
|
|
}
|
|
@Override
|
public int getOpeUseFlow(OpeUseFlow opeUseFlow) {
|
return opeUseFlowDao.countOpeUseFlow(opeUseFlow);
|
}
|
|
}
|