sql拼接

master
陈思豪 2024-09-04 22:32:49 +08:00
parent e363701cd6
commit 34c07a4d85
13 changed files with 205 additions and 22 deletions

View File

@ -17,4 +17,11 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencies>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-core</artifactId>
</dependency>
</dependencies>
</project> </project>

View File

@ -1,7 +0,0 @@
package com.muyu;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@ -0,0 +1,22 @@
package com.muyu.task.feign;
import com.muyu.common.core.domain.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @PackageName:com.muyu.task.feign
* @ClassName:dataValue
* @Description:
* @author:
* @date: 2024/9/4 15:13
*/
//,fallback = TaskFeignFallback.class
@FeignClient(name = "cloud-source")
public interface DataValueFeign {
@PostMapping("/DataValue/findTableValue")
public Result findTableValue(@RequestParam("basicId") Long basicId,@RequestParam("sql") String sql);
}

View File

@ -72,6 +72,12 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-task-remote</artifactId>
<version>1.0.0</version>
</dependency>
<!-- XllJob定时任务 --> <!-- XllJob定时任务 -->

View File

@ -2,8 +2,10 @@ package com.muyu;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication @SpringBootApplication
@EnableFeignClients
public class TaskApplication { public class TaskApplication {
public static void main (String[] args) { public static void main (String[] args) {
SpringApplication.run(TaskApplication.class, args); SpringApplication.run(TaskApplication.class, args);

View File

@ -72,5 +72,16 @@ public class TaskController {
); );
} }
/**
*
*/
@RequestMapping(path = "/executeTask/{taskId}", method = RequestMethod.POST)
@Operation(summary = "对指定任务进行执行", description = "根据传来的id进行执行")
public Result<String> executeTask(@PathVariable(name = "taskId") Integer taskId) {
return Result.success(
taskService.executeTask(taskId), "执行成功"
);
}
} }

View File

@ -1,7 +1,9 @@
package com.muyu.controller; package com.muyu.controller;
import com.muyu.common.core.domain.Result;
import com.muyu.domain.TaskOutputInfo; import com.muyu.domain.TaskOutputInfo;
import com.muyu.mapper.TaskOutputMapper; import com.muyu.mapper.TaskOutputMapper;
import com.muyu.service.TaskOutputService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -16,15 +18,16 @@ import org.springframework.web.bind.annotation.RestController;
*/ */
@RestController @RestController
@RequestMapping(value = "/taskOutPut") @RequestMapping(value = "/taskOutPut")
public class TaskOutPutController { public class TaskOutputController {
@Autowired @Autowired
private TaskOutputMapper taskOutPutMapper; private TaskOutputService taskOutputService;
//添加数据输出方法 //添加数据输出方法
@RequestMapping(value = "addTaskOutPut") @RequestMapping(value = "addTaskOutput")
public void addTaskOutPut(@RequestBody TaskOutputInfo taskOutputInfo){ public Result<String> addTaskOutPut(@RequestBody TaskOutputInfo taskOutputInfo){
int insert = taskOutPutMapper.insert(taskOutputInfo); return Result.success(null,
taskOutputService.addTaskOutput(taskOutputInfo));
} }

View File

@ -0,0 +1,9 @@
package com.muyu.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.domain.TaskOutputInfo;
public interface TaskOutputService extends IService<TaskOutputInfo> {
String addTaskOutput(TaskOutputInfo taskOutputInfo);
}

View File

@ -21,4 +21,6 @@ public interface TaskService extends IService<TaskInfo> {
String updById(TaskInfo taskInfo); String updById(TaskInfo taskInfo);
String deleteById(Integer id); String deleteById(Integer id);
String executeTask(Integer taskId);
} }

View File

@ -48,15 +48,6 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableInfo
tableInfoCasRespDad.setChildren(tableInfoCasRespSon); tableInfoCasRespDad.setChildren(tableInfoCasRespSon);
tableInfo.setTableInfoList(list1); tableInfo.setTableInfoList(list1);
tableInfoCasResp.add(tableInfoCasRespDad); tableInfoCasResp.add(tableInfoCasRespDad);
// LambdaQueryWrapper<TableInfo> queryWrapper = new LambdaQueryWrapper<>();
// queryWrapper.eq(TableInfo::getParentId,0);
// List<TableInfo> list = this.list(queryWrapper);
// list.forEach(tableInfo -> {
// LambdaQueryWrapper<TableInfo> queryWrapperC = new LambdaQueryWrapper<>();
// queryWrapperC.eq(TableInfo::getParentId,tableInfo.getId());
// List<TableInfo> list1 = this.list(queryWrapperC);
// tableInfo.setTableInfoList(list1);
// });
}); });
return tableInfoCasResp; return tableInfoCasResp;

View File

@ -0,0 +1,32 @@
package com.muyu.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.domain.TaskOutputInfo;
import com.muyu.mapper.TaskOutputMapper;
import com.muyu.service.TaskOutputService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @PackageName:com.muyu.service.impl
* @ClassName:TaskOutputServiceImpl
* @Description:
* @author:
* @date: 2024/9/4 11:35
*/
@Service
public class TaskOutputServiceImpl extends ServiceImpl<TaskOutputMapper, TaskOutputInfo> implements TaskOutputService {
@Autowired
private TaskOutputMapper taskOutputMapper;
@Override
public String addTaskOutput(TaskOutputInfo taskOutputInfo) {
int insert = taskOutputMapper.insert(taskOutputInfo);
if(insert<=0){
return "添加失败";
}
return "添加成功";
}
}

View File

