| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package com.persagy.apm.dmpalarm.criteria;
- import java.lang.reflect.Field;
- import java.lang.reflect.Type;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import org.springframework.stereotype.Component;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.persagy.apm.dmpalarm.criteria.JsonCriteria.Order;
- import com.persagy.apm.dmpalarm.utils.UtilsTools;
- import com.persagy.apm.dmpalarm.web.PagedResponse;
- import cn.hutool.core.date.DateUtil;
- import lombok.extern.slf4j.Slf4j;
- @Slf4j
- @Component
- public class CriteriaUtils {
- /**
- * 批量拼接报警条件
- *
- * @param <T>
- *
- */
- @SuppressWarnings("unchecked")
- public <T> QueryWrapper<T> handleAlarmConditions(JsonCriteria jsonCriteria, Class<T> classes) {
- T t = null;
- try {
- t = classes.newInstance();
- } catch (InstantiationException | IllegalAccessException e1) {
- log.error(e1.getMessage(), e1);
- }
- try {
- QueryWrapper<T> queryWrapper = new QueryWrapper<>(t);
- JSONObject criteriaJson = jsonCriteria.getCriteria();
- Field[] fields = classes.getDeclaredFields();
- for (Field field : fields) {
- String fieldName = field.getName();
- Type type = field.getGenericType();
- Object columnValue = criteriaJson.get(fieldName);
- if (columnValue == null) {
- continue;
- }
- if (columnValue instanceof List) {
- List<Object> valueList = (List<Object>) columnValue;
- if(valueList.isEmpty()) {
- valueList.add(-1);
- }
- queryWrapper.in(UtilsTools.humpToLine(fieldName), valueList);
- } else if (columnValue instanceof Map) {
- Map<String, Object> columMap = (Map<String, Object>) columnValue;
- for (Entry<String, Object> columEntry : columMap.entrySet()) {
- Object value = columEntry.getValue();
- switch (columEntry.getKey()) {
- case "$ne":
- if (value instanceof List) {
- List<Object> valueList = (List<Object>) columnValue;
- queryWrapper.notIn(UtilsTools.humpToLine(fieldName), typeConversion(valueList, type.getTypeName()));
- } else {
- queryWrapper.ne(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- }
- break;
- case "$gt":
- queryWrapper.gt(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- break;
- case "$gte":
- queryWrapper.ge(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- break;
- case "$lt":
- queryWrapper.lt(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- break;
- case "$lte":
- queryWrapper.le(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- break;
- case "$in":
- if (value instanceof List) {
- List<Object> valueList = (List<Object>) value;
- if(valueList.isEmpty()) {
- valueList.add(-1);
- }
- queryWrapper.in(UtilsTools.humpToLine(fieldName), typeConversion(valueList, type.getTypeName()));
- } else {
- queryWrapper.eq(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- }
- break;
- case "$like":
- queryWrapper.like(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- break;
- case "$notlike":
- queryWrapper.notLike(UtilsTools.humpToLine(fieldName), typeConversion(value, type.getTypeName()));
- break;
- case "$null":
- Boolean b = (Boolean) value;
- if (b) {
- queryWrapper.isNull(UtilsTools.humpToLine(fieldName));
- } else {
- queryWrapper.isNotNull(UtilsTools.humpToLine(fieldName));
- }
- break;
- }
- }
- } else {
- queryWrapper.eq(UtilsTools.humpToLine(fieldName), typeConversion(columnValue, type.getTypeName()));
- }
- }
- for (Order order : jsonCriteria.getOrders()) {
- if (order.isAsc()) {
- queryWrapper.orderByAsc(UtilsTools.humpToLine(order.getColumn()));
- } else {
- queryWrapper.orderByDesc(UtilsTools.humpToLine(order.getColumn()));
- }
- }
- return queryWrapper;
- } catch (Exception e) {
- log.error("CriteriaUtils handleAlarmConditions error ", e);
- }
- return new QueryWrapper<T>(t);
- }
- public <T> PagedResponse<T> getPagedResponse(JsonCriteria criteria , Class<T> classes , QueryWrapper<T> queryWrapper , BaseMapper<T> baseMapper){
- PagedResponse<T> pagedResponse = new PagedResponse<>();
- if (criteria.isOnlyCount()) {
- Long count = baseMapper.selectCount(queryWrapper);
- pagedResponse.setCount(count);
- } else {
- Page<T> page = baseMapper.selectPage(new Page<>(criteria.getPage(), criteria.getSize()), queryWrapper);
- pagedResponse.setCount(page.getTotal());
- System.out.println(queryWrapper.getCustomSqlSegment());
- pagedResponse.setData(page.getRecords());
- }
- return pagedResponse;
- }
- /**
- * 类型转换
- *
- * @param obj
- * @return
- */
- private Object typeConversion(Object obj, String typeName) {
- Object resultObj = null;
- if (Date.class.getName().equals(typeName) || Timestamp.class.getName().equals(typeName)) {
- resultObj = DateUtil.parse(obj.toString());
- } else if (Integer.class.getName().equals(typeName)) {
- resultObj = Integer.valueOf(obj.toString());
- } else if (Long.class.getName().equals(typeName)) {
- resultObj = Long.valueOf(obj.toString());
- } else if (Double.class.getName().equals(typeName)) {
- resultObj = Double.valueOf(obj.toString());
- } else if (Float.class.getName().equals(typeName)) {
- resultObj = Float.valueOf(obj.toString());
- } else if (Short.class.getName().equals(typeName)) {
- resultObj = Short.valueOf(obj.toString());
- } else {
- resultObj = obj.toString();
- }
- return resultObj;
- }
- /**
- * 批量类型转换
- *
- * @return
- */
- private Collection<?> typeConversion(Collection<?> coll, String typeName) {
- List<Object> resultList = new ArrayList<>();
- for (Object object : coll) {
- resultList.add(this.typeConversion(object, typeName));
- }
- return resultList;
- }
- }
|