| 
					
				 | 
			
			
				@@ -2,14 +2,13 @@ package com.ym.mec.biz.service.impl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import static com.ym.mec.biz.dal.enums.GroupType.GOODS_SELL; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import static com.ym.mec.biz.dal.enums.OrderDetailTypeEnum.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import static com.ym.mec.biz.dal.enums.OrderDetailTypeEnum.ORGAN_SHARE_PROFIT; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.math.BigDecimal; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.math.RoundingMode; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.ArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.Date; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.function.Function; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.util.stream.Collectors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.alibaba.fastjson.JSON; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -19,7 +18,11 @@ import com.ym.mec.biz.dal.mapper.GoodsSubMapper; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.biz.service.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.common.api.CommonResult; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.common.dto.OrderCreate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.ym.mec.common.dto.PmsProductDto; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.ym.mec.common.dto.PmsProductQueryParamDto; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.ym.mec.common.dto.ReturnApplyDto; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.common.exception.BizException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.ym.mec.common.page.PageInfo; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.common.page.WrapperUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.mall.MallFeignService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.commons.collections.CollectionUtils; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -82,7 +85,7 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ /*   @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public List<SellOrder> addSellOrder(Long orderId, String musicGroupId, List<Integer> goodsIds, BigDecimal totalAmount, BigDecimal balance, BigDecimal couponRemitFee) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if (goodsIds == null || goodsIds.size() <= 0) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -262,37 +265,65 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return sellOrderList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public List<SellOrder> addOrderDetail2SellOrder(List<StudentPaymentOrderDetail> orderDetails, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                                                    StudentPaymentOrder studentPaymentOrder, MusicGroup musicGroup,BigDecimal musicRemitFee) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Integer tenantId = studentPaymentOrder.getTenantId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        List<SellOrder> sellOrders = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //总余额支付 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BigDecimal totalBalance = studentPaymentOrder.getBalancePaymentAmount() != null ? studentPaymentOrder.getBalancePaymentAmount() : BigDecimal.ZERO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //总价格 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BigDecimal totalPrice = studentPaymentOrder.getExpectAmount(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //商品总付款 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BigDecimal detailTotalPrice = orderDetails.stream().map(StudentPaymentOrderDetail::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        //商品销售占的余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BigDecimal detailTotalBalance = detailTotalPrice.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                detailTotalPrice.multiply(totalBalance).divide(totalPrice, 2, BigDecimal.ROUND_DOWN); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        int i = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BigDecimal detailRouteBalance = BigDecimal.ZERO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                                    StudentPaymentOrder studentPaymentOrder, MusicGroup musicGroup,BigDecimal balancePaymentAmount) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //过去非商品详情 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<StudentPaymentOrderDetail> goodsOrderDetails = orderDetails.stream().filter(e -> StringUtils.isEmpty(e.getGoodsIdList())).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(CollectionUtils.isEmpty(goodsOrderDetails)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("商品详情为空"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //获取总减免金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal detailTotalRemitPrice = goodsOrderDetails.stream().map(StudentPaymentOrderDetail::getRemitFee).reduce(BigDecimal.ZERO, BigDecimal::add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //获取总金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal detailTotalPrice = goodsOrderDetails.stream().map(StudentPaymentOrderDetail::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        AccountType accountType = sysPaymentConfigService.checkAccountType(PaymentChannelEnum.valueOf(studentPaymentOrder.getPaymentChannel()), studentPaymentOrder.getMerNos(), tenantId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<SellOrder> sellOrders = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        for (StudentPaymentOrderDetail orderDetail : orderDetails) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            BigDecimal detailBalance = orderDetail.getPrice().compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    orderDetail.getPrice().multiply(detailTotalBalance).divide(detailTotalPrice, 2, BigDecimal.ROUND_DOWN); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (i == orderDetails.size()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                detailBalance = detailTotalBalance.subtract(detailRouteBalance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //剩余待分配的余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal subDetailBalance = balancePaymentAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //剩余待分配的优惠券金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal subDetailCouponRemitFee = detailTotalRemitPrice; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //剩余待分配现金 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal subDetailActualAmount = detailTotalPrice.subtract(balancePaymentAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (int i = 0; i < goodsOrderDetails.size(); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            StudentPaymentOrderDetail orderDetail = orderDetails.get(i); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //获取支付金额比例 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal ratioAmount = orderDetail.getPrice().divide(detailTotalPrice, 6, BigDecimal.ROUND_HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //获取优惠券金额比例 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal ratioCouponAmount = orderDetail.getRemitFee().divide(detailTotalRemitPrice, 6, BigDecimal.ROUND_HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //获取分配的余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal detailBalance; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //获取分配的优惠券金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal couponRemitFee; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //获取剩余待分配的现金 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal detailActualAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (i == goodsOrderDetails.size() - 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //获取分配的余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                detailBalance = subDetailBalance; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //获取分配的优惠券金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                couponRemitFee = subDetailCouponRemitFee; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //获取剩余待分配的现金 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                detailActualAmount = subDetailActualAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //获取分配的余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                detailBalance = balancePaymentAmount.multiply(ratioAmount).setScale(2, BigDecimal.ROUND_DOWN); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                subDetailBalance = subDetailBalance.subtract(detailBalance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //获取分配的优惠券金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                couponRemitFee = orderDetail.getRemitFee().multiply(ratioCouponAmount).setScale(2, BigDecimal.ROUND_HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                subDetailCouponRemitFee = subDetailCouponRemitFee.subtract(couponRemitFee); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //获取剩余待分配的现金 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                detailActualAmount = orderDetail.getPrice().subtract(detailBalance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                subDetailActualAmount = subDetailActualAmount.subtract(detailActualAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            detailRouteBalance = detailRouteBalance.add(detailBalance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            i++; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<Integer> goodsIds = Arrays.asList(orderDetail.getGoodsIdList().split(",")).stream().map(Integer::parseInt).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            sellOrders.addAll(this.initSellOrder(studentPaymentOrder, musicGroup.getId(), goodsIds, null, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    detailBalance, orderDetail.getPrice(), couponRemitFee, detailActualAmount, false)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/* 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             BigDecimal goodsTotalPrice = orderDetail.getGoodsList().stream().map(Goods::getGroupPurchasePrice).reduce(BigDecimal.ZERO, BigDecimal::add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             BigDecimal routePrice = BigDecimal.ZERO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             BigDecimal routeBalance = BigDecimal.ZERO; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -416,7 +447,7 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             sellOrderDao.batchInsert(sellOrders); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return sellOrders; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -456,13 +487,16 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //3、退货 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (sellOrders.get(0).getAccountType() != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        /*if (sellOrders.get(0).getAccountType() != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             goodsService.increaseStock(sellOrders, sellOrders.get(0).getAccountType()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }*/ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //4退货同步到商城 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.mallRefundByOrderId(sellOrders); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return sellOrders; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public List<SellOrder> refundByOrderId(Long orderId, Boolean reBackFee) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         List<SellOrder> sellOrders = sellOrderDao.getSellOrderByParentGoodsId(orderId, null); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if (sellOrders.size() <= 0) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -576,6 +610,175 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void mallRefundByOrderId(List<SellOrder> sellOrders) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String skuIds = sellOrders.stream().map(e -> e.getGoodsSkuId().toString()).collect(Collectors.joining(",")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ReturnApplyDto returnApply = new ReturnApplyDto(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        returnApply.setOrderSn(sellOrders.get(0).getOrderNo()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        returnApply.setReason("管乐迷商品更换退货"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        returnApply.setSkuIds(skuIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CommonResult<Boolean> result = mallFeignService.returnApply(returnApply); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (result.getCode() != 200){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("商城订单退货失败",result.getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<SellOrder> initSellOrder(StudentPaymentOrder order, String musicGroupId, List<Integer> goodsIds, KitGroupPurchaseTypeEnum kitGroupPurchaseType, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                         BigDecimal totalBalance, BigDecimal expectTotalAmount, BigDecimal couponRemitAmount, BigDecimal actualTotalAmount,Boolean saveFlag) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //可用优惠券金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal subCouponRemitAmount = couponRemitAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //可用总金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal subExpectTotalAmount = expectTotalAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //可用总余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal subTotalBalance = totalBalance; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //实际金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal subTotalActualAmount = actualTotalAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Integer tenantId = order.getTenantId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        MusicGroup musicGroup = new MusicGroup(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (StringUtils.isNotBlank(musicGroupId)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            musicGroup = musicGroupDao.get(musicGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Goods> goodies = goodsDao.getGoodies(goodsIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal goodsTotalPrice = goodies.stream().map(Goods::getGroupPurchasePrice).reduce(BigDecimal.ZERO, BigDecimal::add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        AccountType accountType = sysPaymentConfigService.checkAccountType(PaymentChannelEnum.valueOf(order.getPaymentChannel()), order.getMerNos(), order.getTenantId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        PmsProductQueryParamDto paramDto = new PmsProductQueryParamDto(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String skuIds = goodsSubService.lambdaQuery().in(GoodsSub::getGoodsId, goodies.stream().map(Goods::getId).collect(Collectors.toList())).list() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                .stream().map(e->e.getSku().toString()).collect(Collectors.joining(",")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        paramDto.setSkuStockIds(skuIds); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        PageInfo<PmsProductDto> productList = mallFeignService.getProductList(paramDto); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (org.springframework.util.CollectionUtils.isEmpty(productList.getRows())) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("商城商品不存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<Integer,PmsProductDto> skuIdProductMap = productList.getRows().stream().collect(Collectors.toMap(PmsProductDto::getSkuStockId, Function.identity())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<SellOrder> sellOrderList = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for (int i = 0; i < goodies.size(); i++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Goods nowGoods = goodies.get(i); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //获取比例 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal goodsRatioAmount = BigDecimal.ZERO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //预计金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal expectAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal goodsBalance; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //减免金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal remitAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //实际金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal actualAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (goodsTotalPrice.compareTo(BigDecimal.ZERO) > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                goodsRatioAmount = nowGoods.getGroupPurchasePrice().divide(goodsTotalPrice, 6, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if(i == goodies.size() - 1){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                expectAmount = subExpectTotalAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                goodsBalance = subTotalBalance; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                remitAmount = subCouponRemitAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                actualAmount = subTotalActualAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                expectAmount = goodsRatioAmount.multiply(expectTotalAmount).setScale(2, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                subExpectTotalAmount = subExpectTotalAmount.subtract(expectAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                goodsBalance = goodsRatioAmount.multiply(totalBalance).setScale(2, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                subTotalBalance = subTotalBalance.subtract(goodsBalance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                remitAmount = goodsRatioAmount.multiply(couponRemitAmount).setScale(2, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                subCouponRemitAmount = subCouponRemitAmount.subtract(remitAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                actualAmount = expectAmount.subtract(goodsBalance).subtract(remitAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                subTotalActualAmount = subTotalActualAmount.subtract(actualAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //剩余预计金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal subExpectAmount = expectAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //剩余余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal subGoodsBalance = goodsBalance; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //剩余减免金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal subRemitAmount = remitAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //剩余实际金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal subActualAmount = actualAmount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //租赁、免费的没有销售收入,有销售成本 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (nowGoods.getType().equals(GoodsType.INSTRUMENT) && (kitGroupPurchaseType.equals(KitGroupPurchaseTypeEnum.LEASE) || kitGroupPurchaseType.equals(KitGroupPurchaseTypeEnum.FREE))) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                goodsBalance = BigDecimal.ZERO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                expectAmount = BigDecimal.ZERO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            List<GoodsSub> goodsSubs = goodsSubService.lambdaQuery().eq(GoodsSub::getGoodsId, nowGoods.getId()).list(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //商品原始总价 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            BigDecimal complementPrice = goodsSubs.stream().map(GoodsSub::getGoodsPrice).reduce(BigDecimal.ZERO, BigDecimal::add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //生成sellOrder 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (int j = 0; j < goodsSubs.size(); j++) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                GoodsSub goodsSub = goodsSubs.get(j); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                PmsProductDto productDto = skuIdProductMap.get(goodsSub.getSku()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if(Objects.isNull(productDto)){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    throw new BizException("商品["+nowGoods.getName()+"]子商品["+goodsSub.getSku()+"]不存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                SellOrder sellOrder = new SellOrder(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //获取比例 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                BigDecimal ratioAmount = goodsSub.getGoodsPrice().divide(complementPrice, 6, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (j == goodsSubs.size() - 1){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setExpectAmount(subExpectAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setBalanceAmount(subGoodsBalance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setActualAmount(subActualAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setCouponRemitAmount(subRemitAmount); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    BigDecimal decimal = expectAmount.multiply(ratioAmount).setScale(2, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setExpectAmount(decimal); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    subExpectAmount = subExpectAmount.subtract(decimal); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    BigDecimal balance = goodsBalance.multiply(ratioAmount).setScale(2, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setBalanceAmount(balance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    subGoodsBalance = subGoodsBalance.add(balance); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    BigDecimal actual = actualAmount.multiply(ratioAmount).setScale(2, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setActualAmount(actual); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    subActualAmount = subActualAmount.subtract(actual); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    BigDecimal remit = remitAmount.multiply(ratioAmount).setScale(2, RoundingMode.HALF_UP); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setCouponRemitAmount(remit); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    subRemitAmount = subRemitAmount.subtract(remit); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setParentGoodsId(nowGoods.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setGoodsSkuId(goodsSub.getSku()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setNum(1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setGoodsName(productDto.getName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setAccountType(accountType); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setOrganSellCost(goodsSub.getGoodsPrice()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setOrganId(order.getOrganId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setCooperationOrganId(musicGroup.getCooperationOrganId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setEduTeacherId(musicGroup.getEducationalTeacherId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setTransNo(order.getTransNo()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setOrderId(order.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setOrderNo(order.getOrderNo()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setUserId(order.getUserId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setPaymentChannel(order.getPaymentChannel()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setMerNo(order.getMerNos()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setSellTime(order.getCreateTime()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrder.setTenantId(tenantId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (nowGoods.getType().equals(GoodsType.INSTRUMENT)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setType(SellTypeEnum.INSTRUMENT); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else if (nowGoods.getType().equals(GoodsType.ACCESSORIES)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setType(SellTypeEnum.ACCESSORIES); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    sellOrder.setType(SellTypeEnum.OTHER); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                sellOrderList.add(sellOrder); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (saveFlag && !org.springframework.util.CollectionUtils.isEmpty(sellOrderList)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.batchInsert(sellOrderList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            this.syncSellOrder2Mall(sellOrderList,order); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return sellOrderList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Transactional(rollbackFor = Exception.class) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public List<SellOrder> initSellOrder(StudentPaymentOrder order, String musicGroupId, List<Integer> goodsIds, KitGroupPurchaseTypeEnum kitGroupPurchaseType,Boolean saveFlag) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal totalBalance = order.getBalancePaymentAmount(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (totalBalance == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            totalBalance = BigDecimal.ZERO; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal expectTotalAmount = order.getExpectAmount(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal couponRemitAmount = order.getCouponRemitFee(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal actualTotalAmount = order.getActualAmount(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return this.initSellOrder(order, musicGroupId, goodsIds, kitGroupPurchaseType, totalBalance, expectTotalAmount, couponRemitAmount, actualTotalAmount,saveFlag==null?true:saveFlag); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public Map<String, BigDecimal> calcSellAmount(StudentPaymentOrder order,BigDecimal cloudBalanceIncome,BigDecimal cloudIncome) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         List<StudentPaymentOrderDetail> orderDetails = studentPaymentOrderDetailDao.getOrderDetail(order.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //总余额支付 
			 |