| | |
| | | import com.hx.phiappt.common.PayMethodTypeConstants; |
| | | import com.hx.phiappt.constants.tool.exception.ExceptionTool; |
| | | import com.hx.phiappt.dao.mapper.*; |
| | | 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.ConsumePay; |
| | |
| | | import com.hx.phiappt.model.order.OrderItemSon; |
| | | import com.hx.phiappt.model.order.OrdersTotal; |
| | | import com.hx.phiappt.model.user.UserCard; |
| | | import com.hx.phiappt.model.user.UserCardPay; |
| | | import com.hx.phiappt.model.user.UserProjectItem; |
| | | import com.hx.phiappt.tool.project.ProjectIntegralCashTool; |
| | | import com.hx.phiappt.vo.payMethod.PayMethodVo; |
| | | import com.hx.phip.config.CustomParameter; |
| | | import com.hx.phip.service.init.OrderInitService; |
| | | import com.hx.phip.tool.deduction.UserDeductionSingleTool; |
| | | import com.hx.phip.tool.user.UserCardTool; |
| | | import com.hx.phip.vo.init.OrderCarryVo; |
| | | import com.hx.phip.vo.user.UserCardItemInfoVo; |
| | | import com.hx.util.StringUtils; |
| | | import com.hz.his.vo.project.IntegralCashVo; |
| | | import com.hz.his.vo.project.SkuDeductionVo; |
| | |
| | | private CommonService commonService; |
| | | @Resource |
| | | private CustomParameter customParameter; |
| | | @Resource |
| | | private RefundMapper refundMapper; |
| | | |
| | | /**重新算订单数据 |
| | | * @param canIntegralCashIdList 可以使用积分结账的商品 |
| | |
| | | twoHandle(ordersTotal,orderItem,orderItemSonList,updateUserProject); |
| | | }else if(OrderItemConstants.CARD_BAG.equals(orderItem.getType())) { |
| | | //卡项处理 |
| | | cardBagTwohandle(ordersTotal,orderItem,orderItemSonList,updateUserProject); |
| | | //cardBagTwohandle(ordersTotal,orderItem,orderItemSonList,updateUserProject); |
| | | //卡项处理-含商品处理版本 |
| | | cardBagTwoHandle(ordersTotal,orderItem,orderItemSonList,updateUserProject); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | /**二级订单积分处理-卡包处理-含商品处理版本*/ |
| | | private void cardBagTwoHandle(OrdersTotal ordersTotal,OrderItem orderItem,List<OrderItemSon> orderItemSonList,boolean updateUserProject){ |
| | | //获取用户卡项 |
| | | UserCard userCard = userCardMapper.selectOneByKey(orderItem.getUserCardId()); |
| | | if (userCard == null) { |
| | | logger.info("未找到用户的卡项!!!!!!!!!!!!!"+orderItem.getUserCardId()); |
| | | return; |
| | | } |
| | | SqlSentence sqlSentence = new SqlSentence(); |
| | | Map<String, Object> values = new HashMap<>(); |
| | | //当时买卡的订单 |
| | | OrderItem orderItemBuy = null; |
| | | if (StringUtils.noNull(userCard.getSourceId())) { |
| | | orderItemBuy = orderItemMapper.selectOneByKey(userCard.getSourceId()); |
| | | } else { |
| | | //获取当时购买这个卡包的订单子单 |
| | | values.put("orderId", userCard.getOrderId()); |
| | | values.put("commonId", userCard.getCardItemId()); |
| | | sqlSentence.sqlSentence("SELECT * FROM order_item WHERE isDel = 0 AND orderId = #{m.orderId} AND commonId = #{m.commonId} LIMIT 1", values); |
| | | orderItemBuy = orderItemMapper.selectOne(sqlSentence); |
| | | if (orderItemBuy != null) { |
| | | //更新用户项目关联的订单 |
| | | values.put("sourceType", UserCard.SOURCE_TYPE_ORDER_ONE); |
| | | values.put("sourceId", orderItemBuy.getId()); |
| | | values.put("id", userCard.getId()); |
| | | sqlSentence.sqlUpdate("sourceType = #{m.sourceType},sourceId = #{m.sourceId} WHERE id = #{m.id}", values); |
| | | userCardMapper.updateWhere(sqlSentence); |
| | | } |
| | | } |
| | | if (orderItemBuy == null) { |
| | | throw new TipsException("获取购买卡项订单数据失败[01],请联系管理员"); |
| | | } |
| | | //获取卡项配置 |
| | | CardItem cardItem = cardItemMapper.selectOneByKey(orderItemBuy.getCommonId()); |
| | | if (cardItem == null) { |
| | | throw new TipsException("获取购买卡项订单数据失败[02],请联系管理员"); |
| | | } |
| | | //子单支付总数据 |
| | | List<PayMethodVo> groupByNumberNoList = refundMapper.getConsumePayOneGroupByNumberNo(orderItemBuy.getId()); |
| | | Map<String, PayMethodVo> groupByNumberNoMap = new HashMap<>(); |
| | | for (PayMethodVo payMethodVo : groupByNumberNoList) { |
| | | //要注意可能买多张 |
| | | payMethodVo.setPayTotal(payMethodVo.getPayTotal().divide(new BigDecimal(orderItemBuy.getBuyNum()),2,RoundingMode.HALF_UP)); |
| | | payMethodVo.setDeductionMoney(payMethodVo.getDeductionMoney().divide(new BigDecimal(orderItemBuy.getBuyNum()),2,RoundingMode.HALF_UP)); |
| | | groupByNumberNoMap.put(payMethodVo.getNumberNo(), payMethodVo); |
| | | } |
| | | //子单支付方式数据 |
| | | List<ConsumePayItem> consumePayItemList = refundMapper.selectConsumePayOneList(orderItemBuy.getId()); |
| | | for (ConsumePayItem consumePayItemVo : consumePayItemList) { |
| | | //要注意可能买多张 |
| | | consumePayItemVo.setActualTotal(consumePayItemVo.getActualTotal().divide(new BigDecimal(orderItemBuy.getBuyNum()),2,RoundingMode.HALF_UP)); |
| | | consumePayItemVo.setDeductionMoney(consumePayItemVo.getDeductionMoney().divide(new BigDecimal(orderItemBuy.getBuyNum()),2,RoundingMode.HALF_UP)); |
| | | } |
| | | |
| | | //卡项权益,支付方式编号,支付方式数据 |
| | | Map<String, Map<String, ConsumePayItem>> cardEquityMethodMap = new HashMap<>(); |
| | | Map<String, ConsumePayItem> methodMap = null; |
| | | ConsumePayItem consumePayItem = null; |
| | | //每个权益的总金额数据 |
| | | Map<String,CardEquity> cardEquityMapTotal = new HashMap<>(); |
| | | CardEquity cardEquity = null; |
| | | //每个权益的金额数据 |
| | | List<CardEquity> cardEquityMoneyList = UserCardTool.getCardEquityMoneyList(null, cardItem, commonService); |
| | | for (int i = 0; i < cardEquityMoneyList.size(); i++) { |
| | | cardEquity = cardEquityMoneyList.get(i); |
| | | cardEquityMapTotal.put(cardEquity.getId(), cardEquity); |
| | | //每个权益的占比,如果是最后一个则占剩余的 |
| | | BigDecimal rate = null; |
| | | if (i != cardEquityMoneyList.size() - 1) { |
| | | rate = cardEquity.getShareMoney().divide(cardItem.getTotal(), 20, RoundingMode.HALF_UP); |
| | | } |
| | | logger.info("权益:{},权益占比卡项金额比例:{}",cardEquity.getId(),rate); |
| | | //权益Map |
| | | methodMap = cardEquityMethodMap.get(cardEquity.getId()); |
| | | if (methodMap == null) { |
| | | methodMap = new HashMap<>(); |
| | | } |
| | | for (ConsumePayItem consumePayItemVo : consumePayItemList) { |
| | | //获取该支付方式的总额 |
| | | PayMethodVo payMethodVo = groupByNumberNoMap.get(consumePayItemVo.getNumberNo()); |
| | | logger.info("循环买卡子单的支付方式{}:,对应的总金额:{}"+JSON.toJSONString(consumePayItemVo),JSON.toJSONString(payMethodVo)); |
| | | |
| | | //计算当前要分摊的金额 |
| | | BigDecimal actualTotal = BigDecimal.ZERO; |
| | | BigDecimal deductionMoney = BigDecimal.ZERO; |
| | | if (rate != null) { |
| | | actualTotal = consumePayItemVo.getActualTotal().multiply(rate).setScale(2, RoundingMode.DOWN); |
| | | deductionMoney = consumePayItemVo.getDeductionMoney().multiply(rate).setScale(2, RoundingMode.DOWN); |
| | | } else { |
| | | //最后一个拿剩余的 |
| | | actualTotal = payMethodVo.getPayTotal(); |
| | | deductionMoney = payMethodVo.getDeductionMoney(); |
| | | } |
| | | //减去分配的金额 |
| | | payMethodVo.setPayTotal(payMethodVo.getPayTotal().subtract(actualTotal)); |
| | | payMethodVo.setDeductionMoney(payMethodVo.getDeductionMoney().subtract(deductionMoney)); |
| | | if (payMethodVo.getPayTotal().compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new RuntimeException("卡包金额分配错误[0223]"); |
| | | } |
| | | if (payMethodVo.getDeductionMoney().compareTo(BigDecimal.ZERO) < 0) { |
| | | throw new RuntimeException("卡包抵扣金额分配错误[0223]"); |
| | | } |
| | | groupByNumberNoMap.put(payMethodVo.getNumberNo(), payMethodVo); |
| | | //封装每个权益的支付方式 |
| | | consumePayItem = methodMap.get(consumePayItemVo.getNumberNo()); |
| | | if (consumePayItem == null) { |
| | | consumePayItem = consumePayItemVo; |
| | | consumePayItem.setActualTotal(BigDecimal.ZERO); |
| | | consumePayItem.setDeductionMoney(BigDecimal.ZERO); |
| | | } |
| | | consumePayItem.actualTotalAdd(actualTotal); |
| | | consumePayItem.deductionMoneyAdd(deductionMoney); |
| | | methodMap.put(consumePayItemVo.getNumberNo(), consumePayItem); |
| | | |
| | | } |
| | | cardEquityMethodMap.put(cardEquity.getId(), methodMap); |
| | | } |
| | | |
| | | //权益标识,开卡选择的条目标识,条目数据 |
| | | Map<String, Map<String,CardItemInfo>> cardEquityMap = new HashMap<>(); |
| | | Map<String,CardItemInfo> cardItemInfoMap = null; |
| | | CardItemInfo cardItemInfo = null; |
| | | //权益标识,是否是全部使用完,通过判断条目的最终剩余次数来看是不是最后一个 |
| | | Map<String, Boolean> cardEquityNumMap = new HashMap<>(); |
| | | for (OrderItemSon itemSon : orderItemSonList) { |
| | | //获取卡项的条目 |
| | | cardItemInfo = cardItemInfoMapper.selectOneByKey(itemSon.getCardItemInfoId()); |
| | | if (cardItemInfo == null){ |
| | | continue; |
| | | } |
| | | //条目数量 |
| | | int deductionNun; |
| | | if (cardItemInfo.getEveryDrawNum() != null && cardItemInfo.getEveryDrawNum() > 0) { |
| | | deductionNun = itemSon.getBuyNum() * cardItemInfo.getEveryDrawNum(); |
| | | } else { |
| | | deductionNun = itemSon.getBuyNum(); |
| | | } |
| | | //这次购买的金额(用于分配金额时排序,规则:用单次x数量) |
| | | BigDecimal infoShareMoney = UserCardTool.countOneMoney(cardItemInfo).multiply(new BigDecimal(deductionNun).setScale(2, RoundingMode.DOWN)); |
| | | cardItemInfo.setInfoShareMoney(infoShareMoney); |
| | | itemSon.setInfoShareMoney(infoShareMoney); |
| | | logger.info("条目:{},条目总次数金额:{}",cardItemInfo.getId(),infoShareMoney); |
| | | //按照权益进行分组 |
| | | cardItemInfoMap = cardEquityMap.get(itemSon.getCardEquityId()); |
| | | if (cardItemInfoMap == null) { |
| | | cardItemInfoMap = new HashMap<>(); |
| | | } |
| | | cardItemInfoMap.put(itemSon.getCardItemInfoId(), cardItemInfo); |
| | | cardEquityMap.put(itemSon.getCardEquityId(), cardItemInfoMap); |
| | | |
| | | //是否使用完这个权益 |
| | | Boolean eqBoolean = cardEquityNumMap.get(itemSon.getCardEquityId()); |
| | | if (eqBoolean == null){ |
| | | eqBoolean = true; |
| | | } |
| | | //获取这个条目剩余的次数 |
| | | UserCardItemInfoVo userCardItemInfoVo = UserCardTool.getSurplusNumber(userCard, cardItemInfo, null, commonService); |
| | | if (userCardItemInfoVo.getSurplusNum() != null && userCardItemInfoVo.getSurplusNum() > 0){ |
| | | eqBoolean = false; |
| | | } |
| | | logger.info("条目:{},剩余:{},判断权益是否是最后一个:{}",cardItemInfo.getId(),userCardItemInfoVo.getSurplusNum(),eqBoolean); |
| | | cardEquityNumMap.put(itemSon.getCardEquityId(),eqBoolean); |
| | | } |
| | | |
| | | //到这一步的时候,用户卡项使用记录会有记录 |
| | | //每个权益已经使用了的金额 |
| | | Map<String, Map<String, ConsumePayItem>> cardEquityMapUsedTotal = new HashMap<>(); |
| | | for (Map.Entry<String, CardEquity> stringCardEquityEntry : cardEquityMapTotal.entrySet()) { |
| | | cardEquity = stringCardEquityEntry.getValue(); |
| | | List<UserCardPay> cardBagEquityMoney = UserCardTool.getCardBagEquityMoney(null, cardEquity, commonService); |
| | | methodMap = cardEquityMapUsedTotal.get(cardEquity.getCardItemId()); |
| | | if (methodMap == null){ |
| | | methodMap = new HashMap<>(); |
| | | } |
| | | if (cardBagEquityMoney != null && cardBagEquityMoney.size() > 0){ |
| | | for (UserCardPay userCardPay : cardBagEquityMoney) { |
| | | consumePayItem = methodMap.get(userCardPay.getNumberNo()); |
| | | if (consumePayItem == null){ |
| | | consumePayItem = new ConsumePayItem(); |
| | | consumePayItem.setNumberNo(userCardPay.getNumberNo()); |
| | | consumePayItem.setActualTotal(BigDecimal.ZERO); |
| | | consumePayItem.setDeductionMoney(BigDecimal.ZERO); |
| | | } |
| | | consumePayItem.deductionMoneyAdd(userCardPay.getDeductionMoney()); |
| | | consumePayItem.actualTotalAdd(userCardPay.getActualTotal()); |
| | | methodMap.put(userCardPay.getNumberNo(),consumePayItem); |
| | | } |
| | | } |
| | | cardEquityMapUsedTotal.put(cardEquity.getCardItemId(),methodMap); |
| | | } |
| | | logger.info("卡项权益已经使用了的金额:{}",JSON.toJSONString(cardEquityMapUsedTotal)); |
| | | |
| | | //按权益分组 |
| | | Map<String,List<OrderItemSon>> cardItemInfoListMap = orderItemSonList.stream().collect(Collectors.groupingBy(OrderItemSon::getCardEquityId)); |
| | | for (Map.Entry<String, List<OrderItemSon>> entry : cardItemInfoListMap.entrySet()) { |
| | | //这个权益的数据 |
| | | cardEquity = cardEquityMapTotal.get(entry.getKey()); |
| | | //这个权益的总支付金额数据 |
| | | methodMap = cardEquityMethodMap.get(entry.getKey()); |
| | | //这个权益已使用支付金额数据 |
| | | Map<String, ConsumePayItem> usedCardEqu = cardEquityMapUsedTotal.get(entry.getKey()); |
| | | //这个权益的条目数据 |
| | | cardItemInfoMap = cardEquityMap.get(entry.getKey()); |
| | | //这个权益下购买的条目,按金额排序 |
| | | List<OrderItemSon> infoList = entry.getValue().stream().sorted(Comparator.comparing(OrderItemSon::getInfoShareMoney)).collect(Collectors.toList()); |
| | | for (int i = 0; i < infoList.size(); i++) { |
| | | OrderItemSon orderItemSon = infoList.get(i); |
| | | //条目的数据 |
| | | cardItemInfo = cardItemInfoMap.get(orderItemSon.getCardItemInfoId()); |
| | | //条目要更新的未执行金额 |
| | | BigDecimal totalNoDeductionMoney = BigDecimal.ZERO; |
| | | //条目占比权益 |
| | | BigDecimal proportion = null; |
| | | //这次购买,权益次数已用完 |
| | | if (cardEquityNumMap.get(entry.getKey())){ |
| | | //权益次数用完,需要直接扣完 |
| | | if (i != infoList.size()-1){ |
| | | proportion = cardItemInfo.getInfoShareMoney().divide(cardEquity.getShareMoney(), 20, RoundingMode.HALF_UP); |
| | | } |
| | | }else { |
| | | proportion = cardItemInfo.getInfoShareMoney().divide(cardEquity.getShareMoney(), 20, RoundingMode.HALF_UP); |
| | | } |
| | | logger.info("条目:{},占比权益:{}",cardItemInfo.getId(),proportion); |
| | | //循环支付方式 |
| | | for (Map.Entry<String, ConsumePayItem> payItemEntry : methodMap.entrySet()) { |
| | | UserCardPay userCardPay = new UserCardPay(); |
| | | //当前订单数据 |
| | | userCardPay.setOrderId(orderItemSon.getOrderId()); |
| | | userCardPay.setOrderItemId(orderItemSon.getOrderItemId()); |
| | | userCardPay.setOrderItemSonId(orderItemSon.getId()); |
| | | userCardPay.setCardEquityId(orderItemSon.getCardEquityId()); |
| | | userCardPay.setCardItemInfoId(orderItemSon.getCardItemInfoId()); |
| | | userCardPay.setGoodsId(orderItemSon.getGoodsId()); |
| | | userCardPay.setGoodsNo(orderItemSon.getGoodsNo()); |
| | | userCardPay.setGoodsName(orderItemSon.getGoodsName()); |
| | | userCardPay.setUsedOne(orderItemSon.getUsedOne()); |
| | | userCardPay.setUsedTotal(orderItemSon.getUsedTotal()); |
| | | |
| | | //权益的支付方式 |
| | | ConsumePayItem value = payItemEntry.getValue(); |
| | | //存储买卡的支付数据 |
| | | userCardPay.setConsumePayItemId(value.getId()); |
| | | userCardPay.setPaymentMethodId(value.getPaymentMethodId()); |
| | | userCardPay.setNumberNo(value.getNumberNo()); |
| | | userCardPay.setName(value.getName()); |
| | | userCardPay.setIsMoneyPay(value.getIsMoneyPay()); |
| | | userCardPay.setIsExecute(value.getIsExecute()); |
| | | userCardPay.setIsPay(value.getIsPay()); |
| | | userCardPay.setDeductionType(value.getDeductionType()); |
| | | //分摊买卡的支付数据 |
| | | BigDecimal actualTotal = BigDecimal.ZERO; |
| | | BigDecimal deductionMoney = BigDecimal.ZERO; |
| | | if (proportion != null){ |
| | | //按比例分配 |
| | | actualTotal = value.getActualTotal().multiply(proportion).setScale(2,RoundingMode.HALF_UP); |
| | | deductionMoney = value.getDeductionMoney().multiply(proportion).setScale(2,RoundingMode.HALF_UP); |
| | | }else { |
| | | //分配剩余的钱 |
| | | ConsumePayItem usedPayItem = usedCardEqu.get(value.getNumberNo()); |
| | | actualTotal = value.getActualTotal().subtract(usedPayItem.getActualTotal()); |
| | | deductionMoney = value.getDeductionMoney().subtract(usedPayItem.getDeductionMoney()); |
| | | } |
| | | userCardPay.setActualTotal(actualTotal); |
| | | userCardPay.setDeductionMoney(deductionMoney); |
| | | //可划扣金额 |
| | | if (userCardPay.getIsExecute() != null && userCardPay.getIsExecute() == 1){ |
| | | userCardPay.setNoDeductionAmount(userCardPay.getActualTotal()); |
| | | totalNoDeductionMoney = totalNoDeductionMoney.add(userCardPay.getNoDeductionAmount()); |
| | | } |
| | | //存储当前买卡的分摊数据 |
| | | commonService.insert(UserCardPayMapper.class,userCardPay); |
| | | } |
| | | //更新二级子单信息 |
| | | values.clear(); |
| | | values.put("noDeductionAmount", totalNoDeductionMoney); |
| | | values.put("id", orderItemSon.getId()); |
| | | sqlSentence.sqlUpdate("noDeductionAmount = #{m.noDeductionAmount} WHERE id = #{m.id}", values); |
| | | orderItemSonMapper.updateWhere(sqlSentence); |
| | | //更新一级子单信息 |
| | | values.clear(); |
| | | values.put("noDeductionAmount", totalNoDeductionMoney); |
| | | values.put("id", orderItem.getId()); |
| | | sqlSentence.sqlUpdate("noDeductionAmount = noDeductionAmount + #{m.noDeductionAmount} WHERE id = #{m.id}", values); |
| | | orderItemMapper.updateWhere(sqlSentence); |
| | | //用户项目 |
| | | if (OrderItemConstants.TYPE_PROJECT.equals(orderItemSon.getType())) { |
| | | if (updateUserProject) { |
| | | //根据用户项目获取疗程数 |
| | | UserProjectItem userProjectItem = getUserProjectItem(orderItemSon.getId(), ordersTotal.getUserId()); |
| | | //更新用户项目 |
| | | updateUserProject(userProjectItem, orderItemSon.getTotal(), orderItemSon.getActualTotal() |
| | | , orderItemSon.getOriPrice(), orderItemSon.getCurPrice(), orderItemSon.getBuyNum() |
| | | , totalNoDeductionMoney, orderItemSon.getUsedTotal(), null); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /**更新用户项目数据*/ |
| | | private void updateUserProject(UserProjectItem userProjectItem,BigDecimal total,BigDecimal actualTotal,BigDecimal oriPrice |
| | | ,BigDecimal curPrice,int buyNum,BigDecimal deductionTotalAmount,int usedTotal,Integer transferNum){ |