liujie
1 天以前 74f8b8074a2fb391b5363b4dca5f99bf31993430
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package com.stylefeng.guns.modular.system.controller.general;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.core.util.SinataUtil;
import com.stylefeng.guns.modular.system.controller.req.CouponSendReq;
import com.stylefeng.guns.modular.system.enums.StatusEnum;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2023-02-14 10:59:23
 */
@Controller
@RequestMapping("/tCoupon")
public class TCouponController extends BaseController {
 
    private String PREFIX = "/system/tCoupon/";
 
    @Autowired
    private ITCouponService tCouponService;
    @Autowired
    private ITUserToCouponService tUserToCouponService;
    @Autowired
    private ITBranchOfficeService branchOfficeService;
    @Autowired
    private ITBranchOfficeAreaService branchOfficeAreaService;
    @Autowired
    private ITActivityCouponService activityCouponService;
    @Autowired
    private ITAppUserService appUserService;
 
    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index(Model model) {
        Integer roleType = ShiroKit.getUser().getRoleType();
        Integer objectId = ShiroKit.getUser().getObjectId();
        List<TBranchOffice> branchOfficeList = new ArrayList<>();
        if(roleType == 1){
            branchOfficeList = branchOfficeService.selectList(new EntityWrapper<TBranchOffice>().eq("status",1));
        }else {
            TBranchOffice branchOffice = new TBranchOffice();
            branchOffice.setId(objectId);
            TBranchOffice tBranchOffice = branchOfficeService.selectById(branchOffice);
            branchOfficeList.add(tBranchOffice);
        }
        model.addAttribute("branchOfficeList",branchOfficeList);
        return PREFIX + "tCoupon.html";
    }
 
    /**
     * 跳转到添加
     */
    @RequestMapping("/tCoupon_add")
    public String tCouponAdd() {
        return PREFIX + "tCoupon_add.html";
    }
 
    /**
     * 跳转到修改
     */
    @RequestMapping("/tCoupon_update/{tCouponId}")
    public String tCouponUpdate(@PathVariable Integer tCouponId, Model model) {
        TCoupon tCoupon = tCouponService.selectById(tCouponId);
        model.addAttribute("item",tCoupon);
        LogObjectHolder.me().set(tCoupon);
        return PREFIX + "tCoupon_edit.html";
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(Integer couponType,Integer couponServiceType,String createTime,Integer branchOfficeId) {
        Integer roleType = ShiroKit.getUser().getRoleType();
        Integer objectId = ShiroKit.getUser().getObjectId();
        EntityWrapper<TCoupon> wrapper = new EntityWrapper<>();
        if(Objects.nonNull(couponType)){
            wrapper.eq("coupon_type",couponType);
        }
        if(Objects.nonNull(couponServiceType)){
            wrapper.eq("coupon_service_type",couponServiceType);
        }
        // 开始,结束时间
        if(StringUtils.hasLength(createTime)){
            String[] split = createTime.split(" - ");
            Date startTime = DateUtil.getDate_str3(split[0]+" 00:00:00");
            Date endTime = DateUtil.getDate_str3(split[1]+" 23:59:59");
            wrapper.between("create_time",startTime,endTime);
        }
        if(roleType != null && roleType ==2){
            wrapper.eq("branch_office_id",objectId);
        }
        if(branchOfficeId != null){
            wrapper.eq("branch_office_id",branchOfficeId);
        }
        if(roleType != null && roleType ==3){
            wrapper.eq("agent_id",objectId);
        }
//        if(activityAreaCode != null && activityAreaCode != ""){
//            String areaCode = null;
//            if(StringUtils.hasLength(activityAreaCode)){
//                String[] split = activityAreaCode.split("-");
//                areaCode = split[2];
//            }
//            List<TBranchOfficeArea> branchOfficeAreaList = branchOfficeAreaService.selectList(new EntityWrapper<TBranchOfficeArea>().eq("areaCode", areaCode));
//            if(CollectionUtils.isEmpty(branchOfficeAreaList)){
//                return new ArrayList<>();
//            }
//            Integer branchOfficeId = branchOfficeAreaList.get(0).getBranchOfficeId();
//            wrapper.eq("branch_office_id",branchOfficeId);
//        }
        wrapper.ne("status", StatusEnum.DELETE.getCode());
        wrapper.orderBy(true,"create_time",false);
        List<TCoupon> tCoupons = tCouponService.selectList(wrapper);
        List<TBranchOffice> tBranchOffices = branchOfficeService.selectList(new EntityWrapper<>());
        for (TCoupon tCoupon : tCoupons) {
            int couponId = tUserToCouponService.selectCount(new EntityWrapper<TUserToCoupon>().eq("couponId", tCoupon.getId()));
            tCoupon.setCouponCount((long) couponId);
            tBranchOffices.stream().filter(tBranchOffice -> tBranchOffice.getId().equals(tCoupon.getBranchOfficeId())).findFirst().ifPresent(tBranchOffice -> {
                tCoupon.setBranchOfficeName(tBranchOffice.getBranchOfficeName());
            });
        }
        return tCoupons;
    }
    /**
     * 获取列表
     */
    @RequestMapping(value = "/userToCouponList")
    @ResponseBody
    public Object userToCouponList(Integer activityId) {
        Integer roleType = ShiroKit.getUser().getRoleType();
        Integer objectId = ShiroKit.getUser().getObjectId();
 
        // 查询活动信息
        List<TActivityCoupon> activityCoupons = activityCouponService.selectList(new EntityWrapper<TActivityCoupon>().eq("activityId", activityId));
        if(CollectionUtils.isEmpty(activityCoupons)){
            return SUCCESS_TIP;
        }
        List<Integer> couponIds = activityCoupons.stream().map(TActivityCoupon::getCouponId).collect(Collectors.toList());
        List<TCoupon> coupons = tCouponService.selectList(new EntityWrapper<TCoupon>().in("id", couponIds));
        List<TUserToCoupon> userToCoupons = tUserToCouponService.selectList(new EntityWrapper<TUserToCoupon>().in("couponId", couponIds));
        if(CollectionUtils.isEmpty(userToCoupons)){
            return SUCCESS_TIP;
        }
        List<Integer> userIds = userToCoupons.stream().map(TUserToCoupon::getUserId).collect(Collectors.toList());
        List<TAppUser> users = appUserService.selectList(new EntityWrapper<TAppUser>().in("id", userIds));
        for (TUserToCoupon userToCoupon : userToCoupons) {
            coupons.stream().filter(coupon -> coupon.getId().equals(userToCoupon.getCouponId())).findFirst().ifPresent(tCoupon -> {
                userToCoupon.setCouponName(tCoupon.getCouponName());
            });
            users.stream().filter(user -> user.getId().equals(userToCoupon.getUserId())).findFirst().ifPresent(user -> {
                userToCoupon.setNickname(user.getNickname());
                userToCoupon.setPhone(user.getPhone());
            });
            userToCoupon.setCreateTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(userToCoupon.getCreateTime()));
        }
        return userToCoupons;
    }
 
