zxl
2025-06-11 05e49980cd556d24239e00d028dc5efa25e0de14
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
package cn.lili.modules.order.aftersale.aop;
 
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.UserEnums;
import cn.lili.common.utils.SpelUtil;
import cn.lili.common.utils.ThreadPoolUtil;
import cn.lili.modules.order.aftersale.entity.dos.AfterSaleLog;
import cn.lili.modules.order.aftersale.service.AfterSaleLogService;
import cn.lili.modules.order.trade.entity.enums.AfterSaleStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 订单操作日志
 *
 * @author Chopper
 * @since 2020/11/17 7:22 下午
 */
@Slf4j
@Aspect
@Component
public class AfterSaleOperationLogAspect {
 
    @Autowired
    private AfterSaleLogService afterSaleLogService;
 
    @AfterReturning(returning = "rvt", pointcut = "@annotation(cn.lili.modules.order.aftersale.aop.AfterSaleLogPoint)")
    public void afterReturning(JoinPoint joinPoint, Object rvt) {
        try {
            AuthUser auth = UserContext.getCurrentUser();
            //日志对象拼接
            //默认操作人员,系统操作
            String userName = "系统操作", id = "-1", role = UserEnums.SYSTEM.getRole();
            if (auth != null) {
                //日志对象拼接
                userName = UserContext.getCurrentUser().getUsername();
                id = UserContext.getCurrentUser().getId();
                role = UserContext.getCurrentUser().getRole().getRole();
            }
            Map<String, String> afterSaleLogPoints = spelFormat(joinPoint, rvt);
            AfterSaleLog afterSaleLog = new AfterSaleLog(afterSaleLogPoints.get("sn"), id, role, userName, afterSaleLogPoints.get("description"));
            //调用线程保存
            ThreadPoolUtil.getPool().execute(new SaveAfterSaleLogThread(afterSaleLog, afterSaleLogService));
        } catch (Exception e) {
            log.error("售后日志错误",e);
        }
    }
 
    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param joinPoint 切点
     * @return 方法描述
     */
    public static Map<String, String> spelFormat(JoinPoint joinPoint, Object rvt) {
 
        Map<String, String> result = new HashMap<>(2);
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AfterSaleLogPoint afterSaleLogPoint = signature.getMethod().getAnnotation(AfterSaleLogPoint.class);
        String description = SpelUtil.compileParams(joinPoint, rvt, afterSaleLogPoint.description());
        String sn = SpelUtil.compileParams(joinPoint, rvt, afterSaleLogPoint.sn());
        if (CharSequenceUtil.isNotEmpty(afterSaleLogPoint.serviceStatus())) {
            description += AfterSaleStatusEnum.valueOf(SpelUtil.compileParams(joinPoint, rvt, afterSaleLogPoint.serviceStatus())).description();
        }
        result.put("description", description);
        result.put("sn", sn);
        return result;
 
    }
 
    /**
     * 保存日志
     */
    private static class SaveAfterSaleLogThread implements Runnable {
 
        private final AfterSaleLog afterSaleLog;
        private final AfterSaleLogService afterSaleLogService;
 
        public SaveAfterSaleLogThread(AfterSaleLog afterSaleLog, AfterSaleLogService afterSaleLogService) {
            this.afterSaleLog = afterSaleLog;
            this.afterSaleLogService = afterSaleLogService;
        }
 
        @Override
        public void run() {
            afterSaleLogService.save(afterSaleLog);
        }
    }
 
}