| | |
| | | import org.springframework.util.ReflectionUtils; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Optional; |
| | | import java.util.Properties; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 用户余额变更拦截器 |
| | |
| | | } |
| | | |
| | | // 关键:先执行前查旧值(需要拿 userId) |
| | | Long userId = extractUserId(param); |
| | | Map<String, Long> mapValues = extractUserId(param); |
| | | Long userId = mapValues.get("userId"); |
| | | if(Objects.isNull(userId)) { |
| | | return invocation.proceed(); |
| | | } |
| | | |
| | | // 查询旧的用户 |
| | | UserInfo oldUser = getMapper().selectById(userId); |
| | | // 执行更新 |
| | | Object result = invocation.proceed(); |
| | | // 查新的用户 |
| | | UserInfo newUser = getMapper().selectById(userId); |
| | | Long balanceChangeType = mapValues.get("balanceChangeType"); |
| | | if(Objects.nonNull(balanceChangeType)) { |
| | | newUser.setBalanceChangeType(balanceChangeType.intValue()); |
| | | } |
| | | |
| | | // 新增余额的变更记录(所有余额的变更记录都需要存) |
| | | if(Objects.nonNull(oldUser) && Objects.nonNull(newUser)) { |
| | |
| | | /** |
| | | * 获取用户id |
| | | */ |
| | | private Long extractUserId(Object param) { |
| | | |
| | | private Map<String, Long> extractUserId(Object param) { |
| | | Map<String, Long> mapValues = new HashMap<>(); |
| | | if (param == null) { |
| | | return null; |
| | | return mapValues; |
| | | } |
| | | |
| | | // 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"); |
| | | } |
| | | |
| | | Object balanceChangeType = map.get("balanceChangeType"); |
| | | if (Objects.nonNull(balanceChangeType)) { |
| | | mapValues.put("balanceChangeType", Long.valueOf(balanceChangeType.toString())); |
| | | } |
| | | Object id = map.get("id"); |
| | | if (Objects.nonNull(id)) { |
| | | mapValues.put("userId", Long.valueOf(id.toString())); |
| | | return mapValues; |
| | | } |
| | | } catch (Exception e) { |
| | | // log.error("sql中没有id"); |
| | | } |
| | | |
| | | // 第二种:MyBatis-Plus update(entity, wrapper) |
| | | Object entity = map.get("param1"); |
| | |
| | | } |
| | | |
| | | if (entity != null) { |
| | | return extractId(entity); |
| | | return extractId(entity, mapValues); |
| | | } |
| | | } |
| | | |
| | | // 直接就是实体 |
| | | return extractId(param); |
| | | return extractId(param, mapValues); |
| | | } |
| | | |
| | | private Long extractId(Object obj) { |
| | | private Map<String, Long> extractId(Object obj, Map<String, Long> mapValues) { |
| | | Field field = ReflectionUtils.findField(obj.getClass(), "id"); |
| | | if (field == null) { |
| | | return null; |
| | | return mapValues; |
| | | } |
| | | |
| | | ReflectionUtils.makeAccessible(field); |
| | | Object value = ReflectionUtils.getField(field, obj); |
| | | mapValues.put("userId", value == null ? null : Long.valueOf(value.toString())); |
| | | |
| | | return value == null ? null : Long.valueOf(value.toString()); |
| | | Field fieldType = ReflectionUtils.findField(obj.getClass(), "balanceChangeType"); |
| | | if (fieldType == null) { |
| | | return mapValues; |
| | | } |
| | | ReflectionUtils.makeAccessible(fieldType); |
| | | Object valueType = ReflectionUtils.getField(fieldType, obj); |
| | | mapValues.put("balanceChangeType", valueType == null ? null : Long.valueOf(valueType.toString())); |
| | | return mapValues; |
| | | } |
| | | |
| | | } |