|
|
@@ -0,0 +1,147 @@
|
|
|
+package com.yingpai.gupiao.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.yingpai.gupiao.domain.po.User;
|
|
|
+import com.yingpai.gupiao.domain.po.UserPointsRecord;
|
|
|
+import com.yingpai.gupiao.mapper.UserMapper;
|
|
|
+import com.yingpai.gupiao.mapper.UserPointsRecordMapper;
|
|
|
+import com.yingpai.gupiao.service.UserPointsService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户积分服务实现类
|
|
|
+ * 积分存储在user表的total_points字段
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class UserPointsServiceImpl implements UserPointsService {
|
|
|
+
|
|
|
+ private final UserMapper userMapper;
|
|
|
+ private final UserPointsRecordMapper userPointsRecordMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 积分类型:收入
|
|
|
+ */
|
|
|
+ private static final int TYPE_INCOME = 1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 积分类型:支出
|
|
|
+ */
|
|
|
+ private static final int TYPE_EXPENSE = 2;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void initUserPoints(Long userId, int initialPoints) {
|
|
|
+ log.info("记录注册积分流水,userId: {}, initialPoints: {}", userId, initialPoints);
|
|
|
+
|
|
|
+ // 创建积分流水记录
|
|
|
+ UserPointsRecord record = new UserPointsRecord();
|
|
|
+ record.setUserId(userId);
|
|
|
+ record.setType(TYPE_INCOME);
|
|
|
+ record.setAmount(initialPoints);
|
|
|
+ record.setBalance(initialPoints);
|
|
|
+ record.setBizType(1); // 注册奖励(对应字典 points_biz_type)
|
|
|
+ record.setRemark("新用户注册奖励");
|
|
|
+ record.setCreateTime(LocalDateTime.now());
|
|
|
+ userPointsRecordMapper.insert(record);
|
|
|
+
|
|
|
+ log.info("注册积分流水记录完成,userId: {}", userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer getUserPoints(Long userId) {
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return user.getTotalPoints() != null ? user.getTotalPoints() : 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void addPoints(Long userId, int amount, int bizType, String bizId, String remark) {
|
|
|
+ log.info("增加积分,userId: {}, amount: {}, bizType: {}", userId, amount, bizType);
|
|
|
+
|
|
|
+ // 1. 获取用户
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 更新积分
|
|
|
+ int currentPoints = user.getTotalPoints() != null ? user.getTotalPoints() : 0;
|
|
|
+ int newBalance = currentPoints + amount;
|
|
|
+ user.setTotalPoints(newBalance);
|
|
|
+ user.setUpdateTime(LocalDateTime.now());
|
|
|
+ userMapper.updateById(user);
|
|
|
+
|
|
|
+ // 3. 记录流水
|
|
|
+ UserPointsRecord record = new UserPointsRecord();
|
|
|
+ record.setUserId(userId);
|
|
|
+ record.setType(TYPE_INCOME);
|
|
|
+ record.setAmount(amount);
|
|
|
+ record.setBalance(newBalance);
|
|
|
+ record.setBizType(bizType);
|
|
|
+ record.setBizId(bizId);
|
|
|
+ record.setRemark(remark);
|
|
|
+ record.setCreateTime(LocalDateTime.now());
|
|
|
+ userPointsRecordMapper.insert(record);
|
|
|
+
|
|
|
+ log.info("积分增加完成,userId: {}, newBalance: {}", userId, newBalance);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean deductPoints(Long userId, int amount, int bizType, String bizId, String remark) {
|
|
|
+ log.info("扣减积分,userId: {}, amount: {}, bizType: {}", userId, amount, bizType);
|
|
|
+
|
|
|
+ // 1. 获取用户
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 检查积分是否足够
|
|
|
+ int currentPoints = user.getTotalPoints() != null ? user.getTotalPoints() : 0;
|
|
|
+ if (currentPoints < amount) {
|
|
|
+ log.warn("积分不足,userId: {}, current: {}, need: {}", userId, currentPoints, amount);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 更新积分
|
|
|
+ int newBalance = currentPoints - amount;
|
|
|
+ user.setTotalPoints(newBalance);
|
|
|
+ user.setUpdateTime(LocalDateTime.now());
|
|
|
+ userMapper.updateById(user);
|
|
|
+
|
|
|
+ // 4. 记录流水
|
|
|
+ UserPointsRecord record = new UserPointsRecord();
|
|
|
+ record.setUserId(userId);
|
|
|
+ record.setType(TYPE_EXPENSE);
|
|
|
+ record.setAmount(amount);
|
|
|
+ record.setBalance(newBalance);
|
|
|
+ record.setBizType(bizType);
|
|
|
+ record.setBizId(bizId);
|
|
|
+ record.setRemark(remark);
|
|
|
+ record.setCreateTime(LocalDateTime.now());
|
|
|
+ userPointsRecordMapper.insert(record);
|
|
|
+
|
|
|
+ log.info("积分扣减完成,userId: {}, newBalance: {}", userId, newBalance);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<UserPointsRecord> getPointsRecords(Long userId) {
|
|
|
+ LambdaQueryWrapper<UserPointsRecord> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(UserPointsRecord::getUserId, userId)
|
|
|
+ .orderByDesc(UserPointsRecord::getCreateTime);
|
|
|
+ return userPointsRecordMapper.selectList(wrapper);
|
|
|
+ }
|
|
|
+}
|