From 757c7e03bee3560bb4d7766fddd03673e2c2e611 Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期四, 23 七月 2026 17:46:34 +0800
Subject: [PATCH] feat(mes): 完善MES模块功能并优化日志配置
---
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/workorder/MesProWorkOrderServiceImpl.java | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 154 insertions(+), 5 deletions(-)
diff --git a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/workorder/MesProWorkOrderServiceImpl.java b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/workorder/MesProWorkOrderServiceImpl.java
index 2a695c9..c3f4e56 100644
--- a/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/workorder/MesProWorkOrderServiceImpl.java
+++ b/yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/pro/workorder/MesProWorkOrderServiceImpl.java
@@ -13,6 +13,14 @@
import cn.iocoder.yudao.module.mes.enums.wm.BarcodeBizTypeEnum;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemBatchConfigService;
import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService;
+import cn.iocoder.yudao.module.mes.controller.admin.pro.workorder.vo.MesProWorkOrderProgressRespVO;
+import cn.iocoder.yudao.module.mes.dal.dataobject.md.workstation.MesMdWorkstationDO;
+import cn.iocoder.yudao.module.mes.dal.dataobject.pro.process.MesProProcessDO;
+import cn.iocoder.yudao.module.mes.dal.dataobject.pro.task.MesProTaskDO;
+import cn.iocoder.yudao.module.mes.dal.dataobject.pro.workorder.MesProWorkOrderProcessDO;
+import cn.iocoder.yudao.module.mes.enums.pro.MesProTaskStatusEnum;
+import cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkstationService;
+import cn.iocoder.yudao.module.mes.service.pro.process.MesProProcessService;
import cn.iocoder.yudao.module.mes.service.pro.task.MesProTaskService;
import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService;
import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi;
@@ -22,10 +30,10 @@
import org.springframework.validation.annotation.Validated;
import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.time.LocalDateTime;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
@@ -45,6 +53,8 @@
@Resource
private MesProWorkOrderBomService workOrderBomService;
@Resource
+ private MesProWorkOrderProcessService workOrderProcessService;
+ @Resource
private MesMdItemService itemService;
@Resource
private MesMdItemBatchConfigService itemBatchConfigService;
@@ -52,6 +62,10 @@
private MesWmBarcodeService barcodeService;
@Resource
private MesProTaskService taskService;
+ @Resource
+ private MesProProcessService processService;
+ @Resource
+ private MesMdWorkstationService workstationService;
@Resource
private CrmCustomerApi customerApi;
@@ -73,7 +87,9 @@
// 3. 鑷姩鐢熸垚 BOM锛氭牴鎹骇鍝� BOM 鐢熸垚宸ュ崟 BOM
workOrderBomService.generateWorkOrderBom(workOrder.getId(), createReqVO, false);
- // 4. 鑷姩鐢熸垚鏉$爜
+ // 4. 宸ュ簭鐢卞墠绔�夋嫨浜у搧鍚庡甫鍏ワ紝涓嶅啀鑷姩鐢熸垚
+
+ // 5. 鑷姩鐢熸垚鏉$爜
barcodeService.autoGenerateBarcode(BarcodeBizTypeEnum.WORKORDER.getValue(),
workOrder.getId(), workOrder.getCode(), workOrder.getName());
return workOrder.getId();
@@ -96,6 +112,7 @@
if (productChanged || quantityChanged) {
workOrderBomService.generateWorkOrderBom(updateReqVO.getId(), updateReqVO, true);
}
+ // 2.1 宸ュ簭鐢卞墠绔鐞嗭紝浜у搧鍙樻洿鏃跺墠绔細閲嶆柊鑾峰彇宸ヨ壓璺嚎宸ュ簭
// 3. 鏇存柊
MesProWorkOrderDO updateObj = BeanUtils.toBean(updateReqVO, MesProWorkOrderDO.class);
@@ -117,9 +134,10 @@
throw exception(PRO_WORK_ORDER_HAS_CHILDREN);
}
- // 2. 鍒犻櫎宸ュ崟 + BOM
+ // 2. 鍒犻櫎宸ュ崟 + BOM + 宸ュ簭
workOrderMapper.deleteById(id);
workOrderBomService.deleteWorkOrderBomByWorkOrderId(id);
+ workOrderProcessService.deleteWorkOrderProcessByWorkOrderId(id);
}
@Override
@@ -251,6 +269,137 @@
}
@Override
+ public void updateScheduledQuantity(Long id, BigDecimal incrQuantityScheduled) {
+ // 鏍¢獙宸ュ崟瀛樺湪
+ validateWorkOrderExists(id);
+ // 鏇存柊鏁伴噺
+ workOrderMapper.updateScheduledQuantity(id, incrQuantityScheduled);
+ }
+
+ @Override
+ public void validateScheduleQuantity(Long id, BigDecimal scheduleQuantity) {
+ MesProWorkOrderDO workOrder = validateWorkOrderExists(id);
+ BigDecimal currentScheduled = workOrder.getQuantityScheduled() != null ? workOrder.getQuantityScheduled() : BigDecimal.ZERO;
+ BigDecimal totalScheduled = currentScheduled.add(scheduleQuantity);
+ if (totalScheduled.compareTo(workOrder.getQuantity()) > 0) {
+ throw exception(PRO_WORK_ORDER_SCHEDULE_QUANTITY_EXCEED,
+ workOrder.getCode(),
+ workOrder.getQuantity(),
+ currentScheduled,
+ scheduleQuantity);
+ }
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public boolean checkAndAutoFinishWorkOrder(Long id) {
+ MesProWorkOrderDO workOrder = getWorkOrder(id);
+ if (workOrder == null) {
+ return false;
+ }
+ // 鏈紑鍚嚜鍔ㄥ畬鎴�
+ if (!Boolean.TRUE.equals(workOrder.getAutoFinishFlag())) {
+ return false;
+ }
+ // 鐘舵�佷笉鏄凡纭
+ if (!ObjUtil.equal(workOrder.getStatus(), MesProWorkOrderStatusEnum.CONFIRMED.getStatus())) {
+ return false;
+ }
+ // 宸茬敓浜ф暟閲� >= 鐢熶骇鏁伴噺
+ BigDecimal quantityProduced = workOrder.getQuantityProduced() != null ? workOrder.getQuantityProduced() : BigDecimal.ZERO;
+ if (quantityProduced.compareTo(workOrder.getQuantity()) >= 0) {
+ finishWorkOrder(id);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public MesProWorkOrderProgressRespVO getWorkOrderProgress(Long id) {
+ MesProWorkOrderDO workOrder = getWorkOrder(id);
+ if (workOrder == null) {
+ return null;
+ }
+ // 1. 鑾峰彇宸ュ崟宸ュ簭鍒楄〃
+ List<MesProWorkOrderProcessDO> processes = workOrderProcessService.getWorkOrderProcessListByWorkOrderId(id);
+ // 2. 鑾峰彇宸ュ崟涓嬫墍鏈変换鍔★紝鎸夊伐搴忓垎缁�
+ List<MesProTaskDO> allTasks = taskService.getTaskListByWorkOrderIds(Collections.singletonList(id));
+ Map<Long, List<MesProTaskDO>> taskMap = allTasks.stream()
+ .collect(Collectors.groupingBy(t -> t.getProcessId() != null ? t.getProcessId() : 0L));
+ // 3. 鎵归噺鏌ヨ宸ュ簭鍚嶇О鍜屽伐浣滅珯鍚嶇О
+ Set<Long> processIds = processes.stream().map(MesProWorkOrderProcessDO::getProcessId).collect(Collectors.toSet());
+ Map<Long, MesProProcessDO> processMap = processService.getProcessMap(processIds);
+ Set<Long> workstationIds = allTasks.stream().map(MesProTaskDO::getWorkstationId).filter(Objects::nonNull).collect(Collectors.toSet());
+ Map<Long, MesMdWorkstationDO> workstationMap = workstationService.getWorkstationMap(workstationIds);
+ // 4. 璁$畻杩涘害
+ BigDecimal quantity = workOrder.getQuantity() != null ? workOrder.getQuantity() : BigDecimal.ZERO;
+ BigDecimal quantityProduced = workOrder.getQuantityProduced() != null ? workOrder.getQuantityProduced() : BigDecimal.ZERO;
+ BigDecimal quantityScheduled = workOrder.getQuantityScheduled() != null ? workOrder.getQuantityScheduled() : BigDecimal.ZERO;
+ MesProWorkOrderProgressRespVO vo = new MesProWorkOrderProgressRespVO()
+ .setWorkOrderId(workOrder.getId())
+ .setWorkOrderCode(workOrder.getCode())
+ .setQuantity(quantity)
+ .setQuantityScheduled(quantityScheduled)
+ .setQuantityProduced(quantityProduced)
+ .setProgressPercent(calcPercent(quantityProduced, quantity));
+ // 5. 缁勮宸ュ簭杩涘害
+ List<MesProWorkOrderProgressRespVO.ProcessProgress> processList = new ArrayList<>();
+ for (MesProWorkOrderProcessDO wp : processes) {
+ MesProProcessDO proc = processMap.get(wp.getProcessId());
+ List<MesProTaskDO> procTasks = taskMap.getOrDefault(wp.getProcessId(), Collections.emptyList());
+ BigDecimal procProduced = procTasks.stream()
+ .map(t -> t.getProducedQuantity() != null ? t.getProducedQuantity() : BigDecimal.ZERO)
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
+ BigDecimal plannedQty = Boolean.TRUE.equals(wp.getKeyFlag()) ? quantity : null;
+ int procStatus = calcProcessStatus(procTasks);
+ List<MesProWorkOrderProgressRespVO.TaskInfo> taskInfoList = procTasks.stream().map(t -> {
+ MesMdWorkstationDO ws = workstationMap.get(t.getWorkstationId());
+ BigDecimal tQty = t.getQuantity() != null ? t.getQuantity() : BigDecimal.ZERO;
+ BigDecimal tProduced = t.getProducedQuantity() != null ? t.getProducedQuantity() : BigDecimal.ZERO;
+ return new MesProWorkOrderProgressRespVO.TaskInfo()
+ .setTaskId(t.getId())
+ .setTaskCode(t.getCode())
+ .setWorkstationName(ws != null ? ws.getName() : null)
+ .setQuantity(tQty)
+ .setProducedQuantity(tProduced)
+ .setProgressPercent(calcPercent(tProduced, tQty))
+ .setStatus(t.getStatus());
+ }).collect(Collectors.toList());
+ processList.add(new MesProWorkOrderProgressRespVO.ProcessProgress()
+ .setProcessId(wp.getProcessId())
+ .setProcessCode(proc != null ? proc.getCode() : null)
+ .setProcessName(proc != null ? proc.getName() : null)
+ .setKeyFlag(wp.getKeyFlag())
+ .setPlannedQuantity(plannedQty)
+ .setProducedQuantity(procProduced)
+ .setProgressPercent(calcPercent(procProduced, plannedQty))
+ .setStatus(procStatus)
+ .setTaskList(taskInfoList));
+ }
+ vo.setProcessList(processList);
+ return vo;
+ }
+
+ private BigDecimal calcPercent(BigDecimal produced, BigDecimal total) {
+ if (total == null || total.compareTo(BigDecimal.ZERO) <= 0 || produced == null) {
+ return BigDecimal.ZERO;
+ }
+ return produced.multiply(new BigDecimal("100")).divide(total, 2, RoundingMode.HALF_UP);
+ }
+
+ private int calcProcessStatus(List<MesProTaskDO> tasks) {
+ if (tasks.isEmpty()) {
+ return 0; // 鏈紑濮�
+ }
+ boolean allFinished = tasks.stream().allMatch(t -> MesProTaskStatusEnum.isEndStatus(t.getStatus()));
+ if (allFinished) {
+ return 2; // 宸插畬鎴�
+ }
+ boolean anyStarted = tasks.stream().anyMatch(t -> !MesProTaskStatusEnum.PREPARE.getStatus().equals(t.getStatus()));
+ return anyStarted ? 1 : 0; // 杩涜涓� : 鏈紑濮�
+ }
+
+ @Override
public Long getWorkOrderCountByVendorId(Long vendorId) {
return workOrderMapper.selectCountByVendorId(vendorId);
}
--
Gitblit v1.9.3