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());
|
}
|
}
|
}
|