Pārlūkot izejas kodu

feature: 智能控制V2版本 第六版(日志结束)

Jayhaw 2 nedēļas atpakaļ
vecāks
revīzija
00f5214930

+ 45 - 0
src/main/java/cn/sciento/farm/automationv2/api/controller/IrrigationTaskScheduleController.java

@@ -0,0 +1,45 @@
+package cn.sciento.farm.automationv2.api.controller;
+
+
+import cn.sciento.core.iam.ResourceLevel;
+import cn.sciento.core.util.Results;
+import cn.sciento.farm.automationv2.api.dto.ScheduleRuleDTO;
+import cn.sciento.farm.automationv2.app.service.taskSchedule.IrrigationTaskScheduleService;
+import cn.sciento.farm.automationv2.domain.entity.TaskScheduleRule;
+import cn.sciento.swagger.annotation.Permission;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+
+/**
+ * 灌溉任务管理API
+ */
+@Api("灌溉任务-定时接口")
+@RestController
+@RequestMapping("/v2/{tenantId}/irrigation-task-schedule")
+public class IrrigationTaskScheduleController {
+
+    private IrrigationTaskScheduleService service;
+
+    public IrrigationTaskScheduleController(IrrigationTaskScheduleService service) {
+        this.service = service;
+    }
+
+    /**
+     * 创建定时
+     *
+     */
+    @PostMapping
+    @ApiOperation("定时任务")
+    @Permission(level = ResourceLevel.ORGANIZATION)
+    public ResponseEntity<TaskScheduleRule> createSchedule(@PathVariable Long tenantId,
+                                                       @Valid @RequestBody ScheduleRuleDTO ruleDTO) {
+
+        ruleDTO.setTenantId(tenantId);
+        return Results.success(service.create(ruleDTO));
+    }
+
+}

+ 0 - 52
src/main/java/cn/sciento/farm/automationv2/api/dto/CreateTaskRequest.java

@@ -26,58 +26,6 @@ public class CreateTaskRequest {
     private String taskName;
 
 
-    // ================== 定时规则列表(新增) ==================
-
-    /**
-     * 定时规则列表(可选,为空则不配置定时触发)
-     * 支持配置多个定时规则,CRON 和 SIMPLE 模式可以共存
-     */
-    private List<ScheduleRuleDTO> scheduleRules;
-
-    /**
-     * 定时规则DTO
-     */
-    @Data
-    public static class ScheduleRuleDTO {
-        /**
-         * 规则名称(用于标识和管理)
-         */
-        @NotBlank(message = "规则名称不能为空")
-        private String ruleName;
-
-        /**
-         * 定时类型:CRON / SIMPLE
-         */
-        @NotBlank(message = "定时类型不能为空")
-        private String scheduleType;
-
-        /**
-         * Cron表达式(scheduleType=CRON时必填)
-         */
-        private String cronExpression;
-
-        /**
-         * 起始时间(scheduleType=SIMPLE时必填)
-         */
-        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
-        private LocalDateTime startTime;
-
-        /**
-         * 执行间隔天数(scheduleType=SIMPLE时必填)
-         */
-        private Integer intervalDays;
-
-        /**
-         * 执行总次数(scheduleType=SIMPLE时必填)
-         */
-        private Integer totalTimes;
-
-        /**
-         * 是否启用(默认true)
-         */
-        private Boolean enabled;
-    }
-
     // ================== 联动规则列表(新增) ==================
 
     /**

+ 67 - 0
src/main/java/cn/sciento/farm/automationv2/api/dto/ScheduleRuleDTO.java

@@ -0,0 +1,67 @@
+package cn.sciento.farm.automationv2.api.dto;
+
+import cn.sciento.farm.automationv2.domain.enums.ScheduleType;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import java.time.LocalDateTime;
+
+/**
+ * @author jayhaw
+ * @date 2026/3/14 14:34
+ */
+@Data
+public class ScheduleRuleDTO {
+
+    public String DEFAULT_NAME = "定时";
+
+    /**
+     * 租户Id
+     */
+    @NotBlank(message = "租户Id不能为空")
+    private Long tenantId;
+
+    /**
+     * 任务Id
+     */
+    @NotBlank(message = "任务Id不能为空")
+    private Long taskId;
+
+    /**
+     * 规则名称(用于标识和管理)
+     */
+    private String ruleName;
+
+    /**
+     * 定时类型:CRON / SIMPLE
+     */
+    @NotBlank(message = "定时类型不能为空")
+    private ScheduleType scheduleType;
+
+    /**
+     * Cron表达式(scheduleType=CRON时必填)
+     */
+    private String cronExpression;
+
+    /**
+     * 起始时间(scheduleType=SIMPLE时必填)
+     */
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private LocalDateTime startTime;
+
+    /**
+     * 执行间隔天数(scheduleType=SIMPLE时必填)
+     */
+    private Integer intervalDays;
+
+    /**
+     * 执行总次数(scheduleType=SIMPLE时必填)
+     */
+    private Integer totalTimes;
+
+    /**
+     * 是否启用(默认true)
+     */
+    private Boolean enabled;
+}

+ 31 - 0
src/main/java/cn/sciento/farm/automationv2/app/service/taskSchedule/IrrigationTaskScheduleService.java

@@ -0,0 +1,31 @@
+package cn.sciento.farm.automationv2.app.service.taskSchedule;
+
+
+import cn.sciento.core.domain.Page;
+import cn.sciento.farm.automationv2.api.dto.CreateTaskRequest;
+import cn.sciento.farm.automationv2.api.dto.DBSelectDTO;
+import cn.sciento.farm.automationv2.api.dto.ScheduleRuleDTO;
+import cn.sciento.farm.automationv2.domain.entity.IrrigationTask;
+import cn.sciento.farm.automationv2.domain.entity.TaskExecution;
+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.valueobject.IrrigationTaskVO;
+import io.choerodon.mybatis.pagehelper.domain.PageRequest;
+
+/**
+ * @author Jayhaw
+ * @description
+ * @date 2021/4/10 11:13
+ */
+public interface IrrigationTaskScheduleService {
+
+    /**
+     * 添加
+     * @param ruleDTO
+     * @return TaskScheduleRule
+     */
+    TaskScheduleRule create(ScheduleRuleDTO ruleDTO);
+
+
+}

+ 101 - 0
src/main/java/cn/sciento/farm/automationv2/app/service/taskSchedule/impl/IrrigationTaskScheduleServiceImpl.java

@@ -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;
+    }
+}

