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;
|
}
|
|
}
|