From c8ea2d96f5b0522a09f3203ae98fe796084d2d15 Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期六, 04 一月 2025 09:34:21 +0800
Subject: [PATCH] Merge branch 'master' of http://120.76.84.145:10101/gitblit/r/java/qijisheng

---
 ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java |  101 ++++++++++++++++++++++++++++++++------------------
 1 files changed, 64 insertions(+), 37 deletions(-)

diff --git a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java
index db675e1..15e3ad8 100644
--- a/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java
+++ b/ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java
@@ -2,22 +2,25 @@
 
 import cn.hutool.core.collection.CollectionUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.account.api.model.AppUser;
 import com.ruoyi.account.api.model.UserPoint;
-import com.ruoyi.account.enums.PointChangeType;
 import com.ruoyi.account.mapper.UserPointMapper;
 import com.ruoyi.account.service.AppUserService;
 import com.ruoyi.account.service.PointSettingService;
 import com.ruoyi.account.service.UserPointService;
 import com.ruoyi.account.service.VipSettingService;
 import com.ruoyi.account.vo.UserPointDetailVO;
+import com.ruoyi.account.vo.UserPointStatistics;
 import com.ruoyi.account.vo.UserPointVO;
 import com.ruoyi.common.core.exception.ServiceException;
 import com.ruoyi.common.core.utils.PhoneNumberValidator;
 import com.ruoyi.common.security.service.TokenService;
 import com.ruoyi.other.api.domain.PointSetting;
 import com.ruoyi.other.api.domain.VipSetting;
+import com.ruoyi.other.api.enums.PointChangeType;
 import com.ruoyi.system.api.model.LoginUser;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -26,10 +29,10 @@
 import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -55,22 +58,17 @@
     @Override
     public UserPointVO getUserPoint(Long userId) {
         AppUser appUser = appUserService.getById(userId);
-        List<UserPoint> userPointList = this.baseMapper.findLatestUserPointByTypeForUser(userId);
-
-        Map<Integer, Integer> userBalanceMap = userPointList.stream()
-                .collect(Collectors.toMap(UserPoint::getType, UserPoint::getBalance));
         VipSetting vipSetting = vipSettingService.getVipSettingByUserId(userId);
 
-        Integer lavePoint = appUser.getLavePoint();
         UserPointVO userPointVO = new UserPointVO();
-        userPointVO.setTotalPoint(lavePoint);
-//        userPointVO.setConsumePoint(lavePoint);
-        userPointVO.setShopPoint(userBalanceMap.get(PointChangeType.CONSUME.getCode()));
-        userPointVO.setSharePoint(userBalanceMap.get(PointChangeType.COMMISSION_RETURN.getCode()));
-        userPointVO.setPullNewPoint(userBalanceMap.get(PointChangeType.NEW_USER_REFERRAL.getCode()));
-        userPointVO.setShopAchievementPoint(userBalanceMap.get(PointChangeType.STORE_PERFORMANCE.getCode()));
-        userPointVO.setShopSharePoint(userBalanceMap.get(PointChangeType.STORE_COMMISSION_RETURN.getCode()));
-        userPointVO.setGiftPoint(vipSetting.getVipGiftRole());
+        userPointVO.setTotalPoint(appUser.getTotalPoint());
+        userPointVO.setConsumePoint(appUser.getAvailablePoint());
+        userPointVO.setShopPoint(appUser.getShopPoint());
+        userPointVO.setSharePoint(appUser.getSharePoint());
+        userPointVO.setPullNewPoint(appUser.getTotalInvitePoint());
+        userPointVO.setShopAchievementPoint(appUser.getShopAchievementPoint());
+        userPointVO.setShopSharePoint(appUser.getShopSharePoint());
+        userPointVO.setGiftPoint(vipSetting.getVipGiftRole() == 1 ? 1 : 0);
         return userPointVO;
     }
 
@@ -79,13 +77,14 @@
         List<UserPoint> userPointList = list(new LambdaQueryWrapper<UserPoint>()
                 .between(startTime != null, UserPoint::getCreateTime, startTime, endTime)
                 .eq(type != null, UserPoint::getType, type)
-                .eq(UserPoint::getAppUserId, userId));
+                .eq(UserPoint::getAppUserId, userId).orderByDesc(UserPoint::getCreateTime));
         if (CollectionUtil.isNotEmpty(userPointList)) {
             return userPointList.stream().map(p -> {
                 UserPointDetailVO userPointDetailVO = new UserPointDetailVO();
                 userPointDetailVO.setType(p.getType());
                 userPointDetailVO.setVariablePoint(p.getVariablePoint());
-                userPointDetailVO.setCreateTime(p.getCreateTime());
+                String format = p.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+                userPointDetailVO.setCreateTime(format);
                 return userPointDetailVO;
             }).collect(Collectors.toList());
         }
@@ -107,7 +106,7 @@
         if (vipSetting == null) {
             throw new ServiceException("VIP 设置未找到");
         }
