package com.ylx.point.service.impl; import cn.hutool.core.date.DateUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ylx.common.core.domain.model.WxLoginUser; import com.ylx.common.utils.SecurityUtils; import com.ylx.massage.domain.TWxUser; import com.ylx.massage.service.TWxUserService; import com.ylx.point.domain.PointUserLog; import com.ylx.point.domain.dto.UserPointPageDTO; import com.ylx.point.domain.vo.UserPointInfoVO; import com.ylx.point.domain.vo.UserPointLogVO; import com.ylx.point.mapper.PointUserLogMapper; import com.ylx.point.service.IPointActivityService; import com.ylx.point.service.IPointUserActivityTaskCompletionService; import com.ylx.point.service.IPointUserLogService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.Collections; import java.util.List; import java.util.Map; /** * 用户积分流水Service业务层处理 * * @author wzj * @date 2026-03-25 */ @Service public class PointUserLogServiceImpl extends ServiceImpl implements IPointUserLogService { @Resource private PointUserLogMapper pointUserLogMapper; @Resource private TWxUserService wxUserService; @Resource private IPointUserActivityTaskCompletionService pointUserActivityTaskCompletionService; @Resource private IPointActivityService pointActivityService; @Override public UserPointInfoVO getUserPointInfo() { // 当前登录用户信息 WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser(); TWxUser user = wxUserService.getByOpenId(wxLoginUser.getCOpenid()); if (user == null) { throw new RuntimeException("用户不存在"); } String userId = user.getId(); // TODO 用户当前所在地区code String cityCode = "140400"; UserPointInfoVO vo = new UserPointInfoVO(); // 1. 准备时间参数 String currentMonth = DateUtil.format(DateUtil.date(), "yyyyMM"); // 本月第一天 00:00:00 String startOfThisMonth = DateUtil.beginOfMonth(DateUtil.date()).toString(); // 本月最后一天 23:59:59 String endOfThisMonth = DateUtil.endOfMonth(DateUtil.date()).toString(); // 2. 查询积分相关数据 (Total, Earned, Expire) // A. 查询积分统计 (Total, Earned, Expire) Map pointStats = pointUserLogMapper.selectPointStatistics(userId, currentMonth, startOfThisMonth, endOfThisMonth); // B. 查询已完成任务数 Integer completedCount = pointUserActivityTaskCompletionService.selectCompletedTaskCount(userId); // C. 查询系统总任务数 Integer totalTasks = pointActivityService.selectTotalActiveTasks(cityCode); if (pointStats != null) { // 数据库返回的是 BigInteger 或 Long,需要转为 BigDecimal vo.setTotalPoints(new BigDecimal(pointStats.get("totalPoints").toString())); vo.setEarnedPoints(new BigDecimal(pointStats.get("earnedPoints").toString())); vo.setExpirePoints(new BigDecimal(pointStats.get("expirePoints").toString())); } else { vo.setTotalPoints(BigDecimal.ZERO); vo.setEarnedPoints(BigDecimal.ZERO); vo.setExpirePoints(BigDecimal.ZERO); } // 任务数据 int completed = (completedCount != null) ? completedCount : 0; vo.setCompletedTask(completed); int pending = (totalTasks != null ? totalTasks : 0) - completed; vo.setPendingTasks(Math.max(pending, 0)); // 防止出现负数 return vo; } @Override public List getUserPointLogList(UserPointPageDTO dto) { // 当前登录用户信息 WxLoginUser wxLoginUser = SecurityUtils.getWxLoginUser(); TWxUser user = wxUserService.getByOpenId(wxLoginUser.getCOpenid()); if (user == null) { throw new RuntimeException("用户不存在"); } String userId = user.getId(); dto.setUserId(userId); return pointUserLogMapper.getUserPointLogList(dto); } }