|
@@ -0,0 +1,112 @@
|
|
|
+package cn.iocoder.yudao.module.trade.framework.order.core.aop;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
|
|
+import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderLogDO;
|
|
|
+import cn.iocoder.yudao.module.trade.framework.order.core.annotations.TradeOrderLog;
|
|
|
+import cn.iocoder.yudao.module.trade.service.order.TradeOrderLogService;
|
|
|
+import cn.iocoder.yudao.module.trade.service.order.bo.logger.TradeOrderLogCreateReqBO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.aspectj.lang.JoinPoint;
|
|
|
+import org.aspectj.lang.annotation.AfterReturning;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
|
|
+import static java.util.Collections.emptyMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 交易订单的操作日志的记录 AOP 切面
|
|
|
+ *
|
|
|
+ * @author 陈賝
|
|
|
+ * @since 2023/6/13 13:54
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Aspect
|
|
|
+@Slf4j
|
|
|
+public class TradeOrderLogAspect {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 订单编号
|
|
|
+ */
|
|
|
+ private static final ThreadLocal<Long> ORDER_ID = new ThreadLocal<>();
|
|
|
+ /**
|
|
|
+ * 操作前的状态
|
|
|
+ */
|
|
|
+ private static final ThreadLocal<Integer> BEFORE_STATUS = new ThreadLocal<>();
|
|
|
+ /**
|
|
|
+ * 操作后的状态
|
|
|
+ */
|
|
|
+ private static final ThreadLocal<Integer> AFTER_STATUS = new ThreadLocal<>();
|
|
|
+ /**
|
|
|
+ * 拓展参数 Map,用于格式化操作内容
|
|
|
+ */
|
|
|
+ private static final ThreadLocal<Map<String, Object>> EXTS = new ThreadLocal<>();
|
|
|
+
|
|
|
+ public TradeOrderLogAspect() {
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private TradeOrderLogService orderLogService;
|
|
|
+
|
|
|
+ @AfterReturning("@annotation(orderLog)")
|
|
|
+ public void doAfterReturning(JoinPoint joinPoint, TradeOrderLog orderLog) {
|
|
|
+ try {
|
|
|
+ // 1.1 操作用户
|
|
|
+ Integer userType = getUserType();
|
|
|
+ Long userId = getUserId();
|
|
|
+ // 1.2 订单信息
|
|
|
+ Long orderId = ORDER_ID.get();
|
|
|
+ Integer beforeStatus = BEFORE_STATUS.get();
|
|
|
+ Integer afterStatus = AFTER_STATUS.get();
|
|
|
+ Map<String, Object> exts = ObjectUtil.defaultIfNull(EXTS.get(), emptyMap());
|
|
|
+ String content = StrUtil.format(orderLog.operateType().getContent(), exts);
|
|
|
+
|
|
|
+ // 2.1 记录日志
|
|
|
+ TradeOrderLogCreateReqBO createBO = new TradeOrderLogCreateReqBO()
|
|
|
+ .setUserId(userId).setUserType(userType)
|
|
|
+ .setOrderId(orderId).setBeforeStatus(beforeStatus).setAfterStatus(afterStatus)
|
|
|
+ .setOperateType(orderLog.operateType().getType()).setContent(content);
|
|
|
+ orderLogService.createOrderLog(createBO);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ // todo 芋艿:清理上下文
|
|
|
+ log.error("[doAfterReturning][orderLog({}) 订单日志错误]", toJsonString(orderLog), ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得用户类型
|
|
|
+ *
|
|
|
+ * 如果没有,则约定为 {@link TradeOrderLogDO#getUserType()} 系统
|
|
|
+ *
|
|
|
+ * @return 用户类型
|
|
|
+ */
|
|
|
+ private static Integer getUserType() {
|
|
|
+ return ObjectUtil.defaultIfNull(WebFrameworkUtils.getLoginUserType(), TradeOrderLogDO.USER_TYPE_SYSTEM);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得用户编号
|
|
|
+ *
|
|
|
+ * 如果没有,则约定为 {@link TradeOrderLogDO#getUserId()} 系统
|
|
|
+ *
|
|
|
+ * @return 用户类型
|
|
|
+ */
|
|
|
+ private static Long getUserId() {
|
|
|
+ return ObjectUtil.defaultIfNull(WebFrameworkUtils.getLoginUserId(), TradeOrderLogDO.USER_ID_SYSTEM);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setOrderInfo(Long id, Integer beforeStatus, Integer afterStatus, Map<String, Object> exts) {
|
|
|
+ ORDER_ID.set(id);
|
|
|
+ BEFORE_STATUS.set(beforeStatus);
|
|
|
+ AFTER_STATUS.set(afterStatus);
|
|
|
+ EXTS.set(exts);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|