hjl
2024-06-18 a7d177dd48ca8c8a7e154573f7306e2b86dd1a77
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
package com.ruoyi.goods.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.goods.domain.*;
import com.ruoyi.goods.dto.*;
import com.ruoyi.goods.service.*;
import com.ruoyi.goods.vo.ExchangeRecordVO;
import com.ruoyi.goods.vo.GoodDetailVO;
import com.ruoyi.goods.vo.TGoodsVO;
import com.ruoyi.goods.vo.TOrderVO;
import com.ruoyi.study.api.domain.TUser;
import com.ruoyi.study.api.feignClient.StudyClient;
import com.ruoyi.system.api.model.LoginUserParent;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 无关风月
 * @since 2024-04-26
 */
@RestController
@RequestMapping("/base/goods")
public class TGoodsController {
    @Resource
    private ITGoodsService goodsService;
    @Autowired
    private ITGoodsTypeService goodsTypeService;
    @Autowired
    private ITOrderService orderService;
    @Resource
    private IRecipientService recipientService;
    @Resource
    private IRegionService regionService;
    @Resource
    private TokenService tokenService;
 
    @PostMapping("/listType")
    @ApiOperation(value = "列表查询", tags = {"后台-商品类型管理"})
    public R<PageInfo<TGoodsType>> listType(@RequestBody GoodsTypeQuery query) {
        QueryWrapper<TGoodsType> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(query.getName())) {
            wrapper.like("name", query.getName());
        }
//        wrapper.ne("isDelete",1);
        wrapper.orderByDesc("id");
        List<TGoodsType> list = goodsTypeService.list(wrapper);
        PageInfo<TGoodsType> res = new PageInfo<>(query.getPageNumber(), query.getPageSize());
        res.setRecords(list);
        return R.ok(res);
    }
 
    @PostMapping("/goodList")
    @ApiOperation(value = "商品列表查询", tags = {"家长端-商品列表"})
    public R<PageInfo<TGoods>> listType(@RequestBody GoodQueryDTO goodQuery) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        List<String> type = goodQuery.getType();
        String keywords = goodQuery.getKeywords();
        // 初始化条件构造器
        QueryWrapper<TGoods> wrapper = new QueryWrapper<>();
        wrapper = keywords != null && "".equals(keywords.trim()) ? wrapper.like("name", keywords) : wrapper;
 
        // 类型匹配 todo
        if (goodQuery.getType() != null && goodQuery.getType().size() > 0) {
            StringBuilder temp = new StringBuilder("");
            for (String s : goodQuery.getType()) {
                wrapper.or().apply("FIND_IN_SET('" + s + "', typeIds)"); // 将每个类型 ID 应用于 FIND_IN_SET 函数
            }
        }
        wrapper.eq("isDelete", 0);
        return R.ok(goodsService.page(new PageInfo<>(goodQuery.getPageNumber(), goodQuery.getPageSize()), wrapper));
    }
 
    @PostMapping("/goodListStudy")
    @ApiOperation(value = "商品列表查询", tags = {"学习端-商城"})
    public R<PageInfo<TGoods>> goodListStudy(@RequestBody GoodQueryDTO goodQuery) {
        if (tokenService.getLoginUserStudy() == null) {
            return R.tokenError("登录失效");
        }
        String keywords = goodQuery.getKeywords();
        // 初始化条件构造器
        QueryWrapper<TGoods> wrapper = new QueryWrapper<>();
        wrapper = keywords != null && !"".equals(keywords.trim()) ? wrapper.like("name", keywords) : wrapper;
 
        // 类型匹配 todo
        if (goodQuery.getType() != null && !goodQuery.getType().isEmpty()) {
            for (String s : goodQuery.getType()) {
                // 将每个类型 ID 应用于 FIND_IN_SET 函数
                wrapper.or().apply("FIND_IN_SET('" + s + "', typeIds)");
            }
        }
        wrapper.eq("isDelete", 0);
        return R.ok(goodsService.page(new PageInfo<>(goodQuery.getPageNumber(), goodQuery.getPageSize()), wrapper));
    }
 
    @PostMapping("/addGoodsType")
    @ApiOperation(value = "添加", tags = {"后台-商品类型管理"})
    public R addGoodsType(@RequestBody TGoodsType dto) {
        goodsTypeService.save(dto);
        return R.ok("添加成功");
    }
 
    @PostMapping("/updateGoodsType")
    @ApiOperation(value = "修改", tags = {"后台-商品类型管理"})
    public R updateGoodsType(@RequestBody TGoodsType dto) {
        goodsTypeService.updateById(dto);
        return R.ok("修改成功");
    }
 
    @PostMapping("/deleteGoodsType/{id}")
    @ApiOperation(value = "删除", tags = {"后台-商品类型管理"})
    public R deleteGoodsType(@PathVariable("id") Integer id) {
        TGoodsType byId = goodsTypeService.getById(id);
        byId.setIsDelete(1);
        goodsTypeService.removeById(byId);
        return R.ok("删除成功");
    }
 
    @PostMapping("/listAll")
    @ApiOperation(value = "列表查询", tags = {"后台-商品管理"})
    public R<PageInfo<TGoods>> listAll(@RequestBody GoodsTypeQuery query) {
        QueryWrapper<TGoods> wrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(query.getName())) {
            wrapper.like("name", query.getName());
        }
        wrapper.orderByDesc("id");
        List<TGoods> list = goodsService.list(wrapper);
        for (TGoods tGoods : list) {
            long goodsId = orderService.count(new QueryWrapper<TOrder>().eq("goodsId", tGoods.getId()));
            tGoods.setInventory(goodsId);
        }
        PageInfo<TGoods> res = new PageInfo<>(query.getPageNumber(), query.getPageSize());
        res.setRecords(list);
        return R.ok(res);
    }
 
    @PostMapping("/addGoods")
    @ApiOperation(value = "添加", tags = {"后台-商品管理"})
    public R addGoods(@RequestBody TGoods dto) {
        goodsService.save(dto);
        return R.ok("添加成功");
    }
 
    @PostMapping("/deleteGoods/{id}")
    @ApiOperation(value = "删除", tags = {"后台-商品管理"})
    public R deleteGoods(@PathVariable("id") Integer id) {
        TGoods byId = goodsService.getById(id);
        goodsService.removeById(byId);
        return R.ok("删除成功");
    }
 
    @PostMapping("/updateGoods")
    @ApiOperation(value = "修改", tags = {"后台-商品管理"})
    public R updateGoods(@RequestBody TGoods dto) {
        goodsService.updateById(dto);
        return R.ok("修改成功");
    }
 
    @PostMapping("/getGoodsInfo/{id}")
    @ApiOperation(value = "查看详情", tags = {"后台-商品管理"})
    public R<TGoodsVO> getGoodsInfo(@PathVariable("id") Integer id) {
        TGoodsVO tGoodsVO = new TGoodsVO();
        TGoods byId = goodsService.getById(id);
        BeanUtils.copyProperties(byId, tGoodsVO);
        long goodsId = orderService.count(new QueryWrapper<TOrder>().eq("goodsId", id));
        tGoodsVO.setInventory(goodsId);
        tGoodsVO.setIntegral(byId.getIntegral());
        return R.ok(tGoodsVO);
    }
 
    @PostMapping("/getGoodsTypeList")
    @ApiOperation(value = "获取商品类型列表", tags = {"后台-商品管理"})
    public R<List<TGoodsType>> getGoodsInfo() {
        List<TGoodsType> res = goodsTypeService.list(new QueryWrapper<>());
        return R.ok(res);
    }
 
    /**
     * 兑换记录
     */
    @GetMapping("/exchangeRecord")
    @ApiOperation(value = "学习端-兑换记录", tags = {"学习端-商城"})
    public R<List<ExchangeRecordVO>> exchangeRecord() {
        List<ExchangeRecordVO> exchangeRecord = orderService.exchangeRecord(tokenService.getLoginUserStudy().getUserid());
        for (ExchangeRecordVO record : exchangeRecord) {
            TGoods goods = goodsService.getById(record.getGoodsId());
            List<String> typeList;
            if (null != goods) {
                List<TGoodsType> goodsTypes = goodsTypeService.lambdaQuery().in(TGoodsType::getId, Arrays.stream(goods.getTypeIds().split(",")).collect(Collectors.toList()))
                        .eq(TGoodsType::getIsDelete, 0).list();
                typeList = goodsTypes.stream().map(TGoodsType::getName).collect(Collectors.toList());
            } else {
                typeList = new ArrayList<>();
            }
            record.setGoodsType(typeList);
        }
        return R.ok(exchangeRecord);
    }
 
    @GetMapping("/exchangeRecordParent")
    @ApiOperation(value = "家长端-兑换记录", tags = {"家长端-兑换记录"})
    public R<Page<TOrder>> exchangeRecordParent(Integer pageNumber, Integer pageSize) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        Page<TOrder> page = orderService.page(new Page<>(pageNumber, pageSize), new QueryWrapper<TOrder>().eq("userId", tokenService.getLoginUser1().getUserid())
                .orderByDesc("createTime"));
        List<TOrder> records = page.getRecords();
        for (TOrder tOrder : records) {
            TGoods byId = goodsService.getById(tOrder.getGoodsId());
            tOrder.setName(byId.getName());
            tOrder.setImg(byId.getCoverImg());
            StringBuilder stringBuilder = new StringBuilder();
            if (StringUtils.hasLength(byId.getTypeIds())) {
                String[] split = byId.getTypeIds().split(",");
                for (String s : split) {
                    TGoodsType byId1 = goodsTypeService.getById(s);
                    if (byId1 != null) {
                        stringBuilder.append(byId1.getName()).append("|");
                    }
                }
                // 去除最后一个字符
                stringBuilder.deleteCharAt(stringBuilder.length() - 1);
                tOrder.setType(stringBuilder.toString());
            }
        }
 
        return R.ok(page);
    }
 
    /**
     * 兑换记录
     */
    @GetMapping("/confirm")
    @ApiOperation(value = "确认收货", tags = {"家长端-兑换记录"})
    @ApiOperationSupport(order = 16)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单id", name = "id", dataType = "String", required = true)
    })
    public R<Boolean> confirm(@RequestParam Integer id) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        TOrder byId = orderService.getById(id);
        byId.setState(3);
        return R.ok(orderService.updateById(byId));
    }
 
    /**
     * 兑换记录
     */
    @GetMapping("/confirmStudy")
    @ApiOperation(value = "确认收货", tags = {"学习端-商城"})
    @ApiOperationSupport(order = 29)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单id", name = "id", dataType = "String", required = true)
    })
    public R<Boolean> confirmStudy(@RequestParam Integer id) {
        if (tokenService.getLoginUserStudy() == null) {
            return R.tokenError("登录失效");
        }
        TOrder byId = orderService.getById(id);
        byId.setState(3);
        return R.ok(orderService.updateById(byId));
    }
 
    /**
     * 获取用户收货地址
     */
    @GetMapping("/shopAddressParent")
    @ApiOperation(value = "获取用户收货地址", tags = {"家长端-获取用户收货地址"})
    @ApiOperationSupport(order = 17)
    public R<List<Recipient>> shopAddressParent() {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        return R.ok(recipientService.lambdaQuery().eq(Recipient::getUserId,
                tokenService.getLoginUser1().getUserid()).list());
    }
 
    /**
     * 设置默认地址
     */
    @GetMapping("/setDefault")
    @ApiOperation(value = "设置默认地址", tags = {"家长端-设置默认地址"})
    @ApiOperationSupport(order = 18)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址id", name = "id", dataType = "String", required = true)
    })
    public R setDefault(@RequestParam Integer id) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        Recipient byId = recipientService.getById(id);
        byId.setIsDefault(1);
        recipientService.updateById(byId);
        // 如果设置为默认地址 那么要将之前的默认地址取消掉
        List<Recipient> userId = recipientService.list(new QueryWrapper<Recipient>()
                .eq("userId", tokenService.getLoginUser1().getUserid())
                .ne("id", byId.getId()));
        for (Recipient recipient1 : userId) {
            recipient1.setIsDefault(0);
            recipientService.updateById(recipient1);
        }
        return R.ok();
    }
 
    /**
     * 设置默认地址
     */
    @GetMapping("/setDefaultStudy")
    @ApiOperation(value = "设置默认地址", tags = {"学习端-收货地址"})
    @ApiOperationSupport(order = 30)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址id", name = "id", dataType = "String", required = true)
    })
    public R setDefaultStudy(@RequestParam Integer id) {
        LoginUserParent userStudy = tokenService.getLoginUserStudy();
        if (null == userStudy) {
            return R.tokenError("登录失效!");
        }
        Recipient byId = recipientService.getById(id);
        byId.setIsDefault(1);
        recipientService.updateById(byId);
        // 如果设置为默认地址 那么要将之前的默认地址取消掉
        List<Recipient> userId = recipientService.list(new QueryWrapper<Recipient>()
                .eq("userId", userStudy.getUserid())
                .ne("id", byId.getId()));
        for (Recipient recipient1 : userId) {
            recipient1.setIsDefault(0);
            recipientService.updateById(recipient1);
        }
        return R.ok();
    }
 
    /**
     * 根据地址id 获取详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getAddressById")
    @ApiOperation(value = "获取地址详情", tags = {"家长端-收货地址"})
    @ApiOperationSupport(order = 19)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址id", name = "id", dataType = "String", required = true)
    })
    public R<Recipient> getAddressById(@RequestParam Integer id) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        Recipient byId = recipientService.getById(id);
        return R.ok(byId);
    }
 
    /**
     * 根据地址id 获取详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getAddressByIdStudy")
    @ApiOperation(value = "获取地址详情", tags = {"学习端-收货地址"})
    @ApiOperationSupport(order = 31)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址id", name = "id", dataType = "String", required = true)
    })
    public R<Recipient> getAddressByIdStudy(@RequestParam Integer id) {
        if (tokenService.getLoginUserStudy() == null) {
            return R.tokenError("登录失效");
        }
        Recipient byId = recipientService.getById(id);
        return R.ok(byId);
    }
 
    /**
     * 获取用户收货地址
     */
    @GetMapping("/shopAddress")
    @ApiOperation(value = "获取用户收货地址列表", tags = {"学习端-收货地址"})
    @ApiOperationSupport(order = 32)
    public R<List<Recipient>> shopAddress() {
        if (tokenService.getLoginUserStudy() == null) {
            return R.tokenError("登录失效");
        }
        return R.ok(recipientService.lambdaQuery().eq(Recipient::getUserId,
                tokenService.getLoginUserStudy().getUserid()).list());
    }
 
    /**
     * 新增收货地址/修改收货地址
     */
    @PostMapping("/addressSaveOrUpdate")
    @ApiOperation(value = "新增收货地址/修改收货地址", tags = {"学习端-收货地址"})
    @ApiOperationSupport(order = 33)
    public R<String> addressSave(@RequestBody Recipient recipient) {
        LoginUserParent userStudy = tokenService.getLoginUserStudy();
        if (null == userStudy) {
            return R.tokenError("登录失效!");
        }
        recipient.setUserId(tokenService.getLoginUserStudy().getUserid());
        if (recipient.getIsDefault() == 1) {
            // 如果设置为默认地址 那么要将之前的默认地址取消掉
            List<Recipient> userId = recipientService.list(new QueryWrapper<Recipient>()
                    .eq("userId", userStudy.getUserid()));
            for (Recipient recipient1 : userId) {
                recipient1.setIsDefault(0);
                recipientService.updateById(recipient1);
            }
        }
        return recipientService.addressSaveOrUpdate(recipient) ? R.ok() : R.fail();
    }
 
    /**
     * 新增收货地址/修改收货地址
     */
    @PostMapping("/addressSaveOrUpdateParent")
    @ApiOperation(value = "家长端-新增收货地址/修改收货地址", tags = {"家长端-新增收货地址/修改收货地址"})
    @ApiOperationSupport(order = 20)
    public R<String> addressSaveOrUpdateParent(@RequestBody Recipient recipient) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效!");
        }
        recipient.setUserId(tokenService.getLoginUser1().getUserid());
        if (recipient.getId() != null) {
            if (recipient.getIsDefault() == 1) {
                // 如果设置为默认地址 那么要将之前的默认地址取消掉
                List<Recipient> userId = recipientService.list(new QueryWrapper<Recipient>()
                        .eq("userId", tokenService.getLoginUser1().getUserid()));
                for (Recipient recipient1 : userId) {
                    recipient1.setIsDefault(0);
                    recipientService.updateById(recipient1);
                }
            }
            recipientService.updateById(recipient);
        } else {
            if (recipient.getIsDefault() == 1) {
                // 如果设置为默认地址 那么要将之前的默认地址取消掉
                List<Recipient> userId = recipientService.list(new QueryWrapper<Recipient>()
                        .eq("userId", tokenService.getLoginUser1().getUserid()));
                for (Recipient recipient1 : userId) {
                    recipient1.setIsDefault(0);
                    recipientService.updateById(recipient1);
                }
            }
            recipientService.save(recipient);
 
        }
        return R.ok();
    }
 
    /**
     * 删除收货地址
     */
    @GetMapping("/addressDelete")
    @ApiOperation(value = "删除收货地址", tags = {"学习端-收货地址"})
    @ApiOperationSupport(order = 34)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址信息id", name = "id", dataType = "Integer", required = true)
    })
    public R<String> addressDelete(@RequestParam Integer id) {
        return R.ok(recipientService.removeById(id) ? "删除成功!" : "删除失败!");
    }
 
    /**
     * 删除收货地址
     */
    @GetMapping("/addressDeleteParent")
    @ApiOperation(value = "家长端-删除收货地址", tags = {"家长端-删除收货地址"})
    @ApiOperationSupport(order = 21)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "地址信息id", name = "id", dataType = "Integer", required = true)
    })
    public R<String> addressDeleteParent(@RequestParam Integer id) {
        return R.ok(recipientService.removeById(id) ? "删除成功!" : "删除失败!");
    }
 
 
    @GetMapping("/getOrderAddressParent")
    @ApiOperation(value = "获取修改订单收货地址", tags = {"家长端-获取修改订单收货地址"})
    @ApiOperationSupport(order = 22)
    public R<List<Recipient>> getOrderAddressParent() {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        List<Recipient> userId = recipientService.list(new QueryWrapper<Recipient>()
                .eq("userId", tokenService.getLoginUser1().getUserid()));
 
        return R.ok(userId);
    }
 
    @GetMapping("/getOrderAddress")
    @ApiOperation(value = "获取修改订单收货地址", tags = {"学习端-收货地址"})
    @ApiOperationSupport(order = 35)
    public R<List<Recipient>> getOrderAddress() {
        if (tokenService.getLoginUserStudy() == null) {
            return R.tokenError("登录失效");
        }
        List<Recipient> userId = recipientService.list(new QueryWrapper<Recipient>()
                .eq("userId", tokenService.getLoginUserStudy().getUserid()));
        return R.ok(userId);
    }
 
    @GetMapping("/updateOrderAddressParent")
    @ApiOperation(value = "修改订单收货地址", tags = {"家长端-修改订单收货地址"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单id", name = "orderId", dataType = "String", required = true),
            @ApiImplicitParam(value = "地址信息id", name = "recipientId", dataType = "String", required = true)
    })
    public R updateOrderAddressParent(@RequestParam Integer orderId, @RequestParam Integer recipientId) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        Recipient userId = recipientService.getById(recipientId);
        TOrder byId = orderService.getById(orderId);
        byId.setConsigneeName(userId.getRecipient());
        byId.setConsigneePhone(userId.getRecipientPhone());
        byId.setConsigneeAddress(userId.getAddress());
        orderService.updateById(byId);
        return R.ok("修改成功");
    }
 
    @GetMapping("/updateOrderAddress")
    @ApiOperation(value = "修改订单收货地址", tags = {"学习端-收货地址"})
    @ApiOperationSupport(order = 36)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "订单id", name = "orderId", dataType = "String", required = true),
            @ApiImplicitParam(value = "地址信息id", name = "recipientId", dataType = "String", required = true)
    })
    public R updateOrderAddress(@RequestParam Integer orderId, @RequestParam Integer recipientId) {
        if (tokenService.getLoginUserStudy() == null) {
            return R.tokenError("登录失效");
        }
        Recipient recipient = recipientService.getById(recipientId);
        TOrder order = orderService.getById(orderId);
        if (null == order) {
            throw new GlobalException("订单不存在!");
        }
        if (!Constants.ONE.equals(order.getState())) {
            throw new GlobalException("订单已发货,无法修改收货地址!");
        }
        order.setConsigneeName(recipient.getAddress());
        order.setConsigneePhone(recipient.getRecipientPhone());
        order.setConsigneeAddress(recipient.getAddress());
        orderService.updateById(order);
        return R.ok();
    }
 
    /**
     * 收货地址省市区三级联动
     */
    @GetMapping("/addressTree")
    @ApiOperation(value = "收货地址省市区三级联动", tags = {"学习端-商城"})
    @ApiOperationSupport(order = 37)
    public R<List<Region>> addressTree() {
        return R.ok(regionService.addressTree());
    }
 
    /**
     * 可兑换商品推荐
     * 远程调用
     */
    @GetMapping("/goodRecommend")
    @ApiOperation(value = "可兑换商品推荐", tags = {"学习端-商城"})
    @ApiOperationSupport(order = 38)
    public R<List<TGoodsVO>> goodRecommend() {
        return R.ok(goodsService.goodRecommend(tokenService.getLoginUserStudy().getUserid()));
    }
 
    /**
     * 获取所有商品分类
     */
    @GetMapping("/goodTypeStudy")
    @ApiOperation(value = "商品分类列表", tags = {"学习端-商城"})
    @ApiOperationSupport(order = 39)
    public R<List<TGoodsType>> goodTypeStudy() {
        return R.ok(goodsTypeService.lambdaQuery().eq(TGoodsType::getIsDelete, 0).list());
    }
 
    /**
     * 商品详情
     *
     * @param goodId 商品id
     */
    @GetMapping("/goodDetail")
    @ApiOperation(value = "商品详情", tags = {"学习端-商城"})
    @ApiOperationSupport(order = 40)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id", name = "goodId", dataType = "String", required = true)
    })
    public R<GoodDetailVO> goodDetail(@RequestParam String goodId) {
        // 商品详情
        TGoods goods = goodsService.lambdaQuery().eq(TGoods::getId, goodId).one();
        if (null == goods) {
            throw new GlobalException("获取商品详情失败,商品不存在!");
        }
        // 商品分类详情
        List<TGoodsType> goodsTypes = goodsTypeService.lambdaQuery().in(TGoodsType::getId, Arrays.asList(goods.getTypeIds().split(","))).list();
        // 已兑换人数
        int number = 0;
        if (null != goods.getBasicCount()) {
            number += goods.getBasicCount();
        }
        number += orderService.getGoodBuyNumber(goods.getId());
        return R.ok(new GoodDetailVO(goods, goodsTypes, number));
    }
 
    /**
     * 商品详情
     *
     * @param goodId 商品id
     */
    @GetMapping("/goodDetailParent")
    @ApiOperation(value = "商品详情", tags = {"家长端-商城"})
    @ApiOperationSupport(order = 24)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id", name = "goodId", dataType = "String", required = true)
    })
    public R<GoodDetailVO> goodDetailParent(@RequestParam String goodId) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        // 商品详情
        TGoods goods = goodsService.lambdaQuery().eq(TGoods::getId, goodId).one();
        if (null == goods) {
            throw new GlobalException("获取商品详情失败,商品不存在!");
        }
        // 商品分类详情
        List<TGoodsType> goodsTypes = goodsTypeService.lambdaQuery().in(TGoodsType::getId, Arrays.asList(goods.getTypeIds().split(","))).list();
        // 已兑换人数
        int number = goods.getBasicCount() + orderService.getGoodBuyNumber(goods.getId());
        return R.ok(new GoodDetailVO(goods, goodsTypes, number));
    }
 
    /**
     * 商城-立即兑换
     */
    @GetMapping("/redeemNow")
    @ApiOperation(value = "商城立即兑换", tags = {"学习端-商城"})
    @ApiOperationSupport(order = 41)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id", name = "goodId", dataType = "String", required = true)
    })
    public R<GoodDetailVO> redeemNow(@RequestParam String goodId) {
        if (tokenService.getLoginUserStudy() == null) {
            return R.tokenError("登录失效");
        }
        Recipient recipient = recipientService.lambdaQuery()
                .eq(Recipient::getUserId, tokenService.getLoginUserStudy().getUserid())
                .eq(Recipient::getIsDefault, 1).one();
        GoodDetailVO goodDetailVO = goodsService.redeemNow(goodId, recipient);
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 格式化日期和时间信息
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = currentDateTime.format(formatter);
        // 生成随机数,填充剩余位数
        Random random = new Random();
        StringBuilder randomPart = new StringBuilder();
        for (int i = 0; i < 3; i++) {
            randomPart.append((char) (random.nextInt(26) + 'A')); // 大写字母
        }
        // 组合订单编号
        String orderNumber = formattedDateTime + randomPart.toString();
        goodDetailVO.setOrderNumber(orderNumber);
        return R.ok(goodDetailVO);
    }
 
    /**
     * 商城-立即兑换
     */
    @GetMapping("/redeemNowParent")
    @ApiOperation(value = "商城立即兑换", tags = {"家长端-商城"})
    @ApiOperationSupport(order = 25)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id", name = "goodId", dataType = "String", required = true)
    })
    public R<GoodDetailVO> redeemNowParent(@RequestParam String goodId) {
        if (tokenService.getLoginUser1() == null) {
            return R.tokenError("登录失效");
        }
        Recipient recipient = recipientService.lambdaQuery()
                .eq(Recipient::getUserId, tokenService.getLoginUser1().getUserid())
                .eq(Recipient::getIsDefault, 1).one();
        if (recipient == null) {
            // 随便取一条地址数据
            List<Recipient> list = recipientService.lambdaQuery()
                    .eq(Recipient::getUserId, tokenService.getLoginUser1().getUserid())
                    .eq(Recipient::getIsDefault, 1).list();
            if (!list.isEmpty()) {
                recipient = list.get(0);
            }
        }
        GoodDetailVO goodDetailVO = goodsService.redeemNow(goodId, recipient);
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 格式化日期和时间信息
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = currentDateTime.format(formatter);
        // 生成随机数,填充剩余位数
        Random random = new Random();
        StringBuilder randomPart = new StringBuilder();
        for (int i = 0; i < 3; i++) {
            // 大写字母
            randomPart.append((char) (random.nextInt(26) + 'A'));
        }
        // 组合订单编号
        String orderNumber = formattedDateTime + randomPart;
        goodDetailVO.setOrderNumber(orderNumber);
        return R.ok(goodDetailVO);
    }
 
 
    /**
     * 商品兑换
     *
     * @param goodExchange 商品信息
     */
    @PostMapping("/goodExchange")
    @ApiOperation(value = "家长端-商品兑换确认", tags = {"家长端-商品兑换确认"})
    public R goodExchange(@RequestBody GoodExchangeDTO goodExchange) {
        Recipient recipient = recipientService.getById(Integer.valueOf(goodExchange.getRecipientId()));
        return goodsService.goodExchange1(goodExchange, recipient);
    }
 
    @PostMapping("/goodExchangeStudy")
    @ApiOperation(value = "商品兑换确认", tags = {"学习端-商城"})
    @ApiOperationSupport(order = 42)
    public R goodExchangeStudy(@RequestBody GoodExchangeDTO goodExchange) {
        Recipient recipient = recipientService.getById(goodExchange.getRecipientId());
        return goodsService.goodExchange(goodExchange, recipient);
    }
 
 
    @Autowired
    private StudyClient studyClient;
 
    @PostMapping("/getOrderInfo/{id}")
    @ApiOperation(value = "查看详情", tags = {"后台-订单管理"})
    @ApiOperationSupport(order = 11)
    public R<TOrderVO> getOrderInfo(@PathVariable("id") Integer id) {
        TOrder byId = orderService.getById(id);
        TGoods byId2 = goodsService.getById(byId.getGoodsId());
        TOrderVO tGoodsVO = new TOrderVO();
        tGoodsVO.setName(byId2.getName());
        BeanUtils.copyProperties(byId, tGoodsVO);
        TUser byId1 = studyClient.getUserById(byId.getUserId()).getData();
        tGoodsVO.setUserName(byId1.getName());
        tGoodsVO.setPhone(byId1.getPhone());
        return R.ok(tGoodsVO);
    }
 
    @PostMapping("/confirm1")
    @ApiOperation(value = "确认发货", tags = {"后台-订单管理"})
    @ApiOperationSupport(order = 12)
    public R getGoodsInfo1(@RequestBody OrderDTO dto) {
        TOrder byId = orderService.getById(dto.getId());
        byId.setState(2);
        byId.setExpress(dto.getExpress());
        byId.setExpressNumber(dto.getExpressNumber());
        byId.setExpressTime(new Date());
        orderService.updateById(byId);
        return R.ok("修改成功");
    }
 
    @PostMapping("/listAll1")
    @ApiOperation(value = "列表查询", tags = {"后台-订单管理"})
    @ApiOperationSupport(order = 13)
    public R<PageInfo<TOrderVO>> listAll1(@RequestBody OrderQuery query) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 
        if (query.getEndTime() != null) {
            Date parse = format.parse(query.getStartTime());
            Date parse1 = format.parse(query.getEndTime());
            query.setStartTime1(parse);
            query.setEndTime1(parse1);
        }
        List<TOrderVO> list = orderService.listAll(query);
        List<TOrderVO> list1 = new ArrayList<>();
        for (TOrderVO tOrderVO : list) {
            TUser data = studyClient.getUserById(tOrderVO.getUserId()).getData();
            if (data != null) {
                tOrderVO.setUserName(data.getName());
                tOrderVO.setPhone(data.getPhone());
            }
            if (!StringUtils.hasLength(query.getPhone()) && !StringUtils.hasLength(query.getUserName())) {
                list1.add(tOrderVO);
                continue;
            }
            // 如果筛选条件输入了电话或者姓名那么需要过滤掉不符合条件的数据
            if (StringUtils.hasLength(query.getPhone()) && StringUtils.hasLength(query.getUserName())) {
                if (tOrderVO.getPhone().contains(query.getPhone()) && tOrderVO.getUserName().contains(query.getUserName())) {
                    list1.add(tOrderVO);
                }
            } else if (StringUtils.hasLength(query.getPhone())) {
                if (tOrderVO.getPhone().contains(query.getPhone())) {
                    list1.add(tOrderVO);
                }
            } else if (StringUtils.hasLength(query.getUserName())) {
                if (tOrderVO.getUserName().contains(query.getUserName())) {
                    list1.add(tOrderVO);
                }
            }
 
 
        }
        PageInfo<TOrderVO> res = new PageInfo<>(query.getPageNumber(), query.getPageSize());
        res.setRecords(list1);
        return R.ok(res);
    }
}