From 90663502f2520e8cfa7f6d8a887e46d2cf61be2f Mon Sep 17 00:00:00 2001
From: yanderong <307678080@qq.com>
Date: 星期五, 03 七月 2026 22:07:57 +0800
Subject: [PATCH] feat: 余额变更增加记录和查询
---
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/core/intercept/BalanceUpdateInterceptor.java | 81 +++++++++++++++++++++++++++++-----------
1 files changed, 58 insertions(+), 23 deletions(-)
diff --git a/UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/core/intercept/BalanceUpdateInterceptor.java b/UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/core/intercept/BalanceUpdateInterceptor.java
index 1aafcfa..9c135bc 100644
--- a/UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/core/intercept/BalanceUpdateInterceptor.java
+++ b/UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/core/intercept/BalanceUpdateInterceptor.java
@@ -1,19 +1,18 @@
package com.stylefeng.guns.core.intercept;
-import com.stylefeng.guns.modular.system.dao.TravelCardMapper;
import com.stylefeng.guns.modular.system.dao.UserInfoMapper;
import com.stylefeng.guns.modular.system.model.UserInfo;
import com.stylefeng.guns.modular.system.model.vo.BalanceModifyRecordVo;
-import org.apache.ibatis.javassist.bytecode.analysis.Executor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
-import org.apache.ibatis.plugin.Interceptor;
-import org.apache.ibatis.plugin.Intercepts;
-import org.apache.ibatis.plugin.Invocation;
-import org.apache.ibatis.plugin.Signature;
+import org.apache.ibatis.plugin.*;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
+import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.util.Map;
@@ -26,15 +25,17 @@
* @version 1.0.0
* @since 2026/7/3 12:03
*/
+@Slf4j
@Component
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class BalanceUpdateInterceptor implements Interceptor {
@Autowired
- private UserInfoMapper userInfoMapper;
+ private ApplicationContext context;
- @Autowired
- private TravelCardMapper travelCardMapper;
+ private UserInfoMapper getMapper() {
+ return context.getBean(UserInfoMapper.class);
+ }
@Override
public Object intercept(Invocation invocation) throws Throwable {
@@ -59,22 +60,22 @@
// 关键:先执行前查旧值(需要拿 userId)
Long userId = extractUserId(param);
// 查询旧的用户
- UserInfo oldUser = userInfoMapper.selectById(userId);
+ UserInfo oldUser = getMapper().selectById(userId);
// 执行更新
Object result = invocation.proceed();
// 查新的用户
- UserInfo newUser = userInfoMapper.selectById(userId);
+ UserInfo newUser = getMapper().selectById(userId);
// 新增余额的变更记录(所有余额的变更记录都需要存)
if(Objects.nonNull(oldUser) && Objects.nonNull(newUser)) {
- travelCardMapper.insertBalanceModifyRecord(BalanceModifyRecordVo.convert(oldUser, newUser));
+ getMapper().insertBalanceModifyRecord(BalanceModifyRecordVo.convert(oldUser, newUser));
}
return result;
}
@Override
public Object plugin(Object target) {
- return null;
+ return Plugin.wrap(target, this);
}
@Override
@@ -86,19 +87,53 @@
* 获取用户id
*/
private Long extractUserId(Object param) {
- // 兼容 Map / entity / wrapper
- if (param instanceof Map) {
- Object id = ((Map<?, ?>) param).get("id");
- return id == null ? null : Long.valueOf(id.toString());
- }
- try {
- Field f = param.getClass().getDeclaredField("id");
- f.setAccessible(true);
- return (Long) f.get(param);
- } catch (Exception e) {
+ if (param == null) {
return null;
}
+
+ // MyBatis ParamMap
+ if (param instanceof Map) {
+
+ Map<?, ?> map = (Map<?, ?>) param;
+
+ // 第一种:直接传id
+ try{
+ Object id = map.get("id");
+ if (id != null) {
+ return Long.valueOf(id.toString());
+ }
+ }
+ catch (Exception e){
+ log.error("sql中没有id");
+ }
+
+
+ // 第二种:MyBatis-Plus update(entity, wrapper)
+ Object entity = map.get("param1");
+ if (entity == null) {
+ entity = map.get("et");
+ }
+
+ if (entity != null) {
+ return extractId(entity);
+ }
+ }
+
+ // 直接就是实体
+ return extractId(param);
+ }
+
+ private Long extractId(Object obj) {
+ Field field = ReflectionUtils.findField(obj.getClass(), "id");
+ if (field == null) {
+ return null;
+ }
+
+ ReflectionUtils.makeAccessible(field);
+ Object value = ReflectionUtils.getField(field, obj);
+
+ return value == null ? null : Long.valueOf(value.toString());
}
}
--
Gitblit v1.7.1