@ -2,13 +2,24 @@ package com.muyu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.StringUtils; import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.TaskInfo; import com.muyu.domain.TaskInfo;
import com.muyu.domain.TaskInputInfo;
import com.muyu.domain.TaskJoinInfo;
import com.muyu.domain.TaskOutputInfo;
import com.muyu.domain.req.TaskInfoReq; import com.muyu.domain.req.TaskInfoReq;
import com.muyu.mapper.TaskMapper; import com.muyu.mapper.TaskMapper;
import com.muyu.service.TaskInputService;
import com.muyu.service.TaskJoinService;
import com.muyu.service.TaskOutputService;
import com.muyu.service.TaskService; import com.muyu.service.TaskService;
import com.muyu.task.feign.DataValueFeign;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List; import java.util.List;
/** /**
@ -19,9 +30,23 @@ import java.util.List;
* @date: 2024/8/22 17:15 * @date: 2024/8/22 17:15
*/ */
@Service @Service
@Log4j2
public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskInfo> implements TaskService { public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskInfo> implements TaskService {
@Autowired
private TaskInputService taskInputService;
@Autowired
private TaskMapper taskMapper;
@Autowired
private TaskJoinService taskJoinService;
@Autowired
private TaskOutputService taskOutputService;
@Autowired
private DataValueFeign dataValue;
@Override @Override
public List<TaskInfo> selectList(TaskInfoReq taskInfoReq) { public List<TaskInfo> selectList(TaskInfoReq taskInfoReq) {
@ -70,4 +95,77 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, TaskInfo> implement
return "success"; return "success";
} }
@Override
public String executeTask(Integer taskId) {
LambdaQueryWrapper<TaskInputInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(TaskInputInfo::getTaskId, taskId);
List<TaskInputInfo> taskInputList = taskInputService.list(queryWrapper);
if(taskInputList.isEmpty()){
return "表节点没有进行相对应的选择,无法继续执行";
}
LambdaQueryWrapper<TaskOutputInfo> outputInfo = new LambdaQueryWrapper<>();
outputInfo.eq(TaskOutputInfo::getTaskId, taskId);
TaskOutputInfo serviceOne = taskOutputService.getOne(outputInfo);
String[] outPutFileName = serviceOne.getLastFileName().split(",");
HashMap<String, String> fieldAsNameMap = new HashMap<>();
String sql = "";
for (int i = 0; i < taskInputList.size(); i++) {
String[] tableFieldList = taskInputList.get(i).getTableField().split(",");
String[] tableAsFieldList = taskInputList.get(i).getTableAsField().split(",");
for (int j = 0; j < tableAsFieldList.length; j++) {
for (int o = 0; o < outPutFileName.length; o++) {
if(tableAsFieldList[j].equals(outPutFileName[o])){
sql += ","+taskInputList.get(i).getTableAsName() + "." + tableFieldList[j] +" " +
tableAsFieldList[j] + " ";
}
}
fieldAsNameMap.put(tableAsFieldList[j], tableFieldList[j]);
// 规则
// tableNameMap.put(tableFieldList[j], tableAsFieId[j]);
}
}
sql = sql.substring(1);
sql = "select " + sql + " from ";
LambdaQueryWrapper<TaskJoinInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(TaskJoinInfo::getTaskId, taskId);
List<TaskJoinInfo> taskJoinInfos = taskJoinService.list(lambdaQueryWrapper);
if(!taskJoinInfos.isEmpty()){
for (TaskJoinInfo taskJoinInfo : taskJoinInfos) {
//拿到左表id
String leftId = taskJoinInfo.getLeftId();
TaskInputInfo leftInput = taskInputService.findByNodeId(leftId);
//拿到右表id
String rightId = taskJoinInfo.getRightId();
TaskInputInfo rightInput = taskInputService.findByNodeId(rightId);
sql += leftInput.getTableName() + " " + leftInput.getTableAsName()+" " +
taskJoinInfo.getJoinType() + " " + rightInput.getTableName() + " " +
rightInput.getTableAsName() + " on " + leftInput.getTableAsName() + "." +
fieldAsNameMap.get(taskJoinInfo.getLeftJoinField()) + "=" + rightInput.getTableAsName() + "." +
fieldAsNameMap.get(taskJoinInfo.getRightJoinField()) + " ";
}
}else{
LambdaQueryWrapper<TaskInputInfo> selectOne = new LambdaQueryWrapper<>();
queryWrapper.eq(TaskInputInfo::getTaskId, taskId);
TaskInputInfo taskInputInfo = taskInputService.getOne(selectOne);
sql += taskInputInfo.getTableName() + " " + taskInputInfo.getTableAsName();
}
System.out.println(sql);
System.out.println(sql);
System.out.println(sql);
System.out.println(sql);
System.out.println(sql);
System.out.println(sql);
log.info(sql);
Result tableValue = dataValue.findTableValue(Long.valueOf(serviceOne.getBasicId()), sql);
log.info(tableValue);
Object data = tableValue.getData();
System.out.println(data);
return "success";
}
} }

View File

@ -11,7 +11,8 @@ nacos:
# text cloud-2112 # text cloud-2112
# Spring # Spring
spring: spring:
main:
allow-bean-definition-overriding: true
application: application:
# 应用名称 # 应用名称
name: cloud-task name: cloud-task
@ -19,6 +20,12 @@ spring:
# 环境配置 # 环境配置
active: dev active: dev
cloud: cloud:
openfeign:
client:
config:
default:
connectTimeout: 50000 # 连接超时时间,单位为毫秒
readTimeout: 5000 # 读取超时时间,单位为毫秒
nacos: nacos:
discovery: discovery:
# 服务注册地址 # 服务注册地址