资产展示{KLTV}的代码

master
冷调 2024-08-30 22:32:32 +08:00
parent 91feeae686
commit c79777ef32
12 changed files with 400 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package com.muyu.source.core;
import java.math.BigDecimal;
import java.util.Date;
/**
@ -11,19 +12,44 @@ import java.util.Date;
* @ Description
*/
public enum DataType {
INT("int", Integer.class),
BIGINT("bigint", Long.class),
DECIMAL("decimal", Double.class),
DATE("date", Date.class),
TIMESTAMP("timestamp", Date.class),
BOOLEAN("boolean", Boolean.class),
TEXT("text", String.class),
VERCHAR("varchar", String.class);
VARCHAR("varchar",String.class,"String"),
BIGINT("bigint", Long.class,"Long"),
INT("int", Integer.class,"Integer"),
DECIMAL("decimal", BigDecimal.class,"BigDecimal"),
DATETIME("datetime", Date.class,"Date"),
TEXT("text", String.class,"String"),
DOUBLE("double", Double.class,"Double");
private final String sourceType;
private final Class<?> targetType;
DataType(String sourceType, Class<?> targetType) {
private final String javaType;
public static Class convertType(String type){
for (DataType dataType : DataType.values()) {
if (dataType.sourceType.equalsIgnoreCase(type)){
return dataType.targetType;
}
}
return String.class;
}
public static String convertTypeString(String type){
for (DataType dataType : DataType.values()) {
if (dataType.sourceType.equalsIgnoreCase(type)){
return dataType.javaType;
}
}
return "String";
}
DataType(String sourceType, Class<?> targetType, String javaType) {
this.sourceType = sourceType;
this.targetType = targetType;
this.javaType = javaType;
}
}

View File

@ -1,6 +1,10 @@
package com.muyu.source.core;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @ ToolIntelliJ IDEA
@ -10,10 +14,13 @@ package com.muyu.source.core;
* @ Description
* @author Lenovo
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class DataValue {
private String label;
private String key;
private String value;
private Object value;
private String type;
}

View File

@ -0,0 +1,20 @@
package com.muyu.source.base;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:34
* @ Version1.0
*/
public abstract class BaseDataAbsSource implements BaseDataSource {
public void setQuery(BaseQuery baseQuery) {
BaseQueryHandler.set(baseQuery);
}
public <T> T getQuery() {
return BaseQueryHandler.get();
}
}

View File

@ -0,0 +1,23 @@
package com.muyu.source.base;
import com.muyu.source.core.DataValue;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:31
* @ Version1.0
*/
public interface BaseDataSource {
public void setQuery(BaseQuery baseQuery);
public DataValue getDataValue();
public <T> T getQuery();
DataValue[] getRow();
DataValue[][] getRows();
}

View File

@ -0,0 +1,19 @@
package com.muyu.source.base;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:33
* @ Version1.0
*/
@Data
@SuperBuilder
@NoArgsConstructor
public class BaseQuery {
}

View File

@ -0,0 +1,20 @@
package com.muyu.source.base;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:35
* @ Version1.0
*/
public class BaseQueryHandler {
private static final ThreadLocal<BaseQuery> BASE_QUERY_THREAD_LOCAL=new ThreadLocal<>();
public static void set(BaseQuery baseQuery){
BASE_QUERY_THREAD_LOCAL.set(baseQuery);
}
public static<T> T get(){
return (T)BASE_QUERY_THREAD_LOCAL.get();
}
}

View File

@ -0,0 +1,60 @@
package com.muyu.source.controller;
import com.muyu.common.core.domain.Result;
import com.muyu.source.core.DataValue;
import com.muyu.source.service.DataValueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:37
* @ Version1.0
* @ Description
*/
@RestController
@RequestMapping("/value")
@Tag(name = "资产展示控制层", description = "进行资产展示的相关操作")
public class DataValueController {
@Autowired
private DataValueService dataValueService;
/**
* IDSQL
*
* @param basicId ID
* @param sql SQL
* @return DataValue{kltv}
*/
@PostMapping("/findTableValue")
@Operation(summary = "根据基础表ID和SQL语句查询数据", description = "根据基础表ID和SQL语句查询数据")
public Result findTableValue(@RequestParam("basicId") Long basicId, @RequestParam("sql") String sql) {
List<DataValue> dataValueList = dataValueService.findTableValue(basicId, sql);
return Result.success(dataValueList);
}
/**
* ID
*
* @param basicId ID
* @param tableName
* @return DataValue{kltv}
*/
@PostMapping("/findTableValueByTableName")
@Operation(summary = "根据基础表ID和SQL语句查询数据", description = "根据基础表ID和SQL语句查询数据")
public Result findTableValueByTableName(@RequestParam("basicId") Long basicId, @RequestParam("tableName") String tableName) {
List<DataValue> dataValueList = dataValueService.findTableValueByTableName(basicId, tableName);
return Result.success(dataValueList);
}
}

View File

@ -0,0 +1,17 @@
package com.muyu.source.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.source.core.DataValue;
import org.apache.ibatis.annotations.Mapper;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:52
* @ Version1.0
* @ Description{KTLV}
*/
@Mapper
public interface DataValueMapper extends BaseMapper<DataValue> {
}

View File

@ -0,0 +1,29 @@
package com.muyu.source.mysql;
import com.muyu.source.base.BaseDataAbsSource;
import com.muyu.source.core.DataValue;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:45
* @ Version1.0
*/
public class MysqlDataSource extends BaseDataAbsSource {
@Override
public DataValue getDataValue() {
return null;
}
@Override
public DataValue[] getRow() {
return new DataValue[0];
}
@Override
public DataValue[][] getRows() {
return new DataValue[0][];
}
}

View File

@ -0,0 +1,25 @@
package com.muyu.source.mysql;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.Map;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:46
* @ Version1.0
*/
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class MysqlQuery {
private String dataSourceId;
private String sql;
private Map<String, Object> params;
}

View File

@ -0,0 +1,20 @@
package com.muyu.source.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.source.core.DataValue;
import java.util.List;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:53
* @ Version1.0
* @ Description{KLTV}
*/
public interface DataValueService extends IService<DataValue> {
List<DataValue> findTableValue(Long basicId, String sql);
List<DataValue> findTableValueByTableName(Long basicId, String tableName);
}

View File

@ -0,0 +1,123 @@
package com.muyu.source.service.Impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.source.core.DataType;
import com.muyu.source.core.DataValue;
import com.muyu.source.domain.DataSource;
import com.muyu.source.mapper.DataValueMapper;
import com.muyu.source.mysql.MysqlQuery;
import com.muyu.source.pool.MysqlPool;
import com.muyu.source.service.DataSourceService;
import com.muyu.source.service.DataValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import static com.muyu.source.pool.config.BaseConfig.SELECTALL;
/**
* @author Lenovo
* @ ToolIntelliJ IDEA
* @ AuthorCHX
* @ Date2024-08-30-21:54
* @ Version1.0
* @ Description{KLTV}
*/
@Service
public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue> implements DataValueService {
@Autowired
private DataSourceService dataSourceService;
@Override
public List<DataValue> findTableValue(Long basicId, String sql) {
List<DataValue> dataValueList = new ArrayList<>();
MysqlQuery mysqlQuery = new MysqlQuery();
mysqlQuery.setDataSourceId(String.valueOf(basicId));
DataSource dataSource = dataSourceService.getById(basicId);
MysqlPool mysqlPool = new MysqlPool(dataSource);
mysqlPool.init();
Connection conn = mysqlPool.getConn();
try {
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()){
for (int i = 1; i <= columnCount; i++) {
String columnTypeName = metaData.getColumnTypeName(i);
DatabaseMetaData metaDataColumns = conn.getMetaData();
ResultSet columns = metaDataColumns.getColumns(null, null, metaData.getTableName(i), metaData.getColumnName(i));
String remarks =null;
while (columns.next()){
remarks = columns.getString("REMARKS");
}
DataValue build = DataValue.builder()
.key(metaData.getColumnName(i))
.label(remarks)
.value(resultSet.getObject(i, DataType.convertType(columnTypeName)))
.type(DataType.convertTypeString(columnTypeName))
.build();
dataValueList.add(build);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return dataValueList;
}
@Override
public List<DataValue> findTableValueByTableName(Long basicId, String tableName) {
MysqlQuery mySqlQuery = new MysqlQuery();
mySqlQuery.setDataSourceId(String.valueOf(basicId));
DataSource etlDataScore = dataSourceService.getById(basicId);
MysqlPool mysqlPool = new MysqlPool(etlDataScore);
mysqlPool.init();
Connection conn = mysqlPool.getConn();
List<DataValue> list = new ArrayList<>();
try {
PreparedStatement preparedStatement = conn.prepareStatement(SELECTALL+tableName);
ResultSet resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()){
for (int i = 1; i <= columnCount; i++) {
String columnTypeName = metaData.getColumnTypeName(i);
DatabaseMetaData metaDataColumns = conn.getMetaData();
ResultSet columns = metaDataColumns.getColumns(null, null, metaData.getTableName(i), metaData.getColumnName(i));
String remarks =null;
while (columns.next()){
remarks = columns.getString("REMARKS");
}
DataValue build = DataValue.builder()
.key(metaData.getColumnName(i))
.label(remarks)
.value(resultSet.getObject(i, DataType.convertType(columnTypeName)))
.type(DataType.convertTypeString(columnTypeName))
.build();
list.add(build);
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
mysqlPool.replease(conn);
mysqlPool.closeConn();
return list;
}
}