初始代码

This commit is contained in:
wangmingwei
2026-04-21 16:49:46 +08:00
parent aae9dc4036
commit f0453ff3a3
2396 changed files with 256575 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yunzhupaas-java-boot</artifactId>
<groupId>com.yunzhupaas</groupId>
<version>5.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yunzhupaas-scheduletask</artifactId>
<packaging>pom</packaging>
<modules>
<module>yunzhupaas-scheduletask-controller</module>
<module>yunzhupaas-scheduletask-biz</module>
</modules>
</project>

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yunzhupaas-scheduletask</artifactId>
<groupId>com.yunzhupaas</groupId>
<version>5.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yunzhupaas-scheduletask-biz</artifactId>
<dependencies>
<dependency>
<groupId>com.yunzhupaas</groupId>
<artifactId>yunzhupaas-common-all</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.yunzhupaas</groupId>
<artifactId>yunzhupaas-common-scheduletask</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.yunzhupaas</groupId>
<artifactId>yunzhupaas-system-biz</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,152 @@
package com.yunzhupaas.scheduletask.task;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.base.entity.DataInterfaceEntity;
import com.yunzhupaas.base.service.DataInterfaceService;
import com.yunzhupaas.base.service.DbLinkService;
import com.yunzhupaas.database.util.TenantDataSourceUtil;
import com.yunzhupaas.emnus.TemplateEnum;
import com.yunzhupaas.exception.TenantInvalidException;
import com.yunzhupaas.model.visualJson.TemplateJsonModel;
import com.yunzhupaas.scheduletask.entity.TimeTaskEntity;
import com.yunzhupaas.scheduletask.model.ContentNewModel;
import com.yunzhupaas.scheduletask.model.UpdateTaskModel;
import com.yunzhupaas.scheduletask.rest.RestScheduleTaskUtil;
import com.yunzhupaas.util.AuthUtil;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* Api和数据接口使用
*
* @author :云筑产品开发平台组
* @version: V3.1.0
* @copyright 深圳市乐程软件有限公司
* @date 2022/3/23 9:25
*/
@Slf4j
@Component
public class ScheduleTaskHandler {
@Autowired
private DataInterfaceService dataInterfaceService;
@Autowired
private DbLinkService dbLinkService;
@XxlJob("defaultHandler")
public void defaultHandler() {
// 获取参数
String param = XxlJobHelper.getJobParam();
// 转换成模型
TimeTaskEntity entity = JsonUtil.getJsonToBean(param, TimeTaskEntity.class);
ContentNewModel model = JsonUtil.getJsonToBean(param, ContentNewModel.class);
String tenantId = StringUtil.isNotEmpty(model.getUserInfo().getTenantId()) ? model.getUserInfo().getTenantId() : "";
String userId = StringUtil.isNotEmpty(model.getUserInfo().getUserId()) ? model.getUserInfo().getUserId() : "";
String token = AuthUtil.loginTempUser(userId, tenantId, true);
// 切换租户
UserInfo userInfo = model.getUserInfo();
// 切换数据源
if (userInfo != null && StringUtil.isNotEmpty(userInfo.getTenantId())) {
try {
TenantDataSourceUtil.switchTenant(userInfo.getTenantId());
}catch (TenantInvalidException e){
// 租户无效 禁用任务
log.error("ScheduleTaskHandler, 租户无效, 禁用任务:{}", userInfo.getTenantId());
entity = RestScheduleTaskUtil.getInfo(entity.getId(), model.getUserInfo());
entity.setEnabledMark(0);
UpdateTaskModel updateTaskModel = new UpdateTaskModel();
updateTaskModel.setEntity(entity);
updateTaskModel.setUserInfo(model.getUserInfo());
RestScheduleTaskUtil.updateTask(updateTaskModel);
}
}
// 如果是http
if ("1".equals(entity.getExecuteType())) {
Boolean callHttp = callHttp(model, token);
// if (callHttp) {
// XxlJobHelper.handleResult(ReturnT.SUCCESS_CODE, "调用成功");
// } else {
// XxlJobHelper.handleResult(ReturnT.FAIL_CODE, "调用失败");
// }
} else if ("2".equals(entity.getExecuteType())) {
boolean callSql = callSql(model, token);
// if ( callSql) {
// XxlJobHelper.handleResult(ReturnT.SUCCESS_CODE, "调用成功");
// } else {
// XxlJobHelper.handleResult(ReturnT.FAIL_CODE, "调用失败");
// }
}
}
// ---------------START callSQL
/**
* 调用SQL
*
* @param model 系统调度参数
*/
private boolean callSql(ContentNewModel model, String token) {
try {
// 得到数据接口信息
String tenantId = StringUtil.isNotEmpty(model.getUserInfo().getTenantId()) ? model.getUserInfo().getTenantId() : "0";
DataInterfaceEntity entity = dataInterfaceService.getInfo(model.getInterfaceId());
if (entity != null) {
Map<String, String> map = null;
if (model.getParameter() != null && model.getParameter().size() > 0) {
map = new HashMap<>(16);
for (TemplateJsonModel parameterModel : model.getParameter()) {
String value = Objects.equals(parameterModel.getSourceType(), TemplateEnum.Empty.getCode()) ? "" : parameterModel.getRelationField();
map.put(parameterModel.getField(), value);
}
}
dataInterfaceService.infoToId(entity.getId(), tenantId, map, token, null, null, null, null);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// ---------------START callHttp
/**
* HTTP调用
*
* @param model 系统调度参数
* @return
*/
private Boolean callHttp(ContentNewModel model, String token) {
try {
// 得到数据接口信息
String tenantId = StringUtil.isNotEmpty(model.getUserInfo().getTenantId()) ? model.getUserInfo().getTenantId() : "0";
DataInterfaceEntity entity = dataInterfaceService.getInfo(model.getInterfaceId());
if (entity != null) {
Map<String, String> map = null;
if (model.getParameter() != null && model.getParameter().size() > 0) {
map = new HashMap<>(16);
for (TemplateJsonModel parameterModel : model.getParameter()) {
String value = Objects.equals(parameterModel.getSourceType(), TemplateEnum.Empty.getCode()) ? "" : parameterModel.getRelationField();
map.put(parameterModel.getField(), value);
}
}
dataInterfaceService.infoToId(entity.getId(), tenantId, map, token, null, null, null, null);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>yunzhupaas-scheduletask</artifactId>
<groupId>com.yunzhupaas</groupId>
<version>5.2.0-RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>yunzhupaas-scheduletask-controller</artifactId>
<dependencies>
<dependency>
<groupId>com.yunzhupaas</groupId>
<artifactId>yunzhupaas-scheduletask-biz</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,186 @@
package com.yunzhupaas.scheduletask.controller;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.base.Pagination;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.scheduletask.entity.HandlerNameEntity;
import com.yunzhupaas.scheduletask.entity.TimeTaskEntity;
import com.yunzhupaas.exception.DataException;
import com.yunzhupaas.scheduletask.model.*;
import com.yunzhupaas.scheduletask.rest.RestScheduleTaskUtil;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.JsonUtilEx;
import com.yunzhupaas.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.ArrayList;
import java.util.List;
/**
* 任务调度
*
* @author 云筑产品开发平台组
* @version V3.1.0
* @copyright 深圳市乐程软件有限公司
* @date 2023/09/27
*/
@Tag(name = "任务调度", description = "TimeTask")
@RestController
@RequestMapping("/api/scheduletask")
public class TimeTaskController {
/**
* 获取任务调度列表
*
* @param pagination
* @return
*/
@Operation(summary = "获取任务调度列表")
@GetMapping
public JSONObject list(Pagination pagination) {
UserInfo userInfo = UserProvider.getUser();
return RestScheduleTaskUtil.getList(pagination, userInfo);
}
/**
* 获取本地任务列表
*
* @return
*/
@Operation(summary = "获取任务调度列表")
@GetMapping("/TaskMethods")
public ActionResult<List<TaskMethodsVO>> taskMethods() {
List<TaskMethodsVO> list = new ArrayList<>(16);
// 获取所有handlerName
List<HandlerNameEntity> handlerNameEntities = RestScheduleTaskUtil.getHandlerList();
for (HandlerNameEntity entity : handlerNameEntities) {
TaskMethodsVO taskMethodsVO = new TaskMethodsVO();
taskMethodsVO.setId(entity.getId());
taskMethodsVO.setFullName(entity.getHandlerName());
list.add(taskMethodsVO);
}
return ActionResult.success(list);
}
/**
* 获取任务调度日志列表
*
* @param pagination
* @param taskId 任务Id
* @return
*/
@Operation(summary = "获取任务调度日志列表")
@GetMapping("/{id}/TaskLog")
public JSONObject list(@PathVariable("id") String taskId, TaskPage pagination) {
return RestScheduleTaskUtil.getLogList(taskId, UserProvider.getUser(), pagination);
}
/**
* 获取任务调度信息
*
* @param id 主键值
* @return
*/
@Operation(summary = "获取任务调度信息")
@GetMapping("/Info/{id}")
public ActionResult<TaskInfoVO> info(@PathVariable("id") String id) throws DataException {
TimeTaskEntity entity = RestScheduleTaskUtil.getInfo(id, UserProvider.getUser());
TaskInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, TaskInfoVO.class);
return ActionResult.success(vo);
}
/**
* 新建任务调度
*
* @param taskCrForm
* @return
*/
@Operation(summary = "新建任务调度")
@PostMapping
public ActionResult create(@RequestBody @Valid TaskCrForm taskCrForm) {
taskCrForm.setUserInfo(UserProvider.getUser());
JSONObject jsonObject = RestScheduleTaskUtil.create(taskCrForm);
return JsonUtil.getJsonToBean(jsonObject, ActionResult.class);
}
/**
* 修改任务调度
*
* @param id 主键值
* @param taskUpForm
* @return
*/
@Operation(summary = "修改任务调度")
@PutMapping("/{id}")
public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid TaskUpForm taskUpForm) {
taskUpForm.setUserInfo(UserProvider.getUser());
JSONObject jsonObject = RestScheduleTaskUtil.update(id, taskUpForm);
return JsonUtil.getJsonToBean(jsonObject, ActionResult.class);
}
/**
* 删除任务
*
* @param id 主键值
* @return
*/
@Operation(summary = "删除任务")
@DeleteMapping("/{id}")
public ActionResult delete(@PathVariable("id") String id) {
JSONObject jsonObject = RestScheduleTaskUtil.delete(id, UserProvider.getUser());
return JsonUtil.getJsonToBean(jsonObject, ActionResult.class);
}
/**
* 停止任务调度
*
* @param id 主键值
* @return
*/
@Operation(summary = "停止任务调度")
@PutMapping("/{id}/Actions/Stop")
public ActionResult stop(@PathVariable("id") String id) {
UpdateTaskModel updateTaskModel = new UpdateTaskModel();
TimeTaskEntity entity = RestScheduleTaskUtil.getInfo(id, UserProvider.getUser());
if (entity != null) {
entity.setEnabledMark(0);
entity.setRunCount(entity.getRunCount());
updateTaskModel.setEntity(entity);
updateTaskModel.setUserInfo(UserProvider.getUser());
RestScheduleTaskUtil.updateTask(updateTaskModel);
return ActionResult.success(MsgCode.SU005.get());
}
return ActionResult.fail(MsgCode.SC001.get());
}
/**
* 启动任务调度
*
* @param id 主键值
* @return
*/
@Operation(summary = "启动任务调度")
@PutMapping("/{id}/Actions/Enable")
public ActionResult enable(@PathVariable("id") String id) {
UpdateTaskModel updateTaskModel = new UpdateTaskModel();
TimeTaskEntity entity = RestScheduleTaskUtil.getInfo(id, UserProvider.getUser());
if (entity != null) {
entity.setEnabledMark(1);
updateTaskModel.setEntity(entity);
updateTaskModel.setUserInfo(UserProvider.getUser());
RestScheduleTaskUtil.updateTask(updateTaskModel);
return ActionResult.success(MsgCode.SU005.get());
}
return ActionResult.fail(MsgCode.SC001.get());
}
}