|
|
@@ -1,241 +1,244 @@
|
|
|
-package com.etotem.cfc.service;
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
|
import com.etotem.cfc.service.api.PointsServiceInterface;
|
|
|
-
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
-import com.etotem.cfc.entity.FamilyMember;
|
|
|
-import com.etotem.cfc.entity.PointsLog;
|
|
|
-import com.etotem.cfc.entity.User;
|
|
|
-import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
-import com.etotem.cfc.mapper.PointsLogMapper;
|
|
|
-import com.etotem.cfc.mapper.UserMapper;
|
|
|
-import javax.annotation.Resource;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-@Service
|
|
|
-public class PointsService implements PointsServiceInterface {
|
|
|
-
|
|
|
- @Resource
|
|
|
- private FamilyMemberMapper familyMemberMapper;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private PointsLogMapper pointsLogMapper;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private UserMapper userMapper;
|
|
|
-
|
|
|
- public Map<String, Object> getBalance(Long memberId) {
|
|
|
- FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
- if (child == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("balance", child.getTotalPoints());
|
|
|
- // 下个月1日0点
|
|
|
- java.util.Calendar cal = java.util.Calendar.getInstance();
|
|
|
- cal.set(java.util.Calendar.DAY_OF_MONTH, 1);
|
|
|
- cal.set(java.util.Calendar.HOUR_OF_DAY, 0);
|
|
|
- cal.set(java.util.Calendar.MINUTE, 0);
|
|
|
- cal.set(java.util.Calendar.SECOND, 0);
|
|
|
- cal.add(java.util.Calendar.MONTH, 1);
|
|
|
- result.put("expireDate", cal.getTime());
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- public Page<PointsLog> getPointsLogs(Long memberId, Integer page, Integer size) {
|
|
|
- Page<PointsLog> pageParam = new Page<>(page, size);
|
|
|
- return pointsLogMapper.selectPage(pageParam, new LambdaQueryWrapper<PointsLog>()
|
|
|
- .eq(PointsLog::getChildId, memberId)
|
|
|
- .orderByDesc(PointsLog::getCreatedAt));
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional
|
|
|
- public Map<String, Object> adjustPoints(Long memberId, Integer amount, String reason, String password, Long userId) {
|
|
|
- // 验证密码(简化版,实际应从数据库验证)
|
|
|
- // 这里需要调用UserService验证密码
|
|
|
-
|
|
|
- FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
- if (child == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- int oldBalance = child.getTotalPoints();
|
|
|
- int newBalance = oldBalance + amount;
|
|
|
-
|
|
|
- if (newBalance < 0) {
|
|
|
- return null; // 积分不能为负
|
|
|
- }
|
|
|
-
|
|
|
- // 更新积分
|
|
|
- child.setTotalPoints(newBalance);
|
|
|
- child.setUpdatedAt(new Date());
|
|
|
- familyMemberMapper.updateById(child);
|
|
|
-
|
|
|
- // 记录流水
|
|
|
- PointsLog log = new PointsLog();
|
|
|
- log.setChildId(memberId);
|
|
|
- log.setAmount(amount);
|
|
|
- log.setType("adjust");
|
|
|
- log.setDescription(reason);
|
|
|
- log.setCreatedAt(new Date());
|
|
|
- pointsLogMapper.insert(log);
|
|
|
-
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- result.put("newBalance", newBalance);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /** @return 系统积分余额 */
|
|
|
- @Transactional
|
|
|
- public int awardSystemPoints(Long memberId, int amount, String reason) {
|
|
|
- if (amount <= 0) {
|
|
|
- throw new IllegalArgumentException("系统积分数量必须为正数");
|
|
|
- }
|
|
|
-
|
|
|
- FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
- if (child == null) {
|
|
|
- throw new RuntimeException("孩子不存在");
|
|
|
- }
|
|
|
-
|
|
|
- int newSystemPoints = (child.getSystemPoints() == null ? 0 : child.getSystemPoints()) + amount;
|
|
|
- child.setSystemPoints(newSystemPoints);
|
|
|
-
|
|
|
- int newTotal = (child.getTotalPoints() == null ? 0 : child.getTotalPoints()) + amount;
|
|
|
- child.setTotalPoints(newTotal);
|
|
|
-
|
|
|
- child.setUpdatedAt(new Date());
|
|
|
- familyMemberMapper.updateById(child);
|
|
|
-
|
|
|
- PointsLog log = new PointsLog();
|
|
|
- log.setChildId(memberId);
|
|
|
- log.setAmount(amount);
|
|
|
- log.setType("earn");
|
|
|
- log.setCategory("system");
|
|
|
- log.setDescription(reason);
|
|
|
- log.setCreatedAt(new Date());
|
|
|
- pointsLogMapper.insert(log);
|
|
|
-
|
|
|
- return newSystemPoints;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 心愿兑换扣除积分(家长审批通过后调用)
|
|
|
- * @param memberId 孩子ID
|
|
|
- * @param amount 扣除数量
|
|
|
- * @return true=扣除成功, false=余额不足
|
|
|
- */
|
|
|
- @Transactional
|
|
|
- public boolean deductForWish(Long memberId, Integer amount) {
|
|
|
- if (amount == null || amount <= 0) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- return deductSystemPoints(memberId, amount, "心愿兑换扣除") >= 0;
|
|
|
- }
|
|
|
-
|
|
|
- /** @return 扣除后余额,余额不足返回-1 */
|
|
|
- @Transactional
|
|
|
- public int deductSystemPoints(Long memberId, int amount, String reason) {
|
|
|
- if (amount <= 0) {
|
|
|
- throw new IllegalArgumentException("扣除数量必须为正数");
|
|
|
- }
|
|
|
-
|
|
|
- FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
- if (child == null) {
|
|
|
- throw new RuntimeException("孩子不存在");
|
|
|
- }
|
|
|
-
|
|
|
- int currentSystem = child.getSystemPoints() == null ? 0 : child.getSystemPoints();
|
|
|
- if (currentSystem < amount) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
-
|
|
|
- int newSystemPoints = currentSystem - amount;
|
|
|
- child.setSystemPoints(newSystemPoints);
|
|
|
-
|
|
|
- int newTotal = (child.getTotalPoints() == null ? 0 : child.getTotalPoints()) - amount;
|
|
|
- child.setTotalPoints(newTotal);
|
|
|
-
|
|
|
- child.setUpdatedAt(new Date());
|
|
|
- familyMemberMapper.updateById(child);
|
|
|
-
|
|
|
- PointsLog log = new PointsLog();
|
|
|
- log.setChildId(memberId);
|
|
|
- log.setAmount(-amount);
|
|
|
- log.setType("spend");
|
|
|
- log.setCategory("system");
|
|
|
- log.setDescription(reason);
|
|
|
- log.setCreatedAt(new Date());
|
|
|
- pointsLogMapper.insert(log);
|
|
|
-
|
|
|
- return newSystemPoints;
|
|
|
- }
|
|
|
-
|
|
|
- public int getSystemPointsBalance(Long memberId) {
|
|
|
- FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
- if (child == null) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- return child.getSystemPoints() == null ? 0 : child.getSystemPoints();
|
|
|
- }
|
|
|
-
|
|
|
- public Page<PointsLog> getPointsLogsByCategory(Long memberId, String category, Integer page, Integer size) {
|
|
|
- Page<PointsLog> pageParam = new Page<>(page, size);
|
|
|
- LambdaQueryWrapper<PointsLog> wrapper = new LambdaQueryWrapper<PointsLog>()
|
|
|
- .eq(PointsLog::getChildId, memberId);
|
|
|
- if (category != null && !category.isEmpty()) {
|
|
|
- wrapper.eq(PointsLog::getCategory, category);
|
|
|
- }
|
|
|
- wrapper.orderByDesc(PointsLog::getCreatedAt);
|
|
|
- return pointsLogMapper.selectPage(pageParam, wrapper);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 检查并发放心愿兑换奖励
|
|
|
- * @param memberId 孩子ID
|
|
|
- * @param amount 奖励积分
|
|
|
- * @return 发放后系统积分余额,余额不足返回-1
|
|
|
- */
|
|
|
- @Transactional
|
|
|
- public int checkAndGrantReward(Long memberId, int amount) {
|
|
|
- int currentBalance = getSystemPointsBalance(memberId);
|
|
|
- if (currentBalance < amount) {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- return awardSystemPoints(memberId, amount, "心愿兑换奖励");
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional
|
|
|
- public boolean deductUserPoints(Long userId, int amount, String reason) {
|
|
|
- User user = userMapper.selectById(userId);
|
|
|
- if (user == null) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- int current = user.getTotalPoints() == null ? 0 : user.getTotalPoints();
|
|
|
- if (current < amount) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- user.setTotalPoints(current - amount);
|
|
|
- user.setUpdatedAt(new Date());
|
|
|
- userMapper.updateById(user);
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional
|
|
|
- public void refundUserPoints(Long userId, int amount, String reason) {
|
|
|
- User user = userMapper.selectById(userId);
|
|
|
- if (user == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- int current = user.getTotalPoints() == null ? 0 : user.getTotalPoints();
|
|
|
- user.setTotalPoints(current + amount);
|
|
|
- user.setUpdatedAt(new Date());
|
|
|
- userMapper.updateById(user);
|
|
|
- }
|
|
|
-}
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.PointsLog;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.PointsLogMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import com.etotem.cfc.util.SortUtil;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class PointsService implements PointsServiceInterface {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PointsLogMapper pointsLogMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ public Map<String, Object> getBalance(Long memberId) {
|
|
|
+ FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
+ if (child == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("balance", child.getTotalPoints());
|
|
|
+ // 下个月1日0点
|
|
|
+ java.util.Calendar cal = java.util.Calendar.getInstance();
|
|
|
+ cal.set(java.util.Calendar.DAY_OF_MONTH, 1);
|
|
|
+ cal.set(java.util.Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(java.util.Calendar.MINUTE, 0);
|
|
|
+ cal.set(java.util.Calendar.SECOND, 0);
|
|
|
+ cal.add(java.util.Calendar.MONTH, 1);
|
|
|
+ result.put("expireDate", cal.getTime());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<PointsLog> getPointsLogs(Long memberId, Integer page, Integer size) {
|
|
|
+ Page<PointsLog> pageParam = new Page<>(page, size);
|
|
|
+ return pointsLogMapper.selectPage(pageParam, new LambdaQueryWrapper<PointsLog>()
|
|
|
+ .eq(PointsLog::getChildId, memberId)
|
|
|
+ .orderByDesc(PointsLog::getCreatedAt));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Map<String, Object> adjustPoints(Long memberId, Integer amount, String reason, String password, Long userId) {
|
|
|
+ // 验证密码(简化版,实际应从数据库验证)
|
|
|
+ // 这里需要调用UserService验证密码
|
|
|
+
|
|
|
+ FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
+ if (child == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ int oldBalance = child.getTotalPoints();
|
|
|
+ int newBalance = oldBalance + amount;
|
|
|
+
|
|
|
+ if (newBalance < 0) {
|
|
|
+ return null; // 积分不能为负
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新积分
|
|
|
+ child.setTotalPoints(newBalance);
|
|
|
+ child.setUpdatedAt(new Date());
|
|
|
+ familyMemberMapper.updateById(child);
|
|
|
+
|
|
|
+ // 记录流水
|
|
|
+ PointsLog log = new PointsLog();
|
|
|
+ log.setChildId(memberId);
|
|
|
+ log.setAmount(amount);
|
|
|
+ log.setType("adjust");
|
|
|
+ log.setDescription(reason);
|
|
|
+ log.setCreatedAt(new Date());
|
|
|
+ pointsLogMapper.insert(log);
|
|
|
+
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
+ result.put("newBalance", newBalance);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @return 系统积分余额 */
|
|
|
+ @Transactional
|
|
|
+ public int awardSystemPoints(Long memberId, int amount, String reason) {
|
|
|
+ if (amount <= 0) {
|
|
|
+ throw new IllegalArgumentException("系统积分数量必须为正数");
|
|
|
+ }
|
|
|
+
|
|
|
+ FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
+ if (child == null) {
|
|
|
+ throw new RuntimeException("孩子不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ int newSystemPoints = (child.getSystemPoints() == null ? 0 : child.getSystemPoints()) + amount;
|
|
|
+ child.setSystemPoints(newSystemPoints);
|
|
|
+
|
|
|
+ int newTotal = (child.getTotalPoints() == null ? 0 : child.getTotalPoints()) + amount;
|
|
|
+ child.setTotalPoints(newTotal);
|
|
|
+
|
|
|
+ child.setUpdatedAt(new Date());
|
|
|
+ familyMemberMapper.updateById(child);
|
|
|
+
|
|
|
+ PointsLog log = new PointsLog();
|
|
|
+ log.setChildId(memberId);
|
|
|
+ log.setAmount(amount);
|
|
|
+ log.setType("earn");
|
|
|
+ log.setCategory("system");
|
|
|
+ log.setDescription(reason);
|
|
|
+ log.setCreatedAt(new Date());
|
|
|
+ pointsLogMapper.insert(log);
|
|
|
+
|
|
|
+ return newSystemPoints;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 心愿兑换扣除积分(家长审批通过后调用)
|
|
|
+ * @param memberId 孩子ID
|
|
|
+ * @param amount 扣除数量
|
|
|
+ * @return true=扣除成功, false=余额不足
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public boolean deductForWish(Long memberId, Integer amount) {
|
|
|
+ if (amount == null || amount <= 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return deductSystemPoints(memberId, amount, "心愿兑换扣除") >= 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** @return 扣除后余额,余额不足返回-1 */
|
|
|
+ @Transactional
|
|
|
+ public int deductSystemPoints(Long memberId, int amount, String reason) {
|
|
|
+ if (amount <= 0) {
|
|
|
+ throw new IllegalArgumentException("扣除数量必须为正数");
|
|
|
+ }
|
|
|
+
|
|
|
+ FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
+ if (child == null) {
|
|
|
+ throw new RuntimeException("孩子不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ int currentSystem = child.getSystemPoints() == null ? 0 : child.getSystemPoints();
|
|
|
+ if (currentSystem < amount) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ int newSystemPoints = currentSystem - amount;
|
|
|
+ child.setSystemPoints(newSystemPoints);
|
|
|
+
|
|
|
+ int newTotal = (child.getTotalPoints() == null ? 0 : child.getTotalPoints()) - amount;
|
|
|
+ child.setTotalPoints(newTotal);
|
|
|
+
|
|
|
+ child.setUpdatedAt(new Date());
|
|
|
+ familyMemberMapper.updateById(child);
|
|
|
+
|
|
|
+ PointsLog log = new PointsLog();
|
|
|
+ log.setChildId(memberId);
|
|
|
+ log.setAmount(-amount);
|
|
|
+ log.setType("spend");
|
|
|
+ log.setCategory("system");
|
|
|
+ log.setDescription(reason);
|
|
|
+ log.setCreatedAt(new Date());
|
|
|
+ pointsLogMapper.insert(log);
|
|
|
+
|
|
|
+ return newSystemPoints;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int getSystemPointsBalance(Long memberId) {
|
|
|
+ FamilyMember child = familyMemberMapper.selectById(memberId);
|
|
|
+ if (child == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return child.getSystemPoints() == null ? 0 : child.getSystemPoints();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<PointsLog> getPointsLogsByCategory(Long memberId, String category, Integer page, Integer size) {
|
|
|
+ Page<PointsLog> pageParam = new Page<>(page, size);
|
|
|
+ LambdaQueryWrapper<PointsLog> wrapper = new LambdaQueryWrapper<PointsLog>()
|
|
|
+ .eq(PointsLog::getChildId, memberId);
|
|
|
+ if (category != null && !category.isEmpty()) {
|
|
|
+ wrapper.eq(PointsLog::getCategory, category);
|
|
|
+ }
|
|
|
+ wrapper.orderByDesc(PointsLog::getCreatedAt);
|
|
|
+ SortUtil.applySort(wrapper);
|
|
|
+ SortUtil.applySort(wrapper);
|
|
|
+ return pointsLogMapper.selectPage(pageParam, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查并发放心愿兑换奖励
|
|
|
+ * @param memberId 孩子ID
|
|
|
+ * @param amount 奖励积分
|
|
|
+ * @return 发放后系统积分余额,余额不足返回-1
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public int checkAndGrantReward(Long memberId, int amount) {
|
|
|
+ int currentBalance = getSystemPointsBalance(memberId);
|
|
|
+ if (currentBalance < amount) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return awardSystemPoints(memberId, amount, "心愿兑换奖励");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public boolean deductUserPoints(Long userId, int amount, String reason) {
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int current = user.getTotalPoints() == null ? 0 : user.getTotalPoints();
|
|
|
+ if (current < amount) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ user.setTotalPoints(current - amount);
|
|
|
+ user.setUpdatedAt(new Date());
|
|
|
+ userMapper.updateById(user);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void refundUserPoints(Long userId, int amount, String reason) {
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int current = user.getTotalPoints() == null ? 0 : user.getTotalPoints();
|
|
|
+ user.setTotalPoints(current + amount);
|
|
|
+ user.setUpdatedAt(new Date());
|
|
|
+ userMapper.updateById(user);
|
|
|
+ }
|
|
|
+}
|