Browse Source

修改巡检任务bug:当前流程环节和当前处理人有可能有,有可能没有

zhongwei
tyty 1 year ago
parent
commit
a0930163a8
  1. 45
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/scheduled/MaintenanceTaskCreateTask.java
  2. 2
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/MaintenanceService.java
  3. 95
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/impl/MaintenanceServiceImpl.java

45
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.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.dto.PlanMsgRecordDto;
import com.hnac.hzims.message.entity.config.MessageTemplateEntity; import com.hnac.hzims.message.entity.config.MessageTemplateEntity;
import com.hnac.hzims.message.fegin.IMessageClient; 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.OperMaintenancePlanEntity;
import com.hnac.hzims.operational.maintenance.entity.OperMaintenanceTaskEntity; import com.hnac.hzims.operational.maintenance.entity.OperMaintenanceTaskEntity;
import com.hnac.hzims.operational.maintenance.mapper.OperMaintenancePlanMapper; 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.IOperMaintenanceTaskService;
import com.hnac.hzims.operational.maintenance.service.MaintenanceService; import com.hnac.hzims.operational.maintenance.service.MaintenanceService;
import com.hnac.hzims.operational.maintenance.template.service.TemplateService; import com.hnac.hzims.operational.maintenance.template.service.TemplateService;
import com.hnac.hzims.operational.maintenance.vo.OperMaintenanceTaskEntityVo; 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.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger; import com.xxl.job.core.log.XxlJobLogger;
@ -70,6 +71,7 @@ public class MaintenanceTaskCreateTask {
private final MaintenanceService maintenanceService; private final MaintenanceService maintenanceService;
private final IOperMaintenancePlanService maintenancePlanService;
private final List<TemplateService> templateService; private final List<TemplateService> templateService;
@ -97,9 +99,16 @@ public class MaintenanceTaskCreateTask {
//生成日常巡检任务并插入数据库 //生成日常巡检任务并插入数据库
for (int i = startIndex; i < endIndex; i++) { for (int i = startIndex; i < endIndex; i++) {
this.generateTaskByPlan(planEntities.get(i),true); Boolean aBoolean = this.generateTaskByPlan(planEntities.get(i), true);
//更新任务派发时间
if (aBoolean) {
//当所有的任务生成后,才会更新计划的时间状态
LambdaUpdateWrapper<OperMaintenancePlanEntity> planEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
planEntityLambdaUpdateWrapper.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date());
planEntityLambdaUpdateWrapper.eq(OperMaintenancePlanEntity::getId, planEntities.get(i));
maintenancePlanService.update(planEntityLambdaUpdateWrapper);
}
} }
Thread.sleep(1000); Thread.sleep(1000);
page++; page++;
} while (page * size < planEntities.size()); } while (page * size < planEntities.size());
@ -113,19 +122,41 @@ public class MaintenanceTaskCreateTask {
* @param planEntity * @param planEntity
* @return * @return
*/ */
public void generateTaskByPlan(OperMaintenancePlanEntity planEntity,Boolean flag) { public Boolean generateTaskByPlan(OperMaintenancePlanEntity planEntity,Boolean flag) {
OperMaintenanceTaskEntity taskEntity = BeanUtil.copy(planEntity,OperMaintenanceTaskEntity.class); OperMaintenanceTaskEntity taskEntity = BeanUtil.copy(planEntity,OperMaintenanceTaskEntity.class);
//拆分设备 每个设备生成一条任务 //拆分设备 每个设备生成一条任务
List<Long> taskIds=new ArrayList<>();
if(StringUtil.isNotBlank(planEntity.getEmCode())){ if(StringUtil.isNotBlank(planEntity.getEmCode())){
List<String> emCodeList = Arrays.asList(planEntity.getEmCode().split(",")); List<String> emCodeList = Arrays.asList(planEntity.getEmCode().split(","));
for(String emCode : emCodeList){ 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 { 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;
} }
/** /**

2
hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/MaintenanceService.java

@ -38,5 +38,5 @@ public interface MaintenanceService {
* @param taskEntity * @param taskEntity
* @param finalPlanEntity * @param finalPlanEntity
*/ */
void fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity,Boolean flag); Long fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity,Boolean flag);
} }

95
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.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.hnac.hzims.common.utils.DateUtil; import com.hnac.hzims.common.utils.DateUtil;
import com.hnac.hzims.message.MessageConstants; import com.hnac.hzims.message.MessageConstants;
import com.hnac.hzims.message.dto.BusinessMessageDTO; 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.beans.BeanUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service; 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.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
@ -49,7 +45,6 @@ import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -87,7 +82,7 @@ public class MaintenanceServiceImpl implements MaintenanceService {
* @param ids * @param ids
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class) // @Transactional(rollbackFor = Exception.class)
public void createTask(List<String> ids) { public void createTask(List<String> ids) {
try { try {
//获取日常维护计划 //获取日常维护计划
@ -95,21 +90,26 @@ public class MaintenanceServiceImpl implements MaintenanceService {
planEntityLambdaQueryWrapper.in(BaseEntity::getId, ids); planEntityLambdaQueryWrapper.in(BaseEntity::getId, ids);
List<OperMaintenancePlanEntity> planEntities = maintenancePlanService.list(planEntityLambdaQueryWrapper); List<OperMaintenancePlanEntity> planEntities = maintenancePlanService.list(planEntityLambdaQueryWrapper);
//筛选当月没生成任务的计划 //筛选当月没生成任务的计划
List<OperMaintenancePlanEntity> finalPlanEntities = planEntities.stream().filter(planEntity -> ObjectUtil.isEmpty(planEntity.getCreateTaskTime()) || !DateUtil.judgeSameDay(DateUtil.DateToLocalDateTime(planEntity.getCreateTaskTime()), LocalDate.now())).collect(Collectors.toList()); List<OperMaintenancePlanEntity> finalPlanEntities = planEntities.stream().
filter(planEntity -> ObjectUtil.isEmpty(planEntity.getCreateTaskTime()) ||
!DateUtil.judgeSameDay(DateUtil.DateToLocalDateTime(planEntity.getCreateTaskTime()), LocalDate.now()))
.collect(Collectors.toList());
if (CollectionUtil.isEmpty(finalPlanEntities)) { if (CollectionUtil.isEmpty(finalPlanEntities)) {
throw new ServiceException("所选计划当天已生成任务"); throw new ServiceException("所选计划当天已生成任务");
} }
for (OperMaintenancePlanEntity finalPlanEntity : finalPlanEntities) { for (OperMaintenancePlanEntity finalPlanEntity : finalPlanEntities) {
// 通过计划生成任务 // 通过计划生成任务
this.createTaskByPlan(finalPlanEntity); Boolean taskByPlan = this.createTaskByPlan(finalPlanEntity);
//更新任务派发时间 if (taskByPlan) {
LambdaUpdateWrapper<OperMaintenancePlanEntity> planEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>(); //更新任务派发时间
planEntityLambdaUpdateWrapper.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()); LambdaUpdateWrapper<OperMaintenancePlanEntity> planEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
planEntityLambdaUpdateWrapper.eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); planEntityLambdaUpdateWrapper.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date());
boolean update = maintenancePlanService.update(planEntityLambdaUpdateWrapper); planEntityLambdaUpdateWrapper.eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId());
if (!update) { boolean update = maintenancePlanService.update(planEntityLambdaUpdateWrapper);
log.error("maintenance:generateTask 更新任务派发失败"); if (!update) {
throw new ServiceException("更新任务派发失败"); log.error("maintenance:generateTask 更新任务派发失败");
throw new ServiceException("更新任务派发失败");
}
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -266,20 +266,40 @@ public class MaintenanceServiceImpl implements MaintenanceService {
* *
* @param finalPlanEntity * @param finalPlanEntity
*/ */
private void createTaskByPlan(OperMaintenancePlanEntity finalPlanEntity) { private Boolean createTaskByPlan(OperMaintenancePlanEntity finalPlanEntity) {
OperMaintenanceTaskEntity taskEntity = BeanUtil.copy(finalPlanEntity, OperMaintenanceTaskEntity.class); OperMaintenanceTaskEntity taskEntity = BeanUtil.copy(finalPlanEntity, OperMaintenanceTaskEntity.class);
//拆分设备 每个设备生成一条任务 //拆分设备 每个设备生成一条任务
List<Long> taskIds = new ArrayList<>();
if (StringUtil.isNotBlank(finalPlanEntity.getEmCode())) { if (StringUtil.isNotBlank(finalPlanEntity.getEmCode())) {
log.info("finalPlanEntity.getEmCode() : {}", finalPlanEntity.getEmCode()); log.info("finalPlanEntity.getEmCode() : {}", finalPlanEntity.getEmCode());
String[] emCodeList = finalPlanEntity.getEmCode().split(","); String[] emCodeList = finalPlanEntity.getEmCode().split(",");
for (String emCode : emCodeList) { for (String emCode : emCodeList) {
taskEntity.setEmCode(emCode); try {
this.fillTask(taskEntity, finalPlanEntity,false); 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 { } 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 * @param finalPlanEntity
*/ */
@Override @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 = new DefaultTransactionDefinition();
// defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); // defaultTransactionDefinition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// TransactionStatus transaction = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition); // TransactionStatus transaction = dataSourceTransactionManager.getTransaction(defaultTransactionDefinition);
@ -320,29 +341,31 @@ public class MaintenanceServiceImpl implements MaintenanceService {
taskEntity.setDisposer(userId.replaceAll("\\^", ",")); taskEntity.setDisposer(userId.replaceAll("\\^", ","));
} else { } else {
log.info("计划ID为:{}未查询到排班记录", finalPlanEntity.getId()); log.info("计划ID为:{}未查询到排班记录", finalPlanEntity.getId());
LambdaUpdateWrapper<OperMaintenancePlanEntity> planQueryWrapper = Wrappers.<OperMaintenancePlanEntity>lambdaUpdate() // LambdaUpdateWrapper<OperMaintenancePlanEntity> planQueryWrapper = Wrappers.<OperMaintenancePlanEntity>lambdaUpdate()
.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date()) // .set(OperMaintenancePlanEntity::getCreateTaskTime, new Date())
.eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId()); // .eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId());
maintenancePlanService.update(planQueryWrapper); // maintenancePlanService.update(planQueryWrapper);
return; return null;
} }
} }
taskService.save(taskEntity); taskService.save(taskEntity);
// dataSourceTransactionManager.commit(transaction); // dataSourceTransactionManager.commit(transaction);
//等当前计划的所有任务生成后更新当前计划时间
// LambdaUpdateWrapper<OperMaintenancePlanEntity> planQueryWrapper = Wrappers.<OperMaintenancePlanEntity>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<OperMaintenanceTaskEntity> taskQueryWrapper = Wrappers.<OperMaintenanceTaskEntity>lambdaUpdate() LambdaUpdateWrapper<OperMaintenanceTaskEntity> taskQueryWrapper = Wrappers.<OperMaintenanceTaskEntity>lambdaUpdate()
.set(OperMaintenanceTaskEntity::getProcessInstanceId, processInstanceId) .set(OperMaintenanceTaskEntity::getProcessInstanceId, processInstanceId)
.eq(OperMaintenanceTaskEntity::getId, taskEntity.getId()); .eq(OperMaintenanceTaskEntity::getId, taskEntity.getId());
taskService.update(taskQueryWrapper); taskService.update(taskQueryWrapper);
LambdaUpdateWrapper<OperMaintenancePlanEntity> planQueryWrapper = Wrappers.<OperMaintenancePlanEntity>lambdaUpdate() return taskEntity.getId();
.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date())
.eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId());
maintenancePlanService.update(planQueryWrapper);
executor.execute(() -> {
OperMaintenanceTaskEntity maintenanceTask = taskService.getById(taskEntity.getId());
this.pushTaskMessage(maintenanceTask);
});
} }

Loading…
Cancel
Save