fix:优化资产结构接口

master
Saisai Liu 2024-04-30 08:31:34 +08:00
parent 8a1699ddfd
commit 9b76b6fae4
9 changed files with 166 additions and 174 deletions

View File

@ -45,50 +45,4 @@ public class AssetImpower extends BaseEntity {
@Excel(name = "用户id") @Excel(name = "用户id")
private Long userId; private Long userId;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setTableId(Long tableId) {
this.tableId = tableId;
}
public Long getTableId() {
return tableId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
public Long getTypeId() {
return typeId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getUserId() {
return userId;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("tableId", getTableId())
.append("typeId", getTypeId())
.append("userId", getUserId())
.append("remark", getRemark())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.toString();
}
} }

View File

@ -0,0 +1,49 @@
package com.muyu.etl.domain.req;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.muyu.common.core.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @ClassName AssetImpowerReq
* @Description
* @Author SaiSai.Liu
* @Date 2024/4/29 14:04
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public class AssetImpowerReq {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId(value = "id",type = IdType.AUTO)
private Long id;
/**
* id
*/
@Excel(name = "表id")
private Long tableId;
/**
* id
*/
@Excel(name = "部门id")
private Long typeId;
/**
* id
*/
@Excel(name = "用户id")
private Long userId;
}

View File

@ -12,11 +12,9 @@ import com.muyu.etl.domain.BasicConfigInfo;
import com.muyu.etl.domain.DictInfo; import com.muyu.etl.domain.DictInfo;
import com.muyu.etl.domain.Structure; import com.muyu.etl.domain.Structure;
import com.muyu.etl.domain.resp.BasicDictResp; import com.muyu.etl.domain.resp.BasicDictResp;
import com.muyu.etl.domain.resp.TableInfoStructureResp;
import com.muyu.etl.domain.resp.TableTreeResp; import com.muyu.etl.domain.resp.TableTreeResp;
import com.muyu.etl.service.AssetDataDictService; import com.muyu.etl.service.*;
import com.muyu.etl.service.BasicConfigInfoService;
import com.muyu.etl.service.DictInfoService;
import com.muyu.etl.service.StructureService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -38,6 +36,8 @@ public class BasicConfigInfoController extends BaseController {
@Autowired @Autowired
private AssetDataDictService assetDataDictService; private AssetDataDictService assetDataDictService;
@Autowired @Autowired
private TableInfoService tableInfoService;
@Autowired
private DictInfoService dictInfoService; private DictInfoService dictInfoService;
@Autowired @Autowired
private StructureService structureService; private StructureService structureService;
@ -149,6 +149,19 @@ public class BasicConfigInfoController extends BaseController {
return Result.success(assetDataDictService.getDict(basicId)); return Result.success(assetDataDictService.getDict(basicId));
} }
/**
* tableId
*
* @return
*/
@Log(title = "描述")
@GetMapping("/getTableInfo/{tableId}")
public Result<TableInfoStructureResp> getTableInfo(@PathVariable Long tableId) {
return Result.success(basicConfigInfoService.getTableInfo(tableId));
}
/** /**
* *
* *

View File

@ -5,6 +5,7 @@ import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.etl.domain.AssetDataDict; import com.muyu.etl.domain.AssetDataDict;
import com.muyu.etl.domain.resp.BasicDictResp; import com.muyu.etl.domain.resp.BasicDictResp;
import com.muyu.etl.domain.resp.TableInfoStructureResp;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -65,4 +66,5 @@ public interface AssetDataDictService extends IService<AssetDataDict>
public int deleteAssetDataDictById(Long id); public int deleteAssetDataDictById(Long id);
List<BasicDictResp> getDict(Long basicId); List<BasicDictResp> getDict(Long basicId);
} }

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.common.core.domain.Result; import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.page.TableDataInfo; import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.etl.domain.BasicConfigInfo; import com.muyu.etl.domain.BasicConfigInfo;
import com.muyu.etl.domain.resp.TableInfoStructureResp;
import com.muyu.etl.domain.resp.TableTreeResp; import com.muyu.etl.domain.resp.TableTreeResp;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -70,4 +71,6 @@ public interface BasicConfigInfoService extends IService<BasicConfigInfo>
// List getDataByEtl(); // List getDataByEtl();
List<TableTreeResp> getTableTree(); List<TableTreeResp> getTableTree();
TableInfoStructureResp getTableInfo(Long tableId);
} }

View File

@ -4,6 +4,7 @@ import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.etl.domain.TableInfo; import com.muyu.etl.domain.TableInfo;
import com.muyu.etl.domain.resp.TableInfoStructureResp;
/** /**
* Service * Service

View File

@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.DateUtils; import com.muyu.common.core.utils.DateUtils;
import com.muyu.etl.domain.DictInfo; import com.muyu.etl.domain.DictInfo;
import com.muyu.etl.domain.resp.BasicDictResp; import com.muyu.etl.domain.resp.BasicDictResp;
import com.muyu.etl.domain.resp.TableInfoStructureResp;
import com.muyu.etl.service.DictInfoService; import com.muyu.etl.service.DictInfoService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -133,4 +134,6 @@ public class AssetDataDictServiceImpl extends ServiceImpl<AssetDataDictMapper, A
}).collect(Collectors.toList()); }).collect(Collectors.toList());
return collect; return collect;
} }
} }

View File

@ -114,6 +114,12 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
return basicConfigInfoMapper.deleteBasicConfigInfoById(id); return basicConfigInfoMapper.deleteBasicConfigInfoById(id);
} }
/**
*
* @param basicConfigInfo
* @return
* @throws ServletException
*/
@Override @Override
@Transactional @Transactional
public boolean connectionTest(BasicConfigInfo basicConfigInfo) throws ServletException { public boolean connectionTest(BasicConfigInfo basicConfigInfo) throws ServletException {
@ -140,7 +146,6 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
.createBy(SecurityUtils.getUsername()) .createBy(SecurityUtils.getUsername())
.createTime(new Date()) .createTime(new Date())
.build(); .build();
tableInfoService.saveOrUpdate(tableInfoInsert, new LambdaUpdateWrapper<TableInfo>(TableInfo.class) {{ tableInfoService.saveOrUpdate(tableInfoInsert, new LambdaUpdateWrapper<TableInfo>(TableInfo.class) {{
eq(TableInfo::getTableName, tableInfoInsert.getTableName()); eq(TableInfo::getTableName, tableInfoInsert.getTableName());
eq(TableInfo::getBasicId, basicConfigInfo.getId()); eq(TableInfo::getBasicId, basicConfigInfo.getId());
@ -166,19 +171,32 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
.basicId(basicConfigInfo.getId()) .basicId(basicConfigInfo.getId())
.tableName(tableName) .tableName(tableName)
//bug点tableRemark为空造成空指针异常 //bug点tableRemark为空造成空指针异常
.tableRemark(tableRemark == null ? "null" : tableRemark) .tableRemark(tableRemark == null ? "" : tableRemark)
.parentId(tableInfo.getId()) .parentId(tableInfo.getId())
.center("Y") .center("Y")
.updateBy(SecurityUtils.getUsername()) .updateBy(SecurityUtils.getUsername())
.dataNum(rowCount) .dataNum(rowCount)
.updateTime(new Date()) .updateTime(new Date())
.build(); .build();
tableInfoService.saveOrUpdate(build, new LambdaUpdateWrapper<TableInfo>(TableInfo.class) {{ tableInfoService.saveOrUpdate(build, new LambdaUpdateWrapper<>(TableInfo.class) {{
eq(TableInfo::getTableName, build.getTableName()); eq(TableInfo::getTableName, build.getTableName());
eq(TableInfo::getBasicId, basicConfigInfo.getId()); eq(TableInfo::getBasicId, basicConfigInfo.getId());
}}); }});
TableInfo table = tableInfoService.selectTableInfoByName(build); TableInfo table = tableInfoService.selectTableInfoByName(build);
//线程池 //线程池
ExecutorService threadPool = Executors.newCachedThreadPool();
threadPool.submit(()->{
try {
syncData(finalConn, databaseName, table);
} catch (SQLException e) {
try {
throw new ServletException("同步数据失败");
} catch (ServletException ex) {
throw new RuntimeException(ex.getMessage());
}
}
});
Runnable thread = new Runnable() { Runnable thread = new Runnable() {
@SneakyThrows @SneakyThrows
@Override @Override
@ -193,17 +211,23 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
} }
}; };
thread.run(); thread.run();
// ps.close(); ps.close();
} }
// conn.close(); conn.close();
} catch (SQLException e) { } catch (SQLException e) {
log.error(e.getMessage()); log.error(e.getMessage());
throw new ServletException("连接失败"); throw new ServletException("连接失败");
} }
return true; return true;
} }
/**
*
* @param conn
* @param databaseName
* @param table
* @throws SQLException
*/
public void syncData(Connection conn, String databaseName, TableInfo table) throws SQLException { public void syncData(Connection conn, String databaseName, TableInfo table) throws SQLException {
ExecutorService threadPool = Executors.newCachedThreadPool(); ExecutorService threadPool = Executors.newCachedThreadPool();
PreparedStatement ps = conn.prepareStatement( PreparedStatement ps = conn.prepareStatement(
@ -257,75 +281,23 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
.isNull("YES".equals(isNullable) ? "Y" : "N") .isNull("YES".equals(isNullable) ? "Y" : "N")
.defaultValue(columnDefault) .defaultValue(columnDefault)
.build(); .build();
threadPool.submit(() -> {
threadPool.submit(new Runnable() { structureService.saveOrUpdate(build, new LambdaUpdateWrapper<Structure>() {{
@Override eq(Structure::getTableId, build.getTableId());
public void run() { eq(Structure::getColumnName, build.getColumnName());
structureService.saveOrUpdate(build, new LambdaUpdateWrapper<Structure>() {{ eq(Structure::getColumnRemark, build.getColumnRemark());
eq(Structure::getTableId, build.getTableId()); }});
eq(Structure::getColumnName, build.getColumnName());
eq(Structure::getColumnRemark, build.getColumnRemark());
}});
}
}); });
} }
threadPool.shutdown(); threadPool.shutdown();
ps.close(); ps.close();
} }
// /**
// * 获取数据接入接口数据
// * @return
// */
// @Override
// public List getDataByEtl() {
// List<BasicConfigInfo> list = this.list();
// List<Map<String,List<String>>> mapList = new ArrayList<>();
// for (BasicConfigInfo info : list) {
// //定义下面需要的对象
// String url = "jdbc:" + info.getDatabaseType() + "://" + info.getHost() + ":" + info.getPort() + "/" + info.getDatabaseName() + "";
// String user = info.getUsername();
// String password = info.getPassword();
// Connection conn = null;
// try {
// conn = DriverManager.getConnection(url, user, password);
// DatabaseMetaData metaData = conn.getMetaData();
// ResultSet resultSet = metaData.getCatalogs();
// while (resultSet.next()){
// //库名
// String catalogs = resultSet.getString("TABLE_CAT");
// log.info(catalogs);
// // 获取表名
// ResultSet rs = metaData.getTables(info.getDatabaseName(),null, "%", new String[]{"TABLE", "VIEW"});
// try {
// ArrayList<String> tableName = new ArrayList<>();
// while (rs.next()) {
// //表名
// String tableName = rs.getString("TABLE_NAME");
// tableName.add( rs.getString("TABLE_NAME"));
// }
// mapList.add(new HashMap<>(){{
// //库名,表名
// put(catalogs,tableName);
// }});
// }catch (Exception exception){
// continue;
// }
// }
// } catch (Exception e){
// log.error(e.getMessage());
// continue;
// }
// }
// return mapList;
// }
/** /**
* *
* *
* @return * @return
*/ */
@Override @Override
public List<TableTreeResp> getTableTree() { public List<TableTreeResp> getTableTree() {
ExecutorService threadPool = Executors.newCachedThreadPool(); ExecutorService threadPool = Executors.newCachedThreadPool();
@ -336,57 +308,29 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
List<TableInfoStructureResp> tableInfoStructureRespList = tableInfoService.selectTableInfoList(new TableInfo() {{ List<TableInfoStructureResp> tableInfoStructureRespList = tableInfoService.selectTableInfoList(new TableInfo() {{
setParentId(tableInfo.getId()); setParentId(tableInfo.getId());
}}).stream().map(info -> { }}).stream().map(info -> {
TableInfoStructureResp tableInfoStructureResp=null; TableInfoStructureResp tableInfoStructureResp = null;
Future<TableInfoStructureResp> submit = threadPool.submit(new Callable<TableInfoStructureResp>() { Future<TableInfoStructureResp> submit = threadPool.submit(() -> {
@Override // List<Structure> structureList = structureService.list(new LambdaQueryWrapper<Structure>().eq(Structure::getTableId, info.getId()));
public TableInfoStructureResp call() throws Exception { return TableInfoStructureResp.builder()
List<Structure> structureList = structureService.list(new LambdaQueryWrapper<Structure>().eq(Structure::getTableId, info.getId())); .id(info.getId())
return TableInfoStructureResp.builder() .tableName(info.getTableName())
.id(info.getId()) .center(info.getCenter())
.tableName(info.getTableName()) .tableRemark(info.getTableRemark())
.center(info.getCenter()) .dataNum(info.getDataNum())
.tableRemark(info.getTableRemark()) .parentId(info.getParentId())
.dataNum(info.getDataNum()) .databaseType(basicConfigInfo.getDatabaseType())
.parentId(info.getParentId()) .structureList(null)
.databaseType(basicConfigInfo.getDatabaseType()) .basicId(info.getBasicId())
.structureList(structureList) .build();
.basicId(info.getBasicId())
.build();
}
}); });
try { try {
tableInfoStructureResp= submit.get(); tableInfoStructureResp = submit.get();
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return tableInfoStructureResp; return tableInfoStructureResp;
// TableInfoStructureResp callable = null;
//同构线程获取响应体对象
// try {
// callable = new Callable<>() {
// @Override
// public TableInfoStructureResp call() throws Exception {
// List<Structure> structureList = structureService.list(new LambdaQueryWrapper<Structure>().eq(Structure::getTableId, info.getId()));
// return TableInfoStructureResp.builder()
// .id(info.getId())
// .tableName(info.getTableName())
// .center(info.getCenter())
// .tableRemark(info.getTableRemark())
// .dataNum(info.getDataNum())
// .parentId(info.getParentId())
// .databaseType(basicConfigInfo.getDatabaseType())
// .structureList(structureList)
// .basicId(info.getBasicId())
// .build();
// }
// }.call();
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// return callable;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
return TableTreeResp.builder() return TableTreeResp.builder()
.tableInfo(tableInfo) .tableInfo(tableInfo)
.basicConfigInfo(basicConfigInfo) .basicConfigInfo(basicConfigInfo)
@ -394,11 +338,29 @@ public class BasicConfigInfoServiceImpl extends ServiceImpl<BasicConfigInfoMappe
.build(); .build();
}).collect(Collectors.toList()); }).collect(Collectors.toList());
threadPool.shutdown(); threadPool.shutdown();
while (true){ while (true) if (threadPool.isTerminated()) break;
if (threadPool.isTerminated())break;
}
return tableTreeRespList; return tableTreeRespList;
} }
@Override
public TableInfoStructureResp getTableInfo(Long tableId) {
TableInfo tableInfo = tableInfoService.selectTableInfoById(tableId);
BasicConfigInfo basicConfigInfo = this.selectBasicConfigInfoById(tableInfo.getBasicId());
List<Structure> structureList = structureService.list(new LambdaQueryWrapper<Structure>()
.eq(Structure::getTableId, tableInfo.getId()));
return TableInfoStructureResp.builder()
.id(tableInfo.getId())
.tableName(tableInfo.getTableName())
.center(tableInfo.getCenter())
.tableRemark(tableInfo.getTableRemark())
.dataNum(tableInfo.getDataNum())
.parentId(tableInfo.getParentId())
.databaseType(basicConfigInfo.getDatabaseType())
.structureList(structureList)
.basicId(tableInfo.getBasicId())
.build();
}
} }

View File

@ -1,9 +1,16 @@
package com.muyu.etl.service.impl; package com.muyu.etl.service.impl;
import java.util.List; import java.util.List;
import java.util.concurrent.Future;
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.utils.DateUtils; import com.muyu.common.core.utils.DateUtils;
import com.muyu.etl.domain.BasicConfigInfo;
import com.muyu.etl.domain.Structure;
import com.muyu.etl.domain.resp.TableInfoStructureResp;
import com.muyu.etl.service.BasicConfigInfoService;
import com.muyu.etl.service.StructureService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -19,10 +26,12 @@ import com.muyu.etl.service.TableInfoService;
*/ */
@Log4j2 @Log4j2
@Service @Service
public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableInfo> implements TableInfoService public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableInfo> implements TableInfoService {
{
@Autowired @Autowired
private TableInfoMapper tableInfoMapper; private TableInfoMapper tableInfoMapper;
@Autowired
private StructureService structureService;
/** /**
* *
@ -31,8 +40,7 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
* @return * @return
*/ */
@Override @Override
public TableInfo selectTableInfoById(Long id) public TableInfo selectTableInfoById(Long id) {
{
return tableInfoMapper.selectTableInfoById(id); return tableInfoMapper.selectTableInfoById(id);
} }
@ -43,8 +51,7 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
* @return * @return
*/ */
@Override @Override
public List<TableInfo> selectTableInfoList(TableInfo tableInfo) public List<TableInfo> selectTableInfoList(TableInfo tableInfo) {
{
return tableInfoMapper.selectTableInfoList(tableInfo); return tableInfoMapper.selectTableInfoList(tableInfo);
} }
@ -55,8 +62,7 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
* @return * @return
*/ */
@Override @Override
public int insertTableInfo(TableInfo tableInfo) public int insertTableInfo(TableInfo tableInfo) {
{
tableInfo.setCreateTime(DateUtils.getNowDate()); tableInfo.setCreateTime(DateUtils.getNowDate());
return tableInfoMapper.insertTableInfo(tableInfo); return tableInfoMapper.insertTableInfo(tableInfo);
} }
@ -68,8 +74,7 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
* @return * @return
*/ */
@Override @Override
public int updateTableInfo(TableInfo tableInfo) public int updateTableInfo(TableInfo tableInfo) {
{
tableInfo.setUpdateTime(DateUtils.getNowDate()); tableInfo.setUpdateTime(DateUtils.getNowDate());
return tableInfoMapper.updateTableInfo(tableInfo); return tableInfoMapper.updateTableInfo(tableInfo);
} }
@ -81,8 +86,7 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
* @return * @return
*/ */
@Override @Override
public int deleteTableInfoByIds(Long[] ids) public int deleteTableInfoByIds(Long[] ids) {
{
return tableInfoMapper.deleteTableInfoByIds(ids); return tableInfoMapper.deleteTableInfoByIds(ids);
} }
@ -93,8 +97,7 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
* @return * @return
*/ */
@Override @Override
public int deleteTableInfoById(Long id) public int deleteTableInfoById(Long id) {
{
return tableInfoMapper.deleteTableInfoById(id); return tableInfoMapper.deleteTableInfoById(id);
} }
@ -102,4 +105,6 @@ public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableIn
public TableInfo selectTableInfoByName(TableInfo table) { public TableInfo selectTableInfoByName(TableInfo table) {
return tableInfoMapper.selectTableInfoByName(table); return tableInfoMapper.selectTableInfoByName(table);
} }
} }