package com.hx.phip.tool.refund; import com.hx.common.service.CommonService; import com.hx.exception.TipsException; import com.hx.phiappt.model.refund.RefundRecordConsumePay; import com.hx.phip.vo.order.payment.PayMethodVo; import com.hx.phip.vo.order.refund.DistributionRedundMethodVo; import com.hx.phip.vo.order.refund.DistributionRedundVo; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /**计算支付方式金额 * @author CJH */ public class PaymentCountTool { /**计算支付方式的金额 * @param itemId 子单方式 * @param total 总金额 * @param realRefundTotal 已退总金额 * @param sum 购买总数量 * @param surplusNum 剩余数量 * @param refundTotal 计算总金额,空值自动计算 * @param refundNum 计算数量 * @param payMethodVoList 支付方式,查询consumePay子类 * @return 参数结构 */ public static DistributionRedundVo countMakeWay(String itemId, BigDecimal total, BigDecimal realRefundTotal, Integer sum, Integer surplusNum , BigDecimal refundTotal,Integer refundNum, List payMethodVoList, CommonService commonService){ if(surplusNum == null){ throw new TipsException("可退款数量不能为空!"); } if(refundNum == null){ throw new TipsException("退款数量不能为空!"); } //创建返回结构 DistributionRedundVo distributionRedundVo = new DistributionRedundVo(); //退款数量占总数占比计算 BigDecimal scale = BigDecimal.valueOf(refundNum).divide(BigDecimal.valueOf(sum),15, RoundingMode.HALF_UP); //剩余可退款金额 BigDecimal surplusTotal = total.subtract(realRefundTotal).setScale(2,RoundingMode.HALF_UP); System.out.println("realRefundTotal:"+realRefundTotal); System.out.println("surplusTotal:"+surplusTotal); //算出本次要退款金额 if(refundTotal == null){ if(surplusNum.equals(refundNum)) { //最后退款数量 refundTotal = surplusTotal; }else{ //不是最后退款数量,根据退款数量占比来算退款金额 refundTotal = total.multiply(scale).setScale(2,RoundingMode.HALF_UP); if(refundTotal.compareTo(surplusTotal) > 0){ refundTotal = surplusTotal; } } } System.out.println("refundTotal:"+refundTotal); //转载分配好的支付方式和金额 List distributionPayList = new ArrayList<>(); if(payMethodVoList != null && payMethodVoList.size() > 0){ //获取已退款的支付方式金额 List refundRecordConsumePayList = PartialRefundUtil.getRefundRecordConsumePay(itemId,null,true,false,commonService); ///////把已退款的金额注入进去 //转化为map,下面注入退款金额用到 Map payMethodVoMap = payMethodVoList.stream().collect(Collectors.toMap(PayMethodVo::getNumberNo,(a) -> a)); PayMethodVo payMethodVo; for(RefundRecordConsumePay refundRecordConsumePay:refundRecordConsumePayList){ payMethodVo = payMethodVoMap.get(refundRecordConsumePay.getNumberNo()); if(payMethodVo != null){ //已退款金额 payMethodVo.setRefundTotal(payMethodVo.getRefundTotal().add(refundRecordConsumePay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP)); //剩余未退款金额 payMethodVo.setSurplusTotal(payMethodVo.getSurplusTotal().subtract(refundRecordConsumePay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP)); } } //支付方式进行升序排序,避免循环支付方式的时候最后一个可分配0元,从而导致算子单最后一个的时候没有钱算了 payMethodVoList = payMethodVoList.stream().sorted(Comparator.comparing(PayMethodVo::getSurplusTotal)).collect(Collectors.toList()); DistributionRedundMethodVo distributionPay; for(int i = 0;i 0){ distributionPay.setRefundTotal(payMethodVo.getSurplusTotal()); } if(distributionPay.getRefundTotal().compareTo(refundTotal) > 0){ distributionPay.setRefundTotal(refundTotal); } //计算剩余要分配的退款金额 refundTotal = refundTotal.subtract(distributionPay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP); //存储封装好的对象 distributionPayList.add(distributionPay); ////返回的其他金额计算 //退款总金额计算 distributionRedundVo.setRefundTotal(distributionRedundVo.getRefundTotal().add(distributionPay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP)); if(distributionPay.getIsMoneyPay() == DistributionRedundMethodVo.NUM_1){ //现金金额 distributionRedundVo.setRefundCash(distributionRedundVo.getRefundCash().add(distributionPay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP)); } if(distributionPay.getIsExecute() == DistributionRedundMethodVo.NUM_1){ //划扣金额 distributionRedundVo.setRefundExecute(distributionRedundVo.getRefundExecute().add(distributionPay.getRefundTotal()).setScale(2,RoundingMode.HALF_UP)); } } } distributionRedundVo.setDistributionRedundMethodVoList(distributionPayList); return distributionRedundVo; } }