1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
@Service public class PointsService { private final PointsMallProperties properties; private final UserPointsRepository userPointsRepository; private final PointsRecordRepository pointsRecordRepository; public PointsService(PointsMallProperties properties) { this.properties = properties; this.userPointsRepository = null; this.pointsRecordRepository = null; }
public UserPoints getUserPoints(Long userId) { try { UserPoints userPoints = userPointsRepository.findByUserId(userId); if (userPoints == null) { userPoints = initializeUserPoints(userId); } return userPoints; } catch (Exception e) { log.error("获取用户积分失败: userId={}", userId, e); return null; } }
public boolean addPoints(Long userId, Integer points, String source, String description) { try { UserPoints userPoints = getUserPoints(userId); if (userPoints == null) { return false; } if (!checkPointsLimit(userId, points, source)) { return false; } userPoints.setTotalPoints(userPoints.getTotalPoints() + points); userPoints.setAvailablePoints(userPoints.getAvailablePoints() + points); userPoints.setUpdateTime(LocalDateTime.now()); checkLevelUp(userPoints); savePointsRecord(userId, points, "EARN", source, description); userPointsRepository.save(userPoints); log.info("用户积分增加成功: userId={}, points={}, source={}", userId, points, source); return true; } catch (Exception e) { log.error("增加积分失败: userId={}, points={}", userId, points, e); return false; } }
public boolean spendPoints(Long userId, Integer points, String source, String description) { try { UserPoints userPoints = getUserPoints(userId); if (userPoints == null) { return false; } if (userPoints.getAvailablePoints() < points) { log.warn("用户积分余额不足: userId={}, available={}, required={}", userId, userPoints.getAvailablePoints(), points); return false; } userPoints.setAvailablePoints(userPoints.getAvailablePoints() - points); userPoints.setUsedPoints(userPoints.getUsedPoints() + points); userPoints.setUpdateTime(LocalDateTime.now()); savePointsRecord(userId, -points, "SPEND", source, description); userPointsRepository.save(userPoints); log.info("用户积分消费成功: userId={}, points={}, source={}", userId, points, source); return true; } catch (Exception e) { log.error("消费积分失败: userId={}, points={}", userId, points, e); return false; } }
private UserPoints initializeUserPoints(Long userId) { UserPoints userPoints = UserPoints.builder() .userId(userId) .totalPoints(0) .availablePoints(0) .usedPoints(0) .level(properties.getDefaultLevel()) .createTime(LocalDateTime.now()) .updateTime(LocalDateTime.now()) .build(); return userPointsRepository.save(userPoints); }
private boolean checkPointsLimit(Long userId, Integer points, String source) { if (points > properties.getMaxPointsPerDay()) { log.warn("积分数量超过每日限制: userId={}, points={}", userId, points); return false; } LocalDateTime startOfMonth = LocalDateTime.now().withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0); Integer monthlyPoints = pointsRecordRepository.getMonthlyPoints(userId, startOfMonth); if (monthlyPoints != null && monthlyPoints + points > properties.getMaxPointsPerMonth()) { log.warn("积分数量超过每月限制: userId={}, monthly={}, points={}", userId, monthlyPoints, points); return false; } return true; }
private void checkLevelUp(UserPoints userPoints) { int newLevel = calculateLevel(userPoints.getTotalPoints()); if (newLevel > userPoints.getLevel()) { userPoints.setLevel(newLevel); if (properties.isEnableLevelUpReward()) { addPoints(userPoints.getUserId(), properties.getLevelUpRewardPoints(), "LEVEL_UP", "等级提升奖励"); } log.info("用户等级提升: userId={}, oldLevel={}, newLevel={}", userPoints.getUserId(), userPoints.getLevel(), newLevel); } }
private int calculateLevel(Integer totalPoints) { return Math.min(properties.getMaxLevel(), totalPoints / properties.getPointsPerLevel() + 1); }
private void savePointsRecord(Long userId, Integer points, String type, String source, String description) { PointsRecord record = PointsRecord.builder() .userId(userId) .points(points) .type(type) .source(source) .description(description) .createTime(LocalDateTime.now()) .build(); pointsRecordRepository.save(record); } }
|