chenjiahe
2023-06-16 949373691bb795099503ec2d9a9d8cffd8d2669c
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
package com.hx.phip.tool.refund;
 
import com.hx.common.service.CommonService;
import com.hx.exception.TipsException;
import com.hx.mybatisTool.SqlSentence;
import com.hx.phiappt.common.OrderItemConstants;
import com.hx.phiappt.common.RefundStatus;
import com.hx.phiappt.model.BaseEntity;
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.order.OrderItem;
import com.hx.phiappt.model.refund.RefundRecordConsumePay;
import com.hx.phiappt.model.refund.RefundRecordItem;
import com.hx.phiappt.model.refund.RefundRecordItemSource;
import com.hx.phiappt.model.user.UserCard;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.tool.user.UserCardTool;
import com.hx.phip.vo.order.payment.PayMethodVo;
import com.hx.phip.vo.order.refund.DistributionRedundVo;
import com.hx.phip.vo.order.refund.RefundCardVo;
import com.hx.phip.vo.user.UserCardItemInfoVo;
import com.hx.util.StringUtils;
import com.hz.his.vo.order.refund.RefundUserCardItemVo;
import com.hz.his.vo.order.refund.RefundUserCardVo;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
import org.springframework.beans.BeanUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class RefundTool {
 
    /**获取已退款方式金额,根据支付方式编号求和返回
     * 注意:groupByONumberNo和groupByConsumePayId不能同时使用
     * @param itemId 订单子单标识
     * @param refundRecordItemId 退款子单标识
     * @param groupByONumberNo 根据支付方式编号求和
     * @param groupByConsumePayId 根据支付方式记录标识求和
     * @param commonService 映射
     * @return 返回已退款方式金额集合
     */
    public static List<RefundRecordConsumePay> getRefundRecordConsumePay(String itemId, String refundRecordItemId, boolean groupByONumberNo
            , boolean groupByConsumePayId, CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        sql.append("SELECT");
        if(groupByONumberNo){
            sql.append(" a.numberNo,");
        }
        if(groupByConsumePayId){
            sql.append(" a.consumePayId,");
        }
        sql.append("SUM(a.refundTotal) AS refundTotal FROM refund_record_consume_pay a");
        sql.append(" JOIN refund_record rr ON rr.id = a.refundRecordId AND rr.isDel = 0 AND rr.refundStatus = #{m.refundStatus}");
        sql.append("  WHERE a.isDel = 0");
        if(StringUtils.noNull(itemId)){
            sql.append("  AND a.commonId = #{m.commonId}");
        }
        if(StringUtils.noNull(refundRecordItemId)){
            sql.append("  AND a.refundRecordItemId = #{m.refundRecordItemId}");
        }
        if(groupByONumberNo){
            sql.append(" GROUP BY a.numberNo");
        }
        if(groupByConsumePayId){
            sql.append(" GROUP BY a.consumePayId");
        }
        //获取已退款的支付方式
        values.put("commonId",itemId);
        values.put("refundStatus", RefundStatus.STATUS_SUCC_REFUND);
        sqlSentence.sqlSentence(sql.toString(),values);
        return commonService.selectList(RefundRecordConsumePayMapper.class,sqlSentence);
    }
 
    /**获取一级退款子单已退金额
     * 主要查询,单个数量分配的退款金额,因为目前是按照数量来分配金额退款的
     * @param orderItemId 一级退款子单关联标识
     * @param goodsType 商品类型
     * @param commonService 映射
     * @return 已退金额
     */
    public static RefundRecordItem getOneAlreadyRefundTotal(String orderItemId,String goodsType, CommonService commonService ){
 
        //查找正常退款的
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
        //获取已退款的金额,剔除掉用户卡项的部分退
        values.put("orderItemId",orderItemId);
        sql.append("SELECT ROUND(SUM(rri.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rri.occupyRefundIntegral),2) AS occupyRefundIntegral");
        sql.append(" FROM refund_record_item rri");
        sql.append(" JOIN refund_record rr ON rr.id = rri.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
        sql.append(" WHERE rri.isDel = 0 AND rri.orderItemId = #{m.orderItemId} AND rri.userCardId IS NULL");
        sqlSentence.sqlSentence(sql.toString(),values);
        RefundRecordItem refundRecordItem = commonService.selectOne(RefundRecordItemMapper.class,sqlSentence);
        if(refundRecordItem == null){
            //设置没有找到
            refundRecordItem.setIsDel(BaseEntity.YES);
            refundRecordItem = new RefundRecordItem();
        }
 
        //用户卡包退款,系统退款总金额,按照退款数量来算的,用户卡包部分退,要特殊查找
        if(OrderItemConstants.TYPE_CARD.equals(goodsType)){
            //查找有没有这个卡包部分退款的记录
            List<RefundRecordItem> refundRecordItemList = findRefundUserCard(orderItemId,null,commonService);
            if(refundRecordItemList.size() > 0){
                refundRecordItem.setIsDel(BaseEntity.NO);
            }
            for(RefundRecordItem refundRecordItem1:refundRecordItemList){
                refundRecordItem.setOccupyRefundTotal(refundRecordItem.getOccupyRefundTotal().add(refundRecordItem1.getOccupyRefundTotal()));
                refundRecordItem.setOccupyRefundIntegral(refundRecordItem.getOccupyRefundIntegral().add(refundRecordItem1.getOccupyRefundIntegral()));
            }
        }
 
        return refundRecordItem;
    }
 
    /**获取二级退款子单已退金额
     * @param orderItemSonId 二级退款子单关联标识/关联卡项条目的id
     * @param commonService 映射
     * @return 已退金额
     */
    public static RefundRecordItemSource getTwoAlreadyRefundTotal(String orderItemSonId, CommonService commonService ){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        values.put("orderItemSonId",orderItemSonId);
        sql.append("SELECT ROUND(SUM(rris.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rris.occupyRefundIntegral),2) AS occupyRefundIntegral");
        sql.append(" FROM refund_record_item_source rris");
        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
        sql.append(" WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId}");
        //获取已退款的金额
        sqlSentence.sqlSentence(sql.toString(),values);
        RefundRecordItemSource refundRecordItemSource = commonService.selectOne(RefundRecordItemSourceMapper.class,sqlSentence);
 
        if(refundRecordItemSource == null){
            //设置是没有找到
            refundRecordItemSource.setIsDel(BaseEntity.YES);
            return new RefundRecordItemSource();
        }else{
            return refundRecordItemSource;
        }
    }
 
    /**计算用户卡包条目退款金额*/
    public static void userCardRefundCount(UserCard userCard,RefundUserCardVo refundUserCardVo, CommonService commonService, RefundMapper refundMapper){
 
        ////算出金额:
        //1、算出该卡可退总金额
        //2、算出卡项条目总金额,就是占订单支付金额的钱
        //3、算出单条目的权益占用金额
        //4、获取该条目已退款金额
        //5、算出“单次抵扣金额”,再乘以数量得出“抵扣总金额”
        //6、用“抵扣总金额”算出占用整个卡项总金额的“比例”
        //7、比较金额,得出可退金额,注意最后一次要全部退掉
 
        //获取卡项
        CardItem cardItem = commonService.selectOneByKey(CardItemMapper.class,userCard.getCardItemId());
        if(cardItem == null){
            throw new TipsException("获取卡项信息失败[210]!");
        }
 
        //获取子单
        OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,userCard.getSourceId());
        if(orderItem == null){
            throw new TipsException("获取订单子单失败[210]!");
        }
 
        //公用对象
        RefundRecordItemSource itemSource;
 
        //用户卡项分配可退总金额
        RefundCardVo refundCard = new RefundCardVo();
 
        CardItemInfo cardItemInfo;
        CardEquity cardEquity;
        //条目金额计算
        BigDecimal sumCardBagMoney;
        //条目积分计算
        BigDecimal sumCardBagIntegral;
 
        //获取卡项的权益数量
        int cardEquityNum = UserCardTool.getCardEquityCount(userCard.getCardItemId(),commonService);
 
        //存储权益分配的退款金额
        Map<String,RefundCardVo> cardEquityMap = new HashMap<>();
 
        //比例
        RefundCardVo refundCardEquity;
        RefundCardVo refundCardInfo;
        BigDecimal proportion;
        for(RefundUserCardItemVo refundUserCardItemVo:refundUserCardVo.getRefundUserCardItemVoList()){
            if(StringUtils.isEmpty(refundUserCardItemVo.getCardItemInfoId())){
                throw new TipsException("卡项条目标识必传[210]!");
            }
            if(refundUserCardItemVo.getRefundQuantity() == null || refundUserCardItemVo.getRefundQuantity() < 1){
                throw new TipsException("卡项条目退款次数必填[210]!");
            }
            cardItemInfo = commonService.selectOneByKey(CardItemInfoMapper.class,refundUserCardItemVo.getCardItemInfoId());
            if(cardItemInfo == null){
                throw new TipsException("获取卡项条目信息失败[210]!");
            }
            cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
            if(cardEquity == null){
                throw new TipsException("获取卡项权益信息失败[210]!");
            }
            //-----校验
            //获取可用剩余使用次数
            UserCardItemInfoVo uerCardItemInfoVo = UserCardTool.getSurplusNumber(userCard,cardItemInfo,cardEquity,commonService);
            //获取当前需要抵扣的次数
            Integer usedNum = UserCardTool.countUsedNumber(refundUserCardItemVo.getRefundQuantity(),cardItemInfo,cardEquity);
            if(!uerCardItemInfoVo.isNumByNum()){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项退款提示:不符合N选M规则,请重试");
            }
            if(uerCardItemInfoVo.getSurplusNum() <= 0 ||usedNum > uerCardItemInfoVo.getSurplusNum()){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项退款提示:卡项的【"+cardItemInfo.getCommonName()+"】次数不够");
            }
 
            refundCardInfo = new RefundCardVo();
 
            //-----算出权益分配到金额
            //查看权益是否已经分配好了的
            refundCardEquity = getCardEquityData(orderItem,userCard,cardItem,cardEquity,refundCard,cardEquityNum,cardEquityMap,commonService,refundMapper);
            //判断是不是最后一个条目
            if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
                //次卡,权益有总次数,所有条目使用的总次数不能大于权益的总次数
                //判断数量
                if(uerCardItemInfoVo.getSurplusNum() < refundUserCardItemVo.getRefundQuantity()){
                    //最后的次数
                    refundCardInfo.setOccupyRefundTotal(refundCardEquity.getSurplusTotal());
                    refundCardInfo.setOccupyRefundIntegral(refundCardEquity.getSurplusIntegral());
                }else{
 
                }
                refundCardInfo.setSurplusTotal(refundCardEquity.getSurplusTotal());
                refundCardInfo.setSurplusIntegral(refundCardEquity.getSurplusIntegral());
            }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
                //判断条目数量,再判断数量
 
 
            }else{
                //固定或者其他
                userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
            }
 
            //-----算出单次划扣金额
            //获取单次价格金额
            sumCardBagMoney = UserCardTool.countOneMoney(cardItemInfo);
            //计算抵扣总的分摊明细
            sumCardBagMoney = sumCardBagMoney.multiply(BigDecimal.valueOf(refundUserCardItemVo.getRefundQuantity()).setScale(2,RoundingMode.DOWN));
            //算比例
            if(cardItem.getTotal() != null && cardItem.getTotal().compareTo(BigDecimal.ZERO) > 0){
                proportion = sumCardBagMoney.divide(cardItem.getTotal(),20,RoundingMode.HALF_UP);
            }else{
                proportion = BigDecimal.ZERO;
            }
 
            //算出占用购买卡项订单时候金额(分摊模式)
            sumCardBagMoney = occupyRefundTotal.multiply(proportion);
            sumCardBagIntegral = occupyRefundIntegral.multiply(proportion);
 
            //获取卡项条目已退款金额
            refundRecordItemSource = getTwoAlreadyRefundTotal(cardItemInfo.getId(),null,null,commonService);
 
            //
 
            //不能超过卡项条目的分摊总金额
 
        }
 
    }
 
    /**查找部分退款的用户卡包信息,根据用户卡包标识去重了
     * @param orderItemId 子单标识
     * @param userCardId 用户卡包标识
     * @param commonService 映射
     */
    public static List<RefundRecordItem> findRefundUserCard(String orderItemId,String userCardId,CommonService commonService){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
 
        StringBuilder sql = new StringBuilder();
 
        //--------获取用户卡包的总金额
        //查找有没有这个卡包部分退款的记录
        sql.append("SELECT rri.occupyRefundTotal,rri.occupyRefundIntegral,rri.userCardId FROM refund_record_item rri");
        sql.append(" JOIN refund_record rr ON rr.id = rri.refundRecordId");
        sql.append(" JOIN user_card uc ON uc.id = rri.userCardId");
        sql.append(" AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
        sql.append(" WHERE rri.isDel = 0");
        if(StringUtils.noNull(orderItemId)){
            values.put("orderItemId",orderItemId);
            sql.append(" AND rri.orderItemId = #{m.orderItemId}");
        }
        if(StringUtils.noNull(userCardId)){
            values.put("userCardId",userCardId);
            sql.append(" AND rri.userCardId = #{m.userCardId}");
        }else{
            sql.append(" AND rri.userCardId IS NOT NULL");
        }
        sql.append(" GROUP BY rri.userCardId");
 
        sqlSentence.sqlSentence(sql.toString(),values);
        return commonService.selectList(UserCardMapper.class,sqlSentence);
    }
 
    /**获取卡包已分配的金额(权益分配的金额)
     * @param userCardId 用户卡项标识,主要获取用户卡项分配的金额
     * @param commonService 映射
     * @return 已退金额
     */
    public static RefundRecordItemSource getCardRealRefund(String userCardId, CommonService commonService ){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        values.put("userCardId",userCardId);
        sql.append("SELECT rris.occupyEqRefundTotal,rris.occupyEqRefundIntegral");
        sql.append(" FROM refund_record_item_source rris");
        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
        sql.append(" WHERE rris.isDel = 0 AND rris.userCardId = #{m.userCardId} GROUP BY rris.cardEquityId");
 
        List<RefundRecordItemSource> refundRecordItemSourceList = commonService.selectList(RefundRecordItemSourceMapper.class,sqlSentence);
 
        RefundRecordItemSource source = new RefundRecordItemSource();
        if(refundRecordItemSourceList.size()==0){
            source.setIsDel(BaseEntity.YES);
            return source;
        }
        for(RefundRecordItemSource refundRecordItemSource:refundRecordItemSourceList){
            //计算有多少个权益参与了退款
            refundRecordItemSource.setRealRefundNum(refundRecordItemSource.getRealRefundNum()+1);
            source.setOccupyRefundTotal(source.getOccupyRefundTotal().add(refundRecordItemSource.getOccupyEqRefundTotal()));
            source.setOccupyRefundIntegral(source.getOccupyRefundIntegral().add(refundRecordItemSource.getOccupyEqRefundIntegral()));
        }
        return source;
    }
 
    /**获取卡包权益已退款金额(条目分配的金额)
     * @param cardEquityId 卡项权益标识,主要获取卡项权益分配的金额
     * @param commonService 映射
     * @return 已退金额
     */
    public static RefundRecordItemSource getCardEquityRealRefund(String cardEquityId, CommonService commonService ){
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        values.put("cardEquityId",cardEquityId);
        sql.append("SELECT rris.occupyItemRefundTotal,rris.occupyItemRefundIntegral,rris.occupyEqRefundTotal,rris.occupyEqRefundIntegral");
        sql.append(" FROM refund_record_item_source rris");
        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
        sql.append(" WHERE rris.isDel = 0 AND rris.cardEquityId = #{m.cardEquityId} GROUP BY orderItemSonId");
 
        List<RefundRecordItemSource> refundRecordItemSourceList = commonService.selectList(RefundRecordItemSourceMapper.class,sqlSentence);
 
        RefundRecordItemSource source = new RefundRecordItemSource();
        if(refundRecordItemSourceList.size()==0){
            source.setIsDel(BaseEntity.YES);
            return source;
        }
        for(RefundRecordItemSource refundRecordItemSource:refundRecordItemSourceList){
            //计算有多少个条目参与了退款
            source.setRealRefundNum(refundRecordItemSource.getRealRefundNum()+1);
            source.setRefundMoney(source.getRefundMoney().add(refundRecordItemSource.getOccupyItemRefundTotal()));
            source.setRefundIntegral(source.getRefundIntegral().add(refundRecordItemSource.getOccupyItemRefundIntegral()));
            source.setOccupyEqRefundTotal(refundRecordItemSource.getOccupyEqRefundTotal());
            source.setOccupyEqRefundIntegral(refundRecordItemSource.getOccupyEqRefundIntegral());
        }
        return source;
    }
 
    /**获取卡包条目已退款金额(条目分配的金额)
     * @param cardInfoId 卡项条目标识,主要获取卡项条目分配的金额
     * @param commonService 映射
     * @return 已退金额
     */
    public static RefundCardVo getCardInfoData(String cardInfoId,CommonService commonService){
 
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> values = new HashMap<>();
        StringBuilder sql = new StringBuilder();
 
        values.put("orderItemSonId",cardInfoId);
        sql.append("SELECT SUM(rris.occupyRefundTotal) AS occupyRefundTotal,SUM(rris.occupyRefundIntegral) AS occupyRefundIntegral");
        sql.append(",SUM(rris.realRefundNum) AS realRefundNum,rris.occupyItemRefundTotal,rris.occupyItemRefundIntegral");
        sql.append(" FROM refund_record_item_source rris");
        sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
        sql.append(" WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId} GROUP BY orderItemSonId");
 
        RefundRecordItemSource itemSource = commonService.selectOne(RefundRecordItemSourceMapper.class,sqlSentence);
 
        RefundCardVo refundCardInfo = new RefundCardVo();
 
        if(itemSource != null){
            refundCardInfo.setJoinRefund(true);
            refundCardInfo.setRefundNum(itemSource.getRealRefundNum());
            refundCardInfo.setOccupyRefundTotal(itemSource.getOccupyItemRefundTotal());
            refundCardInfo.setOccupyRefundIntegral(itemSource.getOccupyItemRefundIntegral());
            refundCardInfo.setSurplusTotal(itemSource.getOccupyItemRefundTotal().subtract(itemSource.getOccupyRefundTotal()));
            refundCardInfo.setSurplusIntegral(itemSource.getOccupyItemRefundIntegral().subtract(itemSource.getOccupyRefundIntegral()));
 
        }
        return refundCardInfo;
 
 
    }
 
 
    /**获取用户卡项的参数,分配金额和剩余可退金额*/
    public static RefundCardVo getUserCardData(OrderItem orderItem,UserCard userCard,CommonService commonService,RefundMapper refundMapper){
 
        RefundCardVo refundCard = new RefundCardVo();
        //--------获取用户卡包分配的的可退款总金额(包含已经部分退的金额在里面)
        //查找有没有这个卡包部分退款的记录
        List<RefundRecordItem> refundRecordItemList = findRefundUserCard(orderItem.getId(),userCard.getId(),commonService);
        if(refundRecordItemList.size() == 0){
            //用户卡包没有分配总金额,那么就按照正常的流程获取可退款金额
            DistributionRedundVo distributionRedundVo = UserCardTool.userCardPay(userCard,orderItem,commonService,refundMapper);
            refundCard.setOccupyRefundTotal(distributionRedundVo.getRefundTotal());
            refundCard.setOccupyRefundIntegral(distributionRedundVo.getRefundIntegral());
        }else{
            refundCard.setJoinRefund(true);
            RefundRecordItem refundRecordItem = refundRecordItemList.get(0);
            refundCard.setOccupyRefundTotal(refundRecordItem.getOccupyRefundTotal());
            refundCard.setOccupyRefundIntegral(refundRecordItem.getOccupyRefundIntegral());
        }
 
        //-----计算剩余可分配给权益退款的金额
        //查找该卡包已部分退款的金额
        RefundRecordItemSource itemSource = getCardRealRefund(userCard.getId(),commonService);
        //计算该用户卡包剩余可退金额和积分,就是可分配给权益退款的金额
        refundCard.setSurplusTotal(refundCard.getOccupyRefundTotal().subtract(itemSource.getOccupyRefundTotal()));
        refundCard.setSurplusIntegral(refundCard.getOccupyRefundIntegral().subtract(itemSource.getOccupyRefundIntegral()));
        refundCard.setRefundNum(itemSource.getRealRefundNum());
 
        return refundCard;
    }
 
    /**获取用户卡项权益的参数,分配金额和剩余可退金额*/
    public static RefundCardVo getCardEquityData(OrderItem orderItem,UserCard userCard,CardItem cardItem,CardEquity cardEquity,RefundCardVo refundCard,int cardEquityNum
            ,Map<String,RefundCardVo> cardEquityMap,CommonService commonService,RefundMapper refundMapper){
 
        //-----算出权益分配到金额
        //查看权益是否已经分配好了的
        BigDecimal proportion;
        RefundCardVo refundCardEquity = cardEquityMap.get(cardEquity.getId());
        if(refundCardEquity == null){
            refundCardEquity = new RefundCardVo();
 
            //获取卡项权益已退款金额和分配的金额
            RefundRecordItemSource cardEquityRealRefund = getCardEquityRealRefund(cardEquity.getId(),commonService);
            if(cardEquityRealRefund.getIsDel().equals(BaseEntity.YES)){
                //没有找到已经分配好的,进行新的分配
                //获取用户卡包分配金额
                if(!refundCard.isJoinRefund()){
                    BeanUtils.copyProperties(getUserCardData(orderItem,userCard,commonService,refundMapper),refundCard);
                    refundCard.setJoinRefund(true);
                }
 
                if(cardEquityNum>=refundCard.getRefundNum()+1){
                    //是最后一个
                    refundCardEquity.setOccupyRefundTotal(refundCard.getSurplusTotal());
                    refundCardEquity.setOccupyRefundIntegral(refundCard.getSurplusIntegral());
                }else{
                    //不是最后一个,通过比例来算金额
                    if(cardItem.getTotal().compareTo(BigDecimal.ZERO) > 0){
                        proportion = cardEquity.getShareMoney().divide(cardItem.getTotal(),20,RoundingMode.HALF_UP);
                    }else{
                        proportion = BigDecimal.ZERO;
                    }
                    refundCardEquity.setOccupyRefundTotal(refundCard.getOccupyRefundTotal().multiply(proportion).setScale(2,RoundingMode.HALF_UP));
                    refundCardEquity.setOccupyRefundIntegral(refundCard.getOccupyRefundIntegral().multiply(proportion).setScale(2,RoundingMode.HALF_UP));
 
                    //判断不能大于权益设置的金额
                    if(refundCardEquity.getOccupyRefundTotal().compareTo(cardEquity.getShareMoney()) > 0){
                        refundCardEquity.setOccupyRefundTotal(cardEquity.getShareMoney());
                    }
                    //判断不能大于剩下的可退金额
                    if(refundCardEquity.getOccupyRefundTotal().compareTo(refundCard.getSurplusTotal()) > 0){
                        refundCardEquity.setOccupyRefundTotal(refundCard.getSurplusTotal());
                    }
                    //判断不能大于剩下的可退积分
                    if(refundCardEquity.getOccupyRefundIntegral().compareTo(refundCard.getSurplusIntegral()) > 0){
                        refundCardEquity.setOccupyRefundIntegral(refundCard.getSurplusIntegral());
                    }
                }
                refundCardEquity.setSurplusTotal(refundCardEquity.getOccupyRefundTotal());
                refundCardEquity.setSurplusIntegral(refundCardEquity.getOccupyRefundIntegral());
            }else{
                refundCardEquity.setJoinRefund(true);
                refundCardEquity.setRefundNum(cardEquityRealRefund.getRealRefundNum());
                refundCardEquity.setOccupyRefundTotal(cardEquityRealRefund.getOccupyEqRefundTotal());
                refundCardEquity.setOccupyRefundIntegral(cardEquityRealRefund.getOccupyEqRefundIntegral());
                refundCardEquity.setSurplusTotal(cardEquityRealRefund.getOccupyEqRefundTotal().subtract(cardEquityRealRefund.getRefundMoney()));
                refundCardEquity.setSurplusIntegral(cardEquityRealRefund.getOccupyEqRefundIntegral().subtract(cardEquityRealRefund.getRefundIntegral()));
            }
            cardEquityMap.put(cardEquity.getId(),refundCardEquity);
        }
        return refundCardEquity;
    }
 
 
}