liusheng
3 天以前 f99cfc5b3f40104749b0eff219b19dae26c1aaab
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.ruoyi.common.aspectj;
 
import com.ruoyi.common.annotation.AddOrgId;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Field;
import java.lang.reflect.Method;
 
/**
 * 设置机构ID
 *
 * @author ls
 */
@Aspect
@Component
public class SetOrgIdAspect
{
    private static final Logger log = LoggerFactory.getLogger(SetOrgIdAspect.class);
 
    @Before("@annotation(setOrgId)")
    public void setOrgId(JoinPoint joinPoint, AddOrgId setOrgId) throws Throwable
    {
        if (setOrgId != null)
        {
            // 获取当前登录用户
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser != null && loginUser.getUser() != null)
            {
                String orgid = loginUser.getUser().getOrgid();
                String campusid = loginUser.getUser().getCampusid();
                
                // 获取方法参数
                Object[] args = joinPoint.getArgs();
                int paramIndex = setOrgId.paramIndex();
                if (paramIndex >= 0 && paramIndex < args.length)
                {
                    Object param = args[paramIndex];
                    if (param != null)
                    {
                        // 设置orgid
                        if (orgid != null)
                        {
                            // 获取字段名称
                            String fieldName = setOrgId.field();
 
                            // 检查字段是否已经有值,如果有且skipIfPresent为true,则跳过设置
                            Object currentValue = getFieldValue(param, fieldName);
                            if (setOrgId.skipIfPresent() && currentValue != null && !currentValue.toString().isEmpty()) {
                                // 如果字段已经有值且skipIfPresent为true,则跳过设置
                                log.debug("字段 {} 已有值: {},跳过设置", fieldName, currentValue);
                            } else {
                                setFieldValue(param, fieldName, orgid);
                            }
                        }
                        
                        // 如果需要同时设置campusid
                        if (setOrgId.withCampusId() && campusid != null)
                        {
                            String campusFieldName = setOrgId.campusField();
                            
                            // 检查字段是否已经有值,如果有且skipIfPresent为true,则跳过设置
                            Object currentValueCampus = getFieldValue(param, campusFieldName);
                            if (setOrgId.skipIfPresent() && currentValueCampus != null && !currentValueCampus.toString().isEmpty()) {
                                // 如果字段已经有值且skipIfPresent为true,则跳过设置
                                log.debug("字段 {} 已有值: {},跳过设置", campusFieldName, currentValueCampus);
                            } else {
                                setFieldValue(param, campusFieldName, campusid);
                            }
                        }
                    }
                }
            }
        }
    }
 
    /**
     * 获取注解
     */
    private AddOrgId getAnnotationLog(JoinPoint joinPoint) throws Exception
    {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        if (method != null)
        {
            return method.getAnnotation(AddOrgId.class);
        }
        return null;
    }
 
    /**
     * 获取对象字段的值
     */
    private Object getFieldValue(Object obj, String fieldName) throws Exception
    {
        Class<?> clazz = obj.getClass();
        Field field = null;
 
        // 查找字段,包括父类中的字段
        while (clazz != null && field == null)
        {
            try
            {
                field = clazz.getDeclaredField(fieldName);
            }
            catch (NoSuchFieldException e)
            {
                clazz = clazz.getSuperclass();
            }
        }
 
        if (field != null)
        {
            field.setAccessible(true);
            return field.get(obj);
        }
 
        return null;
    }
 
    /**
     * 设置对象字段的值
     */
    private void setFieldValue(Object obj, String fieldName, Object value) throws Exception
    {
        Class<?> clazz = obj.getClass();
        Field field = null;
 
        // 查找字段,包括父类中的字段
        while (clazz != null && field == null)
        {
            try
            {
                field = clazz.getDeclaredField(fieldName);
            }
            catch (NoSuchFieldException e)
            {
                clazz = clazz.getSuperclass();
            }
        }
 
        if (field != null)
        {
            field.setAccessible(true);
            field.set(obj, value);
        }
        else
        {
            log.warn("未找到字段: {} 在类: {} 及其父类中", fieldName, obj.getClass().getName());
        }
    }
}