-        if (vipSetting.getVipGiftRole() == 0) {
+        if (vipSetting.getId() == 0 && vipSetting.getVipGiftRole() == 0) {
             throw new ServiceException("转赠积分权限未开启");
         }
 
@@ -115,29 +114,15 @@
         if (appUser == null) {
             throw new ServiceException("用户未找到");
         }
-        Integer totalPoint = appUser.getLavePoint();
 
         PointSetting pointSetting = pointSettingService.getPointSettingByAppUserId(userid);
         if (pointSetting == null) {
             throw new ServiceException("积分设置未找到");
         }
-        Integer buyPointOpen = pointSetting.getBuyPointOpen();
+        // 可转赠积分总数
+        long adjustedPoint = appUser.getAvailablePoint();
 
-        List<UserPoint> userPointList = list(new LambdaQueryWrapper<UserPoint>()
-                .eq(UserPoint::getAppUserId, userid));
-        Map<Integer, Integer> userBalanceMap = userPointList.stream()
-                .collect(Collectors.toMap(UserPoint::getType, UserPoint::getBalance));
-
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.CONSUME.getCode(), buyPointOpen == 0);
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.COMMISSION_RETURN.getCode(), pointSetting.getSharePointOpen() == 0);
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.STORE_COMMISSION_RETURN.getCode(), pointSetting.getShopSharePointOpen() == 0);
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.STORE_PERFORMANCE.getCode(), pointSetting.getShopPointOpen() == 0);
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.TECHNICIAN_PERFORMANCE.getCode(), pointSetting.getPersonPointOpen() == 0);
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.NEW_USER_REFERRAL.getCode(), pointSetting.getGetNewPointOpen() == 0);
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.REGISTRATION.getCode(), pointSetting.getGetRegisPointOpen() == 0);
-        totalPoint -= getAdjustedPoint(userBalanceMap, PointChangeType.WORK_PERFORMANCE.getCode(), pointSetting.getWorkPointOpen() == 0);
-
-        if (point.compareTo(new BigDecimal(totalPoint)) > 0) {
+        if (point.compareTo(new BigDecimal(adjustedPoint)) > 0) {
             throw new ServiceException("转赠积分不足");
         }
 
@@ -151,10 +136,52 @@
         appUserForPhoe.setTotalPoint(appUserForPhoe.getTotalPoint() + point.intValue());
         appUserService.updateById(appUserForPhoe);
 
+        Integer lavePoint = appUser.getLavePoint();
+        appUser.setLavePoint(lavePoint - point.intValue());
+        Integer totalPoint = appUser.getTotalPoint();
+        appUser.setTotalPoint(totalPoint - point.intValue());
+        appUserService.updateById(appUser);
         log.info("积分转赠完成,用户ID: {}, 新积分: {}", appUserForPhoe.getId(), appUserForPhoe.getLavePoint());
     }
 
-    private int getAdjustedPoint(Map<Integer, Integer> userBalanceMap, int pointTypeCode, boolean isOpen) {
-        return isOpen ? 0 : Optional.ofNullable(userBalanceMap.get(pointTypeCode)).orElse(0);
+
+    @Override
+    public UserPointStatistics getStatistics(UserPoint userPoint) {
+        List<UserPoint> userPointList = this.baseMapper.selectUserPoint(userPoint);
+        Map<Integer, Integer> userBalanceMap = userPointList.stream()
+                .collect(Collectors.groupingBy(
+                        UserPoint::getType,
+                        Collectors.summingInt(UserPoint::getVariablePoint)
+                ));
+
+        int consumePoint = userBalanceMap.getOrDefault(PointChangeType.CONSUME.getCode(), 0);
+        int sharePoint = userBalanceMap.getOrDefault(PointChangeType.REBATE.getCode(), 0);
+        int pullNewPoint = userBalanceMap.getOrDefault(PointChangeType.PULL_NEW.getCode(), 0);
+        int registerPoint = userBalanceMap.getOrDefault(PointChangeType.REGISTER.getCode(), 0);
+
+        // 做工积分:签到积分 + 每日分享积分 + 每日签到积分 + 每日使用时长积分
+        int share = userBalanceMap.getOrDefault(PointChangeType.SHARE.getCode(), 0);
+        int signIn = userBalanceMap.getOrDefault(PointChangeType.SIGN_IN.getCode(), 0);
+        int useTime = userBalanceMap.getOrDefault(PointChangeType.USE_TIME.getCode(), 0);
+        int workPoint = share + signIn + useTime;
+
+        int shopAchievementPoint = userBalanceMap.getOrDefault(PointChangeType.TECHNICIAN_ACHIEVEMENT.getCode(), 0);
+        //总积分
+        int totalPoint = consumePoint+sharePoint+pullNewPoint+registerPoint+workPoint+shopAchievementPoint;
+        UserPointStatistics userPointStatistics = new UserPointStatistics();
+        userPointStatistics.setTotalPoint(totalPoint);
+        userPointStatistics.setConsumePoint(consumePoint);
+        userPointStatistics.setSharePoint(sharePoint);
+        userPointStatistics.setPullNewPoint(pullNewPoint);
+        userPointStatistics.setRegisterPoint(registerPoint);
+        userPointStatistics.setWorkPoint(workPoint);
+        userPointStatistics.setShopAchievementPoint(shopAchievementPoint);
+        return userPointStatistics;
+    }
+
+
+    @Override
+    public IPage<UserPoint> getUserPointPage(Page<UserPoint> page, UserPoint userPoint) {
+        return this.baseMapper.queryUserPointPage(page, userPoint);
     }
 }

--
Gitblit v1.7.1