CriteriaUtils.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.persagy.apm.dmpalarm.criteria;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Type;
  4. import java.sql.Timestamp;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Date;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11. import org.springframework.stereotype.Component;
  12. import com.alibaba.fastjson.JSONObject;
  13. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  14. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  15. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  16. import com.persagy.apm.dmpalarm.criteria.JsonCriteria.Order;
  17. import com.persagy.apm.dmpalarm.utils.UtilsTools;
  18. import com.persagy.apm.dmpalarm.web.PagedResponse;
  19. import cn.hutool.core.date.DateUtil;
  20. import lombok.extern.slf4j.Slf4j;
  21. @Slf4j
  22. @Component
  23. public class CriteriaUtils {
  24. /**
  25. * 批量拼接报警条件
  26. *
  27. * @param <T>
  28. *
  29. */
  30. @SuppressWarnings("unchecked")
  31. public <T> QueryWrapper<T> handleAlarmConditions(JsonCriteria jsonCriteria, Class<T> classes) {
  32. T t = null;
  33. try {
  34. t = classes.newInstance();
  35. } catch (InstantiationException | IllegalAccessException e1) {
  36. log.error(e1.getMessage(), e1);
  37. }
  38. try {
  39. QueryWrapper<T> queryWrapper = new QueryWrapper<>(t);
  40. JSONObject criteriaJson = jsonCriteria.getCriteria();
  41. Field[] fields = classes.getDeclaredFields();
  42. for (Field field : fields) {
  43. String fieldName = field.getName();
  44. Type type = field.getGenericType();
  45. Object columnValue = criteriaJson.get(fieldName);
  46. if (columnValue == null) {
  47. continue;
  48. }
  49. if (columnValue instanceof List) {
  50. List<Object> valueList = (List<Object>) columnValue;
  51. if(valueList.isEmpty()) {
  52. valueList.add(-1);
  53. }
  54. queryWrapper.in(UtilsTools.humpToLine(fieldName), valueList);
  55. } else if (columnValue instanceof Map) {
  56. Map<String, Object> columMap = (Map<String, Object>) columnValue;
  57. for (Entry<String, Object> columEntry : columMap.entrySet()) {
  58. Object value = columEntry.getValue();
  59. switch (columEntry.getKey()) {
  60. case "$ne":
  61. if (value instanceof List) {
  62. List<Object> valueList = (List<Object>) columnValue;
  63. queryWrapper.notIn(UtilsTools.humpToLine(fieldName), typeConversion(valueList, type.getTypeName()));
  64. } else {
  65. queryWrapper.ne(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  66. }
  67. break;
  68. case "$gt":
  69. queryWrapper.gt(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  70. break;
  71. case "$gte":
  72. queryWrapper.ge(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  73. break;
  74. case "$lt":
  75. queryWrapper.lt(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  76. break;
  77. case "$lte":
  78. queryWrapper.le(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  79. break;
  80. case "$in":
  81. if (value instanceof List) {
  82. List<Object> valueList = (List<Object>) value;
  83. if(valueList.isEmpty()) {
  84. valueList.add(-1);
  85. }
  86. queryWrapper.in(UtilsTools.humpToLine(fieldName), typeConversion(valueList, type.getTypeName()));
  87. } else {
  88. queryWrapper.eq(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  89. }
  90. break;
  91. case "$like":
  92. queryWrapper.like(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  93. break;
  94. case "$notlike":
  95. queryWrapper.notLike(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
  96. break;
  97. case "$null":
  98. Boolean b = (Boolean) value;
  99. if (b) {
  100. queryWrapper.isNull(UtilsTools.humpToLine(fieldName));
  101. } else {
  102. queryWrapper.isNotNull(UtilsTools.humpToLine(fieldName));
  103. }
  104. break;
  105. }
  106. }
  107. } else {
  108. queryWrapper.eq(UtilsTools.humpToLine(fieldName), typeConversion(columnValue, type.getTypeName()));
  109. }
  110. }
  111. for (Order order : jsonCriteria.getOrders()) {
  112. if (order.isAsc()) {
  113. queryWrapper.orderByAsc(UtilsTools.humpToLine(order.getColumn()));
  114. } else {
  115. queryWrapper.orderByDesc(UtilsTools.humpToLine(order.getColumn()));
  116. }
  117. }
  118. return queryWrapper;
  119. } catch (Exception e) {
  120. log.error("CriteriaUtils handleAlarmConditions error ", e);
  121. }
  122. return new QueryWrapper<T>(t);
  123. }
  124. public <T> PagedResponse<T> getPagedResponse(JsonCriteria criteria , Class<T> classes , QueryWrapper<T> queryWrapper , BaseMapper<T> baseMapper){
  125. PagedResponse<T> pagedResponse = new PagedResponse<>();
  126. if (criteria.isOnlyCount()) {
  127. Long count = baseMapper.selectCount(queryWrapper);
  128. pagedResponse.setCount(count);
  129. } else {
  130. Page<T> page = baseMapper.selectPage(new Page<>(criteria.getPage(), criteria.getSize()), queryWrapper);
  131. pagedResponse.setCount(page.getTotal());
  132. System.out.println(queryWrapper.getCustomSqlSegment());
  133. pagedResponse.setData(page.getRecords());
  134. }
  135. return pagedResponse;
  136. }
  137. /**
  138. * 类型转换
  139. *
  140. * @param obj
  141. * @return
  142. */
  143. private Object typeConversion(Object obj, String typeName) {
  144. Object resultObj = null;
  145. if (Date.class.getName().equals(typeName) || Timestamp.class.getName().equals(typeName)) {
  146. resultObj = DateUtil.parse(obj.toString());
  147. } else if (Integer.class.getName().equals(typeName)) {
  148. resultObj = Integer.valueOf(obj.toString());
  149. } else if (Long.class.getName().equals(typeName)) {
  150. resultObj = Long.valueOf(obj.toString());
  151. } else if (Double.class.getName().equals(typeName)) {
  152. resultObj = Double.valueOf(obj.toString());
  153. } else if (Float.class.getName().equals(typeName)) {
  154. resultObj = Float.valueOf(obj.toString());
  155. } else if (Short.class.getName().equals(typeName)) {
  156. resultObj = Short.valueOf(obj.toString());
  157. } else {
  158. resultObj = obj.toString();
  159. }
  160. return resultObj;
  161. }
  162. /**
  163. * 批量类型转换
  164. *
  165. * @return
  166. */
  167. private Collection<?> typeConversion(Collection<?> coll, String typeName) {
  168. List<Object> resultList = new ArrayList<>();
  169. for (Object object : coll) {
  170. resultList.add(this.typeConversion(object, typeName));
  171. }
  172. return resultList;
  173. }
  174. }