yanderong
2026-07-03 90663502f2520e8cfa7f6d8a887e46d2cf61be2f
feat: 余额变更增加记录和查询
1个文件已添加
8个文件已修改
129 ■■■■■ 已修改文件
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/config/datasource/MultiDataSourceConfig.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/config/datasource/SingleDataSourceConfig.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/config/web/MybatisConfig.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/core/intercept/BalanceUpdateInterceptor.java 81 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/TravelCardMapper.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/UserInfoMapper.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/mapping/TravelCard.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/mapping/UserInfoMapper.xml 4 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/TravelCardServiceImpl.java 补丁 | 查看 | 原始文档 | blame | 历史
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/config/datasource/MultiDataSourceConfig.java
@@ -6,10 +6,8 @@
import com.stylefeng.guns.core.config.properties.DruidProperties;
import com.stylefeng.guns.core.config.properties.MutiDataSourceProperties;
import com.stylefeng.guns.core.datascope.DataScopeInterceptor;
import com.stylefeng.guns.core.intercept.BalanceUpdateInterceptor;
import com.stylefeng.guns.core.mutidatasource.DynamicDataSource;
import com.stylefeng.guns.core.mutidatasource.aop.MultiSourceExAop;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -125,8 +123,4 @@
        return new DataSourceTransactionManager(mutiDataSource);
    }
    @Bean
    public Interceptor balanceUpdateInterceptor() {
        return new BalanceUpdateInterceptor();
    }
}
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/config/datasource/SingleDataSourceConfig.java
@@ -5,8 +5,6 @@
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.stylefeng.guns.core.config.properties.DruidProperties;
import com.stylefeng.guns.core.datascope.DataScopeInterceptor;
import com.stylefeng.guns.core.intercept.BalanceUpdateInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
@@ -59,9 +57,5 @@
        return new OptimisticLockerInterceptor();
    }
    @Bean
    public Interceptor balanceUpdateInterceptor() {
        return new BalanceUpdateInterceptor();
    }
}
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/config/web/MybatisConfig.java
New file
@@ -0,0 +1,21 @@
package com.stylefeng.guns.config.web;
import com.stylefeng.guns.core.intercept.BalanceUpdateInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author Lux
 * @version 1.0.0
 * @since 2026/7/3 14:33
 */
@Configuration
public class MybatisConfig {
    @Bean
    public Interceptor balanceUpdateInterceptor() {
        return new BalanceUpdateInterceptor();
    }
}
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());
    }
}
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/TravelCardMapper.java
@@ -13,9 +13,6 @@
    TravelCard getTravelCardByCardNo(@Param("cardNo") String cardNo);
    void insertBalanceModifyRecord(@Param("recordVo") BalanceModifyRecordVo recordVo);
    List<BalanceModifyRecordVo> getBalanceModifyRecord(@Param("userId") Integer userId,
                                                       @Param("type") Integer type,
                                                       @Param("startDate") String startDate,
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/UserInfoMapper.java
@@ -2,6 +2,7 @@
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.stylefeng.guns.modular.system.model.UserInfo;
import com.stylefeng.guns.modular.system.model.vo.BalanceModifyRecordVo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@@ -58,6 +59,6 @@
    Integer getDiscount();
    void insertBalanceModifyRecord(@Param("recordVo") BalanceModifyRecordVo recordVo);
}
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/mapping/TravelCard.xml
@@ -6,11 +6,6 @@
        select * from t_travel_card where cardNo = #{cardNo}
    </select>
    <select id="insertBalanceModifyRecord">
        insert into t_balance_modify_record (recordID, userId, userName, phone, money, type, createTime)
        values (#{recordVo.recordID}, #{recordVo.userId}, #{recordVo.userName}, #{recordVo.phone}, #{recordVo.money}, #{recordVo.type}, #{recordVo.createTime})
    </select>
    <select id="getBalanceModifyRecord" resultType="com.stylefeng.guns.modular.system.model.vo.BalanceModifyRecordVo">
        select * from t_balance_modify_record
        where userId = #{userId}
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/dao/mapping/UserInfoMapper.xml
@@ -199,4 +199,8 @@
        select id from t_user_activity where now() between startTime and endTime and status =3 order by id limit 1
    </select>
    <select id="insertBalanceModifyRecord">
        insert into t_balance_modify_record (recordID, userId, userName, phone, money, type, createTime)
        values (#{recordVo.recordID}, #{recordVo.userId}, #{recordVo.userName}, #{recordVo.phone}, #{recordVo.money}, #{recordVo.type}, #{recordVo.createTime})
    </select>
</mapper>
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/TravelCardServiceImpl.java