晏有为
2024-05-16 25e4592719e488dd7438ed08b2cd86f9bd9fafa0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.yuanchu.mom.utils;
 
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.CustomLog;
import org.springframework.stereotype.Component;
 
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
 
/*
 * 李林
 * 生成SQL语句*/
 
@Component
@AllArgsConstructor
public class QueryWrappers<T> {
 
    public static <T> QueryWrapper<T> queryWrappers(T entity) {
        if (ObjectUtil.isEmpty(entity)) return null;
        Class<?> aClass = entity.getClass();
        QueryWrapper<T> wrapper = Wrappers.<T>query();
        List<Field> fieldList = new ArrayList<>();
        while (aClass != null) {
            fieldList.addAll(new ArrayList<>(Arrays.asList(aClass.getDeclaredFields())));
            aClass = aClass.getSuperclass();
        }
        for (Field field : fieldList) {
            field.setAccessible(true);
            Object value;
            try {
                value = field.get(entity);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException("查询条件生成错误");
            }
//            System.out.println(field.getName() + "|" + (value == null || value.equals("")) + "|" + value);
            if(value == null || value.equals("")){
                continue;
            }
            /*boolean bool = field.isAnnotationPresent(TableField.class);
            if (bool){
                if(field.getAnnotation(TableField.class).exist()==false)continue;
            }*/
            if (!field.getName().equals("orderBy")) {
                if(value.getClass()== LocalDateTime.class){
                    wrapper.like(StrUtil.toUnderlineCase(field.getName()), ((LocalDateTime) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                }else{
                    wrapper.like(StrUtil.toUnderlineCase(field.getName()), value);
                }
            } else {
                Map<String, String> map = (Map<String, String>) value;
                if(map.get("order")!=null){
                    wrapper.orderBy(true, map.get("order").equals("asc"), StrUtil.toUnderlineCase(map.get("field")));
                }
            }
        }
//        System.out.println(wrapper.getExpression().getSqlSegment());
        return wrapper;
    }
}