Pu Zhibing
2025-03-18 f615ec5c9239327740948501627545f8e78e2a9e
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
package com.ruoyi.other.controller;
 
 
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.feignClient.UserCouponClient;
import com.ruoyi.account.api.model.AppUser;
import com.ruoyi.account.api.model.UserCoupon;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.other.api.domain.CouponInfo;
import com.ruoyi.other.api.domain.Goods;
import com.ruoyi.other.service.CouponInfoService;
import com.ruoyi.other.service.GoodsService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@RestController
@RequestMapping("/coupon-info")
public class CouponInfoController {
    @Resource
    private CouponInfoService couponInfoService;
    @Resource
    private TokenService tokenService;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private UserCouponClient userCouponClient;
    @Resource
    private GoodsService goodsService;
 
    @GetMapping("/list")
    @ApiOperation(value = "优惠劵管理-列表", tags = {"管理后台-活动管理"})
    public R<IPage<CouponInfo>> list(@RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize,
                                     CouponInfo couponInfo) {
        Integer periodType = couponInfo.getPeriodType();
        if (periodType != null && periodType.equals(0)){
            couponInfo.setPeriodType(null);
        }
        IPage<CouponInfo> couponInfoIPage = couponInfoService.queryCouponInfoPage(Page.of(pageNum, pageSize), couponInfo);
        for (CouponInfo record : couponInfoIPage.getRecords()) {
            R<Long> r = userCouponClient.getCouponCount(record.getId());
            record.setSendNumNow(r.getData());
        }
        return R.ok(couponInfoIPage);
    }
 
 
    //查看详情
    @GetMapping("/detail")
    @ApiOperation(value = "优惠劵管理-详情", tags = {"管理后台-活动管理"})
    public R<CouponInfo> detail(@RequestParam("id") Integer id) {
        CouponInfo byId = couponInfoService.getById(id);
        if (byId == null){
            return R.fail("优惠劵不存在");
        }
        String forGoodIds = byId.getForGoodIds();
        if (!"-1".equals(forGoodIds) && StringUtils.isNotEmpty(forGoodIds)){
            List<Goods> goods = goodsService.listByIds(Arrays.asList(forGoodIds.split(",")));
            byId.setGoods(goods);
            byId.setGoodsNameList(goods.stream().map(Goods::getName).collect(Collectors.toList()));
        }
        String personIds = byId.getPersonIds();
        if (StringUtils.isNotEmpty(personIds)){
            List<Long> ids = Arrays.stream(personIds.split(",")).map(Long::valueOf).collect(Collectors.toList());
            List<AppUser> appUserList = appUserClient.listByIds(ids);
            byId.setAppUserList(appUserList);
        }
 
        return R.ok(byId);
    }
 
    // 删除优惠劵
    @DeleteMapping("/delete")
    @ApiOperation(value = "优惠劵管理-删除", tags = {"管理后台-活动管理"})
    public R<Void> delete(@RequestParam("id") Integer id) {
        couponInfoService.removeById(id);
        return R.ok();
    }
 
    // 编辑优惠劵
    @PostMapping("/edit")
    @ApiOperation(value = "优惠劵管理-编辑", tags = {"管理后台-活动管理"})
    public R<Void> edit(@RequestBody CouponInfo couponInfo) {
        List<String> goodsNameList = couponInfo.getGoodsNameList();
        couponInfo.setGoodsNameJson(JSON.toJSONString(goodsNameList));
        if(couponInfo.getPeriodType() == 2){
            Integer periodDays = couponInfo.getPeriodDays();
            couponInfo.setPeriodStartTime(LocalDate.now());
            couponInfo.setPeriodEndTime(LocalDate.now().plusDays(periodDays));
        }
        couponInfoService.updateById(couponInfo);
        return R.ok();
    }
 
    // 修改上/下架状态
    @PostMapping("/editStatus")
    @ApiOperation(value = "优惠劵管理-修改上/下架状态", tags = {"管理后台-活动管理"})
    public R<Void> editStatus(@RequestParam("id") Integer id,
                               @RequestParam("shelfStatus") Integer shelfStatus) {
        CouponInfo couponInfo = new CouponInfo();
        couponInfo.setId(id);
        couponInfo.setShelfStatus(shelfStatus);
        couponInfoService.updateById(couponInfo);
        return R.ok();
    }
 
