干燥机配套车间生产管理系统/云平台服务端
bsw215583320
2024-04-16 c2fccb01b972176dc3da5a497b5e904025e9e98d
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
package org.jeecg.modules.aop;
 
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.jeecg.common.api.dto.LogDTO;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.base.service.BaseCommonService;
import org.jeecg.modules.system.entity.SysTenantPack;
import org.jeecg.modules.system.entity.SysTenantPackUser;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Date;
 
/**
 * @Author taoYan
 * @Date 2023/2/16 14:27
 **/
@Aspect
@Component
public class TenantPackUserLogAspect {
 
    @Resource
    private BaseCommonService baseCommonService;
 
    @Pointcut("@annotation(org.jeecg.modules.aop.TenantLog)")
    public void tenantLogPointCut() {
 
    }
 
    @Around("tenantLogPointCut()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint)throws Throwable {
        //System.out.println("环绕通知>>>>>>>>>");
 
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        TenantLog log = method.getAnnotation(TenantLog.class);
        if(log != null){
            int opType = log.value();
            Integer logType = null;
            String content = null;
            Integer tenantId = null;
            //获取参数
            Object[] args = joinPoint.getArgs();
            if(args.length>0){
                for(Object obj: args){
                    if(obj instanceof SysTenantPack){
                        // logType=3 租户操作日志
                        logType = 3;
                        SysTenantPack pack = (SysTenantPack)obj;
                        if(opType==2){
                            content = "创建了角色权限 "+ pack.getPackName();
                        }
                        tenantId = pack.getTenantId();
                        break;
                    }else if(obj instanceof SysTenantPackUser){
                        logType = 3;
                        SysTenantPackUser packUser = (SysTenantPackUser)obj;
                        if(opType==2){
                            content = "将 "+packUser.getRealname()+" 添加到角色 "+ packUser.getPackName();
                        }else if(opType==4){
                            content = "移除了 "+packUser.getPackName()+" 成员 "+ packUser.getRealname();
                        }
                        tenantId = packUser.getTenantId();
                    }
                } 
            }
            if(logType!=null){
                LogDTO dto = new LogDTO();
                dto.setLogType(logType);
                dto.setLogContent(content);
                dto.setOperateType(opType);
                dto.setTenantId(tenantId);
                //获取登录用户信息
                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                if(sysUser!=null){
                    dto.setUserid(sysUser.getUsername());
                    dto.setUsername(sysUser.getRealname());
 
                }
                dto.setCreateTime(new Date());
                //保存系统日志
                baseCommonService.addLog(dto);
            }
        }
        return joinPoint.proceed();
    }
 
    @AfterThrowing("tenantLogPointCut()")
    public void afterThrowing()throws Throwable{
        System.out.println("异常通知");
    }
}