Commit 5a3e139e by 梁家彪

完善

parent b167f269
......@@ -43,8 +43,8 @@ public class DataCollectApiController {
@ApiOperation("执行接口汇聚")
@PostMapping(value = "/execute")
public ResultVo executeDb(@RequestParam("dataCollectSettingId") Integer dataCollectSettingId, @RequestBody DataCollectApiExecuteReq dataCollectApiExecuteReq) {
dataCollectApiService.execute(dataCollectSettingId, dataCollectApiExecuteReq);
public ResultVo executeDb(@RequestParam("dataCollectSettingId") Integer dataCollectSettingId, @RequestParam("collectType") Integer collectType, @RequestBody DataCollectApiExecuteReq dataCollectApiExecuteReq) {
dataCollectApiService.execute(dataCollectSettingId, collectType, dataCollectApiExecuteReq);
return ResultVo.success("汇聚执行成功");
}
}
\ No newline at end of file
......@@ -41,8 +41,8 @@ public class DataCollectDbController {
@ApiOperation("执行数据库汇聚")
@PostMapping(value = "/execute")
public ResultVo executeDb(@RequestParam("dataCollectSettingId") Integer dataCollectSettingId) {
dataCollectDbService.execute(dataCollectSettingId);
public ResultVo executeDb(@RequestParam("dataCollectSettingId") Integer dataCollectSettingId, @RequestParam("collectType") Integer collectType) {
dataCollectDbService.execute(dataCollectSettingId, collectType);
return ResultVo.success("汇聚执行成功");
}
}
......@@ -48,8 +48,8 @@ public class DataCollectFileController {
@ApiOperation("执行文件汇聚")
@PostMapping(value = "/execute")
public ResultVo executeDb(@RequestParam("dataCollectSettingId") Integer dataCollectSettingId, MultipartFile file) {
dataCollectFileService.execute(dataCollectSettingId, file);
public ResultVo executeDb(@RequestParam("dataCollectSettingId") Integer dataCollectSettingId, @RequestParam("collectType") Integer collectType, MultipartFile file) {
dataCollectFileService.execute(dataCollectSettingId, collectType, file);
return ResultVo.success("汇聚执行成功");
}
}
\ No newline at end of file
......@@ -33,6 +33,12 @@ public class DataCollectLogController {
return ResultVo.success(dataCollectLogService.getList(req));
}
@ApiOperation("分页获取指定汇聚的日志")
@PostMapping(value = "/list/{id}")
public ResultVo getListById(@RequestBody DataCollectLogReq req, @PathVariable Integer id) {
return ResultVo.success(dataCollectLogService.getList(req, id));
}
@ApiOperation("删除数据汇聚日志")
@DeleteMapping(value = "/delete/{id}")
public ResultVo delete(@PathVariable Integer id) {
......
......@@ -53,5 +53,5 @@ public class DataCollectSettingFile {
* updateTime
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;;
private Date updateTime;
}
\ No newline at end of file
......@@ -38,7 +38,7 @@ public class DatabaseRunner implements ApplicationRunner {
switch (ds.getDbType()) {
case 1:
className = "com.mysql.cj.jdbc.Driver";
url = "jdbc:mysql://" + ds.getDbIp() + ":" + ds.getDbPort() + "/" + ds.getDbName() + "?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true";
url = "jdbc:mysql://" + ds.getDbIp() + ":" + ds.getDbPort() + "/" + ds.getDbName() + "?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&autoReconnect=true";
break;
case 2:
className = "oracle.jdbc.driver.OracleDriver";
......
......@@ -12,5 +12,5 @@ public interface DataCollectApiService {
void update(DataCollectApiAddReq dataCollectApiAddReq);
void execute(Integer dataCollectSettingId, DataCollectApiExecuteReq dataCollectApiExecuteReq);
void execute(Integer dataCollectSettingId, Integer collectType, DataCollectApiExecuteReq dataCollectApiExecuteReq);
}
\ No newline at end of file
......@@ -11,5 +11,5 @@ public interface DataCollectDbService {
void update(DataCollectDbAddReq dataCollectDbAddReq);
void execute(Integer dataCollectSettingId);
void execute(Integer dataCollectSettingId, Integer collectType);
}
......@@ -17,5 +17,5 @@ public interface DataCollectFileService {
Map<String, Object> analysisExcel(MultipartFile multipartFile);
void execute(Integer dataCollectSettingId, MultipartFile file);
void execute(Integer dataCollectSettingId, Integer collectType, MultipartFile file);
}
\ No newline at end of file
......@@ -12,6 +12,8 @@ public interface DataCollectLogService {
PageVo getList(DataCollectLogReq req);
PageVo getList(DataCollectLogReq req, Integer id);
List<DataCollectFileLog> file(Integer id);
boolean removeById(Integer id);
......
......@@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
......@@ -86,7 +87,7 @@ public class DataCollectApiServiceImpl extends ServiceImpl<DataCollectSettingApi
@Override
@Transactional
public void execute(Integer dataCollectSettingId, DataCollectApiExecuteReq dataCollectApiExecuteReq) {
public void execute(Integer dataCollectSettingId, Integer collectType, DataCollectApiExecuteReq dataCollectApiExecuteReq) {
long start = System.currentTimeMillis();
DataCollectSettingApi settingApi = lambdaQuery().eq(DataCollectSettingApi::getDataCollectSettingId, dataCollectSettingId).one();
List<DataCollectSettingApiPara> apiParas;
......@@ -106,12 +107,12 @@ public class DataCollectApiServiceImpl extends ServiceImpl<DataCollectSettingApi
throw new BusinessException("不支持的请求类型");
}
if(StrUtil.isNotBlank(dataCollectApiExecuteReq.getHeaders())){
if(StrUtil.isNotBlank(dataCollectApiExecuteReq.getHeaders()) && JSONUtil.isJsonObj(dataCollectApiExecuteReq.getHeaders())){
for (Map.Entry<String, Object> entry : JSONObject.parseObject(dataCollectApiExecuteReq.getHeaders()).entrySet()) {
request.header(entry.getKey(), entry.getValue().toString());
}
}
else if(StrUtil.isNotBlank(settingApi.getHeaders())){
else if(StrUtil.isNotBlank(settingApi.getHeaders()) && JSONUtil.isJsonObj(settingApi.getHeaders())){
for (Map.Entry<String, Object> entry : JSONObject.parseObject(settingApi.getHeaders()).entrySet()) {
request.header(entry.getKey(), entry.getValue().toString());
}
......@@ -130,6 +131,9 @@ public class DataCollectApiServiceImpl extends ServiceImpl<DataCollectSettingApi
JdbcTemplate toDbJdbcTemplate = DatabaseHolder.getJdbcTemplate(dataCollectSetting.getToDbId());
Set<String> set = list.get(0).keySet();
set.remove("id");
if(collectType == 0){
toDbJdbcTemplate.execute("TRUNCATE TABLE " + dataCollectSetting.getToTable() + ";");
}
if(dataCollectSetting.getCreateTable()==1 && toDbJdbcTemplate.queryForList("SHOW TABLES LIKE '"+ dataCollectSetting.getToTable() + "'").size()==0){
toDbJdbcTemplate.execute(SqlUtil.joinCreateTableSql(dataCollectSetting.getToTable(), set));
}
......
......@@ -60,7 +60,7 @@ public class DataCollectDbServiceImpl extends ServiceImpl<DataCollectSettingDbMa
@Override
@Transactional
public void execute(Integer dataCollectSettingId) {
public void execute(Integer dataCollectSettingId, Integer collectType) {
long start = System.currentTimeMillis();
DataCollectSetting dataCollectSetting = dataCollectMapper.selectById(dataCollectSettingId);
DataCollectSettingDb settingDb = getOne(new LambdaQueryWrapper<DataCollectSettingDb>().eq(DataCollectSettingDb::getDataCollectSettingId, dataCollectSettingId));
......@@ -69,6 +69,9 @@ public class DataCollectDbServiceImpl extends ServiceImpl<DataCollectSettingDbMa
List<Map<String, Object>> list = fromDbJdbcTemplate.queryForList("SELECT * FROM " + settingDb.getFromTable());
Set<String> set = list.get(0).keySet();
set.remove("id");
if(collectType == 0){
toDbJdbcTemplate.execute("TRUNCATE TABLE " + dataCollectSetting.getToTable() + ";");
}
if(dataCollectSetting.getCreateTable()==1 && toDbJdbcTemplate.queryForList("SHOW TABLES LIKE '"+ dataCollectSetting.getToTable() + "'").size()==0){
toDbJdbcTemplate.execute(SqlUtil.joinCreateTableSql(dataCollectSetting.getToTable(), set));
}
......
......@@ -67,7 +67,8 @@ public class DataCollectFileServiceImpl extends ServiceImpl<DataCollectSettingFi
String suffix = FileUtil.extName(file.getOriginalFilename());
AssertUtils.isTrue(suffix.equals("xls") || suffix.equals("xlsx") || suffix.equals("et"), "不支持的文件类型");
try {
return EasyExcelUtil.preview100(file.getInputStream());
Map<String, Object> map = EasyExcelUtil.preview100(file.getInputStream());
return map;
} catch (Exception e) {
throw new BusinessException("文件读取失败,请检查后重试");
}
......@@ -75,7 +76,7 @@ public class DataCollectFileServiceImpl extends ServiceImpl<DataCollectSettingFi
@Override
@Transactional
public void execute(Integer dataCollectSettingId, MultipartFile file) {
public void execute(Integer dataCollectSettingId, Integer collectType, MultipartFile file) {
long start = System.currentTimeMillis();
String suffix = FileUtil.extName(file.getOriginalFilename());
AssertUtils.isTrue(suffix.equals("xls") || suffix.equals("xlsx") || suffix.equals("et"), "不支持的文件类型");
......@@ -85,6 +86,9 @@ public class DataCollectFileServiceImpl extends ServiceImpl<DataCollectSettingFi
JdbcTemplate toDbJdbcTemplate = DatabaseHolder.getJdbcTemplate(dataCollectSetting.getToDbId());
Set<String> set = list.get(0).keySet();
set.remove("id");
if(collectType == 0){
toDbJdbcTemplate.execute("TRUNCATE TABLE " + dataCollectSetting.getToTable() + ";");
}
if(dataCollectSetting.getCreateTable()==1 && toDbJdbcTemplate.queryForList("SHOW TABLES LIKE '"+ dataCollectSetting.getToTable() + "'").size()==0){
toDbJdbcTemplate.execute(SqlUtil.joinCreateTableSql(dataCollectSetting.getToTable(), set));
}
......
......@@ -52,6 +52,27 @@ public class DataCollectLogServiceImpl extends ServiceImpl<DataCollectTaskLogMap
}
@Override
public PageVo getList(DataCollectLogReq req, Integer id) {
LambdaQueryWrapper<DataCollectTaskLog> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(DataCollectTaskLog::getDataCollectSettingId, id);
PageVo pageVo = PagingUtils.paging(req, dataCollectTaskLogMapper, wrapper,DataCollectTaskLog.class);
List<DataCollectTaskLogResp> taskLogRespList = new ArrayList<>(pageVo.getRows().size());
for (Object row : pageVo.getRows()) {
DataCollectTaskLog dataCollectTaskLog = (DataCollectTaskLog) row;
DataCollectSetting dataCollectSetting = dataCollectServiceImpl.getById(dataCollectTaskLog.getDataCollectSettingId());
if(null == dataCollectSetting){
continue;
}
DataCollectTaskLogResp resp = new DataCollectTaskLogResp();
resp.setTaskName(dataCollectSetting.getTaskName());
BeanUtil.copyProperties(dataCollectTaskLog, resp);
taskLogRespList.add(resp);
}
pageVo.setRows(taskLogRespList);
return pageVo;
}
@Override
public List<DataCollectFileLog> file(Integer id){
return dataCollectFileLogMapper.selectList(new LambdaQueryWrapper<DataCollectFileLog>().eq(DataCollectFileLog::getDataCollectSettingFileId, id));
}
......
......@@ -43,6 +43,9 @@ public class DataCollectServiceImpl extends ServiceImpl<DataCollectMapper, DataC
@Resource
private DataCollectSettingApiParaMapper dataCollectSettingApiParaMapper;
@Resource
private DataCollectTaskLogMapper dataCollectTaskLogMapper;
@Override
public PageVo<DataCollectSetting> list(DataCollectReq req) {
return PagingUtils.paging(req, dataCollectMapper, new LambdaQueryWrapper<DataCollectSetting>().orderByDesc(DataCollectSetting::getId), DataCollectSetting.class);
......@@ -64,26 +67,29 @@ public class DataCollectServiceImpl extends ServiceImpl<DataCollectMapper, DataC
@Override
@Transactional
public void delete(Integer id){
public void delete(Integer id) {
DataCollectSetting dataCollectSetting = getById(id);
UpdateWrapper wrapper = new UpdateWrapper();
wrapper.eq("data_collect_setting_id", dataCollectSetting.getId());
getDao(dataCollectSetting.getDataType()).delete(wrapper);
if(dataCollectSetting.getDataType()==3){
if (dataCollectSetting.getDataType() == 2) {
dataCollectSettingFileMapper.delete(wrapper);
} else if (dataCollectSetting.getDataType() == 3) {
dataCollectSettingApiParaMapper.delete(wrapper);
}
dataCollectTaskLogMapper.delete(wrapper);
AssertUtils.isTrue(removeById(id), "删除失败");
}
@Override
@Transactional
public void batchDelete(List<Integer> ids){
public void batchDelete(List<Integer> ids) {
for (Integer id : ids) {
delete(id);
}
}
public BaseMapper getDao(int dataType){
public BaseMapper getDao(int dataType) {
switch (dataType) {
case 1:
return dataCollectSettingDbMapper;
......
......@@ -7,7 +7,7 @@ public class SqlUtil {
public static String jointInsertSql(String tableName, Set<String> cloums){
StringBuilder fileds = new StringBuilder();
for (String cloum : cloums) {
fileds.append(cloum.replaceAll("\\p{Punct}", "")).append(",");
fileds.append(cloum.replaceAll("\\p{Punct}", "").replaceAll("(", "").replaceAll(")", "").replaceAll("\r", "").replaceAll("\n", "")).append(",");
}
fileds.deleteCharAt(fileds.length()-1);
StringBuilder sql = new StringBuilder("INSERT INTO ").append(tableName).append("(").append(fileds).append(") VALUES (");
......@@ -25,7 +25,7 @@ public class SqlUtil {
public static String joinCreateTableSql(String tableName, Set<String> cloums){
StringBuilder sql = new StringBuilder("CREATE TABLE ").append(tableName).append("(");
for (String cloum : cloums) {
sql.append(cloum.replaceAll("\\p{Punct}", "")).append(" VARCHAR(255),");
sql.append(cloum.replaceAll("\\p{Punct}", "").replaceAll("(", "").replaceAll(")", "").replaceAll("\r", "").replaceAll("\n", "")).append(" TEXT,");
}
sql.deleteCharAt(sql.length()-1).append(")");
return sql.toString();
......
......@@ -22,7 +22,7 @@ public class DatabaseHolder {
switch (dbType) {
case 1:
className = "com.mysql.cj.jdbc.Driver";
url = "jdbc:mysql://" + dbIp + ":" + dbPort + "/" + dbName + "?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true";
url = "jdbc:mysql://" + dbIp + ":" + dbPort + "/" + dbName + "?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&autoReconnect=true";
break;
case 2:
className = "oracle.jdbc.driver.OracleDriver";
......
......@@ -6,6 +6,7 @@ import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zq.common.context.ThreadContext;
import com.zq.common.exception.BusinessException;
import com.zq.common.utils.AssertUtils;;
import com.zq.common.vo.PageVo;
import com.zq.common.vo.ResultVo;
......@@ -49,6 +50,7 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
}
public ResultVo addDatasource(QueryDb queryDb) {
queryDb.setCreateTime(DateUtil.date());
AssertUtils.isTrue(queryDbDao.insert(queryDb) == 1, "添加失败,请检查后重试");
DatabaseHolder.add(queryDb.getDbType(), queryDb.getDbIp(), queryDb.getDbPort(), queryDb.getDbName(), queryDb.getUsername(), queryDb.getPassword(), queryDb.getId());
return ResultVo.success();
......@@ -93,14 +95,22 @@ public class CommonQueryService extends ServiceImpl<CommonQuerySettingDao, Commo
}
public Object checkConnect(QueryDb queryDb) {
QueryDb queryDb1 = queryDbDao.selectById(queryDb.getId());
String jdbcUrl = queryDb1.getDbIp();
try {
return SqlUtils.testConnection(jdbcUrl, queryDb1.getUsername(), queryDb1.getPassword());
} catch (Exception e) {
log.error(e.getMessage());
return false;
}
String url = null;
switch (queryDb.getDbType()) {
case 1:
url = "jdbc:mysql://" + queryDb.getDbIp() + ":" + queryDb.getDbPort() + "/" + queryDb.getDbName() + "?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&autoReconnect=true";
break;
case 2:
url = "jdbc:oracle:thin:@//" + queryDb.getDbIp() + ":" + queryDb.getDbPort() + "/" + queryDb.getDbName();
break;
case 3:
url = "jdbc:sqlserver://" + queryDb.getDbIp() + ":" + queryDb.getDbPort() + "/" + queryDb.getDbName();
break;
default:
throw new BusinessException("不支持的数据库");
}
AssertUtils.isTrue(SqlUtils.testConnection(url, queryDb.getUsername(), queryDb.getPassword()), "连接失败");
return true;
}
public List<Map<String, Object>> runSelect(Map<String, Object> body) throws Exception {
......
package com.zq.spiderflow.datacollision.executor;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zq.common.exception.BusinessException;
import com.zq.spiderflow.context.SpiderContext;
import com.zq.spiderflow.executor.ShapeExecutor;
......@@ -10,17 +14,13 @@ import com.zq.spiderflow.model.SpiderNode;
import com.zq.spiderflow.model.SpiderOutput;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.*;
@Component
public class DataCollisionExecutor implements ShapeExecutor {
public static final String TABLE1 = "table1";
public static final String FIELD1 = "field1";
public static final String TABLE2 = "table2";
public static final String FIELD2 = "field2";
public static final String COLLISION = "collision";
public static final String RESERVE = "reserve";
@Override
......@@ -40,52 +40,59 @@ public class DataCollisionExecutor implements ShapeExecutor {
@Override
public void execute(SpiderNode node, SpiderContext context, Map<String, Object> variables) throws Exception {
String table1 = null;
String table2 = null;
String field1 = null;
String field2 = null;
try {
table1 = node.getStringJsonValue(TABLE1);
table2 = node.getStringJsonValue(TABLE2);
field1 = node.getStringJsonValue(FIELD1);
field2 = node.getStringJsonValue(FIELD2);
}catch (Exception e){
throw new BusinessException("不是字符串");
}
JSONArray collisionArray = JSONUtil.parseArray(node.getStringJsonValue(COLLISION));
LinkedHashSet<Map<String, Object>> results = new LinkedHashSet<>();
if(StrUtil.isBlank(table1)){
throw new BusinessException("没有表1");
}
if(StrUtil.isBlank(table2)){
throw new BusinessException("没有表2");
}
if(StrUtil.isBlank(field1)){
throw new BusinessException("没有字段1");
}
if(StrUtil.isBlank(field2)){
throw new BusinessException("没有字段2");
}
for (Object o : collisionArray) {
JSONObject jsonObject = JSONUtil.parseObj(o);
String table1 = jsonObject.getStr("table1");
String field1 = jsonObject.getStr("field1");
String condition = jsonObject.getStr("condition");
String table2 = jsonObject.getStr("table2");
String field2 = jsonObject.getStr("field2");
String logic = jsonObject.getStr("logic", "2");
List<Map<String, Object>> list1 = (List<Map<String, Object>>) variables.get(table1);
List<Map<String, Object>> list2 = (List<Map<String, Object>>) variables.get(table2);
Set<Map<String, Object>> temp = new HashSet<>();
if(list1==null){
throw new BusinessException("表1没有数据");
}
if(list2==null){
throw new BusinessException("表2没有数据");
}
List<Map<String, Object>> results = new ArrayList<>();
for (Map<String, Object> map1 : list1) {
String field1Str = map1.get(field1).toString();
for (Map<String, Object> map2 : list2) {
String field2Str = map2.get(field2).toString();
switch (condition) {
case "1":
if(field1Str.equals(field2Str)){
map1.putAll(map2);
results.add(map1);
temp.add(map1);
}
break;
case "2":
if(!field1Str.equals(field2Str)){
map1.putAll(map2);
temp.add(map1);
}
break;
case "3":
if(field1Str.contains(field2Str)){
map1.putAll(map2);
temp.add(map1);
}
break;
case "4":
if(!field1Str.contains(field2Str)){
map1.putAll(map2);
temp.add(map1);
}
break;
}
}
}
if(logic.equals("1")){
results.retainAll(temp);
}else {
results.addAll(temp);
}
}
variables.put(node.getNodeId() + "_" + node.getNodeName(), results);
......
......@@ -86,6 +86,10 @@ public class SpiderNode {
return value;
}
public Object getObjectJsonValue(String key){
return this.jsonProperty.get(key);
}
public String getStringJsonValue(String key,String defaultValue){
String value = getStringJsonValue(key);
return StringUtils.isNotBlank(value) ? value : defaultValue;
......
......@@ -38,14 +38,14 @@ public class WebSocketEditorServer {
context.setDebug(isDebug);
context.setRunning(true);
String flowId = event.getString("flowId");
if(StrUtil.isBlank(flowId)){
if (StrUtil.isBlank(flowId)) {
context.write(new WebSocketEvent<>("error", "flowId不能为空!"));
}
context.setFlowId(flowId);
// new Thread(() -> {
String xml = event.getString("message");
if (xml != null) {
spider. runWithTest(SpiderFlowUtils.loadXMLFromString(xml), context);
spider.runWithTest(SpiderFlowUtils.loadXMLFromString(xml), context);
context.write(new WebSocketEvent<>("finish", null));
} else {
context.write(new WebSocketEvent<>("error", "xml不正确!"));
......@@ -55,7 +55,7 @@ public class WebSocketEditorServer {
} else if ("stop".equals(eventType) && context != null) {
context.setRunning(false);
context.stop();
} else if("resume".equalsIgnoreCase(eventType) && context != null){
} else if ("resume".equalsIgnoreCase(eventType) && context != null) {
context.resume();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment