chenjiahe
2023-06-16 949373691bb795099503ec2d9a9d8cffd8d2669c
提交 | 用户 | age
a036dc 1 package com.hx.phip.tool.refund;
C 2
3 import com.hx.common.service.CommonService;
4 import com.hx.exception.TipsException;
5 import com.hx.mybatisTool.SqlSentence;
6 import com.hx.phiappt.common.OrderItemConstants;
7 import com.hx.phiappt.common.RefundStatus;
949373 8 import com.hx.phiappt.model.BaseEntity;
a036dc 9 import com.hx.phiappt.model.cardItem.CardEquity;
C 10 import com.hx.phiappt.model.cardItem.CardItem;
11 import com.hx.phiappt.model.cardItem.CardItemInfo;
12 import com.hx.phiappt.model.order.OrderItem;
13 import com.hx.phiappt.model.refund.RefundRecordConsumePay;
14 import com.hx.phiappt.model.refund.RefundRecordItem;
15 import com.hx.phiappt.model.refund.RefundRecordItemSource;
16 import com.hx.phiappt.model.user.UserCard;
17 import com.hx.phip.dao.mapper.*;
18 import com.hx.phip.tool.user.UserCardTool;
19 import com.hx.phip.vo.order.payment.PayMethodVo;
20 import com.hx.phip.vo.order.refund.DistributionRedundVo;
949373 21 import com.hx.phip.vo.order.refund.RefundCardVo;
a036dc 22 import com.hx.phip.vo.user.UserCardItemInfoVo;
C 23 import com.hx.util.StringUtils;
24 import com.hz.his.vo.order.refund.RefundUserCardItemVo;
25 import com.hz.his.vo.order.refund.RefundUserCardVo;
26 import com.platform.exception.PlatTipsException;
27 import com.platform.resultTool.PlatformCode;
949373 28 import org.springframework.beans.BeanUtils;
a036dc 29
C 30 import java.math.BigDecimal;
31 import java.math.RoundingMode;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 public class RefundTool {
37
38     /**获取已退款方式金额,根据支付方式编号求和返回
39      * 注意:groupByONumberNo和groupByConsumePayId不能同时使用
40      * @param itemId 订单子单标识
41      * @param refundRecordItemId 退款子单标识
42      * @param groupByONumberNo 根据支付方式编号求和
43      * @param groupByConsumePayId 根据支付方式记录标识求和
44      * @param commonService 映射
45      * @return 返回已退款方式金额集合
46      */
47     public static List<RefundRecordConsumePay> getRefundRecordConsumePay(String itemId, String refundRecordItemId, boolean groupByONumberNo
48             , boolean groupByConsumePayId, CommonService commonService){
49         SqlSentence sqlSentence = new SqlSentence();
50         Map<String,Object> values = new HashMap<>();
51         StringBuilder sql = new StringBuilder();
52
53         sql.append("SELECT");
54         if(groupByONumberNo){
55             sql.append(" a.numberNo,");
56         }
57         if(groupByConsumePayId){
58             sql.append(" a.consumePayId,");
59         }
60         sql.append("SUM(a.refundTotal) AS refundTotal FROM refund_record_consume_pay a");
61         sql.append(" JOIN refund_record rr ON rr.id = a.refundRecordId AND rr.isDel = 0 AND rr.refundStatus = #{m.refundStatus}");
62         sql.append("  WHERE a.isDel = 0");
63         if(StringUtils.noNull(itemId)){
64             sql.append("  AND a.commonId = #{m.commonId}");
65         }
66         if(StringUtils.noNull(refundRecordItemId)){
67             sql.append("  AND a.refundRecordItemId = #{m.refundRecordItemId}");
68         }
69         if(groupByONumberNo){
70             sql.append(" GROUP BY a.numberNo");
71         }
72         if(groupByConsumePayId){
73             sql.append(" GROUP BY a.consumePayId");
74         }
75         //获取已退款的支付方式
76         values.put("commonId",itemId);
77         values.put("refundStatus", RefundStatus.STATUS_SUCC_REFUND);
78         sqlSentence.sqlSentence(sql.toString(),values);
79         return commonService.selectList(RefundRecordConsumePayMapper.class,sqlSentence);
80     }
81
82     /**获取一级退款子单已退金额
949373 83      * 主要查询,单个数量分配的退款金额,因为目前是按照数量来分配金额退款的
a036dc 84      * @param orderItemId 一级退款子单关联标识
C 85      * @param goodsType 商品类型
86      * @param commonService 映射
87      * @return 已退金额
88      */
89     public static RefundRecordItem getOneAlreadyRefundTotal(String orderItemId,String goodsType, CommonService commonService ){
949373 90
C 91         //查找正常退款的
a036dc 92         SqlSentence sqlSentence = new SqlSentence();
C 93         Map<String,Object> values = new HashMap<>();
949373 94         StringBuilder sql = new StringBuilder();
C 95         //获取已退款的金额,剔除掉用户卡项的部分退
a036dc 96         values.put("orderItemId",orderItemId);
949373 97         sql.append("SELECT ROUND(SUM(rri.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rri.occupyRefundIntegral),2) AS occupyRefundIntegral");
C 98         sql.append(" FROM refund_record_item rri");
99         sql.append(" JOIN refund_record rr ON rr.id = rri.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
100         sql.append(" WHERE rri.isDel = 0 AND rri.orderItemId = #{m.orderItemId} AND rri.userCardId IS NULL");
101         sqlSentence.sqlSentence(sql.toString(),values);
a036dc 102         RefundRecordItem refundRecordItem = commonService.selectOne(RefundRecordItemMapper.class,sqlSentence);
949373 103         if(refundRecordItem == null){
C 104             //设置没有找到
105             refundRecordItem.setIsDel(BaseEntity.YES);
a036dc 106             refundRecordItem = new RefundRecordItem();
C 107         }
949373 108
C 109         //用户卡包退款,系统退款总金额,按照退款数量来算的,用户卡包部分退,要特殊查找
a036dc 110         if(OrderItemConstants.TYPE_CARD.equals(goodsType)){
C 111             //查找有没有这个卡包部分退款的记录
949373 112             List<RefundRecordItem> refundRecordItemList = findRefundUserCard(orderItemId,null,commonService);
C 113             if(refundRecordItemList.size() > 0){
114                 refundRecordItem.setIsDel(BaseEntity.NO);
115             }
116             for(RefundRecordItem refundRecordItem1:refundRecordItemList){
117                 refundRecordItem.setOccupyRefundTotal(refundRecordItem.getOccupyRefundTotal().add(refundRecordItem1.getOccupyRefundTotal()));
118                 refundRecordItem.setOccupyRefundIntegral(refundRecordItem.getOccupyRefundIntegral().add(refundRecordItem1.getOccupyRefundIntegral()));
a036dc 119             }
C 120         }
121
122         return refundRecordItem;
123     }
124
125     /**获取二级退款子单已退金额
949373 126      * @param orderItemSonId 二级退款子单关联标识/关联卡项条目的id
a036dc 127      * @param commonService 映射
C 128      * @return 已退金额
129      */
130     public static RefundRecordItemSource getTwoAlreadyRefundTotal(String orderItemSonId, CommonService commonService ){
131         SqlSentence sqlSentence = new SqlSentence();
132         Map<String,Object> values = new HashMap<>();
949373 133         StringBuilder sql = new StringBuilder();
a036dc 134
C 135         values.put("orderItemSonId",orderItemSonId);
949373 136         sql.append("SELECT ROUND(SUM(rris.occupyRefundTotal),2) AS occupyRefundTotal,ROUND(SUM(rris.occupyRefundIntegral),2) AS occupyRefundIntegral");
C 137         sql.append(" FROM refund_record_item_source rris");
138         sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
139         sql.append(" WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId}");
140         //获取已退款的金额
141         sqlSentence.sqlSentence(sql.toString(),values);
a036dc 142         RefundRecordItemSource refundRecordItemSource = commonService.selectOne(RefundRecordItemSourceMapper.class,sqlSentence);
C 143
949373 144         if(refundRecordItemSource == null){
C 145             //设置是没有找到
146             refundRecordItemSource.setIsDel(BaseEntity.YES);
a036dc 147             return new RefundRecordItemSource();
C 148         }else{
149             return refundRecordItemSource;
150         }
151     }
152
153     /**计算用户卡包条目退款金额*/
154     public static void userCardRefundCount(UserCard userCard,RefundUserCardVo refundUserCardVo, CommonService commonService, RefundMapper refundMapper){
949373 155
a036dc 156         ////算出金额:
949373 157         //1、算出该卡可退总金额
C 158         //2、算出卡项条目总金额,就是占订单支付金额的钱
159         //3、算出单条目的权益占用金额
160         //4、获取该条目已退款金额
161         //5、算出“单次抵扣金额”,再乘以数量得出“抵扣总金额”
162         //6、用“抵扣总金额”算出占用整个卡项总金额的“比例”
163         //7、比较金额,得出可退金额,注意最后一次要全部退掉
a036dc 164
C 165         //获取卡项
166         CardItem cardItem = commonService.selectOneByKey(CardItemMapper.class,userCard.getCardItemId());
167         if(cardItem == null){
168             throw new TipsException("获取卡项信息失败[210]!");
169         }
170
171         //获取子单
172         OrderItem orderItem = commonService.selectOneByKey(OrderItemMapper.class,userCard.getSourceId());
173         if(orderItem == null){
174             throw new TipsException("获取订单子单失败[210]!");
175         }
176
949373 177         //公用对象
C 178         RefundRecordItemSource itemSource;
a036dc 179
949373 180         //用户卡项分配可退总金额
C 181         RefundCardVo refundCard = new RefundCardVo();
a036dc 182
C 183         CardItemInfo cardItemInfo;
184         CardEquity cardEquity;
185         //条目金额计算
186         BigDecimal sumCardBagMoney;
949373 187         //条目积分计算
C 188         BigDecimal sumCardBagIntegral;
189
190         //获取卡项的权益数量
191         int cardEquityNum = UserCardTool.getCardEquityCount(userCard.getCardItemId(),commonService);
192
193         //存储权益分配的退款金额
194         Map<String,RefundCardVo> cardEquityMap = new HashMap<>();
195
a036dc 196         //比例
949373 197         RefundCardVo refundCardEquity;
C 198         RefundCardVo refundCardInfo;
a036dc 199         BigDecimal proportion;
C 200         for(RefundUserCardItemVo refundUserCardItemVo:refundUserCardVo.getRefundUserCardItemVoList()){
201             if(StringUtils.isEmpty(refundUserCardItemVo.getCardItemInfoId())){
202                 throw new TipsException("卡项条目标识必传[210]!");
203             }
204             if(refundUserCardItemVo.getRefundQuantity() == null || refundUserCardItemVo.getRefundQuantity() < 1){
205                 throw new TipsException("卡项条目退款次数必填[210]!");
206             }
207             cardItemInfo = commonService.selectOneByKey(CardItemInfoMapper.class,refundUserCardItemVo.getCardItemInfoId());
208             if(cardItemInfo == null){
209                 throw new TipsException("获取卡项条目信息失败[210]!");
210             }
211             cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
212             if(cardEquity == null){
213                 throw new TipsException("获取卡项权益信息失败[210]!");
214             }
949373 215             //-----校验
a036dc 216             //获取可用剩余使用次数
C 217             UserCardItemInfoVo uerCardItemInfoVo = UserCardTool.getSurplusNumber(userCard,cardItemInfo,cardEquity,commonService);
218             //获取当前需要抵扣的次数
219             Integer usedNum = UserCardTool.countUsedNumber(refundUserCardItemVo.getRefundQuantity(),cardItemInfo,cardEquity);
220             if(!uerCardItemInfoVo.isNumByNum()){
221                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项退款提示:不符合N选M规则,请重试");
222             }
223             if(uerCardItemInfoVo.getSurplusNum() <= 0 ||usedNum > uerCardItemInfoVo.getSurplusNum()){
224                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡项退款提示:卡项的【"+cardItemInfo.getCommonName()+"】次数不够");
225             }
226
949373 227             refundCardInfo = new RefundCardVo();
C 228
229             //-----算出权益分配到金额
230             //查看权益是否已经分配好了的
231             refundCardEquity = getCardEquityData(orderItem,userCard,cardItem,cardEquity,refundCard,cardEquityNum,cardEquityMap,commonService,refundMapper);
232             //判断是不是最后一个条目
233             if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
234                 //次卡,权益有总次数,所有条目使用的总次数不能大于权益的总次数
235                 //判断数量
236                 if(uerCardItemInfoVo.getSurplusNum() < refundUserCardItemVo.getRefundQuantity()){
237                     //最后的次数
238                     refundCardInfo.setOccupyRefundTotal(refundCardEquity.getSurplusTotal());
239                     refundCardInfo.setOccupyRefundIntegral(refundCardEquity.getSurplusIntegral());
240                 }else{
241
242                 }
243                 refundCardInfo.setSurplusTotal(refundCardEquity.getSurplusTotal());
244                 refundCardInfo.setSurplusIntegral(refundCardEquity.getSurplusIntegral());
245             }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
246                 //判断条目数量,再判断数量
247
248
249             }else{
250                 //固定或者其他
251                 userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
252             }
253
254             //-----算出单次划扣金额
a036dc 255             //获取单次价格金额
C 256             sumCardBagMoney = UserCardTool.countOneMoney(cardItemInfo);
257             //计算抵扣总的分摊明细
258             sumCardBagMoney = sumCardBagMoney.multiply(BigDecimal.valueOf(refundUserCardItemVo.getRefundQuantity()).setScale(2,RoundingMode.DOWN));
259             //算比例
260             if(cardItem.getTotal() != null && cardItem.getTotal().compareTo(BigDecimal.ZERO) > 0){
261                 proportion = sumCardBagMoney.divide(cardItem.getTotal(),20,RoundingMode.HALF_UP);
262             }else{
263                 proportion = BigDecimal.ZERO;
264             }
265
949373 266             //算出占用购买卡项订单时候金额(分摊模式)
C 267             sumCardBagMoney = occupyRefundTotal.multiply(proportion);
268             sumCardBagIntegral = occupyRefundIntegral.multiply(proportion);
a036dc 269
949373 270             //获取卡项条目已退款金额
C 271             refundRecordItemSource = getTwoAlreadyRefundTotal(cardItemInfo.getId(),null,null,commonService);
272
273             //
274
275             //不能超过卡项条目的分摊总金额
a036dc 276
C 277         }
278
279     }
280
949373 281     /**查找部分退款的用户卡包信息,根据用户卡包标识去重了
C 282      * @param orderItemId 子单标识
283      * @param userCardId 用户卡包标识
284      * @param commonService 映射
285      */
286     public static List<RefundRecordItem> findRefundUserCard(String orderItemId,String userCardId,CommonService commonService){
287         SqlSentence sqlSentence = new SqlSentence();
288         Map<String,Object> values = new HashMap<>();
289
290         StringBuilder sql = new StringBuilder();
291
292         //--------获取用户卡包的总金额
293         //查找有没有这个卡包部分退款的记录
294         sql.append("SELECT rri.occupyRefundTotal,rri.occupyRefundIntegral,rri.userCardId FROM refund_record_item rri");
295         sql.append(" JOIN refund_record rr ON rr.id = rri.refundRecordId");
296         sql.append(" JOIN user_card uc ON uc.id = rri.userCardId");
297         sql.append(" AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
298         sql.append(" WHERE rri.isDel = 0");
299         if(StringUtils.noNull(orderItemId)){
300             values.put("orderItemId",orderItemId);
301             sql.append(" AND rri.orderItemId = #{m.orderItemId}");
302         }
303         if(StringUtils.noNull(userCardId)){
304             values.put("userCardId",userCardId);
305             sql.append(" AND rri.userCardId = #{m.userCardId}");
306         }else{
307             sql.append(" AND rri.userCardId IS NOT NULL");
308         }
309         sql.append(" GROUP BY rri.userCardId");
310
311         sqlSentence.sqlSentence(sql.toString(),values);
312         return commonService.selectList(UserCardMapper.class,sqlSentence);
313     }
314
315     /**获取卡包已分配的金额(权益分配的金额)
316      * @param userCardId 用户卡项标识,主要获取用户卡项分配的金额
317      * @param commonService 映射
318      * @return 已退金额
319      */
320     public static RefundRecordItemSource getCardRealRefund(String userCardId, CommonService commonService ){
321         SqlSentence sqlSentence = new SqlSentence();
322         Map<String,Object> values = new HashMap<>();
323         StringBuilder sql = new StringBuilder();
324
325         values.put("userCardId",userCardId);
326         sql.append("SELECT rris.occupyEqRefundTotal,rris.occupyEqRefundIntegral");
327         sql.append(" FROM refund_record_item_source rris");
328         sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
329         sql.append(" WHERE rris.isDel = 0 AND rris.userCardId = #{m.userCardId} GROUP BY rris.cardEquityId");
330
331         List<RefundRecordItemSource> refundRecordItemSourceList = commonService.selectList(RefundRecordItemSourceMapper.class,sqlSentence);
332
333         RefundRecordItemSource source = new RefundRecordItemSource();
334         if(refundRecordItemSourceList.size()==0){
335             source.setIsDel(BaseEntity.YES);
336             return source;
337         }
338         for(RefundRecordItemSource refundRecordItemSource:refundRecordItemSourceList){
339             //计算有多少个权益参与了退款
340             refundRecordItemSource.setRealRefundNum(refundRecordItemSource.getRealRefundNum()+1);
341             source.setOccupyRefundTotal(source.getOccupyRefundTotal().add(refundRecordItemSource.getOccupyEqRefundTotal()));
342             source.setOccupyRefundIntegral(source.getOccupyRefundIntegral().add(refundRecordItemSource.getOccupyEqRefundIntegral()));
343         }
344         return source;
345     }
346
347     /**获取卡包权益已退款金额(条目分配的金额)
348      * @param cardEquityId 卡项权益标识,主要获取卡项权益分配的金额
349      * @param commonService 映射
350      * @return 已退金额
351      */
352     public static RefundRecordItemSource getCardEquityRealRefund(String cardEquityId, CommonService commonService ){
353         SqlSentence sqlSentence = new SqlSentence();
354         Map<String,Object> values = new HashMap<>();
355         StringBuilder sql = new StringBuilder();
356
357         values.put("cardEquityId",cardEquityId);
358         sql.append("SELECT rris.occupyItemRefundTotal,rris.occupyItemRefundIntegral,rris.occupyEqRefundTotal,rris.occupyEqRefundIntegral");
359         sql.append(" FROM refund_record_item_source rris");
360         sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
361         sql.append(" WHERE rris.isDel = 0 AND rris.cardEquityId = #{m.cardEquityId} GROUP BY orderItemSonId");
362
363         List<RefundRecordItemSource> refundRecordItemSourceList = commonService.selectList(RefundRecordItemSourceMapper.class,sqlSentence);
364
365         RefundRecordItemSource source = new RefundRecordItemSource();
366         if(refundRecordItemSourceList.size()==0){
367             source.setIsDel(BaseEntity.YES);
368             return source;
369         }
370         for(RefundRecordItemSource refundRecordItemSource:refundRecordItemSourceList){
371             //计算有多少个条目参与了退款
372             source.setRealRefundNum(refundRecordItemSource.getRealRefundNum()+1);
373             source.setRefundMoney(source.getRefundMoney().add(refundRecordItemSource.getOccupyItemRefundTotal()));
374             source.setRefundIntegral(source.getRefundIntegral().add(refundRecordItemSource.getOccupyItemRefundIntegral()));
375             source.setOccupyEqRefundTotal(refundRecordItemSource.getOccupyEqRefundTotal());
376             source.setOccupyEqRefundIntegral(refundRecordItemSource.getOccupyEqRefundIntegral());
377         }
378         return source;
379     }
380
381     /**获取卡包条目已退款金额(条目分配的金额)
382      * @param cardInfoId 卡项条目标识,主要获取卡项条目分配的金额
383      * @param commonService 映射
384      * @return 已退金额
385      */
386     public static RefundCardVo getCardInfoData(String cardInfoId,CommonService commonService){
387
388         SqlSentence sqlSentence = new SqlSentence();
389         Map<String,Object> values = new HashMap<>();
390         StringBuilder sql = new StringBuilder();
391
392         values.put("orderItemSonId",cardInfoId);
393         sql.append("SELECT SUM(rris.occupyRefundTotal) AS occupyRefundTotal,SUM(rris.occupyRefundIntegral) AS occupyRefundIntegral");
394         sql.append(",SUM(rris.realRefundNum) AS realRefundNum,rris.occupyItemRefundTotal,rris.occupyItemRefundIntegral");
395         sql.append(" FROM refund_record_item_source rris");
396         sql.append(" JOIN refund_record rr ON rr.id = rris.refundRecordId AND rr.isDel = 0 AND rr.refundStatus IN(0,2)");
397         sql.append(" WHERE rris.isDel = 0 AND rris.orderItemSonId = #{m.orderItemSonId} GROUP BY orderItemSonId");
398
399         RefundRecordItemSource itemSource = commonService.selectOne(RefundRecordItemSourceMapper.class,sqlSentence);
400
401         RefundCardVo refundCardInfo = new RefundCardVo();
402
403         if(itemSource != null){
404             refundCardInfo.setJoinRefund(true);
405             refundCardInfo.setRefundNum(itemSource.getRealRefundNum());
406             refundCardInfo.setOccupyRefundTotal(itemSource.getOccupyItemRefundTotal());
407             refundCardInfo.setOccupyRefundIntegral(itemSource.getOccupyItemRefundIntegral());
408             refundCardInfo.setSurplusTotal(itemSource.getOccupyItemRefundTotal().subtract(itemSource.getOccupyRefundTotal()));
409             refundCardInfo.setSurplusIntegral(itemSource.getOccupyItemRefundIntegral().subtract(itemSource.getOccupyRefundIntegral()));
410
411         }
412         return refundCardInfo;
413
414
415     }
416
417
418     /**获取用户卡项的参数,分配金额和剩余可退金额*/
419     public static RefundCardVo getUserCardData(OrderItem orderItem,UserCard userCard,CommonService commonService,RefundMapper refundMapper){
420
421         RefundCardVo refundCard = new RefundCardVo();
422         //--------获取用户卡包分配的的可退款总金额(包含已经部分退的金额在里面)
423         //查找有没有这个卡包部分退款的记录
424         List<RefundRecordItem> refundRecordItemList = findRefundUserCard(orderItem.getId(),userCard.getId(),commonService);
425         if(refundRecordItemList.size() == 0){
426             //用户卡包没有分配总金额,那么就按照正常的流程获取可退款金额
427             DistributionRedundVo distributionRedundVo = UserCardTool.userCardPay(userCard,orderItem,commonService,refundMapper);
428             refundCard.setOccupyRefundTotal(distributionRedundVo.getRefundTotal());
429             refundCard.setOccupyRefundIntegral(distributionRedundVo.getRefundIntegral());
430         }else{
431             refundCard.setJoinRefund(true);
432             RefundRecordItem refundRecordItem = refundRecordItemList.get(0);
433             refundCard.setOccupyRefundTotal(refundRecordItem.getOccupyRefundTotal());
434             refundCard.setOccupyRefundIntegral(refundRecordItem.getOccupyRefundIntegral());
435         }
436
437         //-----计算剩余可分配给权益退款的金额
438         //查找该卡包已部分退款的金额
439         RefundRecordItemSource itemSource = getCardRealRefund(userCard.getId(),commonService);
440         //计算该用户卡包剩余可退金额和积分,就是可分配给权益退款的金额
441         refundCard.setSurplusTotal(refundCard.getOccupyRefundTotal().subtract(itemSource.getOccupyRefundTotal()));
442         refundCard.setSurplusIntegral(refundCard.getOccupyRefundIntegral().subtract(itemSource.getOccupyRefundIntegral()));
443         refundCard.setRefundNum(itemSource.getRealRefundNum());
444
445         return refundCard;
446     }
447
448     /**获取用户卡项权益的参数,分配金额和剩余可退金额*/
449     public static RefundCardVo getCardEquityData(OrderItem orderItem,UserCard userCard,CardItem cardItem,CardEquity cardEquity,RefundCardVo refundCard,int cardEquityNum
450             ,Map<String,RefundCardVo> cardEquityMap,CommonService commonService,RefundMapper refundMapper){
451
452         //-----算出权益分配到金额
453         //查看权益是否已经分配好了的
454         BigDecimal proportion;
455         RefundCardVo refundCardEquity = cardEquityMap.get(cardEquity.getId());
456         if(refundCardEquity == null){
457             refundCardEquity = new RefundCardVo();
458
459             //获取卡项权益已退款金额和分配的金额
460             RefundRecordItemSource cardEquityRealRefund = getCardEquityRealRefund(cardEquity.getId(),commonService);
461             if(cardEquityRealRefund.getIsDel().equals(BaseEntity.YES)){
462                 //没有找到已经分配好的,进行新的分配
463                 //获取用户卡包分配金额
464                 if(!refundCard.isJoinRefund()){
465                     BeanUtils.copyProperties(getUserCardData(orderItem,userCard,commonService,refundMapper),refundCard);
466                     refundCard.setJoinRefund(true);
467                 }
468
469                 if(cardEquityNum>=refundCard.getRefundNum()+1){
470                     //是最后一个
471                     refundCardEquity.setOccupyRefundTotal(refundCard.getSurplusTotal());
472                     refundCardEquity.setOccupyRefundIntegral(refundCard.getSurplusIntegral());
473                 }else{
474                     //不是最后一个,通过比例来算金额
475                     if(cardItem.getTotal().compareTo(BigDecimal.ZERO) > 0){
476                         proportion = cardEquity.getShareMoney().divide(cardItem.getTotal(),20,RoundingMode.HALF_UP);
477                     }else{
478                         proportion = BigDecimal.ZERO;
479                     }
480                     refundCardEquity.setOccupyRefundTotal(refundCard.getOccupyRefundTotal().multiply(proportion).setScale(2,RoundingMode.HALF_UP));
481                     refundCardEquity.setOccupyRefundIntegral(refundCard.getOccupyRefundIntegral().multiply(proportion).setScale(2,RoundingMode.HALF_UP));
482
483                     //判断不能大于权益设置的金额
484                     if(refundCardEquity.getOccupyRefundTotal().compareTo(cardEquity.getShareMoney()) > 0){
485                         refundCardEquity.setOccupyRefundTotal(cardEquity.getShareMoney());
486                     }
487                     //判断不能大于剩下的可退金额
488                     if(refundCardEquity.getOccupyRefundTotal().compareTo(refundCard.getSurplusTotal()) > 0){
489                         refundCardEquity.setOccupyRefundTotal(refundCard.getSurplusTotal());
490                     }
491                     //判断不能大于剩下的可退积分
492                     if(refundCardEquity.getOccupyRefundIntegral().compareTo(refundCard.getSurplusIntegral()) > 0){
493                         refundCardEquity.setOccupyRefundIntegral(refundCard.getSurplusIntegral());
494                     }
495                 }
496                 refundCardEquity.setSurplusTotal(refundCardEquity.getOccupyRefundTotal());
497                 refundCardEquity.setSurplusIntegral(refundCardEquity.getOccupyRefundIntegral());
498             }else{
499                 refundCardEquity.setJoinRefund(true);
500                 refundCardEquity.setRefundNum(cardEquityRealRefund.getRealRefundNum());
501                 refundCardEquity.setOccupyRefundTotal(cardEquityRealRefund.getOccupyEqRefundTotal());
502                 refundCardEquity.setOccupyRefundIntegral(cardEquityRealRefund.getOccupyEqRefundIntegral());
503                 refundCardEquity.setSurplusTotal(cardEquityRealRefund.getOccupyEqRefundTotal().subtract(cardEquityRealRefund.getRefundMoney()));
504                 refundCardEquity.setSurplusIntegral(cardEquityRealRefund.getOccupyEqRefundIntegral().subtract(cardEquityRealRefund.getRefundIntegral()));
505             }
506             cardEquityMap.put(cardEquity.getId(),refundCardEquity);
507         }
508         return refundCardEquity;
509     }
510
511
512 }