luodangjia
2024-12-19 60f70f7409ec1ece8905e088fb43e0cb0258a70b
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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.api.model.AppUser;
import com.ruoyi.account.api.model.UserPoint;
import com.ruoyi.other.api.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.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;
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;
    @Resource
    private ShopClient shopClient;
 
    @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.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);
        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("用户未找到");
        }
 
        PointSetting pointSetting = pointSettingService.getPointSettingByAppUserId(userid);
        if (pointSetting == null) {
            throw new ServiceException("积分设置未找到");
        }
        // 可转赠积分总数
        int 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);
 
        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;
    }
 
 
    @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);
    }
}