puzhibing
2024-10-23 cfae7e3c080dbb5bdf5850814d12c518537d0bde
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.ruoyi.other.controller;
 
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.account.api.dto.SendCouponDto;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.feignClient.AppUserVipDetailClient;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserVipDetail;
import com.ruoyi.account.api.vo.GetAppUserVipDetail;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.BasePage;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.other.api.domain.TCoupon;
import com.ruoyi.other.api.domain.TIntegralRule;
import com.ruoyi.other.api.domain.TUserTag;
import com.ruoyi.other.api.domain.TVip;
import com.ruoyi.other.api.dto.VipCouponDto;
import com.ruoyi.other.api.dto.VipInfoDto;
import com.ruoyi.other.service.TCouponService;
import com.ruoyi.other.service.TVipService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 无关风月
 * @since 2024-08-06
 */
@RestController
@RequestMapping("/vip")
public class TVipController {
    @Autowired
    private TVipService vipService;
    @Resource
    private TCouponService couponService;
    @Resource
    private TokenService tokenService;
    @PostMapping("/saveVip")
    @ApiOperation(tags = {"管理后台-会员管理"},value = "会员添加")
    @Log(title = "【会员管理】添加会员", businessType = BusinessType.INSERT)
    public AjaxResult saveVip(@RequestBody TVip dto) {
        vipService.save(dto);
        return AjaxResult.success();
    }
    @GetMapping("/delete")
    @ApiOperation(tags = {"管理后台-会员管理"},value = "会员删除")
    @Log(title = "【会员管理】删除会员", businessType = BusinessType.DELETE)
    public AjaxResult delete(String ids) {
        vipService.removeBatchByIds(Arrays.asList(ids.split(",")));
        return AjaxResult.success();
    }
    @PostMapping("/updateVip")
    @ApiOperation(tags = {"管理后台-会员管理"},value = "会员修改")
    @Log(title = "【会员管理】修改会员", businessType = BusinessType.UPDATE)
    public AjaxResult updateVip(@RequestBody TVip dto) {
            vipService.updateById(dto);
        return AjaxResult.success();
    }
    @GetMapping("/getInfo")
    @ApiOperation(tags = {"管理后台-会员管理"},value = "会员查看详情")
    public AjaxResult<TVip> getInfo(Integer id) {
        return AjaxResult.ok(vipService.getById(id));
    }
 
 
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查会员")
    public R<TVip> getById(Integer id) {
        return R.ok(vipService.getById(id));
    }
 
    /**
     * 远程调用
     * @param id
     * @return
     */
    @PostMapping("/getInfo1")
    public R<TVip> getInfo1(@RequestParam("id")Integer id) {
        return R.ok(vipService.getById(id));
    }
 
 
 
 
    @ApiOperation(tags = {"管理后台-会员管理"},value = "会员列表分页查询")
    @PostMapping(value = "/pageList")
    public AjaxResult<PageInfo<TVip>> pageList(@RequestBody BasePage basePage) {
        return AjaxResult.ok(vipService.pageList(basePage.getPageCurr(), basePage.getPageSize()));
    }
 
    @ApiOperation(tags = {"会员下拉框"},value = "会员列表分页查询")
    @GetMapping(value = "/select")
    public AjaxResult<List<TVip>> select() {
        return AjaxResult.ok(vipService.list());
    }
 
    //获取会员Map
    @PostMapping(value = "/getVipMap")
    public R<Map<Integer, String>> getVipMap(@RequestBody List<Integer> ids) {
 
        List<TVip> list = vipService.lambdaQuery().in(!ids.isEmpty(), TVip::getId, ids).list();
        Map<Integer, String> idNameMap = list.stream()
                .collect(Collectors.toMap(
                        TVip::getId,
                        TVip::getName
                ));
        return R.ok(idNameMap);
    }
 