    /**
     * 获取活动券列表
     */
    @RequestMapping(value = "/activityCouponList")
    @ResponseBody
    public Object activityCouponList(String couponName) {
        Integer roleType = ShiroKit.getUser().getRoleType();
        Integer objectId = ShiroKit.getUser().getObjectId();
        EntityWrapper<TCoupon> wrapper = new EntityWrapper<>();
        if(StringUtils.hasLength(couponName)){
            wrapper.like("coupon_name",couponName);
        }
        if(roleType != null && roleType ==2){
            wrapper.eq("branch_office_id",objectId);
        }
        if(roleType != null && roleType ==3){
            wrapper.eq("agent_id",objectId);
        }
        wrapper.eq("coupon_type",1);
        wrapper.eq("coupon_state",1);
        wrapper.orderBy(true,"create_time",false);
        return tCouponService.selectList(wrapper);
    }
 
    /**
     * 获取推广活动优惠券列表
     */
    @RequestMapping(value = "/activityCouponNewList")
    @ResponseBody
    public Object activityCouponNewList(String createTime,Integer couponType,Integer couponServiceType) {
        Integer roleType = ShiroKit.getUser().getRoleType();
        Integer objectId = ShiroKit.getUser().getObjectId();
        Date startTime = null;
        Date endTime = null;
        if (SinataUtil.isNotEmpty(createTime)){
            String[] timeArray = createTime.split(" - ");
            startTime = DateUtil.getDate_str3(timeArray[0]+" 00:00:00");
            endTime = DateUtil.getDate_str3(timeArray[1]+" 23:59:59");
        }
 
        EntityWrapper<TCoupon> wrapper = new EntityWrapper<>();
        if(StringUtils.hasLength(createTime)){
            wrapper.between("create_time",startTime,endTime);
        }
        if(roleType != null && roleType ==2){
            wrapper.eq("branch_office_id",objectId);
        }
        if(roleType != null && roleType ==3){
            wrapper.eq("agent_id",objectId);
        }
        if(couponType != null){
            wrapper.eq("coupon_type",couponType);
        }
        if(couponServiceType != null){
            wrapper.eq("coupon_service_type",couponServiceType);
        }
        wrapper.eq("coupon_state",1);
        wrapper.orderBy(true,"create_time",false);
        List<TCoupon> coupons = tCouponService.selectList(wrapper);
        return coupons;
    }
 
