fwq
2024-10-10 86a7a078c89a671f9ada908e8f82ebd43d463b18
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
package com.hx.phip.tool.user;
 
import com.alibaba.fastjson.JSON;
import com.hx.common.service.CommonService;
import com.hx.exception.TipsException;
import com.hx.mybatisTool.SqlSentence;
import com.hx.phiappt.common.UserProjectConstants;
import com.hx.phiappt.dao.mapper.*;
import com.hx.phiappt.model.BaseEntity;
import com.hx.phiappt.model.User;
import com.hx.phiappt.model.cardItem.CardEquity;
import com.hx.phiappt.model.cardItem.CardItem;
import com.hx.phiappt.model.cardItem.CardItemInfo;
import com.hx.phiappt.model.consume.ConsumePayCard;
import com.hx.phiappt.model.order.OrderItem;
import com.hx.phiappt.model.refund.RefundRecordItem;
import com.hx.phiappt.model.user.UserCard;
import com.hx.phiappt.model.user.UserCardPay;
import com.hx.phiappt.model.user.UserCardUsed;
import com.hx.phip.tool.refund.CardRefundTool;
import com.hx.phip.tool.refund.PaymentCountTool;
import com.hx.phip.tool.refund.RefundTool;
import com.hx.phiappt.vo.payMethod.PayMethodVo;
import com.hx.phip.vo.order.refund.DistributionRedundVo;
import com.hx.phip.vo.order.refund.RefundMakeWayVo;
import com.hx.phip.vo.user.UserCardItemInfoVo;
import com.hz.his.vo.user.card.UserCardEquityVo;
import com.hz.his.vo.user.card.UserCardItemVo;
import com.hz.his.vo.user.card.UserCardVo;
import com.hx.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 *用户卡包处理工具
 * @author CJH
 */
public class UserCardTool {
 
    private static final Logger logger = LoggerFactory.getLogger(UserCardTool.class);
 