    /**
     * 获取最高抵扣、最低起步价,最高折扣的会员
     * @param type 1=最高抵扣、2=最低起步价,3=最高折扣
     * @return
     */
    @PostMapping(value = "/getVipInfoByType")
    public R<TVip> getVipInfoByType(@RequestParam("type") Integer type){
        switch (type){
            case 1:
                return R.ok(vipService.getOne(Wrappers.lambdaQuery(TVip.class)
                        .eq(TVip::getReveal,1)
                        .orderByDesc(TVip::getMaximumDeduction)
                        .orderByDesc(TVip::getCreateTime)
                        .last("LIMIT 1")));
            case 2:
                return R.ok(vipService.getOne(Wrappers.lambdaQuery(TVip.class)
                        .eq(TVip::getReveal,1).eq(TVip::getType,1)
                        .orderByAsc(TVip::getMonthlyCard)
                        .last("LIMIT 1")));
            default:
                return R.ok(vipService.getOne(Wrappers.lambdaQuery(TVip.class)
                        .eq(TVip::getReveal,1)
                        .orderByDesc(TVip::getMonthlyCardDiscount)
                        .orderByDesc(TVip::getCreateTime)
                        .last("LIMIT 1")));
        }
    }
 
 
    @ApiOperation(value = "会员信息", tags = {"小程序-个人中心"})
    @GetMapping("/vipInfo")
    public R<List<VipInfoDto>> vipInfo() {
        List<VipInfoDto> vipInfoDtos = new ArrayList<>();
        List<TVip> vips = vipService.lambdaQuery().eq(TVip::getReveal, 1).list();
        LocalDateTime now = LocalDateTime.now();
        for (TVip vip : vips) {
            VipInfoDto vipInfoDto = new VipInfoDto();
            vipInfoDto.setMonthlyCard(vip.getMonthlyCardReveal()==1?vip.getMonthlyCard():null);
            vipInfoDto.setSeasonCard(vip.getSeasonCardReveal()==1?vip.getSeasonCard():null);
            vipInfoDto.setAnnualCard(vip.getAnnualCardReveal()==1?vip.getAnnualCard():null);
            //判断是否在折扣时间内,然后设置折扣价格
            if (vip.getMonthlyCardDiscountStart()!=null&&(now.isAfter(vip.getMonthlyCardDiscountStart())&&now.isBefore(vip.getMonthlyCardDiscountEnd()))){
                vipInfoDto.setMonthlyCardDiscount(vip.getMonthlyCardDiscount());
            }
            if (vip.getSeasonCardDiscountStart()!=null&&(now.isAfter(vip.getSeasonCardDiscountStart())&&now.isBefore(vip.getSeasonCardDiscountEnd()))){
                vipInfoDto.setSeasonCardDiscount(vip.getSeasonCardDiscount());
            }
            if (vip.getAnnualCardDiscountStart()!=null&&(now.isAfter(vip.getAnnualCardDiscountStart())&&now.isBefore(vip.getAnnualCardDiscountEnd()))){
                vipInfoDto.setAnnualCardDiscount(vip.getAnnualCardDiscount());
            }
 
 
 
            vipInfoDto.setMaximumDeduction(vip.getMaximumDeduction());
            vipInfoDto.setDiscountTimes(vip.getDiscountTimes());
            vipInfoDto.setDoubleIntegration(vip.getDoubleIntegration());
            vipInfoDto.setMallExclusivePrice(vip.getMallExclusivePrice());
            vipInfoDto.setName(vip.getName());
            vipInfoDto.setId(vip.getId());
            List<SendCouponDto> javaList = JSON.parseArray(vip.getCoupon()).toJavaList(SendCouponDto.class);
            List<VipCouponDto> vipCouponDtos = new ArrayList<>();
            if (!javaList.isEmpty()){
                for (SendCouponDto sendCouponDto : javaList) {
                    VipCouponDto vipCouponDto = new VipCouponDto();
                    TCoupon byId = couponService.getById(sendCouponDto.getId());
                    vipCouponDto.setNum(sendCouponDto.getNumber());
                    vipCouponDto.setTCoupon(byId);
                    vipCouponDtos.add(vipCouponDto);
                }
            }
 
            vipInfoDto.setVipCouponDtos(vipCouponDtos);
 
            //计算总折扣
            BigDecimal  total = BigDecimal.ZERO;
            total = total.add(vip.getMaximumDeduction().multiply(BigDecimal.valueOf(vip.getDiscountTimes())));
            for (VipCouponDto vipCouponDto : vipCouponDtos) {
                TCoupon tCoupon = vipCouponDto.getTCoupon();
                if (tCoupon.getPreferentialMode()==2) {
                    total.add(tCoupon.getMaximumDiscountAmount());
                }else {
                    total.add(tCoupon.getDiscountAmount());
                }
            }
            vipInfoDto.setTotalDiscount(total);
            vipInfoDto.setTimeAmount(vip.getMaximumDeduction().multiply(BigDecimal.valueOf(vip.getDiscountTimes())));
            vipInfoDtos.add(vipInfoDto);
        }
        return R.ok(vipInfoDtos);
    }
 
 
    @Resource
    private AppUserClient appUserClient;
 
