xuhy
2025-01-09 712f70b2936079a131ecb1e63c6d337171618cad
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
package com.stylefeng.guns.modular.system.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.account.controller.AppOrderController;
import com.stylefeng.guns.modular.system.dao.TaxiCardMapper;
import com.stylefeng.guns.modular.system.model.*;
import com.stylefeng.guns.modular.system.service.*;
import com.stylefeng.guns.modular.system.util.GDMapGeocodingUtil;
import com.stylefeng.guns.modular.system.util.PayMoneyUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.warpper.CouponWarpper;
import com.stylefeng.guns.modular.system.warpper.TaxiCardWapper;
import com.stylefeng.guns.modular.taxi.model.PaymentRecord;
import com.stylefeng.guns.modular.taxi.service.IPaymentRecordService;
import com.stylefeng.guns.modular.taxi.service.ITransactionDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class TaxiCardServiceImpl extends ServiceImpl<TaxiCardMapper, TaxiCard> implements ITaxiCardService {
 
    @Autowired
    private ICompanyCityService companyCityService;
 
    @Autowired
    private IOpenCityService openCityService;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
    @Autowired
    private ICouponRecordService couponRecordService;
 
    @Autowired
    private ICompanyService companyService;
 
    @Autowired
    private ITaxiCardPaymentService taxiCardPaymentService;
 
    @Autowired
    private IUserUserService userUserService;
 
    @Autowired
    private PayMoneyUtil payMoneyUtil;
 
    @Autowired
    private IUserInfoService userInfoService;
 
    @Autowired
    private IPaymentRecordService paymentRecordService;
 
    @Autowired
    private ITransactionDetailsService transactionDetailsService;
 
    @Autowired
    private IIncomeService incomeService;
 
    @Autowired
    private ISystemNoticeService systemNoticeService;
 
    @Autowired
    private IUserTaxiCardService userTaxiCardService;
 
    @Autowired
    private IRegionService regionService;
 
    @Autowired
    private IUserCouponRecordService userCouponRecordService;
 
    @Value("${callbackPath}")
    private String callbackPath;
 
 
 
 
    @Override
    public ResultUtil<List<TaxiCardWapper>> getTaxiCardList(Integer uid, Integer type, String lon, String lat) throws Exception {
        Company company = companyCityService.query(lon, lat);
        if(null == company ){
            return ResultUtil.error("当前位置暂无企业服务");
        }
        List<Integer> integers1 = companyCityService.query1(lon, lat);
        System.out.println(integers1);
        // TODO: 2022/3/19 打车卡可以重复购买,有效期延长 ---> 只能购买使用完和未购买的打车卡
        List<TaxiCard> taxiCards = this.selectList(new EntityWrapper<TaxiCard>().in("companyCityId", integers1)
                .eq("state", 1).orderBy("createTime desc"));
        List<Integer> ids =new ArrayList<>();
        if(taxiCards.size()==0){
            ids.add(-1);
        }
        ids = taxiCards.stream().map(TaxiCard::getId).collect(Collectors.toList());
        List<TaxiCard> taxiCards1 = this.selectList(new EntityWrapper<TaxiCard>().notIn("id", ids)
                .eq("state", 1)
                .eq("taxiCardType", 2));
        taxiCards.addAll(taxiCards1);
        List<TaxiCardWapper> list = new ArrayList<>();
        taxiCards.forEach(taxiCard -> {
            List<UserTaxiCard> userTaxiCards = userTaxiCardService.selectList(new EntityWrapper<UserTaxiCard>().eq("userId", uid)
                    .eq("taxiCardId", taxiCard.getId())
                    .where("endTime > now()"));
            boolean b = false;
            for (UserTaxiCard userTaxiCard : userTaxiCards) {
                JSONObject jsonObject = JSON.parseObject(userTaxiCard.getContent());
                if(userTaxiCard.getType() == 1 || userTaxiCard.getType() == 3){//次数卡
                    Integer time = jsonObject.getInteger("time");
                    if(time > 0){
                        b = true;
                        break;
                    }
                }
                if(userTaxiCard.getType() == 2 || userTaxiCard.getType() == 4 || userTaxiCard.getType() == 5){
                    b = true;
                    break;
                }
                if(userTaxiCard.getType() == 6){//物流优惠包
                    JSONArray coupons = jsonObject.getJSONArray("coupons");
//                    Integer id = coupons.getInteger(1);
                    for (int i = 0; i < coupons.size(); i++) {
                        JSONArray jsonArray = coupons.getJSONArray(i);
                        int l = userCouponRecordService.selectCount(new EntityWrapper<UserCouponRecord>().eq("userId", uid).eq("couponId", jsonArray.get(1).toString())
                                .eq("activityType", 5).eq("state", 1).eq("couponActivityId", taxiCard.getId()).where("now() <= expirationTime"));
                        if(l > 0){
                            b = true;
                            break;
                        }
                    }
 
                }
 
            }
            if(b){//还未使用完的情况
                return;
            }
 
 
            JSONObject jsonObject = JSON.parseObject(taxiCard.getContent());
            if(taxiCard.getType() != 6){
                List<Integer> integers = jsonObject.getJSONArray("businessTypes").toJavaList(Integer.class);
                if(!integers.contains(0) && !integers.contains(type)){//不包含通用和特定业务类型的数据
                    return;
                }
            }
            if(taxiCard.getType() == 6){
                JSONArray coupons = jsonObject.getJSONArray("coupons");
                Integer num = 0;
                for(int i = 0; i < coupons.size(); i++){
                    CouponRecord couponRecord = couponRecordService.selectById(coupons.getJSONArray(i).getInteger(1));
                    if(null != couponRecord && (couponRecord.getCouponUseType() == 0 || couponRecord.getCouponUseType() == type)){
                        num++;
                        break;
                    }
                }
                if(num == 0){
                    return;
                }
            }
 
            TaxiCardWapper taxiCardWapper = new TaxiCardWapper();
            taxiCardWapper.setId(taxiCard.getId());
            taxiCardWapper.setName(taxiCard.getName());
            taxiCardWapper.setState(taxiCard.getState());
            taxiCardWapper.setValidDate(taxiCard.getValidDate());
            if(taxiCard.getType() == 1 || taxiCard.getType() == 4 || taxiCard.getType() == 5){
                taxiCardWapper.setType(taxiCard.getType());
                taxiCardWapper.setDiscounts(jsonObject.getDouble("discount"));
                taxiCardWapper.setTime(jsonObject.getInteger("time"));
            }
            if(taxiCard.getType() == 2){
                taxiCardWapper.setType(taxiCard.getType());
                JSONArray fullReduction = jsonObject.getJSONArray("fullReduction");
                JSONArray jsonArray = fullReduction.getJSONArray(fullReduction.size() - 1);
                taxiCardWapper.setDiscounts(jsonArray.getDouble(1));
            }
            if(taxiCard.getType() == 3){
                taxiCardWapper.setType(taxiCard.getType());
                taxiCardWapper.setDiscounts(jsonObject.getDouble("discountAmount"));
                taxiCardWapper.setTime(jsonObject.getInteger("time"));
            }
            if(taxiCard.getType() == 6){
                JSONArray coupons = jsonObject.getJSONArray("coupons");
                Integer num = 0;
                for(int i = 0; i < coupons.size(); i++){
                    num += coupons.getJSONArray(i).getInteger(0);
                }
                taxiCardWapper.setType(taxiCard.getType());
                taxiCardWapper.setCouponNum(num);
            }
 
 
            String str = "";
            if(taxiCard.getTaxiCardType() == 1){
                String[] split = taxiCard.getCompanyCityId().split(";");
                for(String id : split){
                    CompanyCity companyCity = companyCityService.selectById(id);
                    if(ToolUtil.isNotEmpty(companyCity.getAreaCode())){
                        Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getAreaCode()));
                        str += region.getName() + ";";
                        continue;
                    }
                    if(ToolUtil.isNotEmpty(companyCity.getCityCode())){
                        Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getCityCode()));
                        str += region.getName() + ";";
                        continue;
                    }
                    if(ToolUtil.isNotEmpty(companyCity.getProvinceCode())){
                        Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getProvinceCode()));
                        str += region.getName() + ";";
                        continue;
                    }
                }
            }else{
                str = "全国;";
            }
 
            taxiCardWapper.setCityName(str.substring(0, str.length() - 1));
            taxiCardWapper.setOriginalPrice(taxiCard.getOriginalPrice());
            taxiCardWapper.setSellingPrice(taxiCard.getSellingPrice());
            list.add(taxiCardWapper);
        });
        return ResultUtil.success(list);
    }
 
    @Override
    public TaxiCardWapper getTaxiCardInfo(Integer id) throws Exception {
        TaxiCard taxiCard = this.selectById(id);
        JSONObject jsonObject = JSON.parseObject(taxiCard.getContent());
        TaxiCardWapper taxiCardWapper = new TaxiCardWapper();
        taxiCardWapper.setId(taxiCard.getId());
        taxiCardWapper.setName(taxiCard.getName());
        taxiCardWapper.setType(taxiCard.getType());
        if(taxiCard.getType() == 1){
            taxiCardWapper.setDiscounts(jsonObject.getDouble("discount"));
            taxiCardWapper.setTime(jsonObject.getInteger("time"));
        }
        if(taxiCard.getType() == 2){
            JSONArray fullReduction = jsonObject.getJSONArray("fullReduction");
            JSONArray jsonArray = fullReduction.getJSONArray(fullReduction.size() - 1);
            taxiCardWapper.setDiscounts(jsonArray.getDouble(1));
        }
        if(taxiCard.getType() == 3){
            taxiCardWapper.setDiscounts(jsonObject.getDouble("discountAmount"));
            taxiCardWapper.setTime(jsonObject.getInteger("time"));
        }
        if(taxiCard.getType() == 4 || taxiCard.getType() == 5){
            taxiCardWapper.setDiscounts(jsonObject.getDouble("discount"));
        }
        if(taxiCard.getType() == 6){
            JSONArray coupons = jsonObject.getJSONArray("coupons");
            Integer num = 0;
            List<CouponWarpper> couponWarppers = new ArrayList<>();
            for(int i = 0; i < coupons.size(); i++){
                JSONArray jsonArray = coupons.getJSONArray(i);
                num += jsonArray.getInteger(0);
                CouponRecord couponRecord = couponRecordService.selectById(jsonArray.getInteger(1));
                CouponWarpper couponWarpper = new CouponWarpper();
                couponWarpper.setId(couponRecord.getId());
                couponWarpper.setMoney(couponRecord.getMoney());
                couponWarpper.setUserType(couponRecord.getCouponUseType());
                couponWarpper.setType(couponRecord.getCouponType());
                couponWarpper.setFullMoney(couponRecord.getFullMoney());
                couponWarpper.setName(companyService.selectById(couponRecord.getCompanyId()).getName());
                couponWarpper.setCouponName(couponRecord.getName());
                couponWarppers.add(couponWarpper);
            }
            taxiCardWapper.setCouponNum(num);
            taxiCardWapper.setCouponList(couponWarppers);
        }
        String str = "";
        if(taxiCard.getTaxiCardType() == 1){
            String[] split = taxiCard.getCompanyCityId().split(";");
            for(String c : split){
                CompanyCity companyCity = companyCityService.selectById(c);
                if(ToolUtil.isNotEmpty(companyCity.getAreaCode())){
                    Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getAreaCode()));
                    str += region.getName() + ";";
                    continue;
                }
                if(ToolUtil.isNotEmpty(companyCity.getCityCode())){
                    Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getCityCode()));
                    str += region.getName() + ";";
                    continue;
                }
                if(ToolUtil.isNotEmpty(companyCity.getProvinceCode())){
                    Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getProvinceCode()));
                    str += region.getName() + ";";
                    continue;
                }
            }
        }else{
            str = "全国;";
        }
        if(taxiCard.getType() < 5){
            List<String> list = JSON.parseObject(taxiCard.getContent()).getJSONArray("timeQuantum").toJavaList(String.class);
            taxiCardWapper.setTimeQuantum(list);
        }
        taxiCardWapper.setCityName(str.substring(0, str.length() - 1));
        taxiCardWapper.setOriginalPrice(taxiCard.getOriginalPrice());
        taxiCardWapper.setSellingPrice(taxiCard.getSellingPrice());
        taxiCardWapper.setValidDate(taxiCard.getValidDate());
        taxiCardWapper.setNote(taxiCard.getNote());
        return taxiCardWapper;
    }
 
    @Override
    public ResultUtil payTaxiCard(Integer uid, Integer id, Integer payType, Integer type) throws Exception {
        UserInfo userInfo = userInfoService.selectById(uid);
        TaxiCard taxiCard = this.selectById(id);
        UserTaxiCard userTaxiCard1 = userTaxiCardService.selectOne(new EntityWrapper<UserTaxiCard>().eq("userId", uid).eq("taxiCardId", id)
                .eq("companyId", taxiCard.getCompanyId()).where("now() between startTime and endTime"));
        if(null != userTaxiCard1){
            //打车卡类型1=打折次数卡,2=优惠卡,3=次数卡,4=打折天数卡,5=物流打折卡)
            if(userTaxiCard1.getType() == 1){
                JSONObject jsonObject = JSON.parseObject(userTaxiCard1.getContent());
                Integer time = jsonObject.getInteger("time");
                if(time > 0){
                    return ResultUtil.error("您已经购买了此打车卡,请先去使用吧!");
                }
            }
            if(userTaxiCard1.getType() == 3){
                JSONObject jsonObject = JSON.parseObject(userTaxiCard1.getContent());
                Integer time = jsonObject.getInteger("time");
                if(time > 0){
                    return ResultUtil.error("您已经购买了此打车卡,请先去使用吧!");
                }
            }
            if(userTaxiCard1.getType() == 2 || userTaxiCard1.getType() == 4 || userTaxiCard1.getType() == 5){
                return ResultUtil.error("您已经购买了此打车卡,请先去使用吧!");
            }
        }
        TaxiCardPayment taxiCardPayment = new TaxiCardPayment();
        taxiCardPayment.setUserId(uid);
        taxiCardPayment.setTaxiCardId(id);
        taxiCardPayment.setPayType(payType);
        taxiCardPayment.setPayStatus(1);
        taxiCardPayment.setCreateTime(new Date());
        taxiCardPayment.setType(taxiCard.getType());
        taxiCardPayment.setCompanyCityId(taxiCard.getCompanyCityId());
        taxiCardPayment.setContent(taxiCard.getContent());
        taxiCardPayment.setCompanyId(taxiCard.getCompanyId());
        taxiCardPayment.setPayMoney(taxiCard.getSellingPrice());
        taxiCardPaymentService.insert(taxiCardPayment);
 
        ResultUtil resultUtil = ResultUtil.success();
        if(payType == 1){//微信支付
            String app = type == 1 ? "APP" : "JSAPI";
            resultUtil = payMoneyUtil.weixinpay("购买出行卡", "", ToolUtil.getRandomString(10), taxiCard.getSellingPrice().toString(),  "/base/wxPayTaxiCardPaymentSpread", app, userInfo.getAppletsOpenId());
            paymentRecordService.saveData(4, taxiCardPayment.getUserId(), 1, taxiCardPayment.getId(), null, 1, taxiCard.getSellingPrice(), "", 1);//添加预支付数据
        }
        if(payType == 2){//支付宝支付
            resultUtil = payMoneyUtil.alipay("购买出行卡", "购买出行卡", "", ToolUtil.getRandomString(10), taxiCard.getSellingPrice().toString(), "/base/aliPayTaxiCardPaymentSpread");
            paymentRecordService.saveData(4, taxiCardPayment.getUserId(), 1, taxiCardPayment.getId(), null, 2, taxiCard.getSellingPrice(), "", 1);//添加预支付数据
        }
        if(payType == 3){//余额支付
            if(userInfo.getBalance() == null || userInfo.getBalance() < taxiCard.getSellingPrice()){
                return ResultUtil.error("余额不足,无法完成支付", "");
            }
//            resultUtil= appOrderController.moneyPay(taxiCardPayment.getId(),userInfo.getId(),taxiCard.getSellingPrice());
//            if(resultUtil.getCode()==500){
//                return ResultUtil.error("电子余额不足,无法完成支付");
//            }
            userInfo.setBalance(new BigDecimal(userInfo.getBalance()).subtract(new BigDecimal(taxiCard.getSellingPrice())).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue());
            //添加交易明细
            transactionDetailsService.saveData(uid, "购买出行卡", taxiCard.getSellingPrice(), 2, 1, 1, 6, taxiCardPayment.getId());
            userInfoService.updateById(userInfo);
 
            taxiCardPayment.setPayStatus(2);//已支付
            taxiCardPayment.setPayTime(new Date());
            taxiCardPaymentService.updateById(taxiCardPayment);
 
 
            //已购买没过期的增加有效期
            UserTaxiCard userTaxiCard = userTaxiCardService.selectOne(new EntityWrapper<UserTaxiCard>().eq("userId", uid).eq("taxiCardId", taxiCardPayment.getTaxiCardId())
                    .eq("companyId", taxiCardPayment.getCompanyId()).where("now() between startTime and endTime"));
            if(null != userTaxiCard){
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(userTaxiCard.getEndTime());
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                userTaxiCard.setEndTime(calendar.getTime());
                if(taxiCard.getType() == 1 || taxiCard.getType() == 3){//次数卡增加次数
                    JSONObject jsonObject = JSON.parseObject(userTaxiCard.getContent());
                    Integer time = jsonObject.getInteger("time");
                    Integer time1 = JSON.parseObject(taxiCard.getContent()).getInteger("time");
                    jsonObject.put("time", time + time1);
                    userTaxiCard.setContent(jsonObject.toJSONString());
                }
                userTaxiCardService.updateById(userTaxiCard);
            }else{
                userTaxiCard = new UserTaxiCard();
                userTaxiCard.setUserId(uid);
                userTaxiCard.setTaxiCardId(id);
                userTaxiCard.setStartTime(new Date());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                userTaxiCard.setEndTime(calendar.getTime());
                userTaxiCard.setType(taxiCard.getType());
                userTaxiCard.setCompanyCityId(taxiCard.getCompanyCityId());
                userTaxiCard.setContent(taxiCard.getContent());
                userTaxiCard.setCompanyId(taxiCard.getCompanyId());
                userTaxiCardService.insert(userTaxiCard);
            }
 
            //单独处理优惠券数据
            if(taxiCard.getType() == 6){
                JSONArray coupons = JSON.parseObject(taxiCard.getContent()).getJSONArray("coupons");
                for(int i = 0; i < coupons.size(); i++){
                    JSONArray jsonArray = coupons.getJSONArray(i);
                    Integer integer = jsonArray.getInteger(1);
                    Integer time = jsonArray.getInteger(0);
                    List<UserCouponRecord> list = userCouponRecordService.selectList(new EntityWrapper<UserCouponRecord>().eq("userId", uid).eq("couponId", integer)
                            .eq("activityType", 5).eq("state", 1).eq("couponActivityId", taxiCard.getId()).where("now() <= expirationTime"));
                    if(list.size() > 0 && time == list.size()){//历史有购买且都还没使用的情况
                        for (UserCouponRecord userCouponRecord : list) {
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(userCouponRecord.getExpirationTime());
                            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                            userCouponRecord.setExpirationTime(calendar.getTime());
                        }
                        userCouponRecordService.updateBatchById(list);
                    }else{//处理历史未购买或购买的使用了部分优惠券的情况
                        for (UserCouponRecord userCouponRecord : list) {
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(userCouponRecord.getExpirationTime());
                            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                            userCouponRecord.setExpirationTime(calendar.getTime());
                            userCouponRecordService.updateById(userCouponRecord);
                        }
                        CouponRecord couponRecord = couponRecordService.selectById(integer);
                        if(null != couponRecord){
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(new Date());
                            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                            for(int j = list.size(); j < time; j++){
                                UserCouponRecord userCouponRecord = new UserCouponRecord();
                                userCouponRecord.setMoney(couponRecord.getMoney());
                                userCouponRecord.setFullMoney(couponRecord.getFullMoney());
                                userCouponRecord.setExpirationTime(calendar.getTime());
                                userCouponRecord.setInsertTime(new Date());
                                userCouponRecord.setCompanyId(couponRecord.getCompanyId());
                                userCouponRecord.setState(1);
                                userCouponRecord.setCouponUseType(couponRecord.getCouponUseType());
                                userCouponRecord.setCouponType(couponRecord.getCouponType());
                                userCouponRecord.setUserId(uid);
                                userCouponRecord.setCouponId(couponRecord.getId());
                                userCouponRecord.setCouponActivityId(taxiCard.getId());//存储打车id
                                userCouponRecord.setActivityType(5);
                                userCouponRecord.setPaymentRecordId(null);
                                userCouponRecordService.insert(userCouponRecord);
                            }
                        }
                    }
                }
            }
 
 
            //添加已收入明细
            incomeService.saveData(1, taxiCardPayment.getCompanyId(), 5, taxiCardPayment.getId(), null,  taxiCardPayment.getPayMoney());
            systemNoticeService.addSystemNotice(1, "您已使用余额成功完成出行卡支付,谢谢使用!", uid, 1);
        }
        if(payType==4){
            Integer integer = paymentRecordService.saveData(4, taxiCardPayment.getUserId(), 1, taxiCardPayment.getId(), null, 4, taxiCard.getSellingPrice(), "", 1);
            resultUtil = appOrderController.placeAnOrder(new BigDecimal(taxiCard.getSellingPrice()), 4,taxiCardPayment.getId(),14,integer);
 
        }
        return resultUtil;
    }
    @Autowired
    private AppOrderController appOrderController;
 
    @Override
    public void payTaxiCardPaymentSpread(Integer id, String order_id) throws Exception {
        try {
            TaxiCardPayment taxiCardPayment = taxiCardPaymentService.selectById(id);
            //添加交易明细
            transactionDetailsService.saveData(taxiCardPayment.getUserId(), "购买出行卡", taxiCardPayment.getPayMoney(), 2, 1, 1, 6, taxiCardPayment.getId());
            taxiCardPayment.setPayStatus(2);//已支付
            taxiCardPayment.setPayTime(new Date());
            taxiCardPayment.setOrderNo(order_id);
            taxiCardPaymentService.updateById(taxiCardPayment);
            System.out.println("wxdck1");
 
            PaymentRecord query = paymentRecordService.query(4, taxiCardPayment.getUserId(), 1, id, null, taxiCardPayment.getPayType(), 1);
            query.setState(2);
            query.setCode(order_id);
            paymentRecordService.updateById(query);
            System.out.println("wxdck2");
            TaxiCard taxiCard = this.selectById(taxiCardPayment.getTaxiCardId());
 
            //已购买没过期的增加有效期
            UserTaxiCard userTaxiCard = userTaxiCardService.selectOne(new EntityWrapper<UserTaxiCard>().eq("userId", taxiCardPayment.getUserId()).eq("taxiCardId", taxiCardPayment.getTaxiCardId())
                    .eq("companyId", taxiCardPayment.getCompanyId()).where("now() between startTime and endTime"));
            if(null != userTaxiCard){
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(userTaxiCard.getEndTime());
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                userTaxiCard.setEndTime(calendar.getTime());
                if(taxiCard.getType() == 1 || taxiCard.getType() == 3){//次数卡增加次数
                    JSONObject jsonObject = JSON.parseObject(userTaxiCard.getContent());
                    Integer time = jsonObject.getInteger("time");
                    Integer time1 = JSON.parseObject(taxiCard.getContent()).getInteger("time");
                    jsonObject.put("time", time + time1);
                    userTaxiCard.setContent(jsonObject.toJSONString());
                }
                userTaxiCardService.updateById(userTaxiCard);
            }else{
                userTaxiCard = new UserTaxiCard();
                userTaxiCard.setUserId(taxiCardPayment.getUserId());
                userTaxiCard.setTaxiCardId(taxiCardPayment.getTaxiCardId());
                userTaxiCard.setStartTime(new Date());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                userTaxiCard.setEndTime(calendar.getTime());
                userTaxiCard.setType(taxiCard.getType());
                userTaxiCard.setCompanyCityId(taxiCard.getCompanyCityId());
                userTaxiCard.setContent(taxiCard.getContent());
                userTaxiCard.setCompanyId(taxiCard.getCompanyId());
                userTaxiCardService.insert(userTaxiCard);
            }
 
            //单独处理优惠券数据
            if(taxiCard.getType() == 6){
                JSONArray coupons = JSON.parseObject(taxiCard.getContent()).getJSONArray("coupons");
                for(int i = 0; i < coupons.size(); i++){
                    JSONArray jsonArray = coupons.getJSONArray(i);
                    Integer integer = jsonArray.getInteger(1);
                    Integer time = jsonArray.getInteger(0);
                    List<UserCouponRecord> list = userCouponRecordService.selectList(new EntityWrapper<UserCouponRecord>().eq("userId", taxiCardPayment.getUserId()).eq("couponId", integer)
                            .eq("activityType", 5).eq("state", 1).eq("couponActivityId", taxiCard.getId()).where("now() <= expirationTime"));
                    if(list.size() > 0 && time == list.size()){//历史有购买且都还没使用的情况
                        for (UserCouponRecord userCouponRecord : list) {
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(userCouponRecord.getExpirationTime());
                            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                            userCouponRecord.setExpirationTime(calendar.getTime());
                        }
                        userCouponRecordService.updateBatchById(list);
                    }else{//处理历史未购买或购买的使用了部分优惠券的情况
                        for (UserCouponRecord userCouponRecord : list) {
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(userCouponRecord.getExpirationTime());
                            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                            userCouponRecord.setExpirationTime(calendar.getTime());
                            userCouponRecordService.updateById(userCouponRecord);
                        }
                        CouponRecord couponRecord = couponRecordService.selectById(integer);
                        if(null != couponRecord){
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(new Date());
                            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + taxiCard.getValidDate());
                            for(int j = list.size(); j < time; j++){
                                UserCouponRecord userCouponRecord = new UserCouponRecord();
                                userCouponRecord.setMoney(couponRecord.getMoney());
                                userCouponRecord.setFullMoney(couponRecord.getFullMoney());
                                userCouponRecord.setExpirationTime(calendar.getTime());
                                userCouponRecord.setInsertTime(new Date());
                                userCouponRecord.setCompanyId(couponRecord.getCompanyId());
                                userCouponRecord.setState(1);
                                userCouponRecord.setCouponUseType(couponRecord.getCouponUseType());
                                userCouponRecord.setCouponType(couponRecord.getCouponType());
                                userCouponRecord.setUserId(taxiCardPayment.getUserId());
                                userCouponRecord.setCouponId(couponRecord.getId());
                                userCouponRecord.setCouponActivityId(taxiCard.getId());//存储打车id
                                userCouponRecord.setActivityType(5);
                                userCouponRecord.setPaymentRecordId(null);
                                userCouponRecordService.insert(userCouponRecord);
                            }
                        }
                    }
                }
            }
 
            //添加已收入明细
            incomeService.saveData(1, taxiCardPayment.getCompanyId(), 5, taxiCardPayment.getId(), null,  taxiCardPayment.getPayMoney());
            systemNoticeService.addSystemNotice(1, "您已使用" + (taxiCardPayment.getPayType() == 1 ? "微信" : (taxiCardPayment.getPayType()==2?"支付宝":"云闪付")) + "成功完成出行卡支付,谢谢使用!", taxiCardPayment.getUserId(), 1);
            System.out.println("wxdck  okok");
        }catch (Exception e){
            System.out.println(e);
 
        }
    }
 
    @Override
    public List<TaxiCardWapper> getMyTaxiCardList(Integer uid, Integer orderType) throws Exception {
        List<UserTaxiCard> userTaxiCards = userTaxiCardService.selectList(new EntityWrapper<UserTaxiCard>().eq("userId", uid).where("endTime >= now()"));
 
        //亲密账户
        List<UserUser> bindUserId = userUserService.selectList(new EntityWrapper<UserUser>().eq("bindUserId", uid));
        bindUserId.forEach(userUser -> {
            List<UserTaxiCard> userTaxiCards1 = userTaxiCardService.selectList(new EntityWrapper<UserTaxiCard>().eq("userId", userUser.getUserId()).where("endTime >= now()"));
            userTaxiCards.addAll(userTaxiCards1);
        });
 
        List<TaxiCardWapper> list = new ArrayList<>();
        userTaxiCards.forEach(userTaxiCard -> {
            JSONObject jsonObject = JSON.parseObject(userTaxiCard.getContent());
            TaxiCard taxiCard1 = this.selectById(userTaxiCard.getTaxiCardId());
            JSONObject jsonObject1 = JSON.parseObject(taxiCard1.getContent());
            if(userTaxiCard.getType() != 6){
                List<Integer> integers = jsonObject.getJSONArray("businessTypes").toJavaList(Integer.class);
                if(!integers.contains(orderType)){
                    return;
                }
            }
            TaxiCard taxiCard = this.selectById(userTaxiCard.getTaxiCardId());
            TaxiCardWapper taxiCardWapper = new TaxiCardWapper();
            taxiCardWapper.setId(userTaxiCard.getId());
            taxiCardWapper.setName(taxiCard.getName());
            taxiCardWapper.setType(userTaxiCard.getType());
            if(userTaxiCard.getType() == 1){
                taxiCardWapper.setDiscounts(jsonObject.getDouble("discount"));
                Integer time = jsonObject.getInteger("time");
                if(0 == time){
                    return;
                }
                taxiCardWapper.setTime(jsonObject1.getInteger("time"));
                taxiCardWapper.setLastTime(time);
            }
            if(userTaxiCard.getType() == 2){
                JSONArray fullReduction = jsonObject.getJSONArray("fullReduction");
                JSONArray jsonArray = fullReduction.getJSONArray(fullReduction.size() - 1);
                taxiCardWapper.setDiscounts(jsonArray.getDouble(1));
            }
            if(userTaxiCard.getType() == 3){
                taxiCardWapper.setDiscounts(jsonObject.getDouble("discountAmount"));
                Integer time = jsonObject.getInteger("time");
                if(0 == time){
                    return;
                }
                taxiCardWapper.setTime(jsonObject1.getInteger("time"));
                taxiCardWapper.setLastTime(time);
            }
            if(userTaxiCard.getType() == 4 || userTaxiCard.getType() == 5){
                taxiCardWapper.setDiscounts(jsonObject.getDouble("discount"));
            }
            if(userTaxiCard.getType() == 6){
                boolean b = true;
                JSONArray coupons = jsonObject.getJSONArray("coupons");
                Integer num = 0;
                for(int i = 0; i < coupons.size(); i++){
                    num += coupons.getJSONArray(i).getInteger(0);
                    Integer couponsId = coupons.getJSONArray(i).getInteger(1);
                    CouponRecord couponRecord = couponRecordService.selectById(couponsId);
                    if(null != couponRecord && (couponRecord.getCouponUseType() == 0 || couponRecord.getCouponUseType().compareTo(orderType) == 0)){
                        b = false;
                    }
                }
                if(b){
                    return;
                }
                taxiCardWapper.setCouponNum(num);
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            taxiCardWapper.setEndTime(sdf.format(userTaxiCard.getEndTime()));
            String str = "";
            if(taxiCard.getTaxiCardType() == 1){
                String[] split = taxiCard.getCompanyCityId().split(";");
                for(String c : split){
                    CompanyCity companyCity = companyCityService.selectById(c);
                    if(ToolUtil.isNotEmpty(companyCity.getAreaCode())){
                        Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getAreaCode()));
                        str += region.getName() + ";";
                        continue;
                    }
                    if(ToolUtil.isNotEmpty(companyCity.getCityCode())){
                        Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getCityCode()));
                        str += region.getName() + ";";
                        continue;
                    }
                    if(ToolUtil.isNotEmpty(companyCity.getProvinceCode())){
                        Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getProvinceCode()));
                        str += region.getName() + ";";
                        continue;
                    }
                }
            }else{
                str = "全国;";
            }
            taxiCardWapper.setCityName(str.substring(0, str.length() - 1));
            if(taxiCard.getType() < 6){
                List<Integer> integers = JSON.parseObject(taxiCard.getContent()).getJSONArray("businessTypes").toJavaList(Integer.class);
                taxiCardWapper.setBusinessTypes(integers);
            }
            list.add(taxiCardWapper);
        });
 
        return list;
    }
 
    public static void main(String[] args) {
        JSONObject jsonObject = JSON.parseObject("{\"businessTypes\":[1,2,3],\"timeQuantum\":[\"00:00:00 - 23:00:00\"],\"fullReduction\":[[2,1]]}");
 
        System.out.println(jsonObject);
    }
 
    @Override
    public TaxiCardWapper getMyTaxiCardInfo(Integer id) throws Exception {
        UserTaxiCard userTaxiCard = userTaxiCardService.selectById(id);
        TaxiCard taxiCard = this.selectById(userTaxiCard.getTaxiCardId());
        JSONObject jsonObject = JSON.parseObject(userTaxiCard.getContent());
        TaxiCardWapper taxiCardWapper = new TaxiCardWapper();
        taxiCardWapper.setId(userTaxiCard.getTaxiCardId());
        taxiCardWapper.setName(taxiCard.getName());
        taxiCardWapper.setType(userTaxiCard.getType());
        if(userTaxiCard.getType() == 1){
            taxiCardWapper.setDiscounts(jsonObject.getDouble("discount"));
            Integer time = jsonObject.getInteger("time");
            taxiCardWapper.setTime(time);
            taxiCardWapper.setLastTime(time);
        }
        if(userTaxiCard.getType() == 2){
            JSONArray fullReduction = jsonObject.getJSONArray("fullReduction");
            JSONArray jsonArray = fullReduction.getJSONArray(fullReduction.size() - 1);
            taxiCardWapper.setDiscounts(jsonArray.getDouble(1));
        }
        if(userTaxiCard.getType() == 3){
            taxiCardWapper.setDiscounts(jsonObject.getDouble("discountAmount"));
            Integer time = jsonObject.getInteger("time");
            taxiCardWapper.setTime(time);
            taxiCardWapper.setLastTime(time);
        }
        if(userTaxiCard.getType() == 4 || userTaxiCard.getType() == 5){
            taxiCardWapper.setDiscounts(jsonObject.getDouble("discount"));
        }
        if(userTaxiCard.getType() == 6){
            JSONArray coupons = jsonObject.getJSONArray("coupons");
            Integer num = 0;
            for(int i = 0; i < coupons.size(); i++){
                num += coupons.getJSONArray(i).getInteger(0);
            }
            taxiCardWapper.setCouponNum(num);
 
            List<CouponWarpper> couponWarppers = new ArrayList<>();
            for(int i = 0; i < coupons.size(); i++){
                JSONArray jsonArray = coupons.getJSONArray(i);
                num += jsonArray.getInteger(0);
                CouponRecord couponRecord = couponRecordService.selectById(jsonArray.getInteger(1));
                CouponWarpper couponWarpper = new CouponWarpper();
                couponWarpper.setId(couponRecord.getId());
                couponWarpper.setMoney(couponRecord.getMoney());
                couponWarpper.setUserType(couponRecord.getCouponUseType());
                couponWarpper.setType(couponRecord.getCouponType());
                couponWarpper.setFullMoney(couponRecord.getFullMoney());
                couponWarpper.setName(companyService.selectById(couponRecord.getCompanyId()).getName());
                couponWarpper.setCouponName(couponRecord.getName());
                couponWarppers.add(couponWarpper);
            }
            taxiCardWapper.setCouponList(couponWarppers);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taxiCardWapper.setEndTime(sdf.format(userTaxiCard.getEndTime()));
        String str = "";
        if(taxiCard.getTaxiCardType() == 1){
            String[] split = taxiCard.getCompanyCityId().split(";");
            for(String c : split){
                CompanyCity companyCity = companyCityService.selectById(c);
                if(ToolUtil.isNotEmpty(companyCity.getAreaCode())){
                    Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getAreaCode()));
                    str += region.getName() + ";";
                    continue;
                }
                if(ToolUtil.isNotEmpty(companyCity.getCityCode())){
                    Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getCityCode()));
                    str += region.getName() + ";";
                    continue;
                }
                if(ToolUtil.isNotEmpty(companyCity.getProvinceCode())){
                    Region region = regionService.selectOne(new EntityWrapper<Region>().eq("code", companyCity.getProvinceCode()));
                    str += region.getName() + ";";
                    continue;
                }
            }
        }else{
            str = "全国;";
        }
        if(taxiCard.getType() < 5){
            List<String> list = JSON.parseObject(taxiCard.getContent()).getJSONArray("timeQuantum").toJavaList(String.class);
            taxiCardWapper.setTimeQuantum(list);
        }
        taxiCardWapper.setCityName(str.substring(0, str.length() - 1));
        taxiCardWapper.setNote(taxiCard.getNote());
        return taxiCardWapper;
    }
}