    // 添加优惠券
    @PostMapping("/add")
    @ApiOperation(value = "优惠劵管理-添加", tags = {"管理后台-活动管理"})
    public R<Void> add(@RequestBody CouponInfo couponInfo) {
        couponInfo.setShelfStatus(0);
        List<String> goodsNameList = couponInfo.getGoodsNameList();
        couponInfo.setGoodsNameJson(JSON.toJSONString(goodsNameList));
        couponInfo.setShelfStatus(1);
        if(couponInfo.getPeriodType() == 2){
            Integer periodDays = couponInfo.getPeriodDays();
            couponInfo.setPeriodStartTime(LocalDate.now());
            couponInfo.setPeriodEndTime(LocalDate.now().plusDays(periodDays));
        }
        couponInfoService.save(couponInfo);
        return R.ok();
    }
 
 
 
 
 
    @GetMapping("/gift/list")
    @ApiOperation(value = "待领取列表", tags = {"小程序-个人中心-优惠劵"})
    public R<List<CouponInfo>> giftlist() {
        Long userid = tokenService.getLoginUserApplet().getUserid();
        AppUser appUserById = appUserClient.getAppUserById(userid);
        LocalDateTime now = LocalDateTime.now();
 
 
        //查出全部可领取的优惠劵
        List<CouponInfo> list = couponInfoService.lambdaQuery()
                .le(CouponInfo::getSendStartTime, now)
                .ge(CouponInfo::getSendEndTime, now)
                .eq(CouponInfo::getPersonType, 1)
                .list();
        //查出指定人员可领取优惠券
        List<CouponInfo> list1 = couponInfoService.lambdaQuery()
                .le(CouponInfo::getSendStartTime, now)
                .ge(CouponInfo::getSendEndTime, now)
                .eq(CouponInfo::getPersonType, 2)
                .apply("FIND_IN_SET('" + appUserById.getId() + "', person_ids)")
                .list();
        //查出指定会员可领取优惠劵
        List<CouponInfo> list2 = couponInfoService.lambdaQuery()
                .le(CouponInfo::getSendStartTime, now)
                .ge(CouponInfo::getSendEndTime, now)
                .eq(CouponInfo::getPersonType, 3)
                .apply("FIND_IN_SET('" + appUserById.getVipId() + "', vip_ids)").list();
        List<CouponInfo> returnList = new ArrayList<>();
        returnList.addAll(list);
        returnList.addAll(list1);
        returnList.addAll(list2);
 
        List<CouponInfo> collect = returnList.stream().filter(couponInfo -> {
            Integer couponInfoId = couponInfo.getId();
            Integer sendNum = couponInfo.getSendNum();
            Integer maxNum = couponInfo.getMaxNum();
            Long count = appUserClient.getCouponCount(-1L, couponInfoId).getData();
            Long count2 = appUserClient.getCouponCount(userid, couponInfoId).getData();
            return count < sendNum && count2 < maxNum;
        }).collect(Collectors.toList());
 
        return R.ok(collect);
    }
 
    private void count(Long userid, List<CouponInfo> list1, List<CouponInfo> returnList) {
        for (CouponInfo couponInfo : list1) {
            Integer couponInfoId = couponInfo.getId();
            Integer sendNum = couponInfo.getSendNum();
            Integer maxNum = couponInfo.getMaxNum();
            Long count = appUserClient.getCouponCount(-1L, couponInfoId).getData();
            if(count >= sendNum){
                continue;
            }
 
            count = appUserClient.getCouponCount(userid, couponInfoId).getData();
            couponInfo.setMaxNum(maxNum - count.intValue());
            if(count < maxNum){
                continue;
            }
            for (int i = 0; i < maxNum; i++) {
                returnList.add(couponInfo);
            }
        }
    }
 
 
    /**
     * 根据id获取优惠券数据
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("/getCouponInfoList")
    public R<List<CouponInfo>> getCouponInfoList(@RequestParam("ids") List<Integer> ids) {
        List<CouponInfo> couponInfos = couponInfoService.listByIds(ids);
        return R.ok(couponInfos);
    }
 
 
    /**
     * 获取领取记录
     */
 
    @GetMapping("/getReceiveRecord")
    public R<IPage<UserCoupon>> getReceiveRecord(@ApiParam("页码") @RequestParam Integer pageNum, @ApiParam("大小") Integer pageSize,UserCoupon userCoupon) {
        // TODO 待完善
        return null;
    }
 
 
    /**
     * 根据类型获取有效优惠券列表
     */
    @PostMapping("/getCouponInfoByPersonType")
    public R<List<CouponInfo>> getCouponInfoByPersonType(@RequestParam("personType") Integer personType){
        List<CouponInfo> list = couponInfoService.list(new LambdaUpdateWrapper<CouponInfo>().eq(CouponInfo::getPersonType, personType).eq(CouponInfo::getDelFlag, 0).eq(CouponInfo::getShelfStatus, 1).last(" and now() between period_start_time and period_end_time"));
        return R.ok(list);
    }
 
 
}