feat:同步资产结构
commit
f3054f958f
|
@ -0,0 +1,83 @@
|
||||||
|
package com.muyu.source.domain;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
import com.muyu.common.core.annotation.Excel;
|
||||||
|
import com.muyu.common.core.web.domain.BaseEntity;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资产结构表
|
||||||
|
*
|
||||||
|
* @ClassName AssetStructure
|
||||||
|
* @Author AnNan.Wang
|
||||||
|
* @Date 2024/4/22 19:10
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@TableName(value = "asset_structure")
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class AssetStructure extends BaseEntity {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
/**
|
||||||
|
*资产表id
|
||||||
|
*/
|
||||||
|
@TableId(value = "id",type = IdType.AUTO)
|
||||||
|
@ApiModelProperty(name = "资产表id", value = "资产表id")
|
||||||
|
private Long id;
|
||||||
|
/**
|
||||||
|
*接入源名称
|
||||||
|
*/
|
||||||
|
@Excel(name = "接入源名称")
|
||||||
|
@ApiModelProperty(name = "接入源名称", value = "接入源名称")
|
||||||
|
private String accessSourceName;
|
||||||
|
/**
|
||||||
|
*数据来源系统名称
|
||||||
|
*/
|
||||||
|
@Excel(name = "数据来源系统名称")
|
||||||
|
@ApiModelProperty(name = "数据来源系统名称", value = "数据来源系统名称")
|
||||||
|
private String dataSourceSystemName;
|
||||||
|
/**
|
||||||
|
*数据库名称
|
||||||
|
*/
|
||||||
|
@Excel(name = "数据库名称")
|
||||||
|
@ApiModelProperty(name = "数据库名称", value = "数据库名称")
|
||||||
|
private String databaseName;
|
||||||
|
/**
|
||||||
|
*数据库表名
|
||||||
|
*/
|
||||||
|
@Excel(name = "数据库表名")
|
||||||
|
@ApiModelProperty(name = "数据库表名", value = "数据库表名")
|
||||||
|
private String databaseTableName;
|
||||||
|
/**
|
||||||
|
*数据量
|
||||||
|
*/
|
||||||
|
@Excel(name = "数据量")
|
||||||
|
@ApiModelProperty(name = "数据量", value = "数据量")
|
||||||
|
private Integer dataVolume;
|
||||||
|
/**
|
||||||
|
*数据数量
|
||||||
|
*/
|
||||||
|
@Excel(name = "数据数量")
|
||||||
|
@ApiModelProperty(name = "数据数量", value = "数据数量")
|
||||||
|
private Integer dataQuantity;
|
||||||
|
/**
|
||||||
|
*是否核心
|
||||||
|
*/
|
||||||
|
@Excel(name = "是否核心")
|
||||||
|
@ApiModelProperty(name = "是否核心", value = "是否核心")
|
||||||
|
private char coreOrNot;
|
||||||
|
|
||||||
|
@Excel(name = "字段名")
|
||||||
|
@ApiModelProperty(name = "字段名", value = "字段名")
|
||||||
|
private String fieldName;
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.muyu.source.controller;
|
||||||
|
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.common.log.annotation.Log;
|
||||||
|
import com.muyu.common.log.enums.BusinessType;
|
||||||
|
import com.muyu.common.security.annotation.RequiresPermissions;
|
||||||
|
import com.muyu.source.domain.SysDataSource;
|
||||||
|
import com.muyu.source.service.AssetStructureService;
|
||||||
|
import com.muyu.source.service.SysAccessService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.apache.commons.math3.analysis.function.Add;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资产Controller
|
||||||
|
*
|
||||||
|
* @ClassName AssetStructureController
|
||||||
|
* @Author AnNan.Wang
|
||||||
|
* @Date 2024/4/22 19:29
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Api(tags = "资产")
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/structure")
|
||||||
|
public class AssetStructureController {
|
||||||
|
@Autowired
|
||||||
|
private AssetStructureService assetStructureService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步资产结构
|
||||||
|
* @param source
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequiresPermissions("source:structure:synchronization")
|
||||||
|
@Log(title = "资产结构同步", businessType = BusinessType.DELETE)
|
||||||
|
@ApiOperation("资产结构同步")
|
||||||
|
@PostMapping("/{id}")
|
||||||
|
public Result<String> synchronization(@PathVariable Long id){
|
||||||
|
assetStructureService.synchronize(id);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.muyu.source.mapper;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.muyu.source.domain.AssetStructure;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资产Mapper接口
|
||||||
|
*
|
||||||
|
* @author AnNan.Wang
|
||||||
|
* @ClassName: AssetStructureMapper
|
||||||
|
* @createTime: 2024/4/22 19:32
|
||||||
|
*/
|
||||||
|
public interface AssetStructureMapper extends BaseMapper<AssetStructure> {
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
package com.muyu.source.service;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.source.domain.AssetStructure;
|
||||||
|
import com.muyu.source.domain.SysDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资产service接口
|
||||||
|
*
|
||||||
|
* @author AnNan.Wang
|
||||||
|
* @ClassName: AssetStructureService
|
||||||
|
* @createTime: 2024/4/22 19:30
|
||||||
|
*/
|
||||||
|
public interface AssetStructureService extends IService<AssetStructure> {
|
||||||
|
void synchronize(Long id);
|
||||||
|
}
|
|
@ -0,0 +1,111 @@
|
||||||
|
package com.muyu.source.service.impl;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import com.muyu.source.domain.AssetStructure;
|
||||||
|
import com.muyu.source.domain.SysAccess;
|
||||||
|
import com.muyu.source.domain.SysDataSource;
|
||||||
|
import com.muyu.source.domain.red.UserPassword;
|
||||||
|
import com.muyu.source.mapper.AssetStructureMapper;
|
||||||
|
import com.muyu.source.mapper.SysAccessMapper;
|
||||||
|
import com.muyu.source.mapper.SysDataSourceMapper;
|
||||||
|
import com.muyu.source.service.AssetStructureService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资产service业务实现层
|
||||||
|
*
|
||||||
|
* @ClassName AssetStructureServiceImpl
|
||||||
|
* @Author AnNan.Wang
|
||||||
|
* @Date 2024/4/22 19:31
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
public class AssetStructureServiceImpl extends ServiceImpl<AssetStructureMapper,AssetStructure>
|
||||||
|
implements AssetStructureService {
|
||||||
|
@Autowired
|
||||||
|
private AssetStructureMapper assetStructureMapper;
|
||||||
|
@Autowired
|
||||||
|
private SysDataSourceMapper sysDataSourceMapper;
|
||||||
|
@Autowired
|
||||||
|
private SysAccessMapper sysAccessMapper;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void synchronize(Long id) {
|
||||||
|
List<SysDataSource> sysDataSources = sysDataSourceMapper.selectList(
|
||||||
|
new LambdaQueryWrapper<SysDataSource>() {{
|
||||||
|
eq(SysDataSource::getId, id);
|
||||||
|
}}
|
||||||
|
);
|
||||||
|
for (SysDataSource sysDataSource : sysDataSources) {
|
||||||
|
String url="jdbc:mysql://"+sysDataSource.getHostAddress()+":"+sysDataSource.getHostNumber()+"/"+sysDataSource.getDatabaseName();
|
||||||
|
SysAccess sysAccess = sysAccessMapper.selectById(sysDataSource.getAccessId());
|
||||||
|
|
||||||
|
UserPassword build = UserPassword.builder()
|
||||||
|
.USERNAME(sysAccess.getUser())
|
||||||
|
.PASSWORD(sysAccess.getPassword())
|
||||||
|
.build();
|
||||||
|
try {
|
||||||
|
Connection connection = DriverManager.getConnection(url, build.getUSERNAME(), build.getPASSWORD());
|
||||||
|
if (connection!=null) {
|
||||||
|
|
||||||
|
String tableName="SELECT TABLE_NAME as 'name', TABLE_ROWS as 'dataTotal' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?";
|
||||||
|
|
||||||
|
PreparedStatement preparedStatement2 = connection.prepareStatement(tableName);//
|
||||||
|
preparedStatement2.setString(1, sysDataSource.getDatabaseName());
|
||||||
|
|
||||||
|
ResultSet resultSet = preparedStatement2.executeQuery();
|
||||||
|
|
||||||
|
while (resultSet.next()) {
|
||||||
|
String name = resultSet.getString("name");
|
||||||
|
log.info("Name: " + name);
|
||||||
|
int dataTotal = resultSet.getInt("dataTotal");
|
||||||
|
log.info("dataTotal: " + dataTotal);
|
||||||
|
PreparedStatement preparedStatement = connection.prepareStatement("select * from " + name);
|
||||||
|
ResultSet executeQuery = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
ResultSetMetaData metaData3 = executeQuery.getMetaData();
|
||||||
|
|
||||||
|
//获取有几行
|
||||||
|
int columnCount = metaData3.getColumnCount();
|
||||||
|
|
||||||
|
for (int i = 1; i <= columnCount; i++) {
|
||||||
|
//表名
|
||||||
|
String catalogName = metaData3.getCatalogName(i);
|
||||||
|
//字段名称
|
||||||
|
String fileName = metaData3.getColumnName(i);
|
||||||
|
System.out.println(fileName);
|
||||||
|
//接入源名称
|
||||||
|
String sourceName = sysDataSource.getSourceName();
|
||||||
|
//数据来源系统名称
|
||||||
|
String systemName = sysDataSource.getSystemName();
|
||||||
|
|
||||||
|
assetStructureMapper.insert(
|
||||||
|
AssetStructure.builder()
|
||||||
|
.accessSourceName(sourceName)
|
||||||
|
.fieldName(fileName)
|
||||||
|
.dataSourceSystemName(systemName)
|
||||||
|
.databaseName(catalogName)
|
||||||
|
.databaseTableName(name)
|
||||||
|
.dataVolume(columnCount)
|
||||||
|
.dataQuantity(dataTotal)
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue