|
@@ -31,12 +31,11 @@ import com.persagy.apm.dmpalarm.service.IShardingService;
|
|
|
import com.persagy.apm.dmpalarm.utils.DatePatternStyle;
|
|
|
import com.persagy.apm.dmpalarm.utils.UtilsTools;
|
|
|
import com.persagy.apm.dmpalarm.web.PagedResponse;
|
|
|
-import com.persagy.common.utils.StringUtil;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.core.text.CharSequenceUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
@@ -44,6 +43,8 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@Service
|
|
|
public class ShardingServiceImpl implements IShardingService {
|
|
|
|
|
|
+ private static final String ORDER_BY = "ORDER BY";
|
|
|
+
|
|
|
private static final String COUNT_SQL = "SELECT COUNT(*) total";
|
|
|
|
|
|
private static final String ALL_FIELD_SQL = "SELECT * ";
|
|
@@ -63,7 +64,7 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
endTimeInterger = Integer.valueOf(DateUtil.format(endTime, DatePatternStyle.PATTERN_YYYYMM));
|
|
|
}
|
|
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
|
|
|
- if (StrUtil.isBlank(tableInfo.getTableName())) {
|
|
|
+ if (CharSequenceUtil.isBlank(tableInfo.getTableName())) {
|
|
|
log.error("getTableNameShardingList tableNamePrefix is empty");
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
@@ -76,8 +77,7 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
List<ShardingEntity> tableList = this.getTableNameShardingList(cls, null, null);
|
|
|
String sql = this.shardingSplicingSQL(cls, queryWrapper, newParamValueMap, COUNT_SQL, tableList);
|
|
|
String queryCountSql = "SELECT SUM(ls.total) FROM ( " + sql + " )ls";
|
|
|
- Long count = shardingMapper.selectShardingCount(queryCountSql, newParamValueMap);
|
|
|
- return count;
|
|
|
+ return shardingMapper.selectShardingCount(queryCountSql, newParamValueMap);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -87,8 +87,7 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
List<ShardingEntity> tableList = this.getTableNameShardingList(cls, startTime, endTime);
|
|
|
String sql = this.shardingSplicingSQL(cls, queryWrapper, newParamValueMap, COUNT_SQL, tableList);
|
|
|
String queryCountSql = "SELECT total totalCount,tableName FROM ( " + sql + " ) ls";
|
|
|
- List<ShardingEntity> list = shardingMapper.selectShardingGroupTableCount(queryCountSql, newParamValueMap);
|
|
|
- return list;
|
|
|
+ return shardingMapper.selectShardingGroupTableCount(queryCountSql, newParamValueMap);
|
|
|
}
|
|
|
|
|
|
private <T> String shardingSplicingSQL(Class<T> cls, QueryWrapper<?> queryWrapper,
|
|
@@ -96,28 +95,26 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
List<String> tempQuerySqlList = new ArrayList<>();
|
|
|
String customSqlSegment = queryWrapper.getCustomSqlSegment();
|
|
|
String orderBySql = "";
|
|
|
- if (customSqlSegment.contains("ORDER BY")) {
|
|
|
- orderBySql = customSqlSegment.substring(customSqlSegment.lastIndexOf("ORDER BY"),
|
|
|
+ if (customSqlSegment.contains(ORDER_BY)) {
|
|
|
+ orderBySql = customSqlSegment.substring(customSqlSegment.lastIndexOf(ORDER_BY),
|
|
|
customSqlSegment.length());
|
|
|
}
|
|
|
Map<String, Object> oldParamValueMap = queryWrapper.getParamNameValuePairs();
|
|
|
for (int i = 0; i < tableNameList.size(); i++) {
|
|
|
ShardingEntity shardingEntity = tableNameList.get(i);
|
|
|
- int j = 0;
|
|
|
String sqlWhere = new String(customSqlSegment.getBytes()).replace(orderBySql, "");
|
|
|
for (Map.Entry<String, Object> entry : oldParamValueMap.entrySet()) {
|
|
|
String oldParamKey = Constants.WRAPPER + ".paramNameValuePairs." + entry.getKey();
|
|
|
- String newParamKey = "table@" + shardingEntity.getTableName() + "@" + Constants.WRAPPER_PARAM + j;
|
|
|
+ String newParamKey = "table@" + shardingEntity.getTableName() + "@" + entry.getKey();
|
|
|
sqlWhere = sqlWhere.replace(oldParamKey, Constants.WRAPPER_DOT + newParamKey);
|
|
|
newParamValueMap.put(newParamKey, entry.getValue());
|
|
|
- j++;
|
|
|
}
|
|
|
tempQuerySqlList.add(sqlPrefix + ",'" + shardingEntity.getTableName()
|
|
|
+ "' tableName,CONVERT(SUBSTRING_INDEX('" + shardingEntity.getTableName()
|
|
|
+ "','_',-1),SIGNED) suffix FROM " + shardingEntity.getTableName() + " " + sqlWhere);
|
|
|
}
|
|
|
if (tempQuerySqlList.size() > 1) {
|
|
|
- return StrUtil.join(" UNION ALL ", tempQuerySqlList);
|
|
|
+ return CharSequenceUtil.join(" UNION ALL ", tempQuerySqlList);
|
|
|
} else {
|
|
|
return tempQuerySqlList.get(0);
|
|
|
}
|
|
@@ -143,7 +140,7 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
column += tableFieldInfo.getColumn() + ",";
|
|
|
Class<? extends TypeHandler<?>> typeHandler = tableFieldInfo.getTypeHandler();
|
|
|
String typeHandlerName = "";
|
|
|
- if (null != typeHandler && StringUtil.isNotBlank(typeHandler.getName())) {
|
|
|
+ if (null != typeHandler && CharSequenceUtil.isNotBlank(typeHandler.getName())) {
|
|
|
typeHandlerName = ",typeHandler=" + typeHandler.getName();
|
|
|
}
|
|
|
value += "#{" + property + " " + typeHandlerName + "},";
|
|
@@ -172,7 +169,7 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
if (entry.getKey().equals(property)) {
|
|
|
Class<? extends TypeHandler<?>> typeHandler = tableFieldInfo.getTypeHandler();
|
|
|
String typeHandlerName = "";
|
|
|
- if (null != typeHandler && StringUtil.isNotBlank(typeHandler.getName())) {
|
|
|
+ if (null != typeHandler && CharSequenceUtil.isNotBlank(typeHandler.getName())) {
|
|
|
typeHandlerName = ",typeHandler=" + typeHandler.getName();
|
|
|
}
|
|
|
String value = "#{" + property + " " + typeHandlerName + "},";
|
|
@@ -304,8 +301,8 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
String sql = this.shardingSplicingSQL(cls, queryWrapper, newParamValueMap, ALL_FIELD_SQL, tableList);
|
|
|
sql = ALL_FIELD_SQL + " FROM (" + sql + ") ls";
|
|
|
String customSqlSegment = queryWrapper.getCustomSqlSegment();
|
|
|
- if (customSqlSegment.contains("ORDER BY")) {
|
|
|
- String orderBySql = customSqlSegment.substring(customSqlSegment.lastIndexOf("ORDER BY"),
|
|
|
+ if (customSqlSegment.contains(ORDER_BY)) {
|
|
|
+ String orderBySql = customSqlSegment.substring(customSqlSegment.lastIndexOf(ORDER_BY),
|
|
|
customSqlSegment.length());
|
|
|
sql += " " + orderBySql;
|
|
|
}
|