From 49f2eff2cf52e0f1719dfad43cafff11b378252c Mon Sep 17 00:00:00 2001
From: Pu Zhibing <393733352@qq.com>
Date: 星期五, 20 十二月 2024 18:14:26 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java |  189 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 162 insertions(+), 27 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 35a79e3..05b2c91 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,20 +2,29 @@
 
 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.feignClient.AppUserClient;
 import com.ruoyi.account.api.model.AppUser;
-import com.ruoyi.account.api.model.BalanceChangeRecord;
 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.domain.R;
+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.feignClient.RemoteVipSettingClient;
+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 javax.annotation.Resource;
 import java.math.BigDecimal;
@@ -34,36 +43,31 @@
  * @since 2024-11-21
  */
 @Service
+@Slf4j
 public class UserPointServiceImpl extends ServiceImpl<UserPointMapper, UserPoint> implements UserPointService {
     @Resource
-    private AppUserClient appUserClient;
+    private AppUserService appUserService;
     @Resource
-    private RemoteVipSettingClient remoteVipSettingClient;
+    private TokenService tokenService;
+    @Resource
+    private VipSettingService vipSettingService;
+    @Resource
+    private PointSettingService pointSettingService;
 
     @Override
     public UserPointVO getUserPoint(Long userId) {
-        AppUser appUser = appUserClient.getAppUserById(userId);
-        List<UserPoint> userPointList = list(new LambdaQueryWrapper<UserPoint>()
-                .eq(UserPoint::getAppUserId, userId));
+        AppUser appUser = appUserService.getById(userId);
+        VipSetting vipSetting = vipSettingService.getVipSettingByUserId(userId);
 
-        Map<Integer, Integer> userBalanceMap = userPointList.stream()
-                .collect(Collectors.toMap(UserPoint::getType, UserPoint::getBalance));
-
-        R<VipSetting> r = remoteVipSettingClient.getVipSettingById(appUser.getVipId());
-        if (!R.isSuccess(r)){
-            throw new RuntimeException("会员等级获取失败");
-        }
-
-        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(r.getData().getVipGiftRole());
+        userPointVO.setTotalPoint(appUser.getTotalPoint());
+        userPointVO.setConsumePoint(appUser.getLavePoint());
+        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;
     }
 
@@ -84,4 +88,135 @@
         }
         return Collections.emptyList();
     }
+
+
+    @Override
+    @Transactional
+    public void transferPoint(BigDecimal point, String phone) {
+        if (!PhoneNumberValidator.isValidChinaPhoneNumber(phone)) {
+            throw new ServiceException("无效的电话号码");
+        }
+
+        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
+        Long userid = loginUserApplet.getUserid();
+
+        VipSetting vipSetting = vipSettingService.getVipSettingByUserId(userid);
+        if (vipSetting == null) {
+            throw new ServiceException("VIP 设置未找到");
+        }
+        if (vipSetting.getId() == 0 && vipSetting.getVipGiftRole() == 0) {
+            throw new ServiceException("转赠积分权限未开启");
+        }
+
+        AppUser appUser = appUserService.getById(userid);
+        if (appUser == null) {
+            throw new ServiceException("用户未找到");
+        }
+
+        PointSetting pointSetting = pointSettingService.getPointSettingByAppUserId(userid);
+        if (pointSetting == null) {
+            throw new ServiceException("积分设置未找到");
+        }
+        // 可转赠积分总数
+        long adjustedPoint = getAdjustedPoint(pointSetting, appUser);
+
+        if (point.compareTo(new BigDecimal(adjustedPoint)) > 0) {
+            throw new ServiceException("转赠积分不足");
+        }
+
+        AppUser appUserForPhoe = appUserService.getOne(new LambdaQueryWrapper<AppUser>()
+                .eq(AppUser::getPhone, phone));
+        if (appUserForPhoe == null) {
+            throw new ServiceException("目标用户未找到");
+        }
+
+        appUserForPhoe.setLavePoint(appUserForPhoe.getLavePoint() + point.intValue());
+        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());
+    }
+
+    /**
+     * 获取可转赠积分
+     * @param pointSetting 积分设置
+     * @param appUser 用户
+     */
+    private long getAdjustedPoint(PointSetting pointSetting, AppUser appUser) {
+        if (pointSetting == null || appUser == null) {
+            throw new ServiceException("pointSetting和appUser不能为空");
+        }
+
+        long transferPoint = 0;
+
+        transferPoint += calculatePoint(pointSetting.getBuyPointGift(), appUser.getShopPoint(), pointSetting.getBuyPoint());
+        transferPoint += calculatePoint(pointSetting.getSharePointGift(), appUser.getSharePoint(), pointSetting.getSharePoint());
+        transferPoint += calculatePoint(pointSetting.getShopSharePointGift(), appUser.getShopSharePoint(), pointSetting.getShopSharePoint());
+        transferPoint += calculatePoint(pointSetting.getShopPointGift(), appUser.getShopAchievementPoint(), pointSetting.getShopPoint());
+        transferPoint += calculatePoint(pointSetting.getPersonPointGift(), appUser.getTotalPerformancePoint(), pointSetting.getPersonPoint());
+        transferPoint += calculatePoint(pointSetting.getGetNewPointGift(), appUser.getTotalInvitePoint(), pointSetting.getGetNewPoint());
+        transferPoint += calculatePoint(pointSetting.getGetRegisPointGift(), appUser.getTotalRegisterPoint(), pointSetting.getRegisPoint());
+        transferPoint += calculatePoint(pointSetting.getWorkPointGift(), appUser.getTotalSharePoint() + appUser.getTotalSignPoint() + appUser.getTotalHourPoint(), pointSetting.getWorkPoint());
+
+        return transferPoint;
+    }
+
+    private long calculatePoint(Integer settingValue, double userValue, double pointValue) {
+        if (settingValue == 1) {
+            try {
+                double result = userValue * (pointValue / 100.0);
+                return Math.round(result);
+            } catch (ArithmeticException e) {
+                return 0;
+            }
+        }
+        return 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