+ 2 - 1
src/main/java/cn/sciento/farm/automationv2/domain/entity/TaskScheduleRule.java

@@ -1,6 +1,7 @@
 package cn.sciento.farm.automationv2.domain.entity;
 
 import cn.sciento.farm.automationv2.domain.enums.ScheduleType;
+import cn.sciento.farm.automationv2.domain.enums.TaskScheduleRuleStatus;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import io.choerodon.mybatis.domain.AuditDomain;
 import io.swagger.annotations.ApiModel;
@@ -164,7 +165,7 @@ public class TaskScheduleRule extends AuditDomain {
      * 是否有效(启用且未完成)
      */
     public boolean isActive() {
-        return Boolean.TRUE.equals(enabled) && "ACTIVE".equals(status);
+        return Boolean.TRUE.equals(enabled) && TaskScheduleRuleStatus.ACTIVE.getCode().equals(status);
     }
 
     /**

+ 39 - 0
src/main/java/cn/sciento/farm/automationv2/domain/enums/TaskScheduleRuleStatus.java

@@ -0,0 +1,39 @@
+package cn.sciento.farm.automationv2.domain.enums;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * 任务状态枚举
+ */
+@Getter
+@AllArgsConstructor
+public enum TaskScheduleRuleStatus {
+
+    /**
+     * 活跃
+     */
+    ACTIVE(0, "活跃"),
+
+    /**
+     * 已完成
+     */
+    COMPLETED(1, "已完成"),
+
+    /**
+     * 暂停中
+     */
+    WAITING(2, "暂停中");
+
+    private final Integer code;
+    private final String desc;
+
+    public static TaskScheduleRuleStatus fromCode(String code) {
+        for (TaskScheduleRuleStatus status : values()) {
+            if (status.getCode().equals(code)) {
+                return status;
+            }
+        }
+        throw new IllegalArgumentException("Unknown TaskStatus code: " + code);
+    }
+}

+ 2 - 1
src/main/resources/messages/messages_wfautoV2_en_US.properties

@@ -11,7 +11,8 @@ wfautoV2.parameter.incomplete=Incomplete parameter
 wfautoV2.parameter.error=error parameter
 
 wfautoV2.group.hadTask=If there are bound tasks in the current Wheel and Tank Group, delete the tasks first
-wfautoV2.task.enabled=Disable the task before deleting it
+wfautoV2.task.enabled=Disable the |task before deleting it
+wfautoV2.task.notFound=The mission does not exist
 wfautoV2.task.hadRunning=The task is running and cannot be disabled for the time being
 wfautoV2.task.hadAction=The task has already started and cannot be started repeatedly
 

+ 1 - 0
src/main/resources/messages/messages_wfautoV2_zh_CN.properties

@@ -12,6 +12,7 @@ wfautoV2.parameter.error=\u53C2\u6570\u9519\u8BEF
 
 wfautoV2.group.hadTask=\u5F53\u524D\u8F6E\u7F50\u7EC4\u5B58\u5728\u5DF2\u7ED1\u5B9A\u4EFB\u52A1\uFF0C\u8BF7\u5148\u5220\u9664\u4EFB\u52A1
 wfautoV2.task.enabled=\u8BF7\u5148\u7981\u7528\u4EFB\u52A1\u518D\u5220\u9664
+wfautoV2.task.notFound=\u4EFB\u52A1\u4E0D\u5B58\u5728
 wfautoV2.task.hadRunning=\u4EFB\u52A1\u6B63\u5728\u8FD0\u884C\uFF0C\u6682\u65F6\u65E0\u6CD5\u7981\u7528
 wfautoV2.task.hadAction=\u4EFB\u52A1\u5DF2\u7ECF\u542F\u52A8\uFF0C\u65E0\u6CD5\u91CD\u590D\u542F\u52A8