|
|
@@ -7,6 +7,7 @@ import easydo.technology.model.*;
|
|
|
import easydo.technology.model.vo.MaterialRequisitionVO;
|
|
|
import easydo.technology.service.FlowNoService;
|
|
|
import easydo.technology.service.MaterialRequisitionService;
|
|
|
+import easydo.technology.system.model.SysUser;
|
|
|
import easydo.technology.utils.SecurityUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -89,6 +90,160 @@ public class MaterialRequisitionServiceImpl implements MaterialRequisitionServic
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public MaterialRequisitionVO save(MaterialRequisitionVO model) throws Exception {
|
|
|
+ Connection connection = dataSource.getConnection();
|
|
|
+ connection.setAutoCommit(false);
|
|
|
+ try {
|
|
|
+ String tenantId = resolveTenantId(model, connection);
|
|
|
+
|
|
|
+ MaterialRequisition requisition = new MaterialRequisition();
|
|
|
+ requisition.setCode(model.getCode());
|
|
|
+ requisition.setName(model.getName());
|
|
|
+ requisition.setDispatchId(model.getDispatchId());
|
|
|
+ requisition.setOrderId(model.getOrderId());
|
|
|
+ requisition.setBomId(model.getBomId());
|
|
|
+ requisition.setRequisitionType(model.getRequisitionType() != null && !model.getRequisitionType().trim().isEmpty()
|
|
|
+ ? model.getRequisitionType()
|
|
|
+ : MESEnum.MATERIAL_REQUISITION_TYPE_MANUAL.getValue());
|
|
|
+ requisition.setOutboundStatus(model.getOutboundStatus() != null && !model.getOutboundStatus().trim().isEmpty()
|
|
|
+ ? model.getOutboundStatus()
|
|
|
+ : MESEnum.MATERIAL_REQUISITION_OUTBOUND_STATUS_APPLIED.getValue());
|
|
|
+ requisition.setStatus(model.getStatus() != null && !model.getStatus().trim().isEmpty()
|
|
|
+ ? model.getStatus()
|
|
|
+ : MESEnum.MATERIAL_REQUISITION_OF_STATUS_PENDING.getValue());
|
|
|
+ requisition.setRequisitionDate(model.getRequisitionDate());
|
|
|
+ requisition.setReceiverId(model.getReceiverId());
|
|
|
+ requisition.setRemark(model.getRemark());
|
|
|
+ requisition.setTenantId(tenantId);
|
|
|
+ requisition.setCreateId(model.getCreateId());
|
|
|
+ requisition.setCreateTime(FORMATTER.format(LocalDateTime.now()));
|
|
|
+
|
|
|
+ String requisitionCode = flowNoService.generateMaterialRequisitionCode(requisition, connection);
|
|
|
+ requisition.setCode(requisitionCode);
|
|
|
+ jdbcClient.jdbcInsert(requisition, connection);
|
|
|
+
|
|
|
+ insertItems(requisition.getId(), tenantId, model.getItems(), connection);
|
|
|
+
|
|
|
+ connection.commit();
|
|
|
+ model.setId(requisition.getId());
|
|
|
+ model.setCode(requisition.getCode());
|
|
|
+ model.setRequisitionType(requisition.getRequisitionType());
|
|
|
+ model.setOutboundStatus(requisition.getOutboundStatus());
|
|
|
+ model.setStatus(requisition.getStatus());
|
|
|
+ model.setTenantId(requisition.getTenantId());
|
|
|
+ model.setCreateTime(requisition.getCreateTime());
|
|
|
+ return model;
|
|
|
+ } catch (Exception e) {
|
|
|
+ connection.rollback();
|
|
|
+ throw new BizException(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public MaterialRequisitionVO update(MaterialRequisitionVO model) throws Exception {
|
|
|
+ Connection connection = dataSource.getConnection();
|
|
|
+ connection.setAutoCommit(false);
|
|
|
+ try {
|
|
|
+ if (model.getId() == null || model.getId().trim().isEmpty()) {
|
|
|
+ throw new BizException("领料单ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ MaterialRequisition query = new MaterialRequisition();
|
|
|
+ query.setId(model.getId());
|
|
|
+ MaterialRequisition requisition = jdbcClient.getJdbcModelById(query, connection);
|
|
|
+ if (requisition == null) {
|
|
|
+ throw new BizException("领料单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model.getDispatchId() == null || model.getDispatchId().trim().isEmpty()) {
|
|
|
+ model.setDispatchId(requisition.getDispatchId());
|
|
|
+ }
|
|
|
+ if (model.getOrderId() == null || model.getOrderId().trim().isEmpty()) {
|
|
|
+ model.setOrderId(requisition.getOrderId());
|
|
|
+ }
|
|
|
+ if (model.getBomId() == null || model.getBomId().trim().isEmpty()) {
|
|
|
+ model.setBomId(requisition.getBomId());
|
|
|
+ }
|
|
|
+ if (model.getTenantId() == null || model.getTenantId().trim().isEmpty()) {
|
|
|
+ model.setTenantId(requisition.getTenantId());
|
|
|
+ }
|
|
|
+
|
|
|
+ String tenantId = resolveTenantId(model, connection);
|
|
|
+
|
|
|
+ requisition.setCode(model.getCode());
|
|
|
+ requisition.setName(model.getName());
|
|
|
+ requisition.setDispatchId(model.getDispatchId());
|
|
|
+ requisition.setOrderId(model.getOrderId());
|
|
|
+ requisition.setBomId(model.getBomId());
|
|
|
+ requisition.setRequisitionType(model.getRequisitionType() != null && !model.getRequisitionType().trim().isEmpty()
|
|
|
+ ? model.getRequisitionType()
|
|
|
+ : requisition.getRequisitionType());
|
|
|
+ requisition.setOutboundStatus(model.getOutboundStatus() != null && !model.getOutboundStatus().trim().isEmpty()
|
|
|
+ ? model.getOutboundStatus()
|
|
|
+ : requisition.getOutboundStatus());
|
|
|
+ requisition.setStatus(model.getStatus() != null && !model.getStatus().trim().isEmpty()
|
|
|
+ ? model.getStatus()
|
|
|
+ : requisition.getStatus());
|
|
|
+ requisition.setRequisitionDate(model.getRequisitionDate());
|
|
|
+ requisition.setReceiverId(model.getReceiverId());
|
|
|
+ requisition.setRemark(model.getRemark());
|
|
|
+ requisition.setTenantId(tenantId);
|
|
|
+ requisition.setUpdateId(model.getUpdateId());
|
|
|
+ requisition.setUpdateTime(FORMATTER.format(LocalDateTime.now()));
|
|
|
+ jdbcClient.jdbcUpdateById(requisition, connection);
|
|
|
+
|
|
|
+ MaterialRequisitionItem removeItemParam = new MaterialRequisitionItem();
|
|
|
+ removeItemParam.setRequisitionId(requisition.getId());
|
|
|
+ jdbcClient.jdbcRemove(removeItemParam, connection);
|
|
|
+ insertItems(requisition.getId(), tenantId, model.getItems(), connection);
|
|
|
+
|
|
|
+ connection.commit();
|
|
|
+ model.setCode(requisition.getCode());
|
|
|
+ model.setTenantId(requisition.getTenantId());
|
|
|
+ model.setUpdateTime(requisition.getUpdateTime());
|
|
|
+ return model;
|
|
|
+ } catch (Exception e) {
|
|
|
+ connection.rollback();
|
|
|
+ throw new BizException(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public MaterialRequisitionVO remove(MaterialRequisitionVO model) throws Exception {
|
|
|
+ Connection connection = dataSource.getConnection();
|
|
|
+ connection.setAutoCommit(false);
|
|
|
+ try {
|
|
|
+ if (model.getId() == null || model.getId().trim().isEmpty()) {
|
|
|
+ throw new BizException("领料单ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ MaterialRequisition requisition = new MaterialRequisition();
|
|
|
+ requisition.setId(model.getId());
|
|
|
+ MaterialRequisition exist = jdbcClient.getJdbcModelById(requisition, connection);
|
|
|
+ if (exist == null) {
|
|
|
+ throw new BizException("领料单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ MaterialRequisitionItem removeItemParam = new MaterialRequisitionItem();
|
|
|
+ removeItemParam.setRequisitionId(model.getId());
|
|
|
+ jdbcClient.jdbcRemove(removeItemParam, connection);
|
|
|
+ jdbcClient.jdbcRemoveById(requisition, connection);
|
|
|
+
|
|
|
+ connection.commit();
|
|
|
+ return model;
|
|
|
+ } catch (Exception e) {
|
|
|
+ connection.rollback();
|
|
|
+ throw new BizException(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ connection.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void generateRequisition(ProductOrderDispatch dispatch, Connection connection) throws Exception {
|
|
|
try {
|
|
|
@@ -222,4 +377,89 @@ public class MaterialRequisitionServiceImpl implements MaterialRequisitionServic
|
|
|
throw new BizException("更新领料单状态失败: " + e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private String resolveTenantId(MaterialRequisitionVO model, Connection connection) throws Exception {
|
|
|
+ if (model.getDispatchId() != null && !model.getDispatchId().trim().isEmpty()) {
|
|
|
+ ProductOrderDispatch dispatchQuery = new ProductOrderDispatch();
|
|
|
+ dispatchQuery.setId(model.getDispatchId());
|
|
|
+ ProductOrderDispatch dispatch = jdbcClient.getJdbcModelById(dispatchQuery, connection);
|
|
|
+ if (dispatch == null) {
|
|
|
+ throw new BizException("关联派工单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model.getOrderId() == null || model.getOrderId().trim().isEmpty()) {
|
|
|
+ model.setOrderId(dispatch.getOrderId());
|
|
|
+ } else if (!model.getOrderId().equals(dispatch.getOrderId())) {
|
|
|
+ throw new BizException("领料单工单与派工单不匹配");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model.getBomId() == null || model.getBomId().trim().isEmpty()) {
|
|
|
+ model.setBomId(dispatch.getBomId());
|
|
|
+ } else if (!model.getBomId().equals(dispatch.getBomId())) {
|
|
|
+ throw new BizException("领料单BOM与派工单不匹配");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dispatch.getTenantId() != null && !dispatch.getTenantId().trim().isEmpty()) {
|
|
|
+ model.setTenantId(dispatch.getTenantId());
|
|
|
+ return dispatch.getTenantId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean hasOrderId = model.getOrderId() != null && !model.getOrderId().trim().isEmpty();
|
|
|
+ boolean hasBomId = model.getBomId() != null && !model.getBomId().trim().isEmpty();
|
|
|
+ if (hasOrderId || hasBomId) {
|
|
|
+ if (!hasOrderId || !hasBomId) {
|
|
|
+ throw new BizException("工单ID和BOM ID需要同时填写");
|
|
|
+ }
|
|
|
+
|
|
|
+ ProductOrder orderQuery = new ProductOrder();
|
|
|
+ orderQuery.setId(model.getOrderId());
|
|
|
+ ProductOrder order = jdbcClient.getJdbcModelById(orderQuery, connection);
|
|
|
+ if (order == null) {
|
|
|
+ throw new BizException("工单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ ProductOrderBom bomQuery = new ProductOrderBom();
|
|
|
+ bomQuery.setOrderId(model.getOrderId());
|
|
|
+ bomQuery.setBomId(model.getBomId());
|
|
|
+ List<ProductOrderBom> orderBomList = jdbcClient.getJdbcList(bomQuery, connection);
|
|
|
+ if (orderBomList == null || orderBomList.isEmpty()) {
|
|
|
+ throw new BizException("工单中不存在该BOM");
|
|
|
+ }
|
|
|
+
|
|
|
+ model.setTenantId(order.getTenantId());
|
|
|
+ return order.getTenantId();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (model.getTenantId() != null && !model.getTenantId().trim().isEmpty()) {
|
|
|
+ return model.getTenantId();
|
|
|
+ }
|
|
|
+
|
|
|
+ Long currentUserId = SecurityUtils.getCurrentUserId();
|
|
|
+ SysUser userQuery = new SysUser();
|
|
|
+ userQuery.setId(currentUserId);
|
|
|
+ SysUser user = jdbcClient.getJdbcModelById(userQuery, connection);
|
|
|
+ if (user == null || user.getTenantId() == null || user.getTenantId().trim().isEmpty()) {
|
|
|
+ throw new BizException("无法获取当前用户租户信息");
|
|
|
+ }
|
|
|
+ model.setTenantId(user.getTenantId());
|
|
|
+ return user.getTenantId();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void insertItems(String requisitionId, String tenantId, List<MaterialRequisitionItem> items, Connection connection) throws Exception {
|
|
|
+ if (items == null || items.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (MaterialRequisitionItem item : items) {
|
|
|
+ item.setId(null);
|
|
|
+ item.setRequisitionId(requisitionId);
|
|
|
+ item.setTenantId(tenantId);
|
|
|
+ if (item.getActualQuantity() == null) {
|
|
|
+ item.setActualQuantity(BigDecimal.ZERO);
|
|
|
+ }
|
|
|
+ item.setCreateTime(FORMATTER.format(LocalDateTime.now()));
|
|
|
+ jdbcClient.jdbcInsert(item, connection);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|