From 550692e281c070209e613492cd1e08a17002634c Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Fri, 5 Sep 2025 20:29:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E7=9A=84=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/controller/TaskController.java | 14 ++++++++++ .../controller/TaskTemplateController.java | 1 + .../leopard/server/service/TaskService.java | 27 ++++++++++++++++++- .../task/UpdateStockInformationTask.java | 2 +- .../src/pages/task/TaskTemplateList.tsx | 23 +++++++++++++--- 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/controller/TaskController.java b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/controller/TaskController.java index 2c6608d..9203a03 100644 --- a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/controller/TaskController.java +++ b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/controller/TaskController.java @@ -7,6 +7,8 @@ import com.lanyuanxiaoyao.service.template.controller.SimpleControllerSupport; import java.time.LocalDateTime; import java.util.function.Function; import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -20,8 +22,11 @@ import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("task") public class TaskController extends SimpleControllerSupport { + private final TaskService taskService; + public TaskController(TaskService service) { super(service); + this.taskService = service; } @Override @@ -29,6 +34,12 @@ public class TaskController extends SimpleControllerSupport execute(@RequestBody ExecuteRequest request) { + taskService.execute(request.templateId()); + return GlobalResponse.responseSuccess(); + } + @Override protected Function saveItemMapper() { throw new UnsupportedOperationException(); @@ -81,4 +92,7 @@ public class TaskController extends SimpleControllerSupport { var template = new ClassTaskTemplate(); + template.setId(item.id()); template.setName(item.name()); template.setDescription(item.description()); template.setClazz(item.clazz()); diff --git a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java index 0d9e3f3..f68290d 100644 --- a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java +++ b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/TaskService.java @@ -1,9 +1,15 @@ package com.lanyuanxiaoyao.leopard.server.service; import com.lanyuanxiaoyao.leopard.server.entity.Task; +import com.lanyuanxiaoyao.leopard.server.entity.templates.ClassTaskTemplate; import com.lanyuanxiaoyao.leopard.server.repository.TaskRepository; +import com.lanyuanxiaoyao.leopard.server.service.task.TaskRunner; import com.lanyuanxiaoyao.service.template.service.SimpleServiceSupport; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; /** @@ -13,7 +19,26 @@ import org.springframework.stereotype.Service; @Slf4j @Service public class TaskService extends SimpleServiceSupport { - public TaskService(TaskRepository repository) { + private final ApplicationContext context; + private final TaskTemplateService taskTemplateService; + private final ExecutorService executors = Executors.newFixedThreadPool(50); + + public TaskService(TaskRepository repository, ApplicationContext context, TaskTemplateService taskTemplateService) { super(repository); + this.context = context; + this.taskTemplateService = taskTemplateService; + } + + public void execute(Long templateId) { + var template = taskTemplateService.detail(templateId); + switch (template.getType()) { + case CLASS -> { + ClassTaskTemplate classTaskTemplate = (ClassTaskTemplate) template; + var runner = context.getBean(classTaskTemplate.getClazz(), TaskRunner.class); + executors.submit(() -> { + runner.runTask(classTaskTemplate, Map.of()); + }); + } + } } } diff --git a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/task/UpdateStockInformationTask.java b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/task/UpdateStockInformationTask.java index c35da82..ba7a478 100644 --- a/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/task/UpdateStockInformationTask.java +++ b/leopard-server/src/main/java/com/lanyuanxiaoyao/leopard/server/service/task/UpdateStockInformationTask.java @@ -10,7 +10,7 @@ import java.util.Map; import java.util.stream.Collectors; import org.springframework.stereotype.Component; -@Component +@Component("com.lanyuanxiaoyao.leopard.server.service.task.UpdateStockInformationTask") public class UpdateStockInformationTask extends TaskRunner { private final StockService stockService; private final TuShareService tuShareService; diff --git a/leopard-web/src/pages/task/TaskTemplateList.tsx b/leopard-web/src/pages/task/TaskTemplateList.tsx index 93f04de..4ef8065 100644 --- a/leopard-web/src/pages/task/TaskTemplateList.tsx +++ b/leopard-web/src/pages/task/TaskTemplateList.tsx @@ -69,8 +69,23 @@ function TaskTemplateList() { { type: 'operation', label: '操作', - width: 100, + width: 150, buttons: [ + { + type: 'action', + label: '执行', + level: 'link', + actionType: 'ajax', + api: { + method: 'post', + url: `${commonInfo.baseUrl}/task/execute`, + data: { + templateId: '${id}', + }, + }, + confirmText: '确认执行模板${name}?', + confirmTitle: '删除', + }, { type: 'action', label: '详情', @@ -96,9 +111,9 @@ function TaskTemplateList() { level: 'link', actionType: 'ajax', api: `get:${commonInfo.baseUrl}/task_template/remove/\${id}`, - confirmText: '确认删除模板[${name}]?', - confirmTitle: "删除", - } + confirmText: '确认删除模板${name}?', + confirmTitle: '删除', + }, ], }, ],