    /**计算使用次数
     * @param buyNumber 购买数量
     * @param cardItemInfo 卡项条目
     * @param cardEquity 卡项权益
     * @return 计算得出抵扣次数
     */
    public static int countUsedNumber(int buyNumber, CardItemInfo cardItemInfo,CardEquity cardEquity){
        if(cardItemInfo == null){
            throw new TipsException("卡项条目获取信息失败");
        }
        if(cardEquity == null){
            throw new TipsException("卡项权益获取信息失败[024]");
        }
        int userdNum ;
        if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
            //次卡,权益有总次数,所有条目使用的总次数不能大于权益的总次数
            userdNum = buyNumber*cardItemInfo.getEveryDrawNum();
        }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
            //N选M
            userdNum = buyNumber;
        }else{
            //固定或者其他
            userdNum = buyNumber;
        }
        return userdNum;
    }
 
    /**获取用户项目可抵扣数据*/
    public static UserCardVo getUserCardSurplus(UserCard userCard, CommonService commonService){
 
        //获取卡项
        CardItem cardItem = commonService.selectOneByKeyBlob(CardItemMapper.class,userCard.getCardItemId());
        if(cardItem == null){
            throw new TipsException("获取卡项详情失败!");
        }
 
 
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
 
        //获取权益,不包含账户权益
        values.put("cardItemId",userCard.getCardItemId());
        sqlSentence.sqlSentence("SELECT * FROM card_equity WHERE isDel = 0 AND equityType != 4 AND cardItemId = #{m.cardItemId}",values);
        List<CardEquity> cardEquityList = commonService.selectList(CardEquityMapper.class,sqlSentence);
 
        //获取所有的卡项条目
        values.clear();
        values.put("cardItemId",userCard.getCardItemId());
        sqlSentence.sqlSentence("SELECT * FROM card_item_info WHERE isDel = 0 AND cardItemId = #{m.cardItemId}",values);
        List<CardItemInfo> cardItemInfoList = commonService.selectList(CardItemInfoMapper.class,sqlSentence);
 
        //卡项条目根据权益分组
        Map<String,List<CardItemInfo>> cardEquityByItemMap = new HashMap<>();
        List<CardItemInfo> cardItemInfos;
        for(CardItemInfo cardItemInfo:cardItemInfoList){
            cardItemInfos = cardEquityByItemMap.computeIfAbsent(cardItemInfo.getCardEquityId(),k->new ArrayList<>());
            cardItemInfos.add(cardItemInfo);
        }
 
        //用户卡项信息总结构
        UserCardVo userCardVo = new UserCardVo();
        userCardVo.setUserCardId(userCard.getId());
        userCardVo.setCardId(cardItem.getId());
        userCardVo.setCardName(cardItem.getName());
        userCardVo.setTotal(cardItem.getTotal());
        userCardVo.setEffectiveStatus(userCard.getEffectiveStatus());
 
        User user = commonService.selectOneByKey(UserMapper.class,userCard.getUserId());
        if(user != null){
            userCardVo.setUserId(user.getId());
            userCardVo.setUserName(user.getName());
            userCardVo.setUserCiq(user.getCIQ());
            userCardVo.setUserShopId(user.getShopId());
        }
 
        //卡项权益信息结构
        List<UserCardEquityVo> userCardEquityVoList = new ArrayList<>();
        UserCardEquityVo userCardEquityVo;
 
        //卡项条目信息结构
        List<UserCardItemVo>  userCardItemVoList;
        UserCardItemVo userCardItemVo;
 
        UserCardItemInfoVo userCardItemInfoVo;
        for(CardEquity cardEquity:cardEquityList){
            userCardEquityVo = new UserCardEquityVo();
            userCardEquityVo.setEquityId(cardEquity.getId());
            userCardEquityVo.setEquityName(cardEquity.getGroupName());
            userCardEquityVo.setEquityType(cardEquity.getEquityType());
            userCardEquityVo.setTotal(cardEquity.getShareMoney());
            userCardEquityVo.setConditionValue(cardEquity.getValue());
 
            //权益条目处理
            cardItemInfos = cardEquityByItemMap.get(cardEquity.getId());
            userCardItemVoList = new ArrayList<>();
            if(cardItemInfos != null){
                for(CardItemInfo cardItemInfo:cardItemInfos){
                    userCardItemVo = new UserCardItemVo();
                    userCardItemVo.setItemId(cardItemInfo.getId());
                    userCardItemVo.setItemName(cardItemInfo.getCommonName());
                    userCardItemVo.setItemType(cardItemInfo.getCommonType());
                    userCardItemVo.setItemCode(cardItemInfo.getCommonCode());
                    userCardItemVo.setTotal(cardItemInfo.getShareMoney());
                    userCardItemVo.setSingleAmount(cardItemInfo.getEveryShareMoney());
                    userCardItemVo.setDeductionNum(cardItemInfo.getMaxNum());
                    userCardItemVo.setDeductionSingle(cardItemInfo.getEveryDrawNum());
 
                    //获取可抵扣信息
                    userCardItemInfoVo = getSurplusNumber(userCard,cardItemInfo,cardEquity,commonService);
                    userCardItemVo.setSurplusNum(userCardItemInfoVo.getSurplusNum());
                    userCardItemVo.setUsedNum(userCardItemInfoVo.getUsedNum());
                    userCardItemVo.setUsedNumNoRefund(userCardItemInfoVo.getUsedNumNoRefund());
                    userCardItemVo.setUsedNumRefund(userCardItemInfoVo.getUsedNumRefund());
 
                    userCardItemVoList.add(userCardItemVo);
                }
            }
            userCardEquityVo.setUserCardItemVoList(userCardItemVoList);
            userCardEquityVoList.add(userCardEquityVo);
        }
 
        //权益集合存储
        userCardVo.setUserCardEquityVoList(userCardEquityVoList);
        return userCardVo;
    }
 
 
 
    /**获取卡包-卡项条目剩余次数
     * @param userCard 用户卡包
     * @param cardItemInfo 卡项条目
     * @param cardEquity 卡项权益
     * @param commonService 映射
     * @return 数据结构
     */
    public static UserCardItemInfoVo getSurplusNumber(UserCard userCard, CardItemInfo cardItemInfo,CardEquity cardEquity, CommonService commonService){
        return getSurplusNumber(userCard.getId(),cardItemInfo,cardEquity,commonService);
    }
 
    /**获取卡包-卡项条目剩余次数
     * @param userCardId 用户卡包标识
     * @param cardItemInfo 卡项条目
     * @param cardEquity 卡项权益,可空,空值从cardItemInfo获取查询
     * @param commonService 映射
     * @return 数据结构
     */
    public static UserCardItemInfoVo getSurplusNumber(String userCardId, CardItemInfo cardItemInfo,CardEquity cardEquity, CommonService commonService){
        if(StringUtils.isNull(userCardId)){
            throw new TipsException("卡包标识错误!");
        }
        if(cardItemInfo == null){
            throw new TipsException("卡项条目获取信息失败");
        }
        if(cardEquity == null){
            cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
        }
        if(cardEquity == null){
            throw new TipsException("卡项权益获取信息失败[023]");
        }
 
        //获取使用记录,注意:是根据权益,不是卡项条目查询
        List<UserCardUsed> userCardUsedList = getUsedRecord(userCardId,cardEquity.getId(),null,commonService);
 
        //已使用的条目标识集合
        Set<String> cardItemInfoIds = new HashSet<>();
        //每个条目使用总次数Map(包括退款)
        Map<String,Integer> cardItemInfoUseMap = new HashMap<>();
        //每个条目使用总次数Map(不包括退款)
        Map<String,Integer> cardItemInfoUseNoRefundMap = new HashMap<>();
        //每个条目退款总次数Map
        Map<String,Integer> cardItemInfoUseRefundMap = new HashMap<>();
        //计算所有条目(整个权益)已使用总次数
        Integer usedTotal = 0;
        //计算所有条目(整个权益)已使用总次数,不包括退款
        Integer usedTotalNoRefund = 0;
        //计算所有条目(整个权益)已退款总次数
        Integer usedTotalRefund = 0;
        //权益已经使用条目数量
        Set<String> selectInfoIds = new HashSet<>();
 
        Integer num;
        for(UserCardUsed userCardUsed:userCardUsedList){
            //计算所有条目已使用总次数
            usedTotal = usedTotal+userCardUsed.getUsedNum();
            //已使用的条目标识集合
            cardItemInfoIds.add(userCardUsed.getCardItemInfoId());
            //每个条目使用总参数Map(包括退款)
            num = cardItemInfoUseMap.computeIfAbsent(userCardUsed.getCardItemInfoId(),k->0);
            num = num + userCardUsed.getUsedNum();
            cardItemInfoUseMap.put(userCardUsed.getCardItemInfoId(),num);
 
            if(UserCardUsed.SOURCE_TYPE_USER_CARD_REFUND == userCardUsed.getSourceType()){
                //退款
                usedTotalRefund = usedTotalRefund + userCardUsed.getUsedNum();
                ////每个条目退款总参数Map
                num = cardItemInfoUseRefundMap.computeIfAbsent(userCardUsed.getCardItemInfoId(),k->0);
                num = num + userCardUsed.getUsedNum();
                cardItemInfoUseRefundMap.put(userCardUsed.getCardItemInfoId(),num);
            }else{
                //使用
                usedTotalNoRefund = usedTotalNoRefund+userCardUsed.getUsedNum();
                ////每个条目使用总参数Map(不包括退款)
                num = cardItemInfoUseNoRefundMap.computeIfAbsent(userCardUsed.getCardItemInfoId(),k->0);
                num = num + userCardUsed.getUsedNum();
                cardItemInfoUseNoRefundMap.put(userCardUsed.getCardItemInfoId(),num);
            }
 
            selectInfoIds.add(userCardUsed.getCardItemInfoId());
        }
 
        //返回结构
        UserCardItemInfoVo userCardItemInfoVo = new UserCardItemInfoVo();
        userCardItemInfoVo.setUsedTotal(usedTotal);
        userCardItemInfoVo.setUsedTotalNoRefund(usedTotalNoRefund);
        userCardItemInfoVo.setUsedTotalRefund(usedTotalRefund);
 
        if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
            //次卡,权益有总次数,所有条目使用的总次数不能大于权益的总次数
            if(usedTotal < cardEquity.getValue()){
                //是否设置了最大的抵扣次数
                int surplusNum = cardEquity.getValue() - usedTotal;
                if(cardItemInfo.getMaxNum() != null && cardItemInfo.getMaxNum() != 0){
                    userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
                    if(userCardItemInfoVo.getSurplusNum() > surplusNum){
                        userCardItemInfoVo.setSurplusNum(surplusNum);
                    }
                }else{
                    userCardItemInfoVo.setSurplusNum(surplusNum);
                }
            }else{
                userCardItemInfoVo.setSurplusNum(0);
            }
        }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
            //N选M,每个条目都有自己的可抵扣总次数
            if(cardItemInfoIds.contains(cardItemInfo.getId())){
                //已经使用条目集合包含当前条目
                userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.get(cardItemInfo.getId()));
            }else{
                //已经使用条目集合没有包含当前条目
                if(cardItemInfoIds.size() >= cardEquity.getValue()){
                    //已经选择满条目数量了
                    userCardItemInfoVo.setSurplusNum(0);
                    //不符合
                    userCardItemInfoVo.setNumByNum(false);
                }else{
                    //没有选择满条目数量
                    userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum());
                }
            }
        }else{
            //固定或者其他
            userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
        }
        //使用次数(包括退款)
        userCardItemInfoVo.setUsedNum(cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
        //使用次数(不包括退款)
        userCardItemInfoVo.setUsedNumNoRefund(cardItemInfoUseNoRefundMap.computeIfAbsent(cardItemInfo.getId(),k->0));
        //退次数
        userCardItemInfoVo.setUsedNumRefund(cardItemInfoUseRefundMap.computeIfAbsent(cardItemInfo.getId(),k->0));
        //当前权益,多少条目已经使用
        userCardItemInfoVo.setSelectInfoIds(selectInfoIds);
 
        return userCardItemInfoVo;
    }
 
 
    /**获取使用记录*/
    public static List<UserCardUsed> getUsedRecord(String userCardId,String cardEquityId,String cardItemInfoId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        sql.append("SELECT * FROM user_card_used WHERE isDel = #{m.isDel}");
        values.put("isDel",UserCardUsed.NO);
        if(StringUtils.noNull(userCardId)){
            values.put("userCardId",userCardId);
            sql.append(" AND userCardId = #{m.userCardId}");
        }
        if(StringUtils.noNull(cardEquityId)){
            values.put("cardEquityId",cardEquityId);
            sql.append(" AND cardEquityId = #{m.cardEquityId}");
        }
        if(StringUtils.noNull(cardItemInfoId)){
            values.put("cardItemInfoId",cardItemInfoId);
            sql.append(" AND cardItemInfoId = #{m.cardItemInfoId}");
        }
 
        //获取使用记录
        sqlSentence.sqlSentence(sql.toString(),values);
        //根据cardItemIfo标识查询用户使用记录
        return commonService.selectList(UserCardUsedMapper.class, sqlSentence);
    }
 
    /**卡包- 创建用户卡包使用记录
     * @param usedNum 当前需要抵扣的次数
     * @param orderId 订单标识
     * @param sourceType 来源类型
     * @param sourceId 来源标识
     * @param userCard 用户卡包
     * @param cardItemInfo 卡项条目
     * @param cardEquity 卡项权益
     * @param commonService 映射
     * @return 用户卡包使用记录
     */
    public static UserCardUsed insertUserCardUsed(Integer usedNum,String orderId,Integer sourceType,String sourceId, UserCard userCard
            , CardItemInfo cardItemInfo, CardEquity cardEquity,CommonService commonService) {
        UserCardUsed userCardUsed=new UserCardUsed();
 
        userCardUsed.setEquityType(cardEquity.getEquityType());
        userCardUsed.setGroupName(cardEquity.getGroupName());
        userCardUsed.setCommonId(cardItemInfo.getCommonId());
        userCardUsed.setCommonName(cardItemInfo.getCommonName());
        userCardUsed.setCommonType(cardItemInfo.getCommonType());
        userCardUsed.setPrice(cardItemInfo.getPrice());
        userCardUsed.setUsedGeneral(usedNum);
        userCardUsed.setUsedNum(usedNum);
        userCardUsed.setCancelNum(0);
        userCardUsed.setEmResultsNum(cardItemInfo.getEmResultsNum());
        userCardUsed.setShareMoney(cardItemInfo.getShareMoney());
        userCardUsed.setEveryDrawNum(cardItemInfo.getEveryDrawNum());
        userCardUsed.setEveryShareMoney(cardItemInfo.getEveryShareMoney());
        userCardUsed.setUserCardCode(userCard.getCode());
        userCardUsed.setUserCardId(userCard.getId());
        userCardUsed.setSourceType(sourceType);
        userCardUsed.setSourceId(sourceId);
        userCardUsed.setOrderId(orderId);
        userCardUsed.setCardEquityId(cardEquity.getId());
        userCardUsed.setCardItemInfoId(cardItemInfo.getId());
        commonService.insert(UserCardUsedMapper.class,userCardUsed);
        return userCardUsed;
    }
 
    /**计算用户卡包购买时候的金额分摊方式-订单*/
    public static DistributionRedundVo userCardPay(UserCard userCard,OrderItem orderItem, CommonService commonService, RefundMapper refundMapper){
        DistributionRedundVo distributionRedundVo = new DistributionRedundVo();
        //不是订单购买的卡包,跳出循环
        if(UserCard.SOURCE_TYPE_ORDER_ONE != userCard.getSourceType()){
            return distributionRedundVo;
        }
 
        //通过购买这个卡项的订单来算单卡的钱是多少
        //查询已经退了多少钱,避免退超了支付方式金额
 
        if(orderItem == null){
            orderItem = commonService.selectOneByKey(OrderItemMapper.class,userCard.getSourceId());
        }
        if(orderItem == null){
            throw new TipsException("获取订单子信息失败[895]");
        }
        if(orderItem.getBuyNum().compareTo(0) < 1){
            throw new TipsException("获取订单子信息购买数量错误!");
        }
 
        //获取未退款的卡包,包含转增的
        List<UserCard> userCardList = getNotRefundCard(orderItem.getId(),commonService);
 
        //获取该子单的支付方式,相同的支付方式求和返回
        List<PayMethodVo> payMethodVoList = refundMapper.getConsumePayOneGroupByNumberNo(orderItem.getId());
        //获取该子单已退的总金额
        RefundRecordItem refundRecordItem = RefundTool.getOneAlreadyRefundTotal(orderItem.getId(),orderItem.getType(),commonService);
        logger.info("用户卡包子单支付方式{},子单退款数据{}", JSON.toJSONString(payMethodVoList),JSON.toJSONString(refundRecordItem));
 
        //计算退款方式金额
        distributionRedundVo = PaymentCountTool.countMakeWay(orderItem.getId()
                , orderItem.getActualTotal(),orderItem.getActualTotalPoints()
                , orderItem.getIntegralCash(),refundRecordItem.getOccupyRefundTotal()
                ,refundRecordItem.getOccupyRefundIntegral(),orderItem.getBuyNum(),userCardList.size(),null
                ,null,1,payMethodVoList,commonService);
        distributionRedundVo.setGoodsType(orderItem.getType());
        distributionRedundVo.setGoodsName(orderItem.getGoodsName());
 
        return distributionRedundVo;
    }
 
    /**获取未退款的卡项数量
     * @param sourceId 订单子单标识
     * @return 可退款的用户卡项
     */
    public static List<UserCard> getNotRefundCard(String sourceId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> sqlMap = new HashMap<>();
        //获取用户卡项
        sqlMap.put("sourceId",sourceId);
        sqlMap.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
        sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND effectiveStatus != #{m.effectiveStatus}",sqlMap);
        List<UserCard> userCardList = commonService.selectList(UserCardMapper.class,sqlSentence);
        if(userCardList.size() == 0){
            return userCardList;
        }
        //获取已经部分退的用户卡包数量
        List<RefundRecordItem> refundRecordItemList = CardRefundTool.findRefundUserCard(sourceId,null,commonService);
        //过滤掉没有使用但有部分退款
        if(refundRecordItemList.size() == 0){
            return userCardList;
        }
 
        Map<String,UserCard> userCardMap = new HashMap<>();
        for(UserCard userCard:userCardList){
            userCardMap.put(userCard.getId(),userCard);
        }
        //去除掉参与部分退款的用户卡包
        for(RefundRecordItem refundRecordItem:refundRecordItemList){
            userCardMap.remove(refundRecordItem.getUserCardId());
        }
        userCardList = new ArrayList<>();
        for (Map.Entry<String,UserCard> entry : userCardMap.entrySet()) {
            userCardList.add(entry.getValue());
        }
 
        return userCardList;
    }
 
    /**计算卡包条目单次抵扣的金额
     * 保留20位小数
     * @param cardItemInfo 卡项条目
     * @return 单次抵扣的金额
     */
    public static BigDecimal countOneMoney(CardItemInfo cardItemInfo){
        //卡项明细分摊金额
        BigDecimal sumCardBagMoney = cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney();
        //卡项明细分摊次数
        Integer num = cardItemInfo.getMaxNum()==null?cardItemInfo.getEveryDrawNum():cardItemInfo.getMaxNum();
        //求出单次的单价
        if(num != null && num > 0){
            return sumCardBagMoney.divide(BigDecimal.valueOf(num),20,RoundingMode.HALF_UP);
        }else{
            return BigDecimal.ZERO;
        }
    }
 
    /**获取卡项的权益数量*/
    public static int getCardEquityCount(String cardId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
 
        values.put("cardItemId",cardId);
        sqlSentence.sqlSentence("SELECT COUNT(*) FROM card_equity WHERE isDel = 0 AND cardItemId = #{m.cardItemId}" +
                " AND equityType != 4",values);
 
        return commonService.selectCountSql(CardEquityMapper.class,sqlSentence);
    }
 
    /**获取卡项权益的条目数量*/
    public static int getCardInfoCount(String cardEquityId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
 
        values.put("cardEquityId",cardEquityId);
        sqlSentence.sqlSentence("SELECT COUNT(*) FROM card_item_info WHERE isDel = 0 AND cardEquityId = #{m.cardEquityId}"
          ,values);
 
        return commonService.selectCountSql(CardItemInfoMapper.class,sqlSentence);
    }
 
    /**获取卡项的条目使用抵扣次数
     * @param cardEquityId 卡项权益标识
     * @param cardInfoId 卡项条目标识
     * @param commonService 映射
     * @return 已抵扣次数
     */
    public static int getCardInfoDeduction(String cardEquityId,String cardInfoId,CommonService commonService){
 
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        sql.append("SELECT IFNULL(SUM(usedNum),0) AS usedNum FROM user_card_used WHERE isDel = 0");
        if(StringUtils.noNull(cardEquityId)){
            values.put("cardEquityId",cardEquityId);
            sql.append(" AND cardEquityId = #{m.cardEquityId}");
        }
        if(StringUtils.noNull(cardInfoId)){
            values.put("cardItemInfoId",cardInfoId);
            sql.append(" AND cardItemInfoId = #{m.cardItemInfoId}");
        }
 
        UserCardUsed userCardUsed = commonService.selectOne(CardEquityMapper.class,sqlSentence);
        return userCardUsed.getUsedNum();
    }
 
    /**
     * 获取卡项分摊的权益金额
     */
    public static List<CardEquity> getCardEquityMoneyList(String cardId, CardItem cardItem, CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> values = new HashMap<>();
        values.put("isDel", BaseEntity.NO);
        if (StringUtils.isEmpty(cardId)){
            cardId = cardItem.getId();
        }
        values.put("cardItemId", cardId);
        sqlSentence.sqlSentence("SELECT * FROM card_equity WHERE isDel = #{m.isDel} AND cardItemId = #{m.cardItemId}", values);
        List<CardEquity> cardEquityList = commonService.selectList(CardEquityMapper.class, sqlSentence);
        if (cardEquityList == null || cardEquityList.size() < 1) {
            throw new TipsException("获取卡项权益信息失败!");
        }
        for (CardEquity cardEquity : cardEquityList) {
            //固定项目权益,分摊的金额查条目的
            if (cardEquity.getEquityType().equals(CardEquity.EQUITY_TYPE_PROJECT)) {
                cardEquity.setShareMoney(BigDecimal.ZERO);
                values.put("cardEquityId", cardEquity.getId());
                sqlSentence.sqlSentence("SELECT SUM(shareMoney) AS shareMoney FROM card_item_info WHERE isDel = #{m.isDel} AND cardEquityId = #{m.cardEquityId} ", values);
                CardItemInfo cardItemInfo = commonService.selectOne(CardItemInfoMapper.class, sqlSentence);
                if (cardItemInfo != null) {
                    cardEquity.setShareMoney(cardItemInfo.getShareMoney());
                }
            }
        }
        return cardEquityList.stream().sorted(Comparator.comparing(CardEquity::getShareMoney)).collect(Collectors.toList());
    }
 
 
    /**
     * 卡包支付过的数据-剔除退款的数据
     */
    public static Map<String,Map<String,UserCardPay>> getCardBagUseList(String cardEquityId, CardEquity cardEquity, CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> values = new HashMap<>();
        values.put("isDel", BaseEntity.NO);
        if (StringUtils.isEmpty(cardEquityId)){
            cardEquityId = cardEquity.getId();
        }
        values.put("cardEquityId", cardEquityId);
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * FROM  user_card_pay AS u ");
        sql.append(" JOIN orders_total AS ot ON ot.id = u.orderId");
        sql.append(" WHERE u.isDel = #{m.isDel} AND u.cardEquityId = #{m.cardEquityId} ");
        //卡包要么全退,要么不退,这里查未退款的
        sql.append(" AND ot.isDel = #{m.isDel} AND ot.payStatus = 3 AND ot.refundStatus = -1 ");
        sqlSentence.sqlSentence(sql.toString(), values);
        List<UserCardPay> cardBagUseList = commonService.selectList(UserCardPayMapper.class, sqlSentence);
        Map<String,Map<String,UserCardPay>> cardBuyMoneyMap = new HashMap<>();
        Map<String, UserCardPay> itemMap = null;
        if (cardBagUseList != null && cardBagUseList.size() > 0){
            for (UserCardPay userCardPay : cardBagUseList) {
                itemMap = cardBuyMoneyMap.get(userCardPay.getCardItemInfoId());
                if (itemMap == null){
                    itemMap = new HashMap<>();
                }
                itemMap.put(userCardPay.getConsumePayItemId(),userCardPay);
                cardBuyMoneyMap.put(userCardPay.getCardItemInfoId(),itemMap);
            }
        }
        return cardBuyMoneyMap;
    }
 
    /**
     * 卡包订单子项分配到的金额
     */
    public static BigDecimal orderItemSonMoney(String orderItemSonId, CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> values = new HashMap<>();
        values.put("isDel", BaseEntity.NO);
        values.put("orderItemSonId", orderItemSonId);
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT SUM(actualTotal) AS actualTotal FROM user_card_pay AS u ");
        sql.append(" JOIN orders_total AS ot ON ot.id = u.orderId");
        sql.append(" WHERE u.isDel = #{m.isDel} AND u.orderItemSonId = #{m.orderItemSonId} ");
        //卡包要么全退,要么不退,这里查未退款的
        sql.append(" AND ot.isDel = #{m.isDel} AND ot.payStatus = 3 AND ot.refundStatus = -1 ");
        sqlSentence.sqlSentence(sql.toString(), values);
        UserCardPay userCardPay = commonService.selectOne(UserCardPayMapper.class, sqlSentence);
        return userCardPay == null?BigDecimal.ZERO:userCardPay.getActualTotal();
    }
 
    /**获取卡项权益的条目数量*/
    public static List<CardItemInfo> getCardInfoList(String cardEquityId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        values.put("cardEquityId",cardEquityId);
        sqlSentence.sqlSentence("SELECT * FROM card_item_info WHERE isDel = 0 AND cardEquityId = #{m.cardEquityId}",values);
        return commonService.selectList(CardItemInfoMapper.class,sqlSentence);
    }
 
    /**
     * 获取买卡单数据-已分配的OCC
     * @return <卡项,支付方式,数据>
     * */
    public static Map<String,Map<String,ConsumePayCard>> getCardBuyMoneyByItemId(String orderItemId,CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        values.put("orderItemId",orderItemId);
        values.put("commonType", ConsumePayCard.TYPE_CARD);
        sqlSentence.sqlSentence("SELECT * FROM consume_pay_card WHERE isDel = 0 AND orderItemId = #{m.orderItemId} AND commonType = #{m.commonType}",values);
        List<ConsumePayCard> consumePayCardList = commonService.selectOne(ConsumePayCardMapper.class,sqlSentence);
        Map<String,Map<String,ConsumePayCard>> totalMap = new HashMap<>();
        Map<String,ConsumePayCard> itemMap = null;
        if (consumePayCardList != null && consumePayCardList.size() > 0){
            for (ConsumePayCard consumePayCard : consumePayCardList) {
                itemMap = totalMap.get(consumePayCard.getUserCardId());
                if (itemMap == null){
                    itemMap = new HashMap<>();
                }
                itemMap.put(consumePayCard.getConsumePayItemId(),consumePayCard);
                totalMap.put(consumePayCard.getUserCardId(),itemMap);
            }
        }
        return totalMap;
    }
 
    /**
     * 获取买卡单数据-已分配的OCC
     * @return <权益或者条目,支付方式,数据>
     * */
    public static Map<String, Map<String, ConsumePayCard>> getCardDetailBuyMoneyByItemId(String commonId,String commonType, CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        values.put("commonId",commonId);
        values.put("commonType", ConsumePayCard.TYPE_EQU);
        sqlSentence.sqlSentence("SELECT * FROM consume_pay_card WHERE isDel = 0 AND commonId = #{m.commonId} AND commonType = #{m.commonType}",values);
        List<ConsumePayCard> consumePayCardList = commonService.selectOne(ConsumePayCardMapper.class,sqlSentence);
        Map<String,Map<String,ConsumePayCard>> totalMap = new HashMap<>();
        Map<String,ConsumePayCard> itemMap = null;
        if (consumePayCardList != null && consumePayCardList.size() > 0){
            for (ConsumePayCard consumePayCard : consumePayCardList) {
                itemMap = totalMap.get(consumePayCard.getCommonId());
                if (itemMap == null){
                    itemMap = new HashMap<>();
                }
                itemMap.put(consumePayCard.getConsumePayItemId(),consumePayCard);
                totalMap.put(consumePayCard.getCommonId(),itemMap);
            }
        }
        return totalMap;
    }
 
}