    @Resource
    private AppUserVipDetailClient vipDetailClient;
 
    @ApiOperation(value = "当前生效会员信息", tags = {"小程序-个人中心"})
    @GetMapping("/recent/vipInfo")
    public R<List<VipInfoDto>> recentVipInfo() {
       Long userId = tokenService.getLoginUserApplet().getUserId();
       TAppUser data = appUserClient.getUserById(userId).getData();
       if (data.getVipEndTime()!=null&&data.getVipEndTime().isAfter(LocalDateTime.now())) {
 
           List<VipInfoDto> vipInfoDtos = new ArrayList<>();
 
//           List<TVip> vips = vipService.lambdaQuery().eq(TVip::getId, data.getVipId()).list();
           List<TVip> vips = new ArrayList<>();
           GetAppUserVipDetail getAppUserVipDetail = new GetAppUserVipDetail();
           getAppUserVipDetail.setAppUserId(userId);
           getAppUserVipDetail.setVipId(data.getVipId());
           TAppUserVipDetail data1 = vipDetailClient.getAppUserVipDetail(getAppUserVipDetail).getData();
           String vipJson = data1.getVipJson();
           TVip tVip = JSON.parseObject(vipJson, TVip.class);
           vips.add(tVip);
           LocalDateTime now = LocalDateTime.now();
           for (TVip vip : vips) {
               VipInfoDto vipInfoDto = new VipInfoDto();
               vipInfoDto.setMonthlyCard(vip.getMonthlyCardReveal() == 1 ? vip.getMonthlyCard() : null);
               vipInfoDto.setSeasonCard(vip.getSeasonCardReveal() == 1 ? vip.getSeasonCard() : null);
               vipInfoDto.setAnnualCard(vip.getAnnualCardReveal() == 1 ? vip.getAnnualCard() : null);
               //判断是否在折扣时间内,然后设置折扣价格
               if (vip.getMonthlyCardDiscountStart() != null && (now.isAfter(vip.getMonthlyCardDiscountStart()) && now.isBefore(vip.getMonthlyCardDiscountEnd()))) {
                   vipInfoDto.setMonthlyCardDiscount(vip.getMonthlyCardDiscount());
               }
               if (vip.getSeasonCardDiscountStart() != null && (now.isAfter(vip.getSeasonCardDiscountStart()) && now.isBefore(vip.getSeasonCardDiscountEnd()))) {
                   vipInfoDto.setSeasonCardDiscount(vip.getSeasonCardDiscount());
               }
               if (vip.getAnnualCardDiscountStart() != null && (now.isAfter(vip.getAnnualCardDiscountStart()) && now.isBefore(vip.getAnnualCardDiscountEnd()))) {
                   vipInfoDto.setAnnualCardDiscount(vip.getAnnualCardDiscount());
               }
 
 
               vipInfoDto.setMaximumDeduction(vip.getMaximumDeduction());
               vipInfoDto.setDiscountTimes(vip.getDiscountTimes());
               vipInfoDto.setDoubleIntegration(vip.getDoubleIntegration());
               vipInfoDto.setMallExclusivePrice(vip.getMallExclusivePrice());
               vipInfoDto.setName(vip.getName());
               vipInfoDto.setId(vip.getId());
               if (vip.getCoupon()!=null) {
                   List<SendCouponDto> javaList = JSON.parseArray(vip.getCoupon()).toJavaList(SendCouponDto.class);
                   List<VipCouponDto> vipCouponDtos = new ArrayList<>();
                   if (!javaList.isEmpty()) {
                       for (SendCouponDto sendCouponDto : javaList) {
                           VipCouponDto vipCouponDto = new VipCouponDto();
                           TCoupon byId = couponService.getById(sendCouponDto.getId());
                           vipCouponDto.setNum(sendCouponDto.getNumber());
                           vipCouponDto.setTCoupon(byId);
                           vipCouponDtos.add(vipCouponDto);
                       }
                   }
 
                   vipInfoDto.setVipCouponDtos(vipCouponDtos);
 
               //计算总折扣
               BigDecimal total = BigDecimal.ZERO;
               total = total.add(vip.getMaximumDeduction().multiply(BigDecimal.valueOf(vip.getDiscountTimes())));
               for (VipCouponDto vipCouponDto : vipCouponDtos) {
                   TCoupon tCoupon = vipCouponDto.getTCoupon();
                   if (tCoupon.getPreferentialMode() == 2) {
                       total.add(tCoupon.getMaximumDiscountAmount());
                   } else {
                       total.add(tCoupon.getDiscountAmount());
                   }
               }
 
               vipInfoDto.setTotalDiscount(total);
 
               vipInfoDto.setTimeAmount(vip.getMaximumDeduction().multiply(BigDecimal.valueOf(vip.getDiscountTimes())));
               vipInfoDtos.add(vipInfoDto);
               }
           }
           return R.ok(vipInfoDtos);
 
       }
       return R.ok();
    }
 
 
 
