You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
10 KiB
Java

package com.weaver.seconddev.cockpit.util;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.ebuilder.datasource.api.enums.SqlParamType;
import com.weaver.ebuilder.datasource.api.query.dto.dw.DynamicParamDto;
import com.weaver.ebuilder.datasource.api.query.dto.dw.FieldQuery;
import com.weaver.ebuilder.datasource.api.query.dto.dw.GroupQuery;
import com.weaver.ebuilder.datasource.api.query.dto.dw.TableQuery;
import com.weaver.ebuilder.datasource.api.entity.ExecuteSqlEntity;
import com.weaver.ebuilder.datasource.api.enums.SourceType;
import com.weaver.ebuilder.datasource.api.service.DataSetService;
import com.weaver.ebuilder.datasource.api.service.impl.EbFormDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* DatabaseUtil SQL
*/
@Component
public class DatabaseUtils {
private final static Logger log = LoggerFactory.getLogger(DatabaseUtils.class);
@Autowired
private DataSetService dataSetService;
@Autowired
private EbFormDataService dataService;
/**
* SQL
*
* @param entity SQL
* @return Map
* @throws RuntimeException SQL
*/
public Map<String, Object> executeSql(ExecuteSqlEntity entity) {
Map<String, Object> map = dataSetService.executeSql(entity);
if ("FAIL".equals(CommonUtils.null2String(map.get("status")).toUpperCase(Locale.ROOT))) {
log.error("sql执行失败=>{}", JSONObject.toJSONString(map));
throw new RuntimeException("sql执行异常");
} else {
return map;
}
}
/**
* SQL
*
* @param entity SQL
* @param pageNo
* @param pageSize
* @return Map
* @throws RuntimeException SQL
*/
public Map<String, Object> executeSql(ExecuteSqlEntity entity, int pageNo, int pageSize) {
entity.setPageNo(pageNo);
entity.setPageSize(pageSize);
Map<String, Object> map = dataSetService.executeForQuery(entity);
if ("FAIL".equals(CommonUtils.null2String(map.get("status")).toUpperCase(Locale.ROOT))) {
log.error("sql执行失败=>{}", JSONObject.toJSONString(map));
throw new RuntimeException("sql执行异常");
} else {
return map;
}
}
/**
* SQL
*
* @param sql SQL
* @param groupId ID
* @paramDesc ID select APPLICATION_MARK,APPLICATION_name from eteams.ds_mark_service_relation
* @return SQL
*/
public ExecuteSqlEntity getExecuteSqlEntity(String sql, String groupId) {
log.error("sql=>{}", sql);
ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity();
executeSqlEntity.setSql(base64(sql));
executeSqlEntity.setGroupId(groupId);
executeSqlEntity.setSourceType(SourceType.LOGIC);
executeSqlEntity.setGroupKey("0");
return executeSqlEntity;
}
/**
* SQL Base64
*
* @param sql SQL
* @return
*/
public String base64(String sql) {
return Base64.encode(sql);
}
/**
*
*
* @param map Map
* @return Map
*/
public List<Map<String, Object>> getDataSourceList(Map<String, Object> map) {
List<Map<String, Object>> entity = new ArrayList();
if ("OK".equals(CommonUtils.null2String(map.get("status")).toUpperCase(Locale.ROOT)) && map.get("count") != null && CommonUtils.getIntValue(map.get("count")) > 0) {
entity = (List) map.get("records");
}
return keyToLowerCase((List) entity);
}
public Map<String, Object> getOneDataSource(Map<String, Object> map) {
List<Map<String, Object>> entity = getDataSourceList(map);
return (Map)(CollectionUtil.isNotEmpty(entity) ? (Map)entity.get(0) : new HashMap());
}
/**
* Map
*
* @param orgMapList Map
* @return Map
*/
public List<Map<String, Object>> keyToLowerCase(List<Map<String, Object>> orgMapList) {
List<Map<String, Object>> resultList = new ArrayList();
Iterator var2 = orgMapList.iterator();
while (var2.hasNext()) {
Map<String, Object> stringObjectMap = (Map) var2.next();
resultList.add(keyToLowerCase(stringObjectMap));
}
return resultList;
}
/**
* Map
*
* @param orgMap Map
* @return Map
*/
public Map<String, Object> keyToLowerCase(Map<String, Object> orgMap) {
Map<String, Object> resultMap = new HashMap();
if (orgMap != null && !orgMap.isEmpty()) {
Set<Map.Entry<String, Object>> entrySet = orgMap.entrySet();
Iterator var3 = entrySet.iterator();
while (var3.hasNext()) {
Map.Entry<String, Object> entry = (Map.Entry) var3.next();
String key = (String) entry.getKey();
Object value = entry.getValue();
resultMap.put(key.toLowerCase(), value);
}
return resultMap;
} else {
return resultMap;
}
}
/**
*
*
* @param sourceType sourceType
* ETEAMS :
* FORM: ebuilder
* LOGIC: ()
* EXTERNAL
* @return
*/
public List<Map<String, Object>> getDataGroups(String sourceType, Boolean flag) {
GroupQuery query = new GroupQuery();
query.setSourceType(SourceType.valueOf(sourceType));
query.setShowSqlDataset(flag);
DynamicParamDto dynamicParamDto = new DynamicParamDto();
dynamicParamDto.setUserId(10000L);
dynamicParamDto.setTenantKey("tk");
query.setDynamicParamDto(dynamicParamDto);
return dataSetService.getDataGroups(query);
}
/**
*
*
* @param sourceType
* @param groupId
* @param pageNum
* @param pageSize
* @return
*/
public Map<String, Object> getDataSets(String sourceType, String groupId, Integer pageNum, Integer pageSize) {
TableQuery tableQuery = new TableQuery();
tableQuery.setSourceType(SourceType.valueOf(sourceType));
tableQuery.setGroupId(groupId);
//非必传
//tableQuery.setName(name);
tableQuery.setPageNo(pageNum);
tableQuery.setPageSize(pageSize);
return dataSetService.getDataSetsByPage(tableQuery);
}
/**
*
* sourceType :LOGIC
* sourceId : 8494845523559165780
* groupId : weaver-crm-service
*
* @param
* @return
*/
public List<Map<String, Object>> getFields(String sourceType, String sourceId, String groupId) {
FieldQuery query = new FieldQuery();
query.setSourceType(SourceType.valueOf(sourceType));
query.setSourceId(sourceId);
query.setGroupId(groupId);
return dataSetService.getFields(query);
}
/**
* sql
* sourceType :LOGIC
* groupId : weaver-ebuilder-app-service
* sql : select * from ebda_app limit 10
*
* @param
* @return
*/
public Map<String, Object> execute(String sourceType, String groupId, String sql) {
//执行sql 参数sourceType groupId sql
ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity();
executeSqlEntity.setSql(base64(sql));
executeSqlEntity.setGroupId(groupId);
executeSqlEntity.setSourceType(SourceType.valueOf(sourceType));
return dataSetService.executeSql(executeSqlEntity);
}
public Map<String, Object> executeForQuery(String sourceType, String groupId, String sql,List<SqlParamEntity> sqlparam) {
//执行sql 参数sourceType groupId sql sqlparam
ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity();
executeSqlEntity.setSql(base64(sql));
executeSqlEntity.setGroupId(groupId);
executeSqlEntity.setSourceType(SourceType.valueOf(sourceType));
executeSqlEntity.setParams(sqlparam);
return dataSetService.executeSql(executeSqlEntity);
}
public String getMysqlPagedSql(String sql,int pageNo, int pageSize) {
if(pageNo<=0){
pageNo = 1;
}
if(pageSize<=0){
pageSize = 20;
}
int start = (pageNo-1)*pageSize;
int end = pageNo*pageSize;
return new StringBuffer().append(sql).append(
" LIMIT "+start+","+(end-start)).toString();
}
/**
* sql
* @param list
* @return
*/
public List<SqlParamEntity> getSqlParamEntity(List<String> list){
List<SqlParamEntity> sqlparam = new ArrayList<SqlParamEntity>();
for (String str : list){
SqlParamEntity sqlParamEntity = new SqlParamEntity();
sqlParamEntity.setParamType(SqlParamType.VARCHAR);
sqlParamEntity.setValue(str);
sqlparam.add(sqlParamEntity);
}
return sqlparam;
}
}