fix 添加积分,扣除积分
This commit is contained in:
@@ -3,10 +3,24 @@ package com.tailbet.mapper;
|
|||||||
import com.tailbet.model.entity.User;
|
import com.tailbet.model.entity.User;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Update;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户Mapper
|
* 用户Mapper
|
||||||
*/
|
*/
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UserMapper extends BaseMapper<User> {
|
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 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();
|
PointsFlow flow = new PointsFlow();
|
||||||
@@ -68,10 +72,14 @@ public class PointsServiceImpl implements IPointsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Long before = user.getPoints();
|
Long before = user.getPoints();
|
||||||
Long after = before - amount;
|
|
||||||
|
|
||||||
user.setPoints(after);
|
// 原子扣除积分(SQL层保证余额充足才扣)
|
||||||
userMapper.updateById(user);
|
int rows = userMapper.subPoints(userId, amount);
|
||||||
|
if (rows == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Long after = before - amount;
|
||||||
|
|
||||||
// 记录流水
|
// 记录流水
|
||||||
PointsFlow flow = new PointsFlow();
|
PointsFlow flow = new PointsFlow();
|
||||||
|
|||||||
Reference in New Issue
Block a user