    @ApiOperation(value = "购买会员", tags = {"小程序-个人中心"})
    @GetMapping("/vipInfo/pay")
    public R vipInfoPay(@RequestParam("vipId")Integer vipId,@RequestParam("buyType") Integer buyType,
        @RequestParam("payType") Integer payType
    ) {
 
        TVip byId = vipService.getById(vipId);
        if (byId==null){
            return R.ok("当前会员不存在,请刷新后重试。");
        }
        BigDecimal payMoney = BigDecimal.ZERO;
        BigDecimal discountMoney = BigDecimal.ZERO;
        BigDecimal discount = BigDecimal.ZERO;
        LocalDateTime now = LocalDateTime.now();
        switch (buyType){
            case 1:
                if (byId.getMonthlyCardDiscountStart()!=null&&(now.isAfter(byId.getMonthlyCardDiscountStart())&&now.isBefore(byId.getMonthlyCardDiscountEnd()))){
                    payMoney = byId.getMonthlyCard().multiply(byId.getMonthlyCardDiscount().divide(new BigDecimal(10)));
                    discountMoney = byId.getMonthlyCard().subtract(payMoney);
                    discount = byId.getMonthlyCardDiscount();
                }else {
                    payMoney = byId.getMonthlyCard();
                }
                break;
                case 2:
                    if (byId.getSeasonCardDiscountStart()!=null&&(now.isAfter(byId.getSeasonCardDiscountStart())&&now.isBefore(byId.getSeasonCardDiscountEnd()))){
                        payMoney = byId.getSeasonCard().multiply(byId.getSeasonCardDiscount().divide(new BigDecimal(10)));
                        discountMoney = byId.getSeasonCard().subtract(payMoney);
                        discount = byId.getSeasonCardDiscount();
                    }else {
                        payMoney = byId.getSeasonCard();
                    }
                break;
            case 3:
                if (byId.getAnnualCardDiscountStart()!=null&&(now.isAfter(byId.getAnnualCardDiscountStart())&&now.isBefore(byId.getAnnualCardDiscountEnd()))){
                    payMoney = byId.getAnnualCard().multiply(byId.getAnnualCardDiscount().divide(new BigDecimal(10)));
                    discountMoney = byId.getAnnualCard().subtract(payMoney);
                    discount = byId.getAnnualCardDiscount();
                }else {
                payMoney = byId.getAnnualCard();
                }
                break;
        }
 
        Long userId = tokenService.getLoginUserApplet().getUserId();
 
        return R.ok(vipService.vipInfoPay(byId,payMoney,payType,discountMoney,discount,buyType,userId));
    }
}