New file |
| | |
| | | package com.ruoyi.account.controller; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | 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.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 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; |
| | | |
| | | |
| | | @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(); |
| | | level.setId(vipSetting.getId()); |
| | | level.setName(vipSetting.getVipName()); |
| | | level.setVipInfo(vipSetting.getVipInfo()); |
| | | level.setVipDesc(baseSetting.getContent()); |
| | | level.setVipLevelUpShop(vipSetting.getVipLevelUpShop()); |
| | | level.setVipLevelUpShare(vipSetting.getVipLevelUpShare()); |
| | | level.setKeepBuyDay(vipSetting.getKeepBuyDay()); |
| | | level.setKeepBuyPoint(vipSetting.getKeepBuyPoint()); |
| | | level.setKeepShareDay(vipSetting.getKeepShareDay()); |
| | | level.setKeepSharePoint(vipSetting.getKeepSharePoint()); |
| | | level.setKeepShopDay(vipSetting.getKeepShopDay()); |
| | | level.setKeepShopPoint(vipSetting.getKeepShopPoint()); |
| | | |
| | | 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("会员等级获取失败"); |
| | | } |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | } |