|
|
|
|
@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.security.core.LoginUser;
|
|
|
|
|
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
|
|
import cn.iocoder.yudao.module.iot.controller.admin.device.scheduled.scheduler.TaskSchedulerManager;
|
|
|
|
|
import cn.iocoder.yudao.module.common.controller.admin.moldticketresults.vo.MoldTicketResultsSaveReqVO;
|
|
|
|
|
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldBrandDO;
|
|
|
|
|
import cn.iocoder.yudao.module.common.dal.dataobject.moldticketmanagement.MoldTicketManagementDO;
|
|
|
|
|
@ -14,6 +15,7 @@ import cn.iocoder.yudao.module.common.dal.mysql.moldticketresults.MoldTicketResu
|
|
|
|
|
import cn.iocoder.yudao.module.mes.controller.admin.moldtaskmanagement.vo.MoldTaskManagementCreateTicketReqVO;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.controller.admin.moldtaskmanagement.vo.MoldTaskManagementPageReqVO;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.controller.admin.moldtaskmanagement.vo.MoldTaskManagementSaveReqVO;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.controller.admin.moldtaskmanagement.vo.MoldTaskManagementUpdateEnabledReqVO;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.controller.admin.moldticketresults.enums.MoldJobResultEnum;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.dal.dataobject.moldplanmaintenance.MoldPlanMaintenanceDO;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.dal.dataobject.moldsubject.MoldSubjectDO;
|
|
|
|
|
@ -23,7 +25,14 @@ import cn.iocoder.yudao.module.mes.dal.mysql.moldplanmaintenance.MoldPlanMainten
|
|
|
|
|
import cn.iocoder.yudao.module.mes.dal.mysql.moldsubject.MoldSubjectMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.dal.mysql.moldtaskmanagement.MoldTaskManagementMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.dal.mysql.subjectmoldplan.SubjectMoldPlanMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.mes.service.notify.MesNotifyMessageService;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
|
import com.cronutils.model.Cron;
|
|
|
|
|
import com.cronutils.model.CronType;
|
|
|
|
|
import com.cronutils.model.definition.CronDefinitionBuilder;
|
|
|
|
|
import com.cronutils.model.time.ExecutionTime;
|
|
|
|
|
import com.cronutils.parser.CronParser;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
@ -31,17 +40,24 @@ import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
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.TASK_CORN_NOT_EXISTS;
|
|
|
|
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.TASK_CORN_NOT_LE_HOUR;
|
|
|
|
|
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.TASK_MANAGEMENT_NOT_EXISTS;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@ -49,6 +65,8 @@ import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.TASK_MANAGEME
|
|
|
|
|
@Validated
|
|
|
|
|
public class MoldTaskManagementServiceImpl implements MoldTaskManagementService {
|
|
|
|
|
|
|
|
|
|
private static final String MOLD_TASK_TICKET_CREATE_NOTIFY_TEMPLATE_CODE = "mes_mold_task_ticket_create";
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private MoldTaskManagementMapper moldtaskManagementMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
@ -64,13 +82,44 @@ public class MoldTaskManagementServiceImpl implements MoldTaskManagementService
|
|
|
|
|
@Resource
|
|
|
|
|
private MoldSubjectMapper moldSubjectMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private TaskSchedulerManager taskSchedulerManager;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private MesNotifyMessageService mesNotifyMessageService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Long createMoldTaskManagement(MoldTaskManagementSaveReqVO createReqVO) {
|
|
|
|
|
isCronIntervalAtLeastOneHour(createReqVO.getCronExpression());
|
|
|
|
|
MoldTaskManagementDO moldtaskManagement = BeanUtils.toBean(createReqVO, MoldTaskManagementDO.class);
|
|
|
|
|
moldtaskManagementMapper.insert(moldtaskManagement);
|
|
|
|
|
return moldtaskManagement.getId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void isCronIntervalAtLeastOneHour(String cronExpr) {
|
|
|
|
|
try {
|
|
|
|
|
if (StringUtils.isBlank(cronExpr)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
|
|
|
|
|
Cron cron = parser.parse(cronExpr);
|
|
|
|
|
cron.validate();
|
|
|
|
|
|
|
|
|
|
ExecutionTime executionTime = ExecutionTime.forCron(cron);
|
|
|
|
|
ZonedDateTime now = ZonedDateTime.now();
|
|
|
|
|
ZonedDateTime next1 = executionTime.nextExecution(now).get();
|
|
|
|
|
ZonedDateTime next2 = executionTime.nextExecution(next1).get();
|
|
|
|
|
|
|
|
|
|
Duration duration = Duration.between(next1, next2);
|
|
|
|
|
if (duration.toHours() < 1) {
|
|
|
|
|
throw exception(TASK_CORN_NOT_LE_HOUR);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw exception(TASK_CORN_NOT_LE_HOUR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateMoldTaskManagement(MoldTaskManagementSaveReqVO updateReqVO) {
|
|
|
|
|
validateMoldTaskManagementExists(updateReqVO.getId());
|
|
|
|
|
@ -170,6 +219,7 @@ public class MoldTaskManagementServiceImpl implements MoldTaskManagementService
|
|
|
|
|
moldticketManagementDO.setConfigName(configName);
|
|
|
|
|
moldticketManagementDO.setTaskEndTime(endDate !=null ? endDate.atStartOfDay(): null);
|
|
|
|
|
moldticketManagementMapper.insert(moldticketManagementDO);
|
|
|
|
|
sendMoldTicketCreateNotifyMessage(taskId, moldticketManagementDO);
|
|
|
|
|
|
|
|
|
|
List<MoldTicketResultsDO> createdResults = new ArrayList<>();
|
|
|
|
|
for (int index = 0; index < moldSubjectDOList.size(); index++) {
|
|
|
|
|
@ -191,6 +241,85 @@ public class MoldTaskManagementServiceImpl implements MoldTaskManagementService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendMoldTicketCreateNotifyMessage(Long taskId, MoldTicketManagementDO moldticketManagementDO) {
|
|
|
|
|
MoldTaskManagementDO moldtaskManagementDO = moldtaskManagementMapper.selectById(taskId);
|
|
|
|
|
if (moldtaskManagementDO == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Map<String, Object> templateParams = new HashMap<>();
|
|
|
|
|
templateParams.put("ticketId", moldticketManagementDO.getId());
|
|
|
|
|
templateParams.put("planNo", Objects.toString(moldticketManagementDO.getPlanNo(), ""));
|
|
|
|
|
templateParams.put("configName", Objects.toString(moldticketManagementDO.getConfigName(), ""));
|
|
|
|
|
templateParams.put("moldName", Objects.toString(moldticketManagementDO.getMoldName(), ""));
|
|
|
|
|
templateParams.put("taskEndTime", formatNotifyDateTime(moldticketManagementDO.getTaskEndTime()));
|
|
|
|
|
|
|
|
|
|
mesNotifyMessageService.sendToAdmins(moldtaskManagementDO.getOperableUsers(), MOLD_TASK_TICKET_CREATE_NOTIFY_TEMPLATE_CODE,
|
|
|
|
|
templateParams, "模具工单", moldticketManagementDO.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String formatNotifyDateTime(LocalDateTime dateTime) {
|
|
|
|
|
return dateTime == null ? "" : dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void updateMoldTaskManagementEnabled(MoldTaskManagementUpdateEnabledReqVO updateEnabledReqVO) {
|
|
|
|
|
MoldTaskManagementDO task = moldtaskManagementMapper.selectById(updateEnabledReqVO.getId());
|
|
|
|
|
if (task == null) {
|
|
|
|
|
throw exception(TASK_MANAGEMENT_NOT_EXISTS);
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isBlank(task.getCronExpression())) {
|
|
|
|
|
throw exception(TASK_CORN_NOT_EXISTS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Objects.equals(task.getEnabled(), updateEnabledReqVO.getEnabled())) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateMoldTaskStatusInDB(task.getId(), updateEnabledReqVO.getEnabled());
|
|
|
|
|
if (updateEnabledReqVO.getEnabled()) {
|
|
|
|
|
enableMoldTaskSchedule(task);
|
|
|
|
|
} else {
|
|
|
|
|
disableMoldTaskSchedule(task);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void enableMoldTaskSchedule(MoldTaskManagementDO task) {
|
|
|
|
|
if (StringUtils.isBlank(task.getCronExpression())) {
|
|
|
|
|
log.warn("模具任务{}没有配置Cron表达式,无法启用调度", task.getId());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
boolean success = taskSchedulerManager.startMoldWorkOrderTask(task.getId(), task.getCronExpression());
|
|
|
|
|
if (success) {
|
|
|
|
|
log.info("模具任务{}调度启动成功", task.getId());
|
|
|
|
|
} else {
|
|
|
|
|
log.error("模具任务{}调度启动失败", task.getId());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("模具任务{}调度启动异常", task.getId(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void disableMoldTaskSchedule(MoldTaskManagementDO task) {
|
|
|
|
|
try {
|
|
|
|
|
boolean success = taskSchedulerManager.stopMoldWorkOrderTask(task.getId());
|
|
|
|
|
if (success) {
|
|
|
|
|
log.info("模具任务{}调度停止成功", task.getId());
|
|
|
|
|
} else {
|
|
|
|
|
log.warn("模具任务{}调度停止失败,可能任务不存在或已停止", task.getId());
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("模具任务{}调度停止异常", task.getId(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateMoldTaskStatusInDB(Long taskId, Boolean enabled) {
|
|
|
|
|
MoldTaskManagementDO updateObj = new MoldTaskManagementDO();
|
|
|
|
|
updateObj.setId(taskId);
|
|
|
|
|
updateObj.setEnabled(enabled);
|
|
|
|
|
moldtaskManagementMapper.updateById(updateObj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private MoldTicketResultsSaveReqVO findMatchedTicketResult(List<MoldTicketResultsSaveReqVO> ticketResultsList,
|
|
|
|
|
String subjectName, int index) {
|
|
|
|
|
if (CollectionUtils.isEmpty(ticketResultsList)) {
|
|
|
|
|
|