1.
phpcjl
2024-12-09 3d6c34f45d76b78151d6586f93acca2943698b08
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
package com.ruoyi.account.controller;
 
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.service.AppUserService;
import com.ruoyi.account.service.VipSettingService;
import com.ruoyi.account.vo.vip.Level;
import com.ruoyi.account.vo.vip.VipLevel;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.other.api.domain.*;
import com.ruoyi.other.api.feignClient.BaseSettingClient;
import com.ruoyi.other.api.feignClient.GoodsVipClient;
import com.ruoyi.other.api.feignClient.RemoteVipSettingClient;
import com.ruoyi.other.api.feignClient.VipGoodClient;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("/vipCenter")
@Api(tags = "小程序-会员中心")
@Log4j2
public class VipCenterController {
    @Resource
    private RemoteVipSettingClient remoteVipSettingClient;
    @Resource
    private BaseSettingClient baseSettingClient;
    @Resource
    private GoodsVipClient goodsVipClient;
    @Resource
    private VipSettingService vipSettingService;
    @Resource
    private TokenService tokenService;
    @Resource
    private VipGoodClient vipGoodClient;
    @Resource
    private AppUserService appUserService;
 
 
    @GetMapping("getVipLevelList")
    @ApiOperation(value = "会员等级列表", tags = {"会员中心-小程序"})
    public R<VipLevel> vipLevelList() {
        try {
            R<List<VipSetting>> r = remoteVipSettingClient.list();
            if (!checkSuccess(r)) {
                return R.fail("会员等级获取失败");
            }
            List<VipSetting> vipSettingList = r.getData();
            if (vipSettingList == null || vipSettingList.isEmpty()) {
                return R.fail("会员等级获取失败");
            }
 
            R<BaseSetting> baseSettingR = baseSettingClient.getBaseSetting(3);
            if (!checkSuccess(baseSettingR)) {
                log.error("【会员设置说明】获取失败:{}", baseSettingR.getMsg());
                return R.fail("会员等级获取失败");
            }
            BaseSetting baseSetting = baseSettingR.getData();
            if (baseSetting == null) {
                log.error("【会员设置说明】未设置");
                return R.fail("会员等级获取失败");
            }
 
            Long userid = tokenService.getLoginUserApplet().getUserid();
            VipSetting loginUserVipSetting = vipSettingService.getVipSettingByUserId(userid);
 
            VipLevel vipLevel = new VipLevel();
            List<Level> levelList = new ArrayList<>();
 
            // 批量获取 goodsVip 和 vipGoods
            List<Integer> vipIds = vipSettingList.stream().map(VipSetting::getId).collect(Collectors.toList());
            Map<Integer, R<GoodsVip>> goodsVipMap = getGoodsVips(vipIds);
            Map<Integer, R<List<VipGood>>> vipGoodsMap = getVipGoods(vipIds);
 
            vipSettingList.forEach(vipSetting -> {
                Level level = new Level();
                BeanUtils.copyBeanProp(level, vipSetting);
                level.setVipDesc(baseSetting.getContent());
 
                R<GoodsVip> goodsVipR = goodsVipMap.get(vipSetting.getId());
                if (R.isSuccess(goodsVipR)) {
                    R<List<VipGood>> vipGoodR = vipGoodsMap.get(vipSetting.getId());
                    if (R.isSuccess(vipGoodR)) {
                        List<VipGood> vipGoodList = vipGoodR.getData();
                        if (vipGoodList != null && !vipGoodList.isEmpty()) {
                            List<String> goodsNames = new ArrayList<>();
                            vipGoodList.forEach(vipGood -> {
                                String goodJson = vipGood.getGoodJson();
                                if (isValidJson(goodJson)) {
                                    Goods goods = JSONObject.parseObject(goodJson, Goods.class);
                                    goodsNames.add(goods.getName());
                                } else {
                                    log.warn("JSON无效: " + goodJson);
                                }
                            });
                            level.setGoodsNames(goodsNames);
                        }
                    }
                }
                levelList.add(level);
            });
 
            vipLevel.setLevelList(levelList);
            vipLevel.setCurrentLevel(loginUserVipSetting.getId());
            return R.ok(vipLevel);
        } catch (Exception e) {
            log.error("会员等级获取失败", e);
            return R.fail("会员等级获取失败");
        }
    }
 
 
    public R<Void> test() {
        LoginUser loginUserApplet = tokenService.getLoginUserApplet();
        AppUser appUser = appUserService.getById(loginUserApplet.getUserid());
        if (appUser.getShopPoint() >= 500){
            return R.ok();
        }
        if (appUser.getSharePoint() >= 400){
            return R.ok();
        }
 
        return R.fail();
    }
 
 
 
 
    private boolean checkSuccess(R<?> r) {
        if (!R.isSuccess(r)) {
            log.error("请求失败: " + r.getMsg());
        }
        return R.isSuccess(r);
    }
 
    private Map<Integer, R<GoodsVip>> getGoodsVips(List<Integer> vipIds) {
        Map<Integer, R<GoodsVip>> result = new HashMap<>();
        for (Integer vipId : vipIds) {
            R<GoodsVip> goodsVipR = goodsVipClient.getGoodsVip(vipId);
            result.put(vipId, goodsVipR);
        }
        return result;
    }
 
    private Map<Integer, R<List<VipGood>>> getVipGoods(List<Integer> vipIds) {
        Map<Integer, R<List<VipGood>>> result = new HashMap<>();
        for (Integer vipId : vipIds) {
//            R<List<VipGood>> vipGoodR = vipGoodClient.getVipGoodsByVipId(vipId);
//            result.put(vipId, vipGoodR);
        }
        return result;
    }
 
    private boolean isValidJson(String json) {
        try {
            JSONObject.parseObject(json);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
 
}