| | |
| | | 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; |
| | |
| | | * @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 { |
| | |
| | | // 关键:先执行前查旧值(需要拿 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 |
| | |
| | | * 获取用户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()); |
| | | } |
| | | |
| | | } |