From f196447bcf0b1cdb910a47e1babad6f284c28bd2 Mon Sep 17 00:00:00 2001
From: luodangjia <luodangjia>
Date: 星期三, 18 十二月 2024 16:58:15 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java |  190 +++++++++++++++++++++++++----------------------
 1 files changed, 101 insertions(+), 89 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 61c0ede..132dc25 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
@@ -16,12 +16,15 @@
 import com.ruoyi.account.vo.UserPointDetailVO;
 import com.ruoyi.account.vo.UserPointStatistics;
 import com.ruoyi.account.vo.UserPointVO;
+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.system.api.model.LoginUser;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -55,6 +58,8 @@
     private PointSettingService pointSettingService;
     @Resource
     private UserPointMapper userPointMapper;
+    @Resource
+    private ShopClient shopClient;
 
     @Override
     public UserPointVO getUserPoint(Long userId) {
@@ -69,9 +74,9 @@
         userPointVO.setConsumePoint(appUser.getLavePoint());
         userPointVO.setShopPoint(appUser.getShopPoint());
         userPointVO.setSharePoint(appUser.getSharePoint());
-        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.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);
         return userPointVO;
     }
@@ -117,30 +122,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();
+        // 可转赠积分总数
+        int adjustedPoint = getAdjustedPoint(pointSetting, appUser);
 
-        UserPoint userPoint = new UserPoint();
-        userPoint.setAppUserId(userid);
-        List<UserPoint> userPointList = userPointMapper.findLatestChangeByType(userPoint);
-        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("转赠积分不足");
         }
 
@@ -157,85 +147,107 @@
         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);
+    /**
+     * 获取可转赠积分
+     * @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;
     }
+
 
     @Override
     public UserPointStatistics getStatistics(UserPoint userPoint) {
-        try {
-            if (StringUtils.isNotEmpty(userPoint.getUserName()) && StringUtils.isNotEmpty(userPoint.getPhone())) {
-                List<Long> userIds = appUserService.listObjs(new LambdaQueryWrapper<AppUser>()
-                                .select(AppUser::getId)
-                                .like(AppUser::getName, userPoint.getUserName())
-                                .like(AppUser::getPhone, userPoint.getPhone()))
-                        .stream()
-                        .map(appUserId -> (Long) appUserId)
-                        .collect(Collectors.toList());
-                userPoint.setUserIds(userIds);
-            }
+        List<UserPoint> userPointList = this.baseMapper.selectUserPoint(userPoint);
+        Map<Integer, Integer> userBalanceMap = userPointList.stream()
+                .collect(Collectors.groupingBy(
+                        UserPoint::getType,
+                        Collectors.summingInt(UserPoint::getVariablePoint)
+                ));
 
-            List<UserPoint> userPointList = userPointMapper.findLatestChangeByType(userPoint);
+        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);
 
-            Map<Integer, Integer> userBalanceMap = userPointList.stream()
-                    .collect(Collectors.groupingBy(
-                            UserPoint::getType,
-                            Collectors.summingInt(UserPoint::getBalance)
-                    ));
+        // 做工积分:签到积分 + 每日分享积分 + 每日签到积分 + 每日使用时长积分
+        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 consumePoint = userBalanceMap.getOrDefault(PointChangeType.CONSUME.getCode(), 0);
-            int sharePoint = userBalanceMap.getOrDefault(PointChangeType.COMMISSION_RETURN.getCode(), 0);
-            int pullNewPoint = userBalanceMap.getOrDefault(PointChangeType.NEW_USER_REFERRAL.getCode(), 0);
-            int registerPoint = userBalanceMap.getOrDefault(PointChangeType.REGISTRATION.getCode(), 0);
-            int workPoint = userBalanceMap.getOrDefault(PointChangeType.WORK_PERFORMANCE.getCode(), 0);
-            int shopAchievementPoint = userBalanceMap.getOrDefault(PointChangeType.TECHNICIAN_PERFORMANCE.getCode(), 0);
-            int exchangeGoodsPoint = userBalanceMap.getOrDefault(PointChangeType.EXCHANGE_GOODS.getCode(), 0);
-            int storeAchievementPoint = userBalanceMap.getOrDefault(PointChangeType.STORE_PERFORMANCE.getCode(), 0);
-            int storeCommissionPoint = userBalanceMap.getOrDefault(PointChangeType.STORE_COMMISSION_RETURN.getCode(), 0);
-            int transferPoint = userBalanceMap.getOrDefault(PointChangeType.TRANSFER_POINTS.getCode(), 0);
-            int totalPoint = consumePoint + sharePoint + pullNewPoint + registerPoint + workPoint + shopAchievementPoint + exchangeGoodsPoint + storeAchievementPoint + storeCommissionPoint + transferPoint;
-
-            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;
-        } catch (Exception e) {
-            // 记录异常日志
-            log.error("获取用户点统计信息时出错", e);
-            throw new RuntimeException("获取用户点统计信息时出错", e);
-        }
+        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) {
-        List<AppUser> appUserList = appUserService.list(new LambdaQueryWrapper<AppUser>()
-                .eq(AppUser::getPhone, userPoint.getPhone())
-                .like(AppUser::getName, userPoint.getUserName()));
-        if (appUserList.isEmpty()){
-            return page;
-        }
-
-        List<Long> userIds = appUserList.stream().map(AppUser::getId).collect(Collectors.toList());
-
-        Page<UserPoint> userPointPage = page(page, new LambdaQueryWrapper<UserPoint>()
-                .in(UserPoint::getAppUserId, userIds)
-                .eq(userPoint.getType() != null, UserPoint::getType, userPoint.getType())
-                .between( userPoint.getStartTime()!= null && userPoint.getEndTime() !=null,UserPoint::getCreateTime, userPoint.getStartTime(), userPoint.getEndTime())
-                .orderByDesc(UserPoint::getCreateTime));
-
-        userPointPage.getRecords().forEach(userPoint1 -> appUserList.stream()
-                .filter(appUser -> appUser.getId().equals(userPoint1.getAppUserId()))
-                .findFirst().ifPresent(appUser -> {
-                    userPoint1.setUserName(appUser.getName());
-                    userPoint1.setPhone(appUser.getPhone());
-                }));
-        return userPointPage;
+        return this.baseMapper.queryUserPointPage(page, userPoint);
     }
 }

--
Gitblit v1.7.1