|
@@ -1,15 +1,10 @@
|
|
|
package com.persagy.apm.dmpalarm.service.impl;
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.Map.Entry;
|
|
|
-import java.util.Objects;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.*;
|
|
|
import org.apache.ibatis.type.TypeHandler;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -21,10 +16,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Assert;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.Constants;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import com.persagy.apm.dmpalarm.dao.ShardingMapper;
|
|
|
import com.persagy.apm.dmpalarm.model.ShardingEntity;
|
|
|
import com.persagy.apm.dmpalarm.service.IShardingService;
|
|
@@ -49,6 +40,9 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
|
|
|
private static final String ALL_FIELD_SQL = "SELECT * ";
|
|
|
|
|
|
+ // 自定义查询的列
|
|
|
+ private static final String CUSTOM_FIELD_SQL = "SELECT CUSTOM_FIELD ";
|
|
|
+
|
|
|
public static final String SHARDING_COLUMN = "trigger_time";
|
|
|
|
|
|
private static final String ASC = "asc";
|
|
@@ -140,6 +134,109 @@ public class ShardingServiceImpl implements IShardingService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public <T> List<T> selectShardingCustomGroupByPageList(Class<T> cls, Set<String> columns, Set<String> groupByColumns, QueryWrapper<T> queryWrapper, Integer pageNo,
|
|
|
+ Integer pageSize, Date startTime, Date endTime) {
|
|
|
+ List<T> resultList = new ArrayList<>();
|
|
|
+ List<ShardingEntity> tableList = this.selectShardingGroupTableCount(cls, queryWrapper, startTime, endTime);
|
|
|
+
|
|
|
+ for (int i = 0; i < tableList.size(); i++) {
|
|
|
+ resultList.addAll(queryCustomGroupBy(cls,columns,groupByColumns, queryWrapper,
|
|
|
+ this.getShardingEntityList(tableList.get(i))));
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param customGroupByColumns 自定义分组的列
|
|
|
+ * @param queryWrapper 查询条件
|
|
|
+ * @param newParamValueMap 参数
|
|
|
+ * @param sqlPrefix SQL前缀
|
|
|
+ * @param tableNameList 表名
|
|
|
+ * @param <T> 对象
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ private <T> String shardingSplicingGroupBySQL(String customGroupByColumns, QueryWrapper<?> queryWrapper,
|
|
|
+ Map<String, Object> newParamValueMap, String sqlPrefix, List<ShardingEntity> tableNameList) {
|
|
|
+ List<String> tempQuerySqlList = new ArrayList<>();
|
|
|
+ String customSqlSegment = queryWrapper.getCustomSqlSegment();
|
|
|
+ String orderBySql = "";
|
|
|
+ if (customSqlSegment.contains(ORDER_BY)) {
|
|
|
+ orderBySql = customSqlSegment.substring(customSqlSegment.lastIndexOf(ORDER_BY));
|
|
|
+ }
|
|
|
+ if (orderBySql.contains(SHARDING_COLUMN)) {
|
|
|
+ String[] orderBys = orderBySql.split(",");
|
|
|
+ for (String string : orderBys) {
|
|
|
+ if(!string.contains(SHARDING_COLUMN)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ int lastIndex = string.lastIndexOf(",");
|
|
|
+ lastIndex=lastIndex == -1?string.length():lastIndex;
|
|
|
+ int index = string.indexOf(SHARDING_COLUMN);
|
|
|
+ String orderByString = string.substring(index, lastIndex);
|
|
|
+ if (orderByString.toUpperCase().contains(ASC.toUpperCase()) || !orderByString.toUpperCase().contains(DESC.toUpperCase())) {
|
|
|
+ Collections.reverse(tableNameList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Map<String, Object> oldParamValueMap = queryWrapper.getParamNameValuePairs();
|
|
|
+ for (int i = 0; i < tableNameList.size(); i++) {
|
|
|
+ ShardingEntity shardingEntity = tableNameList.get(i);
|
|
|
+ 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() + "@" + entry.getKey();
|
|
|
+ sqlWhere = sqlWhere.replace(oldParamKey, Constants.WRAPPER_DOT + newParamKey);
|
|
|
+ newParamValueMap.put(newParamKey, entry.getValue());
|
|
|
+ }
|
|
|
+ // 分组字段不为空,则组装分组SQL
|
|
|
+ if (StringUtils.isNotBlank(customGroupByColumns)) {
|
|
|
+ sqlWhere += " GROUP BY " + customGroupByColumns;
|
|
|
+ }
|
|
|
+ tempQuerySqlList.add(sqlPrefix + ",'" + shardingEntity.getTableName()
|
|
|
+ + "' tableName,CONVERT(SUBSTRING_INDEX('" + shardingEntity.getTableName()
|
|
|
+ + "','_',-1),SIGNED) suffix FROM " + shardingEntity.getTableName() + " " + sqlWhere);
|
|
|
+ }
|
|
|
+ if (tempQuerySqlList.size() > 1) {
|
|
|
+ return CharSequenceUtil.join(" UNION ALL ", tempQuerySqlList);
|
|
|
+ } else {
|
|
|
+ return tempQuerySqlList.get(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义分组统计查询
|
|
|
+ * @param cls 实体
|
|
|
+ * @param columns 查询的列
|
|
|
+ * @param groupByColumns 分组的列
|
|
|
+ * @param queryWrapper 查询条件
|
|
|
+ * @param tableList 表名集合
|
|
|
+ * @param <T> 对象
|
|
|
+ * @return List<T>
|
|
|
+ */
|
|
|
+ private <T> List<T> queryCustomGroupBy(Class<T> cls, Set<String> columns, Set<String> groupByColumns, QueryWrapper<T> queryWrapper, List<ShardingEntity> tableList) {
|
|
|
+ if (CollectionUtils.isEmpty(columns)){
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ Map<String, Object> newParamValueMap = new HashMap<>();
|
|
|
+
|
|
|
+ // 替换为传参的列
|
|
|
+ String selectFieldSql=CUSTOM_FIELD_SQL.replace("CUSTOM_FIELD",CharSequenceUtil.join(", ", columns));
|
|
|
+ // 拼接分组查询的SQL
|
|
|
+ String sql = this.shardingSplicingGroupBySQL(CharSequenceUtil.join(", ",groupByColumns), queryWrapper, newParamValueMap, selectFieldSql, tableList);
|
|
|
+ // 拼接为完成的查询SQL
|
|
|
+ sql = ALL_FIELD_SQL + " FROM (" + sql + ") ls";
|
|
|
+
|
|
|
+ List<Map<String, Object>> list = shardingMapper.selectShardingListMap(sql, newParamValueMap);
|
|
|
+ if (CollectionUtils.isEmpty(list)){
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 组装查询结果
|
|
|
+ return handleResultList(cls, list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public void save(Object obj, Date time) {
|
|
|
TableInfo tableInfo = TableInfoHelper.getTableInfo(obj.getClass());
|
|
|
String suffix = DateUtil.format(time, DatePatternStyle.PATTERN_YYYYMM);
|