月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-11 fee67ca8a0760315047a52fc4101a8f4f80b7a7f
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
package com.moon.server.aspect;
 
import com.moon.server.annotation.SysLog;
import com.moon.server.entity.all.StaticData;
import com.moon.server.entity.sys.OperateEntity;
import com.moon.server.entity.sys.UserEntity;
import com.moon.server.helper.AsyncHelper;
import com.moon.server.helper.WebHelper;
import com.moon.server.service.sys.OperateService;
import com.moon.server.service.sys.TokenService;
import io.swagger.annotations.Api;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.TimerTask;
 
@Aspect
@Component
@SuppressWarnings("ALL")
public class LogAspect {
    @Autowired
    private TokenService tokenService;
 
    @Autowired
    private OperateService operateService;
 
    private final static String SELECT = "select";
 
    private final static String INSERT = "insert";
 
    private final static String UPDATE = "update";
 
    private final static String DELETE = "delete";
 
    private final static String UPLOAD = "upload";
 
    private final static String DOWNLOAD = "download";
 
    private final static String COUNT = "count";
 
    private static final Log log = LogFactory.getLog(LogAspect.class);
 
    @Pointcut("@annotation(com.moon.server.annotation.SysLog)")
    public void logPointCut() {
        //
    }
 
    /**
     * @Around,@Before,@After
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
 
        Object result = point.proceed();
 
        long time = System.currentTimeMillis() - beginTime;
 
        saveLog(point, time);
 
        return result;
    }
 
    private void saveLog(ProceedingJoinPoint joinPoint, long time) {
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Class<?> clazz = joinPoint.getTarget().getClass();
            Method method = signature.getMethod();
 
            HttpServletRequest req = WebHelper.getRequest();
            String url = req.getServletPath();
 
            OperateEntity oe = new OperateEntity();
            oe.setIp(WebHelper.getIpAddress(req));
            oe.setUrl(url);
            oe.setExec(time);
 
            Api api = clazz.getAnnotation(Api.class);
            if (api != null) {
                String[] strs = api.tags()[0].split(StaticData.BACKSLASH);
                oe.setModular1(strs[0]);
                oe.setModular2(strs[1]);
            }
 
            oe.setClazz(clazz.getName() + "." + method.getName());
            oe.setType(getType(method.getName()));
 
            SysLog sysLog = method.getAnnotation(SysLog.class);
            if (sysLog != null) {
                oe.setBak(sysLog.value());
            }
 
            UserEntity ue = tokenService.getCurrentUser(req);
            if (ue != null) {
                oe.setUserid(ue.getId());
            }
 
            // operateService.insertOperate(oe)
            AsyncHelper helper = new AsyncHelper();
            helper.execute(new TimerTask() {
                @Override
                public void run() {
                    operateService.insertOperate(oe);
                }
            });
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    private static int getType(String methodName) {
        if (methodName.contains(SELECT)) {
            return 1;
        }
        if (methodName.contains(INSERT)) {
            return 2;
        }
        if (methodName.contains(UPDATE)) {
            return 3;
        }
        if (methodName.contains(DELETE)) {
            return 4;
        }
        if (methodName.contains(UPLOAD)) {
            return 5;
        }
        if (methodName.contains(DOWNLOAD)) {
            return 6;
        }
        // methodName.indexOf(COUNT) > -1
        if (methodName.contains(COUNT)) {
            return 7;
        }
 
        return 0;
    }
}