phpcjl
2024-12-05 a40ad9a47d99b40282ea6becb8cae78829a25e70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.ruoyi.account.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.UserPointService;
import com.ruoyi.account.vo.UserPointDetailVO;
import com.ruoyi.account.vo.UserPointVO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.other.api.domain.VipSetting;
import com.ruoyi.other.api.feignClient.RemoteVipSettingClient;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-21
 */
@Service
public class UserPointServiceImpl extends ServiceImpl<UserPointMapper, UserPoint> implements UserPointService {
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private RemoteVipSettingClient remoteVipSettingClient;
 
    @Override
    public UserPointVO getUserPoint(Long userId) {
        AppUser appUser = appUserClient.getAppUserById(userId);
        List<UserPoint> userPointList = list(new LambdaQueryWrapper<UserPoint>()
                .eq(UserPoint::getAppUserId, 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());
        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();
    }
}