Browse Source

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

zhongwei
tyty 1 year ago
parent
commit
a0930163a8
  1. 43
      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. 77
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/maintenance/service/impl/MaintenanceServiceImpl.java

43
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> 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<OperMaintenancePlanEntity> 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<Long> taskIds=new ArrayList<>();
if(StringUtil.isNotBlank(planEntity.getEmCode())){
List<String> emCodeList = Arrays.asList(planEntity.getEmCode().split(","));
for(String emCode : emCodeList){
//一旦有一条任务失败,则删除当前生成的任务,不更新当前的计划状态
try {
taskEntity.setEmCode(emCode);
maintenanceService.fillTask(taskEntity,planEntity,flag);
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;
}
/**

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 finalPlanEntity
*/
void fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity,Boolean flag);
Long fillTask(OperMaintenanceTaskEntity taskEntity, OperMaintenancePlanEntity finalPlanEntity,Boolean flag);
}

77
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<String> ids) {
try {
//获取日常维护计划
@ -95,13 +90,17 @@ public class MaintenanceServiceImpl implements MaintenanceService {
planEntityLambdaQueryWrapper.in(BaseEntity::getId, ids);
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)) {
throw new ServiceException("所选计划当天已生成任务");
}
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());
@ -112,6 +111,7 @@ public class MaintenanceServiceImpl implements MaintenanceService {
throw new ServiceException("更新任务派发失败");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
@ -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<Long> taskIds = new ArrayList<>();
if (StringUtil.isNotBlank(finalPlanEntity.getEmCode())) {
log.info("finalPlanEntity.getEmCode() : {}", finalPlanEntity.getEmCode());
String[] emCodeList = finalPlanEntity.getEmCode().split(",");
for (String emCode : emCodeList) {
try {
taskEntity.setEmCode(emCode);
this.fillTask(taskEntity, finalPlanEntity,false);
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<OperMaintenancePlanEntity> planQueryWrapper = Wrappers.<OperMaintenancePlanEntity>lambdaUpdate()
.set(OperMaintenancePlanEntity::getCreateTaskTime, new Date())
.eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId());
maintenancePlanService.update(planQueryWrapper);
return;
// LambdaUpdateWrapper<OperMaintenancePlanEntity> planQueryWrapper = Wrappers.<OperMaintenancePlanEntity>lambdaUpdate()
// .set(OperMaintenancePlanEntity::getCreateTaskTime, new Date())
// .eq(OperMaintenancePlanEntity::getId, finalPlanEntity.getId());
// maintenancePlanService.update(planQueryWrapper);
return null;
}
}
taskService.save(taskEntity);
// 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()
.set(OperMaintenanceTaskEntity::getProcessInstanceId, processInstanceId)
.eq(OperMaintenanceTaskEntity::getId, taskEntity.getId());
taskService.update(taskQueryWrapper);
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);
});
return taskEntity.getId();
}

Loading…
Cancel
Save