add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.dobbinsoft.fw.support.aspect;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dobbinsoft.fw.support.annotation.Query;
import com.dobbinsoft.fw.support.annotation.QueryCondition;
import com.dobbinsoft.fw.support.context.QueryContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
 
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
 
/**
 * ClassName: QueryWrapperAspect
 * Description: 构建查询条件切面
 *
 * @author: rize
 * @date: 2021-04-21
 */
@Aspect
@Component
public class QueryWrapperAspect {
 
    @Pointcut("@annotation(com.dobbinsoft.fw.support.annotation.Query)")
    public void wrapperPointCut() {
    }
 
    @Before("wrapperPointCut()")
    public void before(JoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Query query = method.getAnnotation(Query.class);
        Parameter[] parameters = method.getParameters();
        Object[] args = joinPoint.getArgs();
        QueryWrapper wrapper = new QueryWrapper();
        if (query.select() != null && query.select().length > 0) {
            wrapper.select(query.select());
        }
        for (int i = 0; i < parameters.length; i++) {
            if (!ObjectUtils.isEmpty(args[i])) {
                Parameter parameter = parameters[i];
                QueryCondition queryCondition = parameter.getAnnotation(QueryCondition.class);
                if (queryCondition != null) {
                    String field;
                    if (!"".equals(queryCondition.field())) {
                        field = queryCondition.field();
                    } else {
                        field = parameter.getName();
                    }
                    switch (queryCondition.condition()) {
                        case EQUAL:
                            wrapper.eq(field, args[i]);
                            break;
                        case LIKE:
                            wrapper.like(field, args[i]);
                            break;
                        case GT:
                            wrapper.gt(field, args[i]);
                            break;
                        case LT:
                            wrapper.lt(field, args[i]);
                            break;
                        case GE:
                            wrapper.ge(field, args[i]);
                            break;
                        case LE:
                            wrapper.le(field, args[i]);
                            break;
                    }
                }
            }
        }
        if (query.isAsc()) {
            wrapper.orderByAsc(query.sort());
        } else {
            wrapper.orderByDesc(query.sort());
        }
        QueryContext.set(wrapper);
    }
 
}