|
@@ -0,0 +1,101 @@
|
|
|
|
|
+package cn.sciento.farm.automationv2.app.service.taskSchedule.impl;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import cn.sciento.core.domain.Page;
|
|
|
|
|
+import cn.sciento.core.exception.CommonException;
|
|
|
|
|
+import cn.sciento.core.util.ValidUtils;
|
|
|
|
|
+import cn.sciento.farm.automationv2.api.dto.CreateTaskRequest;
|
|
|
|
|
+import cn.sciento.farm.automationv2.api.dto.DBSelectDTO;
|
|
|
|
|
+import cn.sciento.farm.automationv2.api.dto.IrrigationGroupDTO;
|
|
|
|
|
+import cn.sciento.farm.automationv2.api.dto.ScheduleRuleDTO;
|
|
|
|
|
+import cn.sciento.farm.automationv2.app.service.QuartzManagementService;
|
|
|
|
|
+import cn.sciento.farm.automationv2.app.service.SafeShutdownService;
|
|
|
|
|
+import cn.sciento.farm.automationv2.app.service.irrigationTask.IrrigationTaskService;
|
|
|
|
|
+import cn.sciento.farm.automationv2.app.service.taskSchedule.IrrigationTaskScheduleService;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.entity.IrrigationTask;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.entity.TaskExecution;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.entity.TaskGroupConfig;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.entity.TaskScheduleRule;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.entity.mongo.IrrigationTaskLog;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.entity.mongo.IrrigationTaskMainVO;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.enums.TaskScheduleRuleStatus;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.enums.TaskStatus;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.repository.*;
|
|
|
|
|
+import cn.sciento.farm.automationv2.domain.valueobject.IrrigationTaskVO;
|
|
|
|
|
+import cn.sciento.farm.automationv2.infra.constant.BaseConstant;
|
|
|
|
|
+import cn.sciento.farm.automationv2.infra.constant.RedisConstant;
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import io.choerodon.mybatis.pagehelper.PageHelper;
|
|
|
|
|
+import io.choerodon.mybatis.pagehelper.domain.PageRequest;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import javax.validation.Validator;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author Jayhaw
|
|
|
|
|
+ * @description
|
|
|
|
|
+ * @date 2021/1/18 15:51
|
|
|
|
|
+ */
|
|
|
|
|
+@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class IrrigationTaskScheduleServiceImpl implements IrrigationTaskScheduleService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private QuartzManagementService quartzManagementService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private TaskScheduleRuleRepository taskScheduleRuleRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private IrrigationTaskRepository taskRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public TaskScheduleRule create(ScheduleRuleDTO ruleDTO) {
|
|
|
|
|
+ IrrigationTask irrigationTask = taskRepository.selectByPrimaryKey(ruleDTO.getTaskId());
|
|
|
|
|
+ if (irrigationTask == null){
|
|
|
|
|
+ throw new CommonException("wfautoV2.task.notFound");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ TaskScheduleRule rule = TaskScheduleRule.builder()
|
|
|
|
|
+ .taskId(ruleDTO.getTaskId())
|
|
|
|
|
+ .ruleName(ruleDTO.DEFAULT_NAME)
|
|
|
|
|
+ .scheduleType(ruleDTO.getScheduleType())
|
|
|
|
|
+ .cronExpression(ruleDTO.getCronExpression())
|
|
|
|
|
+ .startTime(ruleDTO.getStartTime())
|
|
|
|
|
+ .intervalDays(ruleDTO.getIntervalDays())
|
|
|
|
|
+ .totalTimes(ruleDTO.getTotalTimes())
|
|
|
|
|
+ .executedCount(0)
|
|
|
|
|
+ .enabled(ruleDTO.getEnabled() != null ? ruleDTO.getEnabled() : true)
|
|
|
|
|
+ .status(TaskScheduleRuleStatus.ACTIVE.getCode())
|
|
|
|
|
+ .tenantId(ruleDTO.getTenantId())
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ // 插入规则
|
|
|
|
|
+ taskScheduleRuleRepository.insertSelective(rule);
|
|
|
|
|
+
|
|
|
|
|
+ // 填充Quartz相关字段
|
|
|
|
|
+ rule.fillQuartzNames();
|
|
|
|
|
+ taskScheduleRuleRepository.updateByPrimaryKeySelective(rule);
|
|
|
|
|
+ // 为启用的规则创建Quartz调度
|
|
|
|
|
+ if (Boolean.TRUE.equals(rule.getEnabled())) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ quartzManagementService.scheduleTaskRule(irrigationTask, rule);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("创建规则调度失败,将继续处理,taskId={}, ruleId={}",
|
|
|
|
|
+ rule.getTaskId(), rule.getId(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return rule;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|