jinpengyong
2023-09-27 68e22957db996437fa20a9a4aa5ff37c54d4056f
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
package com.moral.api.utils;
 
import lombok.extern.slf4j.Slf4j;
 
import java.util.Map;
import java.util.Objects;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import com.moral.api.entity.UserLog;
import com.moral.api.service.UserLogService;
import com.moral.util.TokenUtils;
import com.moral.util.WebUtils;
 
@Slf4j
@Component
public class OperationLogUtils {
 
    @Autowired
    private UserLogService userLogService;
 
    @Transactional
    public void insertLog(String content, String type) {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        UserLog userLog = new UserLog();
        userLog.setType(type);
        userLog.setIp(WebUtils.getIpAddr(request));
        userLog.setOperateId((Integer) currentUserInfo.get("userId"));
        Map<String, Object> orgInfo = (Map<String, Object>) currentUserInfo.get("organization");
        userLog.setOrganizationId((Integer) orgInfo.get("id"));
        userLog.setContent(content);
        userLogService.save(userLog);
    }
}