From a0930163a896b15464aa494f4217b64c5d63f9bf Mon Sep 17 00:00:00 2001 From: tyty Date: Wed, 22 Nov 2023 16:48:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B7=A1=E6=A3=80=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1bug=EF=BC=9A=E5=BD=93=E5=89=8D=E6=B5=81=E7=A8=8B?= =?UTF-8?q?=E7=8E=AF=E8=8A=82=E5=92=8C=E5=BD=93=E5=89=8D=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=BA=BA=E6=9C=89=E5=8F=AF=E8=83=BD=E6=9C=89=EF=BC=8C=E6=9C=89?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E6=B2=A1=E6=9C=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduled/MaintenanceTaskCreateTask.java | 45 ++++++++-- .../maintenance/service/MaintenanceService.java | 2 +- .../service/impl/MaintenanceServiceImpl.java | 95 ++++++++++++++-------- 3 files changed, 98 insertions(+), 44 deletions(-) diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/scheduled/MaintenanceTaskCreateTask.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/scheduled/MaintenanceTaskCreateTask.java index 32e1ffa..8e39143 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/scheduled/MaintenanceTaskCreateTask.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/scheduled/MaintenanceTaskCreateTask.java @@ -2,6 +2,7 @@ package com.hnac.hzims.operational.maintenance.scheduled; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.hnac.hzims.message.dto.PlanMsgRecordDto; import com.hnac.hzims.message.entity.config.MessageTemplateEntity; import com.hnac.hzims.message.fegin.IMessageClient; @@ -10,11 +11,11 @@ import com.hnac.hzims.operational.duty.service.IImsDutyMainService; import com.hnac.hzims.operational.maintenance.entity.OperMaintenancePlanEntity; import com.hnac.hzims.operational.maintenance.entity.OperMaintenanceTaskEntity; import com.hnac.hzims.operational.maintenance.mapper.OperMaintenancePlanMapper; +import com.hnac.hzims.operational.maintenance.service.IOperMaintenancePlanService; import com.hnac.hzims.operational.maintenance.service.IOperMaintenanceTaskService; import com.hnac.hzims.operational.maintenance.service.MaintenanceService; import com.hnac.hzims.operational.maintenance.template.service.TemplateService; import com.hnac.hzims.operational.maintenance.vo.OperMaintenanceTaskEntityVo; -import com.hnac.hzims.operational.maintenance.vo.OperMaintenanceTaskVO; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.log.XxlJobLogger; @@ -70,6 +71,7 @@ public class MaintenanceTaskCreateTask { private final MaintenanceService maintenanceService; + private final IOperMaintenancePlanService maintenancePlanService; private final List templateService; @@ -97,9 +99,16 @@ public class MaintenanceTaskCreateTask { //生成日常巡检任务并插入数据库 for (int i = startIndex; i < endIndex; i++) { - this.generateTaskByPlan(planEntities.get(i),true); + Boolean aBoolean = this.generateTaskByPlan(planEntities.get(i), true); + //更新任务派发时间 + if (aBoolean) { + //当所有的任务生成后,才会更新计划的时间状态 + LambdaUpdateWrapper planEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>(); + planEntityLambdaUpdateWrapper.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()); + planEntityLambdaUpdateWrapper.eq(OperMaintenancePlanEntity::getId, planEntities.get(i)); + maintenancePlanService.update(planEntityLambdaUpdateWrapper); + } } - Thread.sleep(1000); page++; } while (page * size < planEntities.size()); @@ -113,19 +122,41 @@ public class MaintenanceTaskCreateTask { * @param planEntity * @return */ - public void generateTaskByPlan(OperMaintenancePlanEntity planEntity,Boolean flag) { + public Boolean generateTaskByPlan(OperMaintenancePlanEntity planEntity,Boolean flag) { OperMaintenanceTaskEntity taskEntity = BeanUtil.copy(planEntity,OperMaintenanceTaskEntity.class); //拆分设备 每个设备生成一条任务 + List taskIds=new ArrayList<>(); if(StringUtil.isNotBlank(planEntity.getEmCode())){ List emCodeList = Arrays.asList(planEntity.getEmCode().split(",")); for(String emCode : emCodeList){ - taskEntity.setEmCode(emCode); - maintenanceService.fillTask(taskEntity,planEntity,flag); + //一旦有一条任务失败,则删除当前生成的任务,不更新当前的计划状态 + try { + taskEntity.setEmCode(emCode); + Long task = maintenanceService.fillTask(taskEntity, planEntity, flag); + if (ObjectUtil.isNotEmpty(task)){ + taskIds.add(task); + } + }catch (Exception e){ + log.error("日常维护任务生成失败:" + "emCode:" + emCode + "错误原因:" + e); + log.error("日常维护任务生成失败:" + "param:" + planEntity + "错误原因:" + e); + taskService.deleteTaskBatch(taskIds); + return false; + } } } else { - maintenanceService.fillTask(taskEntity,planEntity,flag); + try { + Long task = maintenanceService.fillTask(taskEntity, planEntity, flag); + if (ObjectUtil.isNotEmpty(task)) { + taskIds.add(task); + } + } catch (Exception e) { + log.error("日常维护任务生成失败:" + "param:" + planEntity + "错误原因:" + e); + taskService.deleteTaskBatch(taskIds); + return false; + } } + return true; } /** diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/MaintenanceService.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/MaintenanceService.java index d58df2a..3a09896 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/MaintenanceService.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/MaintenanceService.java @@ -38,5 +38,5 @@ public interface MaintenanceService { * @param taskEntity * @param finalPlanEntity */ - void fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity,Boolean flag); + Long fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity,Boolean flag); } diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/impl/MaintenanceServiceImpl.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/impl/MaintenanceServiceImpl.java index e93d73f..b9c9785 100644 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/impl/MaintenanceServiceImpl.java +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/impl/MaintenanceServiceImpl.java @@ -7,7 +7,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.google.common.collect.Lists; import com.hnac.hzims.common.utils.DateUtil; import com.hnac.hzims.message.MessageConstants; import com.hnac.hzims.message.dto.BusinessMessageDTO; @@ -38,10 +37,7 @@ import org.springblade.system.feign.ISysClient; import org.springframework.beans.BeanUtils; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.annotation.Transactional; -import org.springframework.transaction.support.DefaultTransactionDefinition; import java.time.Instant; import java.time.LocalDate; @@ -49,7 +45,6 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.*; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ThreadPoolExecutor; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -87,7 +82,7 @@ public class MaintenanceServiceImpl implements MaintenanceService { * @param ids */ @Override - @Transactional(rollbackFor = Exception.class) +// @Transactional(rollbackFor = Exception.class) public void createTask(List ids) { try { //获取日常维护计划 @@ -95,21 +90,26 @@ public class MaintenanceServiceImpl implements MaintenanceService { planEntityLambdaQueryWrapper.in(BaseEntity::getId, ids); List planEntities = maintenancePlanService.list(planEntityLambdaQueryWrapper); //筛选当月没生成任务的计划 - List finalPlanEntities = planEntities.stream().filter(planEntity -> ObjectUtil.isEmpty(planEntity.getCreateTaskTime()) || !DateUtil.judgeSameDay(DateUtil.DateToLocalDateTime(planEntity.getCreateTaskTime()), LocalDate.now())).collect(Collectors.toList()); + List finalPlanEntities = planEntities.stream(). + filter(planEntity -> ObjectUtil.isEmpty(planEntity.getCreateTaskTime()) || + !DateUtil.judgeSameDay(DateUtil.DateToLocalDateTime(planEntity.getCreateTaskTime()), LocalDate.now())) + .collect(Collectors.toList()); if (CollectionUtil.isEmpty(finalPlanEntities)) { throw new ServiceException("所选计划当天已生成任务"); } for (OperMaintenancePlanEntity finalPlanEntity : finalPlanEntities) { // 通过计划生成任务 - this.createTaskByPlan(finalPlanEntity); - //更新任务派发时间 - LambdaUpdateWrapper planEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>(); - planEntityLambdaUpdateWrapper.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()); - planEntityLambdaUpdateWrapper.eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); - boolean update = maintenancePlanService.update(planEntityLambdaUpdateWrapper); - if (!update) { - log.error("maintenance:generateTask 更新任务派发失败"); - throw new ServiceException("更新任务派发失败"); + Boolean taskByPlan = this.createTaskByPlan(finalPlanEntity); + if (taskByPlan) { + //更新任务派发时间 + LambdaUpdateWrapper planEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>(); + planEntityLambdaUpdateWrapper.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()); + planEntityLambdaUpdateWrapper.eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); + boolean update = maintenancePlanService.update(planEntityLambdaUpdateWrapper); + if (!update) { + log.error("maintenance:generateTask 更新任务派发失败"); + throw new ServiceException("更新任务派发失败"); + } } } } catch (Exception e) { @@ -266,20 +266,40 @@ public class MaintenanceServiceImpl implements MaintenanceService { * * @param finalPlanEntity */ - private void createTaskByPlan(OperMaintenancePlanEntity finalPlanEntity) { + private Boolean createTaskByPlan(OperMaintenancePlanEntity finalPlanEntity) { OperMaintenanceTaskEntity taskEntity = BeanUtil.copy(finalPlanEntity, OperMaintenanceTaskEntity.class); //拆分设备 每个设备生成一条任务 + List taskIds = new ArrayList<>(); if (StringUtil.isNotBlank(finalPlanEntity.getEmCode())) { log.info("finalPlanEntity.getEmCode() : {}", finalPlanEntity.getEmCode()); String[] emCodeList = finalPlanEntity.getEmCode().split(","); for (String emCode : emCodeList) { - taskEntity.setEmCode(emCode); - this.fillTask(taskEntity, finalPlanEntity,false); + try { + taskEntity.setEmCode(emCode); + Long task = this.fillTask(taskEntity, finalPlanEntity, false); + if (ObjectUtil.isNotEmpty(task)) { + taskIds.add(task); + } + } catch (Exception e) { + log.error("日常维护任务生成失败:" + "emCode:" + emCode + "错误原因:" + e); + log.error("日常维护任务生成失败:" + "param:" + finalPlanEntity + "错误原因:" + e); + taskService.deleteTaskBatch(taskIds); + return false; + } } } else { - this.fillTask(taskEntity, finalPlanEntity,false); + try { + Long task = this.fillTask(taskEntity, finalPlanEntity, false); + if (ObjectUtil.isNotEmpty(task)) { + taskIds.add(task); + } + } catch (Exception e) { + log.error("日常维护任务生成失败:" + "param:" + finalPlanEntity + "错误原因:" + e); + taskService.deleteTaskBatch(taskIds); + return false; + } } - + return true; } @@ -290,7 +310,8 @@ public class MaintenanceServiceImpl implements MaintenanceService { * @param finalPlanEntity */ @Override - public void fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity,Boolean flag) { + @Transactional(rollbackFor = Exception.class) + public Long fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity, Boolean flag) { // DefaultTransactionDefinition = new DefaultTransactionDefinition(); // defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // TransactionStatus transaction = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition); @@ -320,29 +341,31 @@ public class MaintenanceServiceImpl implements MaintenanceService { taskEntity.setDisposer(userId.replaceAll("\\^", ",")); } else { log.info("计划ID为:{}未查询到排班记录", finalPlanEntity.getId()); - LambdaUpdateWrapper planQueryWrapper = Wrappers.lambdaUpdate() - .set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()) - .eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); - maintenancePlanService.update(planQueryWrapper); - return; +// LambdaUpdateWrapper planQueryWrapper = Wrappers.lambdaUpdate() +// .set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()) +// .eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); +// maintenancePlanService.update(planQueryWrapper); + return null; } } taskService.save(taskEntity); // dataSourceTransactionManager.commit(transaction); +//等当前计划的所有任务生成后更新当前计划时间 +// LambdaUpdateWrapper planQueryWrapper = Wrappers.lambdaUpdate() +// .set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()) +// .eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); +// maintenancePlanService.update(planQueryWrapper); + executor.execute(() -> { + OperMaintenanceTaskEntity maintenanceTask = taskService.getById(taskEntity.getId()); + this.pushTaskMessage(maintenanceTask); + }); //启动流程 - String processInstanceId = this.startProcess(finalPlanEntity.getProcDefId(), taskEntity,flag); + String processInstanceId = this.startProcess(finalPlanEntity.getProcDefId(), taskEntity, flag); LambdaUpdateWrapper taskQueryWrapper = Wrappers.lambdaUpdate() .set(OperMaintenanceTaskEntity::getProcessInstanceId, processInstanceId) .eq(OperMaintenanceTaskEntity::getId, taskEntity.getId()); taskService.update(taskQueryWrapper); - LambdaUpdateWrapper planQueryWrapper = Wrappers.lambdaUpdate() - .set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()) - .eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); - maintenancePlanService.update(planQueryWrapper); - executor.execute(() -> { - OperMaintenanceTaskEntity maintenanceTask = taskService.getById(taskEntity.getId()); - this.pushTaskMessage(maintenanceTask); - }); + return taskEntity.getId(); }