    /**
     * 获取列表
     */
    @RequestMapping(value = "/list-back")
    @ResponseBody
    public Object listBack(String condition) {
        return tCouponService.selectList(null);
    }
 
    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TCoupon tCoupon) {
        // 校验是否添加的是同类型同名称的优惠券
        Boolean isExit = tCouponService.isExit(tCoupon);
        if(isExit){
            return new SuccessTip(500,"该优惠券名称与类型已存在!");
        }
        // 如果是新人优惠券,查询是否存在已启用的新人优惠券
//        if(tCoupon.getCouponType() == 2){
//            int count = tCouponService.selectCount(new EntityWrapper<TCoupon>()
//                    .eq("coupon_type", 2)
//                    .eq("coupon_state", 1)
//                    .eq("status", true));
//            if(count>0){
//                return new SuccessTip(500,"已存在已启用的新人优惠券!");
//            }
//        }
 
        Integer roleType = ShiroKit.getUser().getRoleType();
        Integer objectId = ShiroKit.getUser().getObjectId();
        if(roleType == 2){
            tCoupon.setBranchOfficeId(objectId);
        }
        if(roleType == 3){
            tCoupon.setAgentId(objectId);
        }
        tCouponService.insert(tCoupon);
        return SUCCESS_TIP;
    }
 
    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tCouponId) {
        /*TCoupon tCoupon = tCouponService.selectById(tCouponId);
        tCoupon.setCouponStatus(StatusEnum.DELETE.getCode());
        tCouponService.updateById(tCoupon);*/
        tCouponService.deleteById(tCouponId);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改优惠券状态
     */
    @RequestMapping(value = "/update-status")
    @ResponseBody
    public Object updateStatus(Integer id,Integer status) {
        TCoupon tCoupon = tCouponService.selectById(id);
 
        // 查询已启用的新人券数量
        int count = tCouponService.selectCount(new EntityWrapper<TCoupon>().eq("coupon_state", 1)
                .eq("coupon_type",2));
 
        // 判断是否为新人券,新人券只可启用一条记录
        if(count > 0 && 2 == tCoupon.getCouponType() && 2 == status){
            return new SuccessTip(500,"已有启动的新人券!");
        }
 
        if(1 == status){
            tCoupon.setCouponState(2);
        }
        if(2 == status){
            tCoupon.setCouponState(1);
        }
        tCouponService.updateById(tCoupon);
        return SUCCESS_TIP;
    }
 
    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TCoupon tCoupon) {
        tCouponService.updateById(tCoupon);
        return SUCCESS_TIP;
    }
 
    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{tCouponId}")
    @ResponseBody
    public Object detail(@PathVariable("tCouponId") Integer tCouponId) {
        return tCouponService.selectById(tCouponId);
    }
 
    /**
     * 发放优惠券
     */
    @ApiOperation(value = "发放优惠券")
    @RequestMapping(value = "/sendCouponToUser")
    @ResponseBody
    public Object sendCouponToUser(CouponSendReq couponSendReq) {
 
        List<Integer> userIds = couponSendReq.getUserIds();
 
        // 查询选择的优惠券
        TCoupon tCoupon = tCouponService.selectById(couponSendReq.getCouponId());
 
        List<TUserToCoupon> tUserToCoupons = new ArrayList<>(userIds.size());
 
        for (Integer userId : userIds) {
            // 创建用户优惠券关联表
            TUserToCoupon tUserToCoupon = new TUserToCoupon();
            tUserToCoupon.setCouponId(tCoupon.getId());
            tUserToCoupon.setUserId(userId);
            tUserToCoupon.setCouponTotal(1);
            tUserToCoupon.setValidCount(1);
            Date expireTime = Date.from(LocalDate.now().plusDays(tCoupon.getCouponValidity()).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
            tUserToCoupon.setExpireTime(expireTime);
 
            // 添加发放人id和类型
            if(ShiroKit.getUser().getRoleType() == 1){
                tUserToCoupon.setObjectId(1);
            }else {
                tUserToCoupon.setObjectId(ShiroKit.getUser().getObjectId());
            }
            tUserToCoupon.setRoleType(ShiroKit.getUser().getRoleType());
 
            tUserToCoupons.add(tUserToCoupon);
        }
        tUserToCouponService.insertBatch(tUserToCoupons);
 
        // 查询该优惠券的列表
        /*List<TCoupon> tCoupons = tCouponService.selectList(new EntityWrapper<TCoupon>().eq("coupon_name", couponSendReq.getCouponName())
                .eq("coupon_status",CouponStatusEnum.UNISSUED.getCode()));
        Assert.isTrue(!CollectionUtils.isEmpty(tCoupons),"该优惠券不存在!");
        List<TCoupon> tCouponList = new ArrayList<>();
        // 判断是选中的人多还是优惠券的数量较多
        if(tCoupons.size() > userIds.size()){
            for (Integer userId : userIds) {
                Iterator<TCoupon> iterator = tCoupons.iterator();
                while (iterator.hasNext()){
                    TCoupon next = iterator.next();
                    next.setCouponStatus(CouponStatusEnum.NOT_USED.getCode());
                    next.setUserId(userId);
                    tCouponList.add(next);
                    iterator.remove();
                    break;
                }
            }
        }else {
            for (TCoupon tCoupon : tCoupons) {
                Iterator<Integer> iterator = userIds.iterator();
                while (iterator.hasNext()){
                    tCoupon.setCouponStatus(CouponStatusEnum.NOT_USED.getCode());
                    tCoupon.setUserId(iterator.next());
                    tCouponList.add(tCoupon);
                    iterator.remove();
                    break;
                }
            }
        }
        if(!CollectionUtils.isEmpty(tCouponList)){
            tCouponService.updateBatchById(tCouponList);
        }*/
        return SUCCESS_TIP;
    }
 
}