luodangjia
2024-12-16 a8d2cb07f6440dc54dc4005b0b06d5a47cb1517d
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
package com.ruoyi.account.service.impl;
 
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.UserPoint;
import com.ruoyi.account.service.AppUserService;
import com.ruoyi.account.service.UserPointService;
import com.ruoyi.account.service.VipSettingService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.other.api.domain.VipSetting;
import com.ruoyi.other.api.feignClient.RemoteVipSettingClient;
import com.ruoyi.other.api.feignClient.VipSettingClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
 
@Service
public class VipSettingServiceImpl implements VipSettingService {
 
    @Resource
    private RemoteVipSettingClient remoteVipSettingClient;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private AppUserService appUserService;
    @Resource
    private VipSettingClient vipSettingClient;
    @Resource
    private UserPointService userPointService;
    @Autowired
    private PointSettingServiceImpl pointSettingServiceImpl;
 
 
    @Override
    public VipSetting getVipSettingById(Integer id) {
        R<VipSetting> vipSettingById = remoteVipSettingClient.getVipSettingById(id);
        if (!R.isSuccess(vipSettingById)){
            throw new ServiceException("会员等级获取失败");
        }
        return vipSettingById.getData();
    }
 
    @Override
    public VipSetting getVipSettingByUserId(Long appUserId) {
        AppUser appUser = appUserClient.getAppUserById(appUserId);
        if(null == appUser){
            throw new ServiceException("用户不存在");
        }
        return getVipSettingById(appUser.getVipId());
    }
 
    @Override
    public void downUsers() {
                //查出可能需要降级的人员
        List<Integer> vipIds =  new ArrayList<>();
        vipIds.add(5);
        vipIds.add(6);
        vipIds.add(7);
        List<AppUser> list = appUserService.lambdaQuery().in(AppUser::getVipId, vipIds).list();
        VipSetting vipSetting5 = vipSettingClient.getVipSetting(5).getData();
        VipSetting vipSetting6 = vipSettingClient.getVipSetting(6).getData();
        VipSetting vipSetting7 = vipSettingClient.getVipSetting(7).getData();
        LocalDate now = LocalDate.now();
        //循环判断是否要展示
        if (list.size()>0){
            for (AppUser appUser : list){
                boolean danger = false;
                if (appUser.getVipId()==5){
                    extracted(vipSetting5, now,danger);
                }
                if (appUser.getVipId()==6){
                    extracted(vipSetting6, now,danger);
                }
                if (appUser.getVipId()==7){
                    extracted(vipSetting7, now,danger);
                }
                if (danger){
                    appUser.setIsDanger(1);
                }else {
                    appUser.setIsDanger(0);
                }
            }
            appUserService.updateBatchById(list);
        }
    }
 
    private void extracted(VipSetting vipSetting5, LocalDate now,boolean danger) {
 
        if (vipSetting5.getKeepBuyPoint()!=null){
            //如果消费不为空,查找对应天数的消费积分
            List<UserPoint> list1 = userPointService.lambdaQuery().eq(UserPoint::getType,1).ge(UserPoint::getCreateTime, now.minusDays(vipSetting5.getKeepBuyDay())).list();
            //如果消费积分小于保级积分,设置用户降级标志并将降级信息
            Integer point = 0;
            for (UserPoint userPoint : list1) {
                point = point+userPoint.getVariablePoint();
            }
            if (point<= vipSetting5.getKeepBuyPoint()){
                danger = true;
            }
        }
        if (vipSetting5.getKeepSharePoint()!=null){
            //如果消费不为空,查找对应天数的消费积分
            List<UserPoint> list1 = userPointService.lambdaQuery().eq(UserPoint::getType,2).ge(UserPoint::getCreateTime, now.minusDays(vipSetting5.getKeepBuyDay())).list();
            //如果消费积分小于保级积分,设置用户降级标志并将降级信息
            Integer point = 0;
            for (UserPoint userPoint : list1) {
                point = point+userPoint.getVariablePoint();
            }
            if (point<= vipSetting5.getKeepBuyPoint()){
                danger = true;
            }
        }
        if (vipSetting5.getKeepShopPoint()!=null){
            //如果消费不为空,查找对应天数的消费积分
            List<UserPoint> list1 = userPointService.lambdaQuery().eq(UserPoint::getType,5).ge(UserPoint::getCreateTime, now.minusDays(vipSetting5.getKeepBuyDay())).list();
            //如果消费积分小于保级积分,设置用户降级标志并将降级信息
            Integer point = 0;
            for (UserPoint userPoint : list1) {
                point = point+userPoint.getVariablePoint();
            }
            if (point<= vipSetting5.getKeepBuyPoint()){
                danger = true;
            }
        }
    }
}