From b22df417e0bc423c788b013feaad686531d69eed Mon Sep 17 00:00:00 2001
From: puzhibing <393733352@qq.com>
Date: 星期三, 08 一月 2025 09:51:37 +0800
Subject: [PATCH] 修改bug

---
 ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java |  155 +++++++++++++++++++--------------------------------
 1 files changed, 58 insertions(+), 97 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 132dc25..8223d51 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
@@ -7,7 +7,6 @@
 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;
@@ -19,22 +18,22 @@
 import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.core.exception.ServiceException;
 import com.ruoyi.common.core.utils.PhoneNumberValidator;
-import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.security.service.TokenService;
 import com.ruoyi.other.api.domain.PointSetting;
-import com.ruoyi.other.api.domain.Shop;
 import com.ruoyi.other.api.domain.VipSetting;
-import com.ruoyi.other.api.feignClient.ShopClient;
+import com.ruoyi.other.api.enums.PointChangeType;
 import com.ruoyi.system.api.model.LoginUser;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.CollectionUtils;
 
 import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
-import java.util.*;
+import java.time.format.DateTimeFormatter;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 /**
@@ -57,27 +56,25 @@
     @Resource
     private PointSettingService pointSettingService;
     @Resource
-    private UserPointMapper userPointMapper;
-    @Resource
-    private ShopClient shopClient;
+    private UserPointService userPointService;
+
+
+
 
     @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);
 
         UserPointVO userPointVO = new UserPointVO();
         userPointVO.setTotalPoint(appUser.getTotalPoint());
-        userPointVO.setConsumePoint(appUser.getLavePoint());
+        userPointVO.setConsumePoint(appUser.getAvailablePoint());
         userPointVO.setShopPoint(appUser.getShopPoint());
         userPointVO.setSharePoint(appUser.getSharePoint());
-        userPointVO.setPullNewPoint(userBalanceMap.get(PointChangeType.PULL_NEW.getCode()));
-        userPointVO.setShopAchievementPoint(userBalanceMap.get(PointChangeType.SHOP_ACHIEVEMENT.getCode()));
-        userPointVO.setShopSharePoint(userBalanceMap.get(PointChangeType.SHOP_REBATE.getCode()));
-        userPointVO.setGiftPoint(vipSetting.getVipGiftRole() == 1 && vipSetting.getId() == 1 ? 1 : 0);
+        userPointVO.setPullNewPoint(appUser.getTotalInvitePoint());
+        userPointVO.setShopAchievementPoint(appUser.getShopAchievementPoint());
+        userPointVO.setShopSharePoint(appUser.getShopSharePoint());
+        userPointVO.setGiftPoint(vipSetting.getVipGiftRole() == 1 ? 1 : 0);
         return userPointVO;
     }
 
@@ -86,13 +83,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());
         }
@@ -102,9 +100,9 @@
 
     @Override
     @Transactional
-    public void transferPoint(BigDecimal point, String phone) {
+    public R transferPoint(Integer point, String phone) {
         if (!PhoneNumberValidator.isValidChinaPhoneNumber(phone)) {
-            throw new ServiceException("无效的电话号码");
+            return R.fail("无效的电话号码");
         }
 
         LoginUser loginUserApplet = tokenService.getLoginUserApplet();
@@ -112,102 +110,65 @@
 
         VipSetting vipSetting = vipSettingService.getVipSettingByUserId(userid);
         if (vipSetting == null) {
-            throw new ServiceException("VIP 设置未找到");
+            return R.fail("VIP 设置未找到");
         }
         if (vipSetting.getId() == 0 && vipSetting.getVipGiftRole() == 0) {
-            throw new ServiceException("转赠积分权限未开启");
+            return R.fail("转赠积分权限未开启");
         }
 
         AppUser appUser = appUserService.getById(userid);
         if (appUser == null) {
-            throw new ServiceException("用户未找到");
+            return R.fail("用户未找到");
         }
 
         PointSetting pointSetting = pointSettingService.getPointSettingByAppUserId(userid);
         if (pointSetting == null) {
-            throw new ServiceException("积分设置未找到");
+            return R.fail("积分设置未找到");
         }
         // 可转赠积分总数
-        int adjustedPoint = getAdjustedPoint(pointSetting, appUser);
-
-        if (point.compareTo(new BigDecimal(adjustedPoint)) > 0) {
-            throw new ServiceException("转赠积分不足");
+        Integer transferablePoint = appUser.getTransferablePoint();
+        if (point > transferablePoint) {
+            return R.fail("转赠积分不足");
         }
 
         AppUser appUserForPhoe = appUserService.getOne(new LambdaQueryWrapper<AppUser>()
                 .eq(AppUser::getPhone, phone));
         if (appUserForPhoe == null) {
-            throw new ServiceException("目标用户未找到");
+            return R.fail("目标用户未找到");
         }
-
-        appUserForPhoe.setLavePoint(appUserForPhoe.getLavePoint() + point.intValue());
-        appUserForPhoe.setTotalPoint(appUserForPhoe.getTotalPoint() + point.intValue());
+        Integer lavePoint1 = appUserForPhoe.getLavePoint();
+        appUserForPhoe.setLavePoint(appUserForPhoe.getLavePoint() + point);
+        appUserForPhoe.setTotalPoint(appUserForPhoe.getTotalPoint() + point);
+        appUserForPhoe.setTransferablePoint(transferablePoint + point);
+        appUserForPhoe.setAvailablePoint(appUserForPhoe.getAvailablePoint() + point);
         appUserService.updateById(appUserForPhoe);
+        //构建积分流水记录
+        UserPoint userPoint = new UserPoint();
+        userPoint.setType(12);
+        userPoint.setHistoricalPoint(lavePoint1);
+        userPoint.setVariablePoint(point);
+        userPoint.setBalance(appUserForPhoe.getLavePoint());
+        userPoint.setCreateTime(LocalDateTime.now());
+        userPoint.setAppUserId(appUserForPhoe.getId());
+        userPointService.save(userPoint);
 
-        log.info("积分转赠完成,用户ID: {}, 新积分: {}", appUserForPhoe.getId(), appUserForPhoe.getLavePoint());
-    }
-
-    /**
-     * 获取可转赠积分
-     * @param pointSetting 积分设置
-     * @param appUser 用户
-     * @return
-     */
-    private int getAdjustedPoint(PointSetting pointSetting, AppUser appUser) {
-        int transferPoint = 0;
-
-        // 消费积分数
-        Integer buyPointGift = pointSetting.getBuyPointGift();
-        if (buyPointGift == 1){
-            transferPoint += appUser.getShopPoint();
-        }
-
-        // 返佣积分
-        Integer sharePointOpen = pointSetting.getSharePointGift();
-        if (sharePointOpen == 1){
-            transferPoint += appUser.getSharePoint();
-        }
-
-        // 门店返佣积分
-        Integer shopSharePointGift = pointSetting.getShopSharePointGift();
-        R<List<Shop>> shops = shopClient.getShopByUserIds(Collections.singletonList(appUser.getId()));
-        List<Shop> shopList = shops.getData();
-        if (CollectionUtils.isEmpty(shopList)) {
-            throw new ServiceException("未找到门店");
-        }
-        if (shopSharePointGift == 1){
-            transferPoint += shopList.stream().mapToInt(Shop::getSharePoint).sum();
-        }
-        // 门店业绩积分
-        Integer shopPointOpen = pointSetting.getShopPointGift();
-        if (shopPointOpen == 1){
-            transferPoint += shopList.stream().mapToInt(Shop::getShopPoint).sum();
-        }
-
-        // 技师业绩积分
-        Integer personPointOpen = pointSetting.getPersonPointGift();
-        if (personPointOpen == 1){
-            transferPoint += appUser.getTotalPerformancePoint();
-        }
-
-        // 拉新积分
-        Integer getNewPointOpen = pointSetting.getGetNewPointGift();
-        if (getNewPointOpen == 1){
-            transferPoint += appUser.getTotalInvitePoint();
-        }
-
-        // 注册积分
-        Integer getRegisPointGift = pointSetting.getGetRegisPointGift();
-        if (getRegisPointGift == 1){
-            transferPoint += appUser.getTotalRegisterPoint();
-        }
-
-        // 做工积分
-        Integer workPointOpen = pointSetting.getWorkPointGift();
-        if (workPointOpen == 1){
-            transferPoint += appUser.getTotalSharePoint() + appUser.getTotalSignPoint() + appUser.getTotalHourPoint();
-        }
-        return transferPoint;
+        Integer lavePoint = appUser.getLavePoint();
+        appUser.setLavePoint(lavePoint - point);
+        Integer totalPoint = appUser.getTotalPoint();
+        appUser.setTotalPoint(totalPoint - point);
+        appUser.setTransferablePoint(appUser.getTransferablePoint() - point);
+        appUser.setAvailablePoint(appUser.getAvailablePoint() - point);
+        appUserService.updateById(appUser);
+        //构建积分流水记录
+        userPoint = new UserPoint();
+        userPoint.setType(13);
+        userPoint.setHistoricalPoint(lavePoint);
+        userPoint.setVariablePoint(point);
+        userPoint.setBalance(appUser.getLavePoint());
+        userPoint.setCreateTime(LocalDateTime.now());
+        userPoint.setAppUserId(appUser.getId());
+        userPointService.save(userPoint);
+        return R.ok();
     }
 
 

--
Gitblit v1.7.1