PointUserLogServiceImpl.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.ylx.point.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.ylx.common.core.domain.model.WxLoginUser;
  5. import com.ylx.common.utils.SecurityUtils;
  6. import com.ylx.massage.domain.TWxUser;
  7. import com.ylx.massage.service.TWxUserService;
  8. import com.ylx.point.domain.PointUserLog;
  9. import com.ylx.point.domain.dto.UserPointPageDTO;
  10. import com.ylx.point.domain.vo.UserPointInfoVO;
  11. import com.ylx.point.domain.vo.UserPointLogVO;
  12. import com.ylx.point.mapper.PointUserLogMapper;
  13. import com.ylx.point.service.IPointActivityService;
  14. import com.ylx.point.service.IPointUserActivityTaskCompletionService;
  15. import com.ylx.point.service.IPointUserLogService;
  16. import org.springframework.stereotype.Service;
  17. import javax.annotation.Resource;
  18. import java.math.BigDecimal;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * 用户积分流水Service业务层处理
  24. *
  25. * @author wzj
  26. * @date 2026-03-25
  27. */
  28. @Service
  29. public class PointUserLogServiceImpl extends ServiceImpl<PointUserLogMapper, PointUserLog> implements IPointUserLogService {
  30. @Resource
  31. private PointUserLogMapper pointUserLogMapper;
  32. @Resource
  33. private TWxUserService wxUserService;
  34. @Resource
  35. private IPointUserActivityTaskCompletionService pointUserActivityTaskCompletionService;
  36. @Resource
  37. private IPointActivityService pointActivityService;
  38. @Override
  39. public UserPointInfoVO getUserPointInfo() {
  40. // 当前登录用户信息
  41. WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
  42. TWxUser user = wxUserService.getByOpenId(wxLoginUser.getCOpenid());
  43. if (user == null) {
  44. throw new RuntimeException("用户不存在");
  45. }
  46. String userId = user.getId();
  47. // TODO 用户当前所在地区code
  48. String cityCode = "140400";
  49. UserPointInfoVO vo = new UserPointInfoVO();
  50. // 1. 准备时间参数
  51. String currentMonth = DateUtil.format(DateUtil.date(), "yyyyMM");
  52. // 本月第一天 00:00:00
  53. String startOfThisMonth = DateUtil.beginOfMonth(DateUtil.date()).toString();
  54. // 本月最后一天 23:59:59
  55. String endOfThisMonth = DateUtil.endOfMonth(DateUtil.date()).toString();
  56. // 2. 查询积分相关数据 (Total, Earned, Expire)
  57. // A. 查询积分统计 (Total, Earned, Expire)
  58. Map<String, Object> pointStats = pointUserLogMapper.selectPointStatistics(userId, currentMonth, startOfThisMonth, endOfThisMonth);
  59. // B. 查询已完成任务数
  60. Integer completedCount = pointUserActivityTaskCompletionService.selectCompletedTaskCount(userId);
  61. // C. 查询系统总任务数
  62. Integer totalTasks = pointActivityService.selectTotalActiveTasks(cityCode);
  63. if (pointStats != null) {
  64. // 数据库返回的是 BigInteger 或 Long,需要转为 BigDecimal
  65. vo.setTotalPoints(new BigDecimal(pointStats.get("totalPoints").toString()));
  66. vo.setEarnedPoints(new BigDecimal(pointStats.get("earnedPoints").toString()));
  67. vo.setExpirePoints(new BigDecimal(pointStats.get("expirePoints").toString()));
  68. } else {
  69. vo.setTotalPoints(BigDecimal.ZERO);
  70. vo.setEarnedPoints(BigDecimal.ZERO);
  71. vo.setExpirePoints(BigDecimal.ZERO);
  72. }
  73. // 任务数据
  74. int completed = (completedCount != null) ? completedCount : 0;
  75. vo.setCompletedTask(completed);
  76. int pending = (totalTasks != null ? totalTasks : 0) - completed;
  77. vo.setPendingTasks(Math.max(pending, 0)); // 防止出现负数
  78. return vo;
  79. }
  80. @Override
  81. public List<UserPointLogVO> getUserPointLogList(UserPointPageDTO dto) {
  82. // 当前登录用户信息
  83. WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser();
  84. TWxUser user = wxUserService.getByOpenId(wxLoginUser.getCOpenid());
  85. if (user == null) {
  86. throw new RuntimeException("用户不存在");
  87. }
  88. String userId = user.getId();
  89. dto.setUserId(userId);
  90. return pointUserLogMapper.getUserPointLogList(dto);
  91. }
  92. }