资产展示{KLTV}的代码
parent
91feeae686
commit
c79777ef32
|
@ -1,5 +1,6 @@
|
||||||
package com.muyu.source.core;
|
package com.muyu.source.core;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,19 +12,44 @@ import java.util.Date;
|
||||||
* @ Description:数据类型枚举
|
* @ Description:数据类型枚举
|
||||||
*/
|
*/
|
||||||
public enum DataType {
|
public enum DataType {
|
||||||
INT("int", Integer.class),
|
VARCHAR("varchar",String.class,"String"),
|
||||||
BIGINT("bigint", Long.class),
|
BIGINT("bigint", Long.class,"Long"),
|
||||||
DECIMAL("decimal", Double.class),
|
INT("int", Integer.class,"Integer"),
|
||||||
DATE("date", Date.class),
|
DECIMAL("decimal", BigDecimal.class,"BigDecimal"),
|
||||||
TIMESTAMP("timestamp", Date.class),
|
DATETIME("datetime", Date.class,"Date"),
|
||||||
BOOLEAN("boolean", Boolean.class),
|
TEXT("text", String.class,"String"),
|
||||||
TEXT("text", String.class),
|
DOUBLE("double", Double.class,"Double");
|
||||||
VERCHAR("varchar", String.class);
|
|
||||||
private final String sourceType;
|
private final String sourceType;
|
||||||
|
|
||||||
private final Class<?> targetType;
|
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.sourceType = sourceType;
|
||||||
this.targetType = targetType;
|
this.targetType = targetType;
|
||||||
|
this.javaType = javaType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package com.muyu.source.core;
|
package com.muyu.source.core;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ Tool:IntelliJ IDEA
|
* @ Tool:IntelliJ IDEA
|
||||||
|
@ -10,10 +14,13 @@ package com.muyu.source.core;
|
||||||
* @ Description:数据值
|
* @ Description:数据值
|
||||||
* @author Lenovo
|
* @author Lenovo
|
||||||
*/
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@SuperBuilder
|
||||||
public class DataValue {
|
public class DataValue {
|
||||||
private String label;
|
private String label;
|
||||||
private String key;
|
private String key;
|
||||||
private String value;
|
private Object value;
|
||||||
private String type;
|
private String type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.muyu.source.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:34
|
||||||
|
* @ Version:1.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
public abstract class BaseDataAbsSource implements BaseDataSource {
|
||||||
|
|
||||||
|
public void setQuery(BaseQuery baseQuery) {
|
||||||
|
BaseQueryHandler.set(baseQuery);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> T getQuery() {
|
||||||
|
return BaseQueryHandler.get();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.muyu.source.base;
|
||||||
|
|
||||||
|
import com.muyu.source.core.DataValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:31
|
||||||
|
* @ Version:1.0
|
||||||
|
*/
|
||||||
|
public interface BaseDataSource {
|
||||||
|
public void setQuery(BaseQuery baseQuery);
|
||||||
|
|
||||||
|
public DataValue getDataValue();
|
||||||
|
|
||||||
|
public <T> T getQuery();
|
||||||
|
|
||||||
|
DataValue[] getRow();
|
||||||
|
|
||||||
|
DataValue[][] getRows();
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.muyu.source.base;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.experimental.SuperBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:33
|
||||||
|
* @ Version:1.0
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class BaseQuery {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package com.muyu.source.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:35
|
||||||
|
* @ Version:1.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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:37
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:资产展示控制层
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/value")
|
||||||
|
@Tag(name = "资产展示控制层", description = "进行资产展示的相关操作")
|
||||||
|
public class DataValueController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataValueService dataValueService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据基础表ID和SQL语句查询数据
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:52
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:资产展示{KTLV}控制层
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface DataValueMapper extends BaseMapper<DataValue> {
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.muyu.source.mysql;
|
||||||
|
|
||||||
|
import com.muyu.source.base.BaseDataAbsSource;
|
||||||
|
import com.muyu.source.core.DataValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Lenovo
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:45
|
||||||
|
* @ Version:1.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][];
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:46
|
||||||
|
* @ Version:1.0
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class MysqlQuery {
|
||||||
|
private String dataSourceId;
|
||||||
|
private String sql;
|
||||||
|
private Map<String, Object> params;
|
||||||
|
}
|
|
@ -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
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:53
|
||||||
|
* @ Version:1.0
|
||||||
|
* @ Description:资产展示{KLTV}业务层
|
||||||
|
*/
|
||||||
|
public interface DataValueService extends IService<DataValue> {
|
||||||
|
List<DataValue> findTableValue(Long basicId, String sql);
|
||||||
|
|
||||||
|
List<DataValue> findTableValueByTableName(Long basicId, String tableName);
|
||||||
|
}
|
|
@ -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
|
||||||
|
* @ Tool:IntelliJ IDEA
|
||||||
|
* @ Author:CHX
|
||||||
|
* @ Date:2024-08-30-21:54
|
||||||
|
* @ Version:1.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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue