diff --git a/src/main/java/xuan/service/UserInfoService.java b/src/main/java/xuan/service/UserInfoService.java index d050087..0b78f50 100644 --- a/src/main/java/xuan/service/UserInfoService.java +++ b/src/main/java/xuan/service/UserInfoService.java @@ -70,7 +70,8 @@ public class UserInfoService { } UserInfo user = userInfoRepository.findByUserId(userId); if (user != null) { - user.setCoins(user.getCoins() + amount); + Integer userCoins = user.getCoins() != null ? user.getCoins() : 0; + user.setCoins(userCoins + amount); return userInfoRepository.save(user); } return null; @@ -82,10 +83,11 @@ public class UserInfoService { } UserInfo user = userInfoRepository.findByUserId(userId); if (user != null) { - if (user.getCoins() < amount) { + Integer userCoins = user.getCoins() != null ? user.getCoins() : 0; + if (userCoins < amount) { throw new IllegalArgumentException("金币不足"); } - user.setCoins(user.getCoins() - amount); + user.setCoins(userCoins - amount); return userInfoRepository.save(user); } return null; @@ -135,10 +137,11 @@ public class UserInfoService { if (user.getVipLevel() != null && user.getVipLevel() > 0) { return user; } - if (user.getCoins() < amount) { + Integer userCoins = user.getCoins() != null ? user.getCoins() : 0; + if (userCoins < amount) { return null; } - user.setCoins(user.getCoins() - amount); + user.setCoins(userCoins - amount); userInfoRepository.save(user); CoinConsumeRecord record = new CoinConsumeRecord();