From 086a293cfc5fdf56cf25fc5989332ec40e319940 Mon Sep 17 00:00:00 2001
From: phpcjl <phpcjl@gmail.com>
Date: 星期二, 17 十二月 2024 14:03:41 +0800
Subject: [PATCH] 1
---
ruoyi-service/ruoyi-account/src/main/java/com/ruoyi/account/service/impl/UserPointServiceImpl.java | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 215 insertions(+), 4 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 f010f2e..5751140 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
@@ -1,26 +1,237 @@
package com.ruoyi.account.service.impl;
+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.mapper.UserPointMapper;
+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.core.utils.StringUtils;
+import com.ruoyi.common.security.service.TokenService;
+import com.ruoyi.other.api.domain.PointSetting;
+import com.ruoyi.other.api.domain.VipSetting;
+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.util.stream.Collectors;
/**
* <p>
- * 服务实现类
+ * 服务实现类
* </p>
*
* @author luodangjia
* @since 2024-11-21
*/
@Service
+@Slf4j
public class UserPointServiceImpl extends ServiceImpl<UserPointMapper, UserPoint> implements UserPointService {
+ @Resource
+ private AppUserService appUserService;
+ @Resource
+ private TokenService tokenService;
+ @Resource
+ private VipSettingService vipSettingService;
+ @Resource
+ private PointSettingService pointSettingService;
+ @Resource
+ private UserPointMapper userPointMapper;
@Override
public UserPointVO getUserPoint(Long userId) {
- // TODO 待实现
- return null;
+ 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.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.setGiftPoint(vipSetting.getVipGiftRole() == 1 && vipSetting.getId() == 1 ? 1 : 0);
+ return userPointVO;
+ }
+
+ @Override
+ public List<UserPointDetailVO> getUserPointDetail(Long userId, LocalDateTime startTime, LocalDateTime endTime, Integer type) {
+ List<UserPoint> userPointList = list(new LambdaQueryWrapper<UserPoint>()
+ .between(startTime != null, UserPoint::getCreateTime, startTime, endTime)
+ .eq(type != null, UserPoint::getType, type)
+ .eq(UserPoint::getAppUserId, userId));
+ 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());
+ return userPointDetailVO;
+ }).collect(Collectors.toList());
+ }
+ 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("用户未找到");
+ }
+ Integer totalPoint = appUser.getLavePoint();
+
+ PointSetting pointSetting = pointSettingService.getPointSettingByAppUserId(userid);
+ if (pointSetting == null) {
+ throw new ServiceException("积分设置未找到");
+ }
+ Integer buyPointOpen = pointSetting.getBuyPointOpen();
+
+ 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) {
+ 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);
+
+ 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) {
+ 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 = userPointMapper.findLatestChangeByType(userPoint);
+
+ Map<Integer, Integer> userBalanceMap = userPointList.stream()
+ .collect(Collectors.groupingBy(
+ UserPoint::getType,
+ Collectors.summingInt(UserPoint::getBalance)
+ ));
+
+ 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);
+ }
+ }
+
+
+ @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()));
+ List<Long> userIds = appUserList.stream().map(AppUser::getId).collect(Collectors.toList());
+
+ Page<UserPoint> userPointPage = page(page, new LambdaQueryWrapper<UserPoint>()
+ .in(!CollectionUtils.isEmpty(userIds), 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;
}
}
--
Gitblit v1.7.1