PointUserLogServiceImpl.java 4.7 KB

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