fwq
2024-10-10 86a7a078c89a671f9ada908e8f82ebd43d463b18
提交 | 用户 | age
a88f94 1 package com.hx.phip.tool.user;
C 2
a89d49 3 import com.alibaba.fastjson.JSON;
a88f94 4 import com.hx.common.service.CommonService;
78d1e8 5 import com.hx.exception.TipsException;
a88f94 6 import com.hx.mybatisTool.SqlSentence;
b5d7a5 7 import com.hx.phiappt.common.UserProjectConstants;
b33cd3 8 import com.hx.phiappt.dao.mapper.*;
e542d7 9 import com.hx.phiappt.model.BaseEntity;
275465 10 import com.hx.phiappt.model.User;
a88f94 11 import com.hx.phiappt.model.cardItem.CardEquity;
ef80f6 12 import com.hx.phiappt.model.cardItem.CardItem;
a88f94 13 import com.hx.phiappt.model.cardItem.CardItemInfo;
33b85f 14 import com.hx.phiappt.model.consume.ConsumePayCard;
78d1e8 15 import com.hx.phiappt.model.order.OrderItem;
a036dc 16 import com.hx.phiappt.model.refund.RefundRecordItem;
a88f94 17 import com.hx.phiappt.model.user.UserCard;
e542d7 18 import com.hx.phiappt.model.user.UserCardPay;
a88f94 19 import com.hx.phiappt.model.user.UserCardUsed;
ea4351 20 import com.hx.phip.tool.refund.CardRefundTool;
a036dc 21 import com.hx.phip.tool.refund.PaymentCountTool;
C 22 import com.hx.phip.tool.refund.RefundTool;
b33cd3 23 import com.hx.phiappt.vo.payMethod.PayMethodVo;
a036dc 24 import com.hx.phip.vo.order.refund.DistributionRedundVo;
c72884 25 import com.hx.phip.vo.order.refund.RefundMakeWayVo;
a88f94 26 import com.hx.phip.vo.user.UserCardItemInfoVo;
ef80f6 27 import com.hz.his.vo.user.card.UserCardEquityVo;
C 28 import com.hz.his.vo.user.card.UserCardItemVo;
29 import com.hz.his.vo.user.card.UserCardVo;
968f82 30 import com.hx.util.StringUtils;
a89d49 31 import org.slf4j.Logger;
F 32 import org.slf4j.LoggerFactory;
a88f94 33
78d1e8 34 import java.math.BigDecimal;
C 35 import java.math.RoundingMode;
a88f94 36 import java.util.*;
e542d7 37 import java.util.stream.Collectors;
a88f94 38
C 39 /**
40  *用户卡包处理工具
41  * @author CJH
42  */
43 public class UserCardTool {
a89d49 44
F 45     private static final Logger logger = LoggerFactory.getLogger(UserCardTool.class);
a88f94 46
C 47     /**计算使用次数
48      * @param buyNumber 购买数量
49      * @param cardItemInfo 卡项条目
50      * @param cardEquity 卡项权益
51      * @return 计算得出抵扣次数
52      */
53     public static int countUsedNumber(int buyNumber, CardItemInfo cardItemInfo,CardEquity cardEquity){
932a23 54         if(cardItemInfo == null){
C 55             throw new TipsException("卡项条目获取信息失败");
56         }
57         if(cardEquity == null){
4f41a7 58             throw new TipsException("卡项权益获取信息失败[024]");
932a23 59         }
a88f94 60         int userdNum ;
C 61         if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
62             //次卡,权益有总次数,所有条目使用的总次数不能大于权益的总次数
63             userdNum = buyNumber*cardItemInfo.getEveryDrawNum();
64         }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
65             //N选M
66             userdNum = buyNumber;
67         }else{
68             //固定或者其他
69             userdNum = buyNumber;
70         }
71         return userdNum;
72     }
73
ef80f6 74     /**获取用户项目可抵扣数据*/
C 75     public static UserCardVo getUserCardSurplus(UserCard userCard, CommonService commonService){
76
77         //获取卡项
78         CardItem cardItem = commonService.selectOneByKeyBlob(CardItemMapper.class,userCard.getCardItemId());
79         if(cardItem == null){
80             throw new TipsException("获取卡项详情失败!");
81         }
82
83
84         SqlSentence sqlSentence = new SqlSentence();
85         Map<String,Object> values = new HashMap<>();
86
87         //获取权益,不包含账户权益
88         values.put("cardItemId",userCard.getCardItemId());
89         sqlSentence.sqlSentence("SELECT * FROM card_equity WHERE isDel = 0 AND equityType != 4 AND cardItemId = #{m.cardItemId}",values);
90         List<CardEquity> cardEquityList = commonService.selectList(CardEquityMapper.class,sqlSentence);
91
92         //获取所有的卡项条目
93         values.clear();
94         values.put("cardItemId",userCard.getCardItemId());
95         sqlSentence.sqlSentence("SELECT * FROM card_item_info WHERE isDel = 0 AND cardItemId = #{m.cardItemId}",values);
96         List<CardItemInfo> cardItemInfoList = commonService.selectList(CardItemInfoMapper.class,sqlSentence);
97
98         //卡项条目根据权益分组
99         Map<String,List<CardItemInfo>> cardEquityByItemMap = new HashMap<>();
100         List<CardItemInfo> cardItemInfos;
101         for(CardItemInfo cardItemInfo:cardItemInfoList){
102             cardItemInfos = cardEquityByItemMap.computeIfAbsent(cardItemInfo.getCardEquityId(),k->new ArrayList<>());
103             cardItemInfos.add(cardItemInfo);
104         }
105
106         //用户卡项信息总结构
107         UserCardVo userCardVo = new UserCardVo();
108         userCardVo.setUserCardId(userCard.getId());
109         userCardVo.setCardId(cardItem.getId());
110         userCardVo.setCardName(cardItem.getName());
111         userCardVo.setTotal(cardItem.getTotal());
275465 112         userCardVo.setEffectiveStatus(userCard.getEffectiveStatus());
C 113
114         User user = commonService.selectOneByKey(UserMapper.class,userCard.getUserId());
115         if(user != null){
3ad1aa 116             userCardVo.setUserId(user.getId());
275465 117             userCardVo.setUserName(user.getName());
C 118             userCardVo.setUserCiq(user.getCIQ());
3ad1aa 119             userCardVo.setUserShopId(user.getShopId());
275465 120         }
ef80f6 121
C 122         //卡项权益信息结构
123         List<UserCardEquityVo> userCardEquityVoList = new ArrayList<>();
124         UserCardEquityVo userCardEquityVo;
125
126         //卡项条目信息结构
127         List<UserCardItemVo>  userCardItemVoList;
128         UserCardItemVo userCardItemVo;
129
130         UserCardItemInfoVo userCardItemInfoVo;
131         for(CardEquity cardEquity:cardEquityList){
132             userCardEquityVo = new UserCardEquityVo();
133             userCardEquityVo.setEquityId(cardEquity.getId());
134             userCardEquityVo.setEquityName(cardEquity.getGroupName());
135             userCardEquityVo.setEquityType(cardEquity.getEquityType());
136             userCardEquityVo.setTotal(cardEquity.getShareMoney());
137             userCardEquityVo.setConditionValue(cardEquity.getValue());
138
139             //权益条目处理
140             cardItemInfos = cardEquityByItemMap.get(cardEquity.getId());
141             userCardItemVoList = new ArrayList<>();
142             if(cardItemInfos != null){
143                 for(CardItemInfo cardItemInfo:cardItemInfos){
144                     userCardItemVo = new UserCardItemVo();
145                     userCardItemVo.setItemId(cardItemInfo.getId());
146                     userCardItemVo.setItemName(cardItemInfo.getCommonName());
d53fef 147                     userCardItemVo.setItemType(cardItemInfo.getCommonType());
F 148                     userCardItemVo.setItemCode(cardItemInfo.getCommonCode());
ef80f6 149                     userCardItemVo.setTotal(cardItemInfo.getShareMoney());
C 150                     userCardItemVo.setSingleAmount(cardItemInfo.getEveryShareMoney());
151                     userCardItemVo.setDeductionNum(cardItemInfo.getMaxNum());
152                     userCardItemVo.setDeductionSingle(cardItemInfo.getEveryDrawNum());
153
154                     //获取可抵扣信息
155                     userCardItemInfoVo = getSurplusNumber(userCard,cardItemInfo,cardEquity,commonService);
156                     userCardItemVo.setSurplusNum(userCardItemInfoVo.getSurplusNum());
fb5e2e 157                     userCardItemVo.setUsedNum(userCardItemInfoVo.getUsedNum());
C 158                     userCardItemVo.setUsedNumNoRefund(userCardItemInfoVo.getUsedNumNoRefund());
159                     userCardItemVo.setUsedNumRefund(userCardItemInfoVo.getUsedNumRefund());
ef80f6 160
C 161                     userCardItemVoList.add(userCardItemVo);
162                 }
163             }
164             userCardEquityVo.setUserCardItemVoList(userCardItemVoList);
165             userCardEquityVoList.add(userCardEquityVo);
166         }
167
168         //权益集合存储
169         userCardVo.setUserCardEquityVoList(userCardEquityVoList);
170         return userCardVo;
171     }
172
173
a88f94 174
C 175     /**获取卡包-卡项条目剩余次数
176      * @param userCard 用户卡包
177      * @param cardItemInfo 卡项条目
178      * @param cardEquity 卡项权益
179      * @param commonService 映射
180      * @return 数据结构
181      */
182     public static UserCardItemInfoVo getSurplusNumber(UserCard userCard, CardItemInfo cardItemInfo,CardEquity cardEquity, CommonService commonService){
932a23 183         return getSurplusNumber(userCard.getId(),cardItemInfo,cardEquity,commonService);
C 184     }
185
186     /**获取卡包-卡项条目剩余次数
187      * @param userCardId 用户卡包标识
188      * @param cardItemInfo 卡项条目
189      * @param cardEquity 卡项权益,可空,空值从cardItemInfo获取查询
190      * @param commonService 映射
191      * @return 数据结构
192      */
193     public static UserCardItemInfoVo getSurplusNumber(String userCardId, CardItemInfo cardItemInfo,CardEquity cardEquity, CommonService commonService){
194         if(StringUtils.isNull(userCardId)){
195             throw new TipsException("卡包标识错误!");
196         }
197         if(cardItemInfo == null){
198             throw new TipsException("卡项条目获取信息失败");
199         }
200         if(cardEquity == null){
201             cardEquity = commonService.selectOneByKey(CardEquityMapper.class,cardItemInfo.getCardEquityId());
202         }
203         if(cardEquity == null){
4f41a7 204             throw new TipsException("卡项权益获取信息失败[023]");
932a23 205         }
a88f94 206
C 207         //获取使用记录,注意:是根据权益,不是卡项条目查询
932a23 208         List<UserCardUsed> userCardUsedList = getUsedRecord(userCardId,cardEquity.getId(),null,commonService);
a88f94 209
C 210         //已使用的条目标识集合
211         Set<String> cardItemInfoIds = new HashSet<>();
d0b78f 212         //每个条目使用总次数Map(包括退款)
a88f94 213         Map<String,Integer> cardItemInfoUseMap = new HashMap<>();
d0b78f 214         //每个条目使用总次数Map(不包括退款)
C 215         Map<String,Integer> cardItemInfoUseNoRefundMap = new HashMap<>();
1e3f9e 216         //每个条目退款总次数Map
C 217         Map<String,Integer> cardItemInfoUseRefundMap = new HashMap<>();
218         //计算所有条目(整个权益)已使用总次数
219         Integer usedTotal = 0;
220         //计算所有条目(整个权益)已使用总次数,不包括退款
221         Integer usedTotalNoRefund = 0;
222         //计算所有条目(整个权益)已退款总次数
223         Integer usedTotalRefund = 0;
8eea74 224         //权益已经使用条目数量
C 225         Set<String> selectInfoIds = new HashSet<>();
226
a88f94 227         Integer num;
C 228         for(UserCardUsed userCardUsed:userCardUsedList){
229             //计算所有条目已使用总次数
1e3f9e 230             usedTotal = usedTotal+userCardUsed.getUsedNum();
a88f94 231             //已使用的条目标识集合
C 232             cardItemInfoIds.add(userCardUsed.getCardItemInfoId());
d0b78f 233             //每个条目使用总参数Map(包括退款)
a88f94 234             num = cardItemInfoUseMap.computeIfAbsent(userCardUsed.getCardItemInfoId(),k->0);
C 235             num = num + userCardUsed.getUsedNum();
236             cardItemInfoUseMap.put(userCardUsed.getCardItemInfoId(),num);
d0b78f 237
1e3f9e 238             if(UserCardUsed.SOURCE_TYPE_USER_CARD_REFUND == userCardUsed.getSourceType()){
C 239                 //退款
240                 usedTotalRefund = usedTotalRefund + userCardUsed.getUsedNum();
241                 ////每个条目退款总参数Map
242                 num = cardItemInfoUseRefundMap.computeIfAbsent(userCardUsed.getCardItemInfoId(),k->0);
243                 num = num + userCardUsed.getUsedNum();
244                 cardItemInfoUseRefundMap.put(userCardUsed.getCardItemInfoId(),num);
245             }else{
246                 //使用
247                 usedTotalNoRefund = usedTotalNoRefund+userCardUsed.getUsedNum();
d0b78f 248                 ////每个条目使用总参数Map(不包括退款)
C 249                 num = cardItemInfoUseNoRefundMap.computeIfAbsent(userCardUsed.getCardItemInfoId(),k->0);
250                 num = num + userCardUsed.getUsedNum();
251                 cardItemInfoUseNoRefundMap.put(userCardUsed.getCardItemInfoId(),num);
252             }
8eea74 253
C 254             selectInfoIds.add(userCardUsed.getCardItemInfoId());
a88f94 255         }
C 256
257         //返回结构
258         UserCardItemInfoVo userCardItemInfoVo = new UserCardItemInfoVo();
1e3f9e 259         userCardItemInfoVo.setUsedTotal(usedTotal);
C 260         userCardItemInfoVo.setUsedTotalNoRefund(usedTotalNoRefund);
261         userCardItemInfoVo.setUsedTotalRefund(usedTotalRefund);
a88f94 262
C 263         if(CardEquity.EQUITY_TYPE_TIME_CARD.equals(cardEquity.getEquityType())){
264             //次卡,权益有总次数,所有条目使用的总次数不能大于权益的总次数
1e3f9e 265             if(usedTotal < cardEquity.getValue()){
a88f94 266                 //是否设置了最大的抵扣次数
1e3f9e 267                 int surplusNum = cardEquity.getValue() - usedTotal;
a88f94 268                 if(cardItemInfo.getMaxNum() != null && cardItemInfo.getMaxNum() != 0){
C 269                     userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
36e260 270                     if(userCardItemInfoVo.getSurplusNum() > surplusNum){
C 271                         userCardItemInfoVo.setSurplusNum(surplusNum);
272                     }
a88f94 273                 }else{
36e260 274                     userCardItemInfoVo.setSurplusNum(surplusNum);
a88f94 275                 }
C 276             }else{
277                 userCardItemInfoVo.setSurplusNum(0);
278             }
279         }else if(CardEquity.EQUITY_TYPE_N_M.equals(cardEquity.getEquityType())){
280             //N选M,每个条目都有自己的可抵扣总次数
281             if(cardItemInfoIds.contains(cardItemInfo.getId())){
282                 //已经使用条目集合包含当前条目
283                 userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.get(cardItemInfo.getId()));
284             }else{
285                 //已经使用条目集合没有包含当前条目
286                 if(cardItemInfoIds.size() >= cardEquity.getValue()){
287                     //已经选择满条目数量了
288                     userCardItemInfoVo.setSurplusNum(0);
289                     //不符合
290                     userCardItemInfoVo.setNumByNum(false);
291                 }else{
292                     //没有选择满条目数量
293                     userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum());
294                 }
295             }
296         }else{
297             //固定或者其他
298             userCardItemInfoVo.setSurplusNum(cardItemInfo.getMaxNum() - cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
299         }
d0b78f 300         //使用次数(包括退款)
a88f94 301         userCardItemInfoVo.setUsedNum(cardItemInfoUseMap.computeIfAbsent(cardItemInfo.getId(),k->0));
d0b78f 302         //使用次数(不包括退款)
C 303         userCardItemInfoVo.setUsedNumNoRefund(cardItemInfoUseNoRefundMap.computeIfAbsent(cardItemInfo.getId(),k->0));
1e3f9e 304         //退次数
C 305         userCardItemInfoVo.setUsedNumRefund(cardItemInfoUseRefundMap.computeIfAbsent(cardItemInfo.getId(),k->0));
38a476 306         //当前权益,多少条目已经使用
8eea74 307         userCardItemInfoVo.setSelectInfoIds(selectInfoIds);
a88f94 308
C 309         return userCardItemInfoVo;
ea4351 310     }
C 311
932a23 312
ea4351 313     /**获取使用记录*/
C 314     public static List<UserCardUsed> getUsedRecord(String userCardId,String cardEquityId,String cardItemInfoId,CommonService commonService){
315         SqlSentence sqlSentence = new SqlSentence();
316         Map<String,Object> values = new HashMap<>();
317         StringBuilder sql = new StringBuilder();
318
319         sql.append("SELECT * FROM user_card_used WHERE isDel = #{m.isDel}");
320         values.put("isDel",UserCardUsed.NO);
321         if(StringUtils.noNull(userCardId)){
322             values.put("userCardId",userCardId);
323             sql.append(" AND userCardId = #{m.userCardId}");
324         }
325         if(StringUtils.noNull(cardEquityId)){
326             values.put("cardEquityId",cardEquityId);
327             sql.append(" AND cardEquityId = #{m.cardEquityId}");
328         }
329         if(StringUtils.noNull(cardItemInfoId)){
330             values.put("cardItemInfoId",cardItemInfoId);
331             sql.append(" AND cardItemInfoId = #{m.cardItemInfoId}");
332         }
333
334         //获取使用记录
335         sqlSentence.sqlSentence(sql.toString(),values);
336         //根据cardItemIfo标识查询用户使用记录
337         return commonService.selectList(UserCardUsedMapper.class, sqlSentence);
a88f94 338     }
C 339
340     /**卡包- 创建用户卡包使用记录
341      * @param usedNum 当前需要抵扣的次数
342      * @param orderId 订单标识
343      * @param sourceType 来源类型
344      * @param sourceId 来源标识
345      * @param userCard 用户卡包
346      * @param cardItemInfo 卡项条目
347      * @param cardEquity 卡项权益
348      * @param commonService 映射
349      * @return 用户卡包使用记录
350      */
351     public static UserCardUsed insertUserCardUsed(Integer usedNum,String orderId,Integer sourceType,String sourceId, UserCard userCard
352             , CardItemInfo cardItemInfo, CardEquity cardEquity,CommonService commonService) {
353         UserCardUsed userCardUsed=new UserCardUsed();
354
355         userCardUsed.setEquityType(cardEquity.getEquityType());
356         userCardUsed.setGroupName(cardEquity.getGroupName());
357         userCardUsed.setCommonId(cardItemInfo.getCommonId());
358         userCardUsed.setCommonName(cardItemInfo.getCommonName());
359         userCardUsed.setCommonType(cardItemInfo.getCommonType());
360         userCardUsed.setPrice(cardItemInfo.getPrice());
361         userCardUsed.setUsedGeneral(usedNum);
362         userCardUsed.setUsedNum(usedNum);
363         userCardUsed.setCancelNum(0);
364         userCardUsed.setEmResultsNum(cardItemInfo.getEmResultsNum());
365         userCardUsed.setShareMoney(cardItemInfo.getShareMoney());
366         userCardUsed.setEveryDrawNum(cardItemInfo.getEveryDrawNum());
367         userCardUsed.setEveryShareMoney(cardItemInfo.getEveryShareMoney());
368         userCardUsed.setUserCardCode(userCard.getCode());
369         userCardUsed.setUserCardId(userCard.getId());
370         userCardUsed.setSourceType(sourceType);
371         userCardUsed.setSourceId(sourceId);
372         userCardUsed.setOrderId(orderId);
373         userCardUsed.setCardEquityId(cardEquity.getId());
374         userCardUsed.setCardItemInfoId(cardItemInfo.getId());
375         commonService.insert(UserCardUsedMapper.class,userCardUsed);
376         return userCardUsed;
377     }
378
78d1e8 379     /**计算用户卡包购买时候的金额分摊方式-订单*/
a036dc 380     public static DistributionRedundVo userCardPay(UserCard userCard,OrderItem orderItem, CommonService commonService, RefundMapper refundMapper){
C 381         DistributionRedundVo distributionRedundVo = new DistributionRedundVo();
78d1e8 382         //不是订单购买的卡包,跳出循环
C 383         if(UserCard.SOURCE_TYPE_ORDER_ONE != userCard.getSourceType()){
a036dc 384             return distributionRedundVo;
78d1e8 385         }
C 386
387         //通过购买这个卡项的订单来算单卡的钱是多少
388         //查询已经退了多少钱,避免退超了支付方式金额
389
a036dc 390         if(orderItem == null){
C 391             orderItem = commonService.selectOneByKey(OrderItemMapper.class,userCard.getSourceId());
392         }
78d1e8 393         if(orderItem == null){
C 394             throw new TipsException("获取订单子信息失败[895]");
395         }
396         if(orderItem.getBuyNum().compareTo(0) < 1){
397             throw new TipsException("获取订单子信息购买数量错误!");
398         }
399
4eab4e 400         //获取未退款的卡包,包含转增的
a036dc 401         List<UserCard> userCardList = getNotRefundCard(orderItem.getId(),commonService);
78d1e8 402
a036dc 403         //获取该子单的支付方式,相同的支付方式求和返回
C 404         List<PayMethodVo> payMethodVoList = refundMapper.getConsumePayOneGroupByNumberNo(orderItem.getId());
405         //获取该子单已退的总金额
406         RefundRecordItem refundRecordItem = RefundTool.getOneAlreadyRefundTotal(orderItem.getId(),orderItem.getType(),commonService);
1c26c2 407         logger.info("用户卡包子单支付方式{},子单退款数据{}", JSON.toJSONString(payMethodVoList),JSON.toJSONString(refundRecordItem));
c72884 408
a036dc 409         //计算退款方式金额
086b97 410         distributionRedundVo = PaymentCountTool.countMakeWay(orderItem.getId()
f2b57c 411                 , orderItem.getActualTotal(),orderItem.getActualTotalPoints()
F 412                 , orderItem.getIntegralCash(),refundRecordItem.getOccupyRefundTotal()
c72884 413                 ,refundRecordItem.getOccupyRefundIntegral(),orderItem.getBuyNum(),userCardList.size(),null
41a07f 414                 ,null,1,payMethodVoList,commonService);
949373 415         distributionRedundVo.setGoodsType(orderItem.getType());
C 416         distributionRedundVo.setGoodsName(orderItem.getGoodsName());
78d1e8 417
a036dc 418         return distributionRedundVo;
78d1e8 419     }
C 420
a036dc 421     /**获取未退款的卡项数量
C 422      * @param sourceId 订单子单标识
423      * @return 可退款的用户卡项
424      */
425     public static List<UserCard> getNotRefundCard(String sourceId,CommonService commonService){
426         SqlSentence sqlSentence = new SqlSentence();
427         Map<String,Object> sqlMap = new HashMap<>();
428         //获取用户卡项
429         sqlMap.put("sourceId",sourceId);
430         sqlMap.put("effectiveStatus", UserProjectConstants.EFF_STATUS_CANCEL);
4eab4e 431         sqlSentence.sqlSentence("SELECT * FROM user_card WHERE isDel = 0 AND sourceId = #{m.sourceId} AND effectiveStatus != #{m.effectiveStatus}",sqlMap);
949373 432         List<UserCard> userCardList = commonService.selectList(UserCardMapper.class,sqlSentence);
C 433         if(userCardList.size() == 0){
434             return userCardList;
435         }
436         //获取已经部分退的用户卡包数量
ea4351 437         List<RefundRecordItem> refundRecordItemList = CardRefundTool.findRefundUserCard(sourceId,null,commonService);
949373 438         //过滤掉没有使用但有部分退款
C 439         if(refundRecordItemList.size() == 0){
440             return userCardList;
441         }
442
443         Map<String,UserCard> userCardMap = new HashMap<>();
444         for(UserCard userCard:userCardList){
445             userCardMap.put(userCard.getId(),userCard);
446         }
447         //去除掉参与部分退款的用户卡包
448         for(RefundRecordItem refundRecordItem:refundRecordItemList){
449             userCardMap.remove(refundRecordItem.getUserCardId());
450         }
451         userCardList = new ArrayList<>();
452         for (Map.Entry<String,UserCard> entry : userCardMap.entrySet()) {
453             userCardList.add(entry.getValue());
454         }
455
456         return userCardList;
a036dc 457     }
C 458
459     /**计算卡包条目单次抵扣的金额
460      * 保留20位小数
461      * @param cardItemInfo 卡项条目
462      * @return 单次抵扣的金额
463      */
464     public static BigDecimal countOneMoney(CardItemInfo cardItemInfo){
465         //卡项明细分摊金额
466         BigDecimal sumCardBagMoney = cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney();
467         //卡项明细分摊次数
468         Integer num = cardItemInfo.getMaxNum()==null?cardItemInfo.getEveryDrawNum():cardItemInfo.getMaxNum();
469         //求出单次的单价
470         if(num != null && num > 0){
471             return sumCardBagMoney.divide(BigDecimal.valueOf(num),20,RoundingMode.HALF_UP);
472         }else{
473             return BigDecimal.ZERO;
474         }
475     }
476
949373 477     /**获取卡项的权益数量*/
C 478     public static int getCardEquityCount(String cardId,CommonService commonService){
479         SqlSentence sqlSentence = new SqlSentence();
480         Map<String,Object> values = new HashMap<>();
481
482         values.put("cardItemId",cardId);
968f82 483         sqlSentence.sqlSentence("SELECT COUNT(*) FROM card_equity WHERE isDel = 0 AND cardItemId = #{m.cardItemId}" +
949373 484                 " AND equityType != 4",values);
C 485
486         return commonService.selectCountSql(CardEquityMapper.class,sqlSentence);
487     }
488
8eea74 489     /**获取卡项权益的条目数量*/
968f82 490     public static int getCardInfoCount(String cardEquityId,CommonService commonService){
C 491         SqlSentence sqlSentence = new SqlSentence();
492         Map<String,Object> values = new HashMap<>();
493
494         values.put("cardEquityId",cardEquityId);
495         sqlSentence.sqlSentence("SELECT COUNT(*) FROM card_item_info WHERE isDel = 0 AND cardEquityId = #{m.cardEquityId}"
496           ,values);
497
498         return commonService.selectCountSql(CardItemInfoMapper.class,sqlSentence);
499     }
500
501     /**获取卡项的条目使用抵扣次数
502      * @param cardEquityId 卡项权益标识
503      * @param cardInfoId 卡项条目标识
504      * @param commonService 映射
505      * @return 已抵扣次数
506      */
507     public static int getCardInfoDeduction(String cardEquityId,String cardInfoId,CommonService commonService){
508
509         SqlSentence sqlSentence = new SqlSentence();
510         Map<String,Object> values = new HashMap<>();
511         StringBuilder sql = new StringBuilder();
512
513         sql.append("SELECT IFNULL(SUM(usedNum),0) AS usedNum FROM user_card_used WHERE isDel = 0");
514         if(StringUtils.noNull(cardEquityId)){
515             values.put("cardEquityId",cardEquityId);
516             sql.append(" AND cardEquityId = #{m.cardEquityId}");
517         }
518         if(StringUtils.noNull(cardInfoId)){
519             values.put("cardItemInfoId",cardInfoId);
520             sql.append(" AND cardItemInfoId = #{m.cardItemInfoId}");
521         }
522
523         UserCardUsed userCardUsed = commonService.selectOne(CardEquityMapper.class,sqlSentence);
524         return userCardUsed.getUsedNum();
525     }
526
e542d7 527     /**
F 528      * 获取卡项分摊的权益金额
529      */
530     public static List<CardEquity> getCardEquityMoneyList(String cardId, CardItem cardItem, CommonService commonService) {
531         SqlSentence sqlSentence = new SqlSentence();
532         Map<String, Object> values = new HashMap<>();
533         values.put("isDel", BaseEntity.NO);
534         if (StringUtils.isEmpty(cardId)){
535             cardId = cardItem.getId();
536         }
537         values.put("cardItemId", cardId);
538         sqlSentence.sqlSentence("SELECT * FROM card_equity WHERE isDel = #{m.isDel} AND cardItemId = #{m.cardItemId}", values);
539         List<CardEquity> cardEquityList = commonService.selectList(CardEquityMapper.class, sqlSentence);
540         if (cardEquityList == null || cardEquityList.size() < 1) {
541             throw new TipsException("获取卡项权益信息失败!");
542         }
543         for (CardEquity cardEquity : cardEquityList) {
544             //固定项目权益,分摊的金额查条目的
545             if (cardEquity.getEquityType().equals(CardEquity.EQUITY_TYPE_PROJECT)) {
546                 cardEquity.setShareMoney(BigDecimal.ZERO);
547                 values.put("cardEquityId", cardEquity.getId());
548                 sqlSentence.sqlSentence("SELECT SUM(shareMoney) AS shareMoney FROM card_item_info WHERE isDel = #{m.isDel} AND cardEquityId = #{m.cardEquityId} ", values);
549                 CardItemInfo cardItemInfo = commonService.selectOne(CardItemInfoMapper.class, sqlSentence);
550                 if (cardItemInfo != null) {
551                     cardEquity.setShareMoney(cardItemInfo.getShareMoney());
552                 }
553             }
554         }
555         return cardEquityList.stream().sorted(Comparator.comparing(CardEquity::getShareMoney)).collect(Collectors.toList());
556     }
968f82 557
a036dc 558
e542d7 559     /**
33b85f 560      * 卡包支付过的数据-剔除退款的数据
e542d7 561      */
33b85f 562     public static Map<String,Map<String,UserCardPay>> getCardBagUseList(String cardEquityId, CardEquity cardEquity, CommonService commonService) {
e542d7 563         SqlSentence sqlSentence = new SqlSentence();
F 564         Map<String, Object> values = new HashMap<>();
565         values.put("isDel", BaseEntity.NO);
566         if (StringUtils.isEmpty(cardEquityId)){
567             cardEquityId = cardEquity.getId();
568         }
569         values.put("cardEquityId", cardEquityId);
570         StringBuilder sql = new StringBuilder();
33b85f 571         sql.append(" SELECT * FROM  user_card_pay AS u ");
e542d7 572         sql.append(" JOIN orders_total AS ot ON ot.id = u.orderId");
ffe822 573         sql.append(" WHERE u.isDel = #{m.isDel} AND u.cardEquityId = #{m.cardEquityId} ");
F 574         //卡包要么全退,要么不退,这里查未退款的
575         sql.append(" AND ot.isDel = #{m.isDel} AND ot.payStatus = 3 AND ot.refundStatus = -1 ");
e542d7 576         sqlSentence.sqlSentence(sql.toString(), values);
33b85f 577         List<UserCardPay> cardBagUseList = commonService.selectList(UserCardPayMapper.class, sqlSentence);
F 578         Map<String,Map<String,UserCardPay>> cardBuyMoneyMap = new HashMap<>();
579         Map<String, UserCardPay> itemMap = null;
580         if (cardBagUseList != null && cardBagUseList.size() > 0){
581             for (UserCardPay userCardPay : cardBagUseList) {
582                 itemMap = cardBuyMoneyMap.get(userCardPay.getCardItemInfoId());
583                 if (itemMap == null){
584                     itemMap = new HashMap<>();
585                 }
586                 itemMap.put(userCardPay.getConsumePayItemId(),userCardPay);
587                 cardBuyMoneyMap.put(userCardPay.getCardItemInfoId(),itemMap);
588             }
589         }
590         return cardBuyMoneyMap;
e542d7 591     }
F 592
86a7a0 593     /**
F 594      * 卡包订单子项分配到的金额
595      */
596     public static BigDecimal orderItemSonMoney(String orderItemSonId, CommonService commonService) {
597         SqlSentence sqlSentence = new SqlSentence();
598         Map<String, Object> values = new HashMap<>();
599         values.put("isDel", BaseEntity.NO);
600         values.put("orderItemSonId", orderItemSonId);
601         StringBuilder sql = new StringBuilder();
602         sql.append(" SELECT SUM(actualTotal) AS actualTotal FROM user_card_pay AS u ");
603         sql.append(" JOIN orders_total AS ot ON ot.id = u.orderId");
604         sql.append(" WHERE u.isDel = #{m.isDel} AND u.orderItemSonId = #{m.orderItemSonId} ");
605         //卡包要么全退,要么不退,这里查未退款的
606         sql.append(" AND ot.isDel = #{m.isDel} AND ot.payStatus = 3 AND ot.refundStatus = -1 ");
607         sqlSentence.sqlSentence(sql.toString(), values);
608         UserCardPay userCardPay = commonService.selectOne(UserCardPayMapper.class, sqlSentence);
609         return userCardPay == null?BigDecimal.ZERO:userCardPay.getActualTotal();
610     }
611
ffe822 612     /**获取卡项权益的条目数量*/
F 613     public static List<CardItemInfo> getCardInfoList(String cardEquityId,CommonService commonService){
614         SqlSentence sqlSentence = new SqlSentence();
615         Map<String,Object> values = new HashMap<>();
616         values.put("cardEquityId",cardEquityId);
617         sqlSentence.sqlSentence("SELECT * FROM card_item_info WHERE isDel = 0 AND cardEquityId = #{m.cardEquityId}",values);
618         return commonService.selectList(CardItemInfoMapper.class,sqlSentence);
619     }
620
33b85f 621     /**
F 622      * 获取买卡单数据-已分配的OCC
623      * @return <卡项,支付方式,数据>
624      * */
625     public static Map<String,Map<String,ConsumePayCard>> getCardBuyMoneyByItemId(String orderItemId,CommonService commonService) {
626         SqlSentence sqlSentence = new SqlSentence();
627         Map<String,Object> values = new HashMap<>();
628         values.put("orderItemId",orderItemId);
629         values.put("commonType", ConsumePayCard.TYPE_CARD);
630         sqlSentence.sqlSentence("SELECT * FROM consume_pay_card WHERE isDel = 0 AND orderItemId = #{m.orderItemId} AND commonType = #{m.commonType}",values);
631         List<ConsumePayCard> consumePayCardList = commonService.selectOne(ConsumePayCardMapper.class,sqlSentence);
632         Map<String,Map<String,ConsumePayCard>> totalMap = new HashMap<>();
633         Map<String,ConsumePayCard> itemMap = null;
634         if (consumePayCardList != null && consumePayCardList.size() > 0){
635             for (ConsumePayCard consumePayCard : consumePayCardList) {
636                 itemMap = totalMap.get(consumePayCard.getUserCardId());
637                 if (itemMap == null){
638                     itemMap = new HashMap<>();
639                 }
640                 itemMap.put(consumePayCard.getConsumePayItemId(),consumePayCard);
641                 totalMap.put(consumePayCard.getUserCardId(),itemMap);
642             }
643         }
644         return totalMap;
645     }
646
647     /**
648      * 获取买卡单数据-已分配的OCC
649      * @return <权益或者条目,支付方式,数据>
650      * */
651     public static Map<String, Map<String, ConsumePayCard>> getCardDetailBuyMoneyByItemId(String commonId,String commonType, CommonService commonService) {
652         SqlSentence sqlSentence = new SqlSentence();
653         Map<String,Object> values = new HashMap<>();
654         values.put("commonId",commonId);
655         values.put("commonType", ConsumePayCard.TYPE_EQU);
656         sqlSentence.sqlSentence("SELECT * FROM consume_pay_card WHERE isDel = 0 AND commonId = #{m.commonId} AND commonType = #{m.commonType}",values);
657         List<ConsumePayCard> consumePayCardList = commonService.selectOne(ConsumePayCardMapper.class,sqlSentence);
658         Map<String,Map<String,ConsumePayCard>> totalMap = new HashMap<>();
659         Map<String,ConsumePayCard> itemMap = null;
660         if (consumePayCardList != null && consumePayCardList.size() > 0){
661             for (ConsumePayCard consumePayCard : consumePayCardList) {
662                 itemMap = totalMap.get(consumePayCard.getCommonId());
663                 if (itemMap == null){
664                     itemMap = new HashMap<>();
665                 }
666                 itemMap.put(consumePayCard.getConsumePayItemId(),consumePayCard);
667                 totalMap.put(consumePayCard.getCommonId(),itemMap);
668             }
669         }
670         return totalMap;
671     }
672
a88f94 673 }