| 
					
				 | 
			
			
				@@ -1,5 +1,7 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 package com.ym.mec.biz.service.impl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import static com.ym.mec.biz.dal.enums.DealStatusEnum.SUCCESS; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.biz.dal.dao.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.biz.dal.entity.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.biz.dal.enums.*; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -13,6 +15,7 @@ import com.ym.mec.common.service.impl.BaseServiceImpl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.thirdparty.message.MessageSenderPluginContext; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.util.collection.MapUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.ym.mec.util.date.DateUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.apache.commons.lang3.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.slf4j.LoggerFactory; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -72,6 +75,8 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private StudentInstrumentService studentInstrumentService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private MusicGroupPurchaseListDao musicGroupPurchaseListDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private StudentRegistrationDao studentRegistrationDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     private final Logger logger = LoggerFactory.getLogger(this.getClass()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -334,11 +339,14 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     public SubjectChange getStudentOriginal(Integer studentId, String musicGroupId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    	 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //1、存在历史的更换 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         SubjectChange subjectChange = new SubjectChange(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         SubjectChange studentLastChange = subjectChangeDao.getStudentLastChange(studentId, musicGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if (studentLastChange != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            Set<Integer> refundSellOrderGoodsIds = getRefundGoodsId(studentLastChange.getOrderId().longValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        	List<Long> orderIdList = new ArrayList<Long>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        	orderIdList.add(studentLastChange.getOrderId().longValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        	Set<Integer> refundSellOrderGoodsIds = getRefundGoodsId(orderIdList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             subjectChange.setStudentId(studentLastChange.getStudentId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             subjectChange.setOrderId(studentLastChange.getOrderId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             subjectChange.setCooperationOrganId(studentLastChange.getCooperationOrganId()); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -374,12 +382,30 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return subjectChange; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //2.1不存在历史的更换 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        List<StudentPaymentOrderDetail> details = studentPaymentOrderDetailDao.getStudentApplyDetail(studentId, musicGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (details.size() <= 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Set<Integer> refundSellOrderGoodsIds = getRefundGoodsId(details.get(0).getPaymentOrderId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        StudentRegistration studentRegistration = studentRegistrationDao.queryByUserIdAndMusicGroupId(studentId, musicGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (studentRegistration == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new BizException("用户注册信息不存在"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<StudentPaymentOrder> studentPaymentOrderList = new ArrayList<StudentPaymentOrder>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		if (studentRegistration.getMusicGroupPaymentCalenderId() == null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			if(studentRegistration.getMusicGroupStatus() == StudentMusicGroupStatusEnum.NORMAL){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				StudentPaymentOrder studentPaymentOrder = studentPaymentOrderService.findMusicGroupApplyOrderByStatus(studentId, musicGroupId, SUCCESS); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				if (studentPaymentOrder != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					studentPaymentOrderList.add(studentPaymentOrder); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+				} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+			studentPaymentOrderList = studentPaymentOrderService.queryByBatchNo(studentId, studentRegistration.getMusicGroupPaymentCalenderId() + "", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+					DealStatusEnum.SUCCESS); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+		} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+         
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<Long> paymentOrderIdList = studentPaymentOrderList.stream().map(t -> t.getId()).collect(Collectors.toList()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<StudentPaymentOrderDetail> details = studentPaymentOrderDetailDao.getWithIds(paymentOrderIdList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Set<Integer> refundSellOrderGoodsIds = getRefundGoodsId(details.stream().map(t -> t.getPaymentOrderId()).collect(Collectors.toList())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         MusicGroup musicGroup = musicGroupDao.get(musicGroupId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         subjectChange.setStudentId(studentId); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -423,15 +449,14 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             BigDecimal price = accessoriesGoods.stream().map(Goods::getGroupPurchasePrice).reduce(BigDecimal.ZERO, BigDecimal::add); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             subjectChange.setOriginalAccessoriesPrice(price); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        Long orderId = details.get(0).getPaymentOrderId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //2.2 计算销售成本 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BigDecimal orderSellCost = sellOrderDao.getOrderSellCost(orderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BigDecimal orderSellCost = sellOrderDao.getOrderSellCost(paymentOrderIdList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         subjectChange.setOriginalCost(orderSellCost == null ? BigDecimal.ZERO : orderSellCost); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return subjectChange; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    private Set<Integer> getRefundGoodsId(Long orderId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        List<SellOrder> refundSellOrders = sellOrderService.getRefundSellOrder(orderId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Set<Integer> getRefundGoodsId(List<Long> orderIdList) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<SellOrder> refundSellOrders = sellOrderService.getRefundSellOrder(orderIdList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Set<Integer> refundSellOrderGoodsIds = refundSellOrders.stream().filter(e -> e.getParentGoodsId() == null).map(SellOrder::getGoodsId).collect(Collectors.toSet()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         Set<Integer> refundSellOrderParentGoodsId = refundSellOrders.stream().filter(e -> e.getParentGoodsId() != null).map(SellOrder::getParentGoodsId).collect(Collectors.toSet()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         refundSellOrderGoodsIds.addAll(refundSellOrderParentGoodsId); 
			 |