Forráskód Böngészése

pms 定时任务 保养计划生成保养工单 功能优化

zhangcl 2 hónapja
szülő
commit
03d16a5959

+ 46 - 64
yudao-module-pms/yudao-module-pms-biz/src/main/java/cn/iocoder/yudao/module/pms/job/mainworkorder/CreateMainWorkOrderJob.java

@@ -136,10 +136,7 @@ public class CreateMainWorkOrderJob implements JobHandler {
                     // 累计运行时间规则   累计运行时间 >= (上次保养运行时间+运行时间周期-提前量)
                     BigDecimal lastRunningTime = bom.getLastRunningTime();      // 上次保养运行时间
                     BigDecimal runningTimePeriod = bom.getNextRunningTime();    // 运行时间周期
-                    BigDecimal timePeriodLead = bom.getTimePeriodLead();    // 运行时间周期提前量
-                    /* if (totalRunTime.compareTo(lastRunningTime.add(runningTimePeriod).subtract(timePeriodLead)) >= 0) {
-                        runningTimeArrive = true;
-                    } */
+                    BigDecimal timePeriodLead = bom.getTimePeriodLead();        // 运行时间周期提前量
                     if (((runningTimePeriod.subtract(totalRunTime.subtract(lastRunningTime))).subtract(timePeriodLead)).compareTo(BigDecimal.ZERO) <= 0) {
                         runningTimeArrive = true;
                     }
@@ -148,31 +145,27 @@ public class CreateMainWorkOrderJob implements JobHandler {
                     // 累计运行里程规则 累计运行里程 >= (上次保养运行里程+运行里程周期-提前量)
                     BigDecimal lastRunningKilo = bom.getLastRunningKilometers();      // 上次保养运行里程
                     BigDecimal runningKiloPeriod = bom.getNextRunningKilometers();    // 运行里程周期
-                    BigDecimal kiloCycleLead = bom.getKiloCycleLead();    // 运行里程周期提前量
-                    /* if (totalMileage.compareTo(lastRunningKilo.add(runningKiloPeriod).subtract(kiloCycleLead)) >= 0) {
-                        runningKiloArrive = true;
-                    } */
+                    BigDecimal kiloCycleLead = bom.getKiloCycleLead();                // 运行里程周期提前量
                     if (((runningKiloPeriod.subtract(totalMileage.subtract(lastRunningKilo))).subtract(kiloCycleLead)).compareTo(BigDecimal.ZERO) <= 0) {
                         runningTimeArrive = true;
                     }
                 }
-            } else {
-                // 没有维护 累计运行时间、累计运行里程 运行记录的设备 可以触发 自然日周期 保养规则
-                if (0 == bom.getNaturalDateRule()) {
-                    // 自然日期规则  当前日期 >= (上次保养自然日期+自然日周期-提前量)
-                    LocalDateTime lastNaturalDate = bom.getLastNaturalDate();      // 上次保养自然日期
-                    BigDecimal naturalDatePeriod = bom.getNextNaturalDate();        // 自然日周期
-                    BigDecimal natualDateLead = bom.getNaturalDatePeriodLead();    // 自然日周期提前量
-                    if (ObjUtil.isNotEmpty(lastNaturalDate) && ObjUtil.isNotEmpty(naturalDatePeriod) && ObjUtil.isNotEmpty(natualDateLead)) {
-                        // 计算有效天数 = 自然日周期 - 提前量
-                        long days = naturalDatePeriod.subtract(natualDateLead).longValue(); // 转为长整型天数
-                        // 计算目标日期:上次保养日期 + 有效天数(注意:LocalDateTime加天数会自动处理日期进位)
-                        LocalDateTime targetDate = lastNaturalDate.plusDays(days);
-                        // 获取当前日期时间(可根据需求调整为指定时区,如:ZoneId.systemDefault())
-                        LocalDateTime now = LocalDateTime.now();
-                        // 判断当前时间是否大于等于目标日期
-                        naturalDateArrive = now.isAfter(targetDate) || now.isEqual(targetDate);
-                    }
+            }
+            // 没有维护 累计运行时间、累计运行里程 运行记录的设备 可以触发 自然日周期 保养规则
+            if (0 == bom.getNaturalDateRule()) {
+                // 自然日期规则  当前日期 >= (上次保养自然日期+自然日周期-提前量)
+                LocalDateTime lastNaturalDate = bom.getLastNaturalDate();      // 上次保养自然日期
+                BigDecimal naturalDatePeriod = bom.getNextNaturalDate();       // 自然日周期
+                BigDecimal natualDateLead = bom.getNaturalDatePeriodLead();    // 自然日周期提前量
+                if (ObjUtil.isNotEmpty(lastNaturalDate) && ObjUtil.isNotEmpty(naturalDatePeriod) && ObjUtil.isNotEmpty(natualDateLead)) {
+                    // 计算有效天数 = 自然日周期 - 提前量
+                    long days = naturalDatePeriod.subtract(natualDateLead).longValue(); // 转为长整型天数
+                    // 计算目标日期:上次保养日期 + 有效天数(注意:LocalDateTime加天数会自动处理日期进位)
+                    LocalDateTime targetDate = lastNaturalDate.plusDays(days);
+                    // 获取当前日期时间(可根据需求调整为指定时区,如:ZoneId.systemDefault())
+                    LocalDateTime now = LocalDateTime.now();
+                    // 判断当前时间是否大于等于目标日期
+                    naturalDateArrive = now.isAfter(targetDate) || now.isEqual(targetDate);
                 }
             }
             if (runningTimeArrive || runningKiloArrive || naturalDateArrive) {
@@ -242,20 +235,9 @@ public class CreateMainWorkOrderJob implements JobHandler {
             // 最终使用的保养工单明细 设置了保养工单主表id
             List<IotMainWorkOrderBomDO> finalWorkOrderBomS = new ArrayList<>();
             if (CollUtil.isNotEmpty(workOrderBOMs)) {
-                /* workOrderBOMs.forEach(workOrderBom -> {
-                    if (workOrderBomPair.containsKey(workOrderBom.getWorkOrderId())) {
-                        List<IotMainWorkOrderBomDO> tempOrderBomS = workOrderBomPair.get(workOrderBom.getWorkOrderId());
-                        tempOrderBomS.add(workOrderBom);
-                        workOrderBomPair.put(workOrderBom.getWorkOrderId(), tempOrderBomS);
-                    } else {
-                        List<IotMainWorkOrderBomDO> tempOrderBomS = new ArrayList<>();
-                        tempOrderBomS.add(workOrderBom);
-                        workOrderBomPair.put(workOrderBom.getWorkOrderId(), tempOrderBomS);
-                    }
-                }); */
                 // 组装设备及对应的 保养项集合 map
                 workOrderBOMs.forEach(bom -> {
-                    String uniqueKey = bom.getWorkOrderId()+"-"+bom.getDeviceId();
+                    String uniqueKey = bom.getWorkOrderId() + "-" + bom.getDeviceId();
                     if (deviceBomPair.containsKey(uniqueKey)) {
                         List<IotMainWorkOrderBomDO> tempOrderBomS = deviceBomPair.get(uniqueKey);
                         tempOrderBomS.add(bom);
@@ -315,33 +297,34 @@ public class CreateMainWorkOrderJob implements JobHandler {
                         }
                     }
                 });
-
-                /* workOrderBomPair.forEach((k,v) -> {
-                    // k保养计划id   v已经触发的保养工单明细
-                    IotMaintenancePlanDO plan = maintenancePair.get(k);
-                    IotMainWorkOrderDO workOrder = new IotMainWorkOrderDO();
-                    theMaxId.getAndSet(theMaxId.get() + 1);
-                    workOrder.setId(theMaxId.get());
-                    workOrder.setDeptId(plan.getDeptId());
-                    workOrder.setPlanId(plan.getId());
-                    workOrder.setOrderNumber(iotMainWorkOrderService.createWorkOrderNumber());
-                    workOrder.setName(plan.getName() + " 工单");
-                    workOrder.setType(1);   // 计划生成
-                    workOrder.setResult(1); // 待执行
-                    workOrder.setResponsiblePerson(plan.getResponsiblePerson());
-                    workOrder.setAuditStatus(FailureAuditStatusEnum.DRAFT.getStatus());
-                    workOrder.setCreator(plan.getResponsiblePerson());
-                    workOrders.add(workOrder);
-                    // 2. 处理关联的BOM明细
-                    final long workOrderId = workOrder.getId(); // 获取当前工单ID
-                    v.forEach(bom -> bom.setWorkOrderId(workOrderId)); // 设置明细关联ID
-                    // 3. 添加到最终集合
-                    finalWorkOrderBomS.addAll(v);
-                }); */
             }
             // 批量新增 保养工单记录
             if (CollUtil.isNotEmpty(workOrders)) {
-                iotMainWorkOrderService.batchAddOrders(workOrders);
+                // 插入数据时深拷贝集合 后续发送消息提醒时需要用到 工单 id
+                List<IotMainWorkOrderDO> copiedWorkOrders = new ArrayList<>();
+                copiedWorkOrders.addAll(workOrders);
+                // 深度拷贝工单对象
+                List<IotMainWorkOrderDO> deepCopiedOrders = workOrders.stream()
+                        .map(order -> {
+                            IotMainWorkOrderDO copy = new IotMainWorkOrderDO();
+                            // 手动复制所有属性(根据实际字段补充)
+                            copy.setOrderGroupId(order.getOrderGroupId());
+                            copy.setDeptId(order.getDeptId());
+                            copy.setName(order.getName());
+                            copy.setPlanId(order.getPlanId());
+                            copy.setOrderNumber(order.getOrderNumber());
+                            copy.setType(order.getType());
+                            copy.setResult(order.getResult());
+                            copy.setResponsiblePerson(order.getResponsiblePerson());
+                            copy.setAuditStatus(order.getAuditStatus());
+                            copy.setCreator(order.getCreator());
+                            // 注意:不要复制ID,由服务层生成
+                            copy.setId(order.getId());
+                            return copy;
+                        })
+                        .collect(Collectors.toList());
+
+                iotMainWorkOrderService.batchAddOrders(deepCopiedOrders);
             }
             // 批量新增 保养工单明细
             if (CollUtil.isNotEmpty(finalWorkOrderBomS)) {
@@ -355,8 +338,8 @@ public class CreateMainWorkOrderJob implements JobHandler {
                 // 建立保养工单与 用户手机号的 对应关系
                 Map<Long, String> orderMobilePair = new HashMap<>();
                 workOrders.forEach(order -> {
-                    if (users.containsKey(order.getResponsiblePerson())) {
-                        AdminUserRespDTO user = users.get(order.getResponsiblePerson());
+                    if (users.containsKey(Long.valueOf(order.getResponsiblePerson()))) {
+                        AdminUserRespDTO user = users.get(Long.valueOf(order.getResponsiblePerson()));
                         orderMobilePair.put(order.getId(), user.getMobile());
                     }
                 });
@@ -369,7 +352,6 @@ public class CreateMainWorkOrderJob implements JobHandler {
                                 String mobile = orderMobilePair.get(order.getId());
                                 pmsMessage.sendMessage(order.getId(), order.getName(), PmsConstants.GENERATE_MAINTENANCE,
                                         Long.valueOf(order.getResponsiblePerson()), mobile);
-                                System.out.println("保养工单消息:" + order.getId() + "-" + mobile);
                             }
                         } finally {
                             latch.countDown();