fix 添加积分,扣除积分

This commit is contained in:
wells
2026-08-05 18:23:02 +08:00
parent 7f96115a6a
commit e5f60b7b30
2 changed files with 28 additions and 6 deletions
@@ -3,10 +3,24 @@ package com.tailbet.mapper;
import com.tailbet.model.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
/**
* 用户Mapper
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
/**
* 原子增加积分
*/
@Update("UPDATE user SET points = points + #{amount} WHERE id = #{id}")
int addPoints(@Param("id") Long id, @Param("amount") Long amount);
/**
* 原子扣除积分(积分不足时返回0)
*/
@Update("UPDATE user SET points = points - #{amount} WHERE id = #{id} AND points >= #{amount}")
int subPoints(@Param("id") Long id, @Param("amount") Long amount);
}
@@ -35,10 +35,14 @@ public class PointsServiceImpl implements IPointsService {
}
Long before = user.getPoints();
Long after = before + amount;
user.setPoints(after);
userMapper.updateById(user);
// 原子增加积分
int rows = userMapper.addPoints(userId, amount);
if (rows == 0) {
throw new RuntimeException("积分增加失败");
}
Long after = before + amount;
// 记录流水
PointsFlow flow = new PointsFlow();
@@ -68,10 +72,14 @@ public class PointsServiceImpl implements IPointsService {
}
Long before = user.getPoints();
Long after = before - amount;
user.setPoints(after);
userMapper.updateById(user);
// 原子扣除积分(SQL层保证余额充足才扣)
int rows = userMapper.subPoints(userId, amount);
if (rows == 0) {
return false;
}
Long after = before - amount;
// 记录流水
PointsFlow flow = new PointsFlow();