Sfoglia il codice sorgente

Merge branch 'saas' of http://git.dayaedu.com/yonge/mec into master_saas

zouxuan 3 anni fa
parent
commit
ff9fc7a240

+ 1 - 1
audio-analysis/src/main/java/com/yonge/netty/dto/UserChannelContext.java

@@ -38,7 +38,7 @@ public class UserChannelContext {
 	//打击乐
 	private final static List<Integer> percussionList = Arrays.asList(23, 113);
 	
-	private final static int MIN_FREQUECY = 100;
+	private final static int MIN_FREQUECY = 43;
 	
 	private final static int MAX_FREQUECY = 2000;
 	

+ 22 - 4
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/IndexBaseDto.java

@@ -7,9 +7,8 @@ import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
 import java.time.LocalDate;
-import java.util.Comparator;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @Author Joburgess
@@ -27,6 +26,24 @@ public class IndexBaseDto {
 
     private List<IndexBaseMonthData> indexMonthDataDetail;
 
+    private Map<Integer,BigDecimal> organIndexMonthData;
+
+    public Map<Integer,BigDecimal> getOrganIndexMonthData() {
+        return organIndexMonthData;
+    }
+
+    public void setOrganIndexMonthData(List<IndexBaseMonthData> organIndexMonthData) {
+        Map<Integer,BigDecimal> organData = new HashMap<>(20);
+        if (!CollectionUtils.isEmpty(organIndexMonthData)){
+            List<IndexBaseMonthData> collect = organIndexMonthData.stream().filter(e -> e.getOrganId() != 0).collect(Collectors.toList());
+            if(!CollectionUtils.isEmpty(collect)){
+                Map<Integer, List<IndexBaseMonthData>> listMap = collect.stream().collect(Collectors.groupingBy(IndexBaseMonthData::getOrganId));
+                listMap.keySet().stream().forEach(organId->organData.put(organId,listMap.get(organId).stream().map(e->e.getPercent()).reduce(BigDecimal.ZERO,BigDecimal::add)));
+            }
+        }
+        this.organIndexMonthData = organData;
+    }
+
     public List<IndexBaseMonthData> getIndexMonthDataDetail() {
         return indexMonthDataDetail;
     }
@@ -103,7 +120,8 @@ public class IndexBaseDto {
                     this.percent = total;
                 }
             }else{
-                this.percent = indexMonthData.stream().filter(i->currentMonth.compareTo(i.getMonth())>=0).max(Comparator.comparing(IndexBaseMonthData::getMonth)).get().getPercent();
+                this.percent = indexMonthData.stream().filter(i->currentMonth.compareTo(i.getMonth())>=0).
+                        max(Comparator.comparing(IndexBaseMonthData::getMonth)).get().getPercent();
             }
         }
     }

+ 10 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/StudentPaymentOrderDetail.java

@@ -65,6 +65,8 @@ public class StudentPaymentOrderDetail extends BaseEntity {
 	
 	private BigDecimal income = BigDecimal.ZERO;
 	
+	private BigDecimal balanceIncome = BigDecimal.ZERO;
+	
 	private String minuendStockGoodsIdList;
 
 	@ApiModelProperty(value = "子商品列表",required = false)
@@ -231,6 +233,14 @@ public class StudentPaymentOrderDetail extends BaseEntity {
 		this.income = income;
 	}
 
+	public BigDecimal getBalanceIncome() {
+		return balanceIncome;
+	}
+
+	public void setBalanceIncome(BigDecimal balanceIncome) {
+		this.balanceIncome = balanceIncome;
+	}
+
 	public String getMinuendStockGoodsIdList() {
 		return minuendStockGoodsIdList;
 	}

+ 1 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/IndexBaseMonthDataServiceImpl.java

@@ -299,6 +299,7 @@ public class IndexBaseMonthDataServiceImpl extends BaseServiceImpl<Long, IndexBa
 					VIP_AMOUNT.equals(typeDateMapEntry.getKey()) ||
 					PRACTICE_AMOUNT.equals(typeDateMapEntry.getKey()) ||
 					OTHER_AMOUNT.equals(typeDateMapEntry.getKey())){
+				indexBaseData.setOrganIndexMonthData(value);
 				indexBaseData.setPercent(value.stream().map(IndexBaseMonthData::getPercent).reduce(BigDecimal.ZERO, BigDecimal::add));
 			}
 			if(IndexDataType.VIP_GROUP_COURSE.equals(typeDateMapEntry.getKey()) || VIP_GROUP_ONLINE_COURSE.equals(typeDateMapEntry.getKey()) ||

+ 46 - 18
mec-biz/src/main/java/com/ym/mec/biz/service/impl/PayServiceImpl.java

@@ -242,13 +242,13 @@ public class PayServiceImpl implements PayService {
         								totalGroupPurchaseAmount =  totalGroupPurchaseAmount.add(goodsList.stream().map(Goods :: getGroupPurchasePrice).reduce(BigDecimal.ZERO,BigDecimal :: add));
         								
         								for(Goods subGoods : goodsList){
+    										
+    										if(batchUpdateGoodsMap.get(subGoods.getId()) != null){
+    											subGoods = batchUpdateGoodsMap.get(subGoods.getId());
+    										}
         									//判断是否有内部库存
         									if(subGoods.getStockCount() > 0){
         										
-        										if(batchUpdateGoodsMap.get(subGoods.getId()) != null){
-        											subGoods = batchUpdateGoodsMap.get(subGoods.getId());
-        										}
-        										
         										groupPurchaseAmount = groupPurchaseAmount.add(subGoods.getGroupPurchasePrice());
         										subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).decrementAndGet());
             									subGoods.setSellCount(new AtomicInteger(subGoods.getSellCount()).incrementAndGet());
@@ -258,7 +258,7 @@ public class PayServiceImpl implements PayService {
         										
         										minuendStockGoodsIdList.add(subGoods.getId());
         										
-        										goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
+        										goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(subGoods.getId());
         										if(goodsProcurement != null){
         											if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
         												goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
@@ -271,13 +271,14 @@ public class PayServiceImpl implements PayService {
         								}
         							}else{
     									totalGroupPurchaseAmount = totalGroupPurchaseAmount.add(goods.getGroupPurchasePrice());
+    									
+    									if(batchUpdateGoodsMap.get(goods.getId()) != null){
+    										goods = batchUpdateGoodsMap.get(goods.getId());
+										}
+    									
         								//判断是否有内部库存
         								if(goods.getStockCount() > 0){
         									
-        									if(batchUpdateGoodsMap.get(goods.getId()) != null){
-        										goods = batchUpdateGoodsMap.get(goods.getId());
-    										}
-        									
     										groupPurchaseAmount = groupPurchaseAmount.add(goods.getGroupPurchasePrice());
     										goods.setStockCount(new AtomicInteger(goods.getStockCount()).decrementAndGet());
     										goods.setSellCount(new AtomicInteger(goods.getSellCount()).incrementAndGet());
@@ -300,19 +301,27 @@ public class PayServiceImpl implements PayService {
         						}
         					}
         					
-							if (groupPurchaseAmount.doubleValue() > 0) {
+							if (groupPurchaseAmount.compareTo(BigDecimal.ZERO) > 0) {
 	        					// 3.是否使用余额
-								if (balanceAmount.doubleValue() > 0) {
+								if (balanceAmount.compareTo(BigDecimal.ZERO) > 0) {
 									/*tempBalance = balanceAmount.multiply(spod.getPrice()).divide(totalAmout).multiply(groupPurchaseAmount)
 											.divide(totalGroupPurchaseAmount);*/
 									tempBalance = balanceAmount.multiply(spod.getPrice()).multiply(groupPurchaseAmount)
-											.divide(totalGroupPurchaseAmount.multiply(totalAmout), RoundingMode.HALF_UP);
+											.divide(totalGroupPurchaseAmount.multiply(totalAmout), BigDecimal.ROUND_UP).setScale(2, BigDecimal.ROUND_UP);
+									
+									if(subBalance.add(tempBalance).compareTo(balanceAmount) > 0){
+										tempBalance = balanceAmount.subtract(subBalance);
+									}
 									subBalance = subBalance.add(tempBalance);
 								}
-								tempCashAmount = spod.getPrice().subtract(tempBalance).multiply(groupPurchaseAmount).divide(totalGroupPurchaseAmount, RoundingMode.HALF_UP);
+								tempCashAmount = spod.getPrice().subtract(tempBalance).multiply(groupPurchaseAmount).divide(totalGroupPurchaseAmount, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN);
+								if(subCashAmount.add(tempCashAmount).compareTo(amount) > 0){
+									tempCashAmount = amount.subtract(subCashAmount);
+								}
 								subCashAmount = subCashAmount.add(tempCashAmount);
 								spod.setIncomeItem(OrderDetailTypeEnum.CLOUD_TEACHER.name());
 								spod.setIncome(tempCashAmount);
+								spod.setBalanceIncome(tempBalance);
 								if(minuendStockGoodsIdList.size() > 0){
 									spod.setMinuendStockGoodsIdList(minuendStockGoodsIdList.stream().map(t -> t.toString()).collect(Collectors.joining(",")));
 								}
@@ -334,7 +343,7 @@ public class PayServiceImpl implements PayService {
         			goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
         		}
         		
-        		if(subCashAmount.doubleValue() > 0){
+        		if(subCashAmount.compareTo(BigDecimal.ZERO) > 0){
         			StudentPaymentRouteOrder studentPaymentRouteOrder = new StudentPaymentRouteOrder();
     	            studentPaymentRouteOrder.setOrderNo(orderNo);
     	            studentPaymentRouteOrder.setRouteOrganId(sysPaymentConfig.getOrganId());
@@ -347,7 +356,6 @@ public class PayServiceImpl implements PayService {
     	            studentPaymentRouteOrder.setCreateTime(date);
     	            studentPaymentRouteOrder.setUpdateTime(date);
     	            studentPaymentRouteOrderDao.insert(studentPaymentRouteOrder);
-    	            
 
     	            routeScaleDtoForGoodsSell = new RouteScaleDto();
     	            routeScaleDtoForGoodsSell.setAmount(subCashAmount);
@@ -360,8 +368,16 @@ public class PayServiceImpl implements PayService {
         	}
         }
 		
-		amount = amount.subtract(subCashAmount);
-		balanceAmount = balanceAmount.subtract(subBalance);
+        if(amount.compareTo(subCashAmount) <= 0){
+        	amount = BigDecimal.ZERO;
+        }else{
+        	amount = amount.subtract(subCashAmount);
+        }
+        if(balanceAmount.compareTo(subBalance) <= 0){
+        	balanceAmount = BigDecimal.ZERO;
+        }else{
+        	balanceAmount = balanceAmount.subtract(subBalance);
+        }
         
         String usePaymentConfig = sysConfigDao.findConfigValue("use_payment_config");// 是否用收费配置(1:使用 0:不使用)
         List<RouteScaleDto> routeScaleDtos = null;
@@ -396,8 +412,20 @@ public class PayServiceImpl implements PayService {
         if(routeScaleDtoForGoodsSell != null){
         	routeScaleDtos.add(routeScaleDtoForGoodsSell);
         }
+        
+        Map<String, Object> map = getPayRoute(amount, balanceAmount, orderNo, notifyUrl, returnUrl, orderSubject, orderBody, routeScaleDtos, subCashAmount);
+        
+		if (routeScaleDtoForGoodsSell != null) {
+			String routingMerNos = (String) map.get("routingMerNos");
+			List<String> list = new ArrayList<String>(Arrays.asList(routingMerNos.split(",")));
+			if(!list.contains(routeScaleDtoForGoodsSell.getMerNo())){
+				list.add(routeScaleDtoForGoodsSell.getMerNo());
+			}
 
-        return getPayRoute(amount, balanceAmount, orderNo, notifyUrl, returnUrl, orderSubject, orderBody, routeScaleDtos, subCashAmount);
+	        map.put("routingMerNos", String.join(",", list));
+		}
+        
+        return map;
     }
 
     @Override

+ 20 - 7
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SellOrderServiceImpl.java

@@ -485,8 +485,10 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
         
         List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(order.getId());
         BigDecimal cloudIncome = BigDecimal.ZERO;
+        BigDecimal cloudBalanceIncome = BigDecimal.ZERO;
         if(studentPaymentOrderDetailList != null) {
         	cloudIncome = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNoneBlank(t.getMinuendStockGoodsIdList()) && StringUtils.equals(t.getIncomeItem(), OrderDetailTypeEnum.CLOUD_TEACHER.name())).map(t -> t.getIncome()).reduce(BigDecimal.ZERO, BigDecimal::add);
+        	cloudBalanceIncome = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNoneBlank(t.getMinuendStockGoodsIdList()) && StringUtils.equals(t.getIncomeItem(), OrderDetailTypeEnum.CLOUD_TEACHER.name())).map(t -> t.getBalanceIncome()).reduce(BigDecimal.ZERO, BigDecimal::add);
         }
 
         //零星支付除了充值其他都是服务费用
@@ -500,19 +502,19 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
         //乐器销售,声部更改
         if (order.getType().equals(OrderTypeEnum.GOODS_SELL) || order.getType().equals(OrderTypeEnum.SUBJECT_CHANGE)) {
             sellAmount.put("actualAmount", order.getActualAmount().subtract(cloudIncome));
-            sellAmount.put("balance", order.getBalancePaymentAmount() == null ? BigDecimal.ZERO : order.getBalancePaymentAmount());
+            sellAmount.put("balance", order.getBalancePaymentAmount() == null ? BigDecimal.ZERO : order.getBalancePaymentAmount().subtract(cloudBalanceIncome));
             return sellAmount;
         }
         //乐器置换
         if (order.getType().equals(OrderTypeEnum.REPLACEMENT)) {
             sellAmount.put("actualAmount", order.getActualAmount().subtract(cloudIncome));
-            sellAmount.put("balance", order.getBalancePaymentAmount() == null ? BigDecimal.ZERO : order.getBalancePaymentAmount());
+            sellAmount.put("balance", order.getBalancePaymentAmount() == null ? BigDecimal.ZERO : order.getBalancePaymentAmount().subtract(cloudBalanceIncome));
             return sellAmount;
         }
 
         //乐器维修
         if (order.getType().equals(OrderTypeEnum.REPAIR)) {
-            StudentRepair repairInfo = studentRepairDao.getRepairInfo(Integer.parseInt(order.getMusicGroupId()));
+            /*StudentRepair repairInfo = studentRepairDao.getRepairInfo(Integer.parseInt(order.getMusicGroupId()));
 
             if (StringUtils.isBlank(repairInfo.getGoodsJson())) {
                 sellAmount.put("actualAmount", BigDecimal.ZERO);
@@ -550,7 +552,10 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
             BigDecimal goodsTotalBalance = goodsTotalPrice.multiply(balance).divide(totalAmount, 2, BigDecimal.ROUND_DOWN);
             BigDecimal goodsTotalActualAmount = goodsTotalPrice.multiply(order.getActualAmount()).divide(totalAmount, 2, BigDecimal.ROUND_DOWN);
             sellAmount.put("actualAmount", goodsTotalActualAmount.subtract(cloudIncome));
-            sellAmount.put("balance", goodsTotalBalance);
+            sellAmount.put("balance", goodsTotalBalance.subtract(cloudBalanceIncome));*/
+        	
+            sellAmount.put("actualAmount", order.getActualAmount().subtract(cloudIncome));
+            sellAmount.put("balance", order.getBalancePaymentAmount() == null ? BigDecimal.ZERO : order.getBalancePaymentAmount().subtract(cloudBalanceIncome));
             return sellAmount;
         }
 
@@ -565,9 +570,17 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
             BigDecimal detailTotalPrice = orderDetails.stream().map(StudentPaymentOrderDetail::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
             //商品销售占的余额
             BigDecimal detailTotalBalance = detailTotalPrice.multiply(totalBalance).divide(totalPrice, 2, BigDecimal.ROUND_DOWN);
-
-            sellAmount.put("actualAmount", detailTotalPrice.subtract(detailTotalBalance).subtract(cloudIncome));
-            sellAmount.put("balance", detailTotalBalance);
+            
+            if(detailTotalBalance.subtract(cloudBalanceIncome).compareTo(BigDecimal.ZERO) < 0) {
+            	detailTotalBalance = cloudBalanceIncome;
+            }
+            
+            if(detailTotalPrice.subtract(detailTotalBalance).subtract(cloudIncome).compareTo(BigDecimal.ZERO) < 0){
+            	sellAmount.put("actualAmount", BigDecimal.ZERO);
+            }else{
+            	sellAmount.put("actualAmount", detailTotalPrice.subtract(detailTotalBalance).subtract(cloudIncome));
+            }
+            sellAmount.put("balance", detailTotalBalance.subtract(cloudBalanceIncome));
             return sellAmount;
         }
 

+ 56 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentPaymentOrderServiceImpl.java

@@ -30,6 +30,7 @@ import com.ym.mec.thirdparty.adapay.Payment;
 import com.ym.mec.thirdparty.yqpay.*;
 import com.ym.mec.util.collection.MapUtil;
 import com.ym.mec.util.date.DateUtil;
+
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
@@ -43,6 +44,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
 @Service
@@ -84,6 +86,8 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
     @Autowired
     private GoodsDao goodsDao;
     @Autowired
+    private GoodsService goodsService;
+    @Autowired
     private StudentInstrumentService studentInstrumentService;
     @Autowired
     private ReplacementInstrumentActivityService replacementInstrumentActivityService;
@@ -109,6 +113,8 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
     private SysUserFeignService sysUserFeignService;
     @Autowired
     private TenantConfigService tenantConfigService;
+    @Autowired
+    private GoodsProcurementDao goodsProcurementDao;
 
     @Override
     public BaseDAO<Long, StudentPaymentOrder> getDAO() {
@@ -793,6 +799,56 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
         //处理关闭订单
         order.setStatus(DealStatusEnum.CLOSE);
         order.setMemo("关闭订单");
+
+        
+        Date nowDate = new Date();
+        //增加商品库存
+        List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(order.getId());
+        String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+        if(StringUtils.isNotBlank(goodsIds)){
+        	GoodsProcurement goodsProcurement = null;
+        	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+        	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+        	Goods goods = null;
+			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+        	
+        	for(String goodsIdStr : goodsIds.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				if(batchUpdateGoodsMap.get(goods.getId()) != null){
+					goods = batchUpdateGoodsMap.get(goods.getId());
+				}
+				goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+				goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+				goods.setUpdateTime(nowDate);
+				
+				batchUpdateGoodsMap.put(goods.getId(), goods);
+				
+				// 进货清单
+				goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
+				if(goodsProcurement != null){
+					if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+						goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+					}
+					goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
+					goodsProcurement.setUpdateTime(nowDate);
+					goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
+				}
+				
+        	}
+        	
+        	if(goodsProcurementMap.size() > 0){
+        		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
+        	}
+            
+            if(batchUpdateGoodsMap.size() > 0){
+            	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+            }
+        }
+        
         //如果是报名订单,订单关闭时需要更改已缴费学员数
         cutSubjectPlan(order);
         if (order.getBalancePaymentAmount() != null && order.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {

+ 30 - 15
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRegistrationServiceImpl.java

@@ -1379,36 +1379,51 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
             //减去缴费人数(器乐收费,0元时不减缴费人数)
             studentPaymentOrderService.cutSubjectPlan(studentPaymentOrder);
             
-			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
             //增加商品库存
             List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
             String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
             if(StringUtils.isNotBlank(goodsIds)){
             	GoodsProcurement goodsProcurement = null;
-            	List<GoodsProcurement> goodsProcurementList = new ArrayList<GoodsProcurement>();
             	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
-            	for(Goods subGoods : goodsList){
-					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
-					subGoods.setSellCount(new AtomicInteger(subGoods.getSellCount()).decrementAndGet());
-					subGoods.setUpdateTime(nowDate);
-					batchUpdateGoodsList.add(subGoods);
+            	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+            	Goods goods = null;
+    			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+    			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+            	
+            	for(String goodsIdStr : goodsIds.split(",")){
+            		if(StringUtils.isBlank(goodsIdStr)){
+						continue;
+					}
+					goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+					if(batchUpdateGoodsMap.get(goods.getId()) != null){
+						goods = batchUpdateGoodsMap.get(goods.getId());
+					}
+					goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+					goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+					goods.setUpdateTime(nowDate);
+					
+					batchUpdateGoodsMap.put(goods.getId(), goods);
 					
 					// 进货清单
-					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(subGoods.getId());
+					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
 					if(goodsProcurement != null){
+						if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+							goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+						}
 						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
 						goodsProcurement.setUpdateTime(nowDate);
-						goodsProcurementList.add(goodsProcurement);
+						goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
 					}
+					
             	}
             	
-            	if(goodsProcurementList.size() > 0){
-            		goodsProcurementDao.batchUpdate(goodsProcurementList);
+            	if(goodsProcurementMap.size() > 0){
+            		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
             	}
-            }
-            
-            if(batchUpdateGoodsList.size() > 0){
-            	goodsService.batchUpdate(batchUpdateGoodsList);
+                
+                if(batchUpdateGoodsMap.size() > 0){
+                	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+                }
             }
 
             //退还优惠券和余额

+ 295 - 90
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRepairServiceImpl.java

@@ -207,6 +207,54 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                 }
                 //退优惠券
                 sysCouponCodeService.quit(orderByOrderNo.getCouponCodeId());
+                
+                Date nowDate = new Date();
+                //增加商品库存
+                List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(orderByOrderNo.getId());
+                String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+                if(StringUtils.isNotBlank(goodsIds)){
+                	GoodsProcurement goodsProcurement = null;
+                	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+                	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+                	Goods goods = null;
+        			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+        			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+                	
+                	for(String goodsIdStr : goodsIds.split(",")){
+                		if(StringUtils.isBlank(goodsIdStr)){
+							continue;
+						}
+						goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+						if(batchUpdateGoodsMap.get(goods.getId()) != null){
+							goods = batchUpdateGoodsMap.get(goods.getId());
+						}
+						goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+						goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+						goods.setUpdateTime(nowDate);
+    					
+						batchUpdateGoodsMap.put(goods.getId(), goods);
+    					
+    					// 进货清单
+    					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
+    					if(goodsProcurement != null){
+							if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+								goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+							}
+    						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
+    						goodsProcurement.setUpdateTime(nowDate);
+							goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
+    					}
+						
+                	}
+                	
+                	if(goodsProcurementMap.size() > 0){
+                		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
+                	}
+                    
+                    if(batchUpdateGoodsMap.size() > 0){
+                    	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+                    }
+                }
             }
             StudentGoodsSell byOrderNo = studentGoodsSellDao.findByOrderNo(studentGoodsSell.getOrderNo());
             if (byOrderNo != null) {
@@ -257,11 +305,11 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             goodsSellDto.setGoodsGroupPrice(groupPriceMap.get(goodsSellDto.getGoodsId()));
             goodsSellDto.setTotalGoodsPrice(map.get(goodsSellDto.getGoodsId()).multiply(new BigDecimal(goodsSellDto.getGoodsNum())));
         }
-        Map<Integer, List<GoodsSellDto>> goodsMap = goodsSellDtos.stream().collect(Collectors.groupingBy(GoodsSellDto::getGoodsId));
+        Map<Integer, List<GoodsSellDto>> goodsSellDtoMap = goodsSellDtos.stream().collect(Collectors.groupingBy(GoodsSellDto::getGoodsId));
         BigDecimal amount = BigDecimal.ZERO;
         BigDecimal groupAmount = BigDecimal.ZERO;
         for (Integer id : goodsIds) {
-            GoodsSellDto goodsSellDto = goodsMap.get(id).get(0);
+            GoodsSellDto goodsSellDto = goodsSellDtoMap.get(id).get(0);
             amount = amount.add(goodsSellDto.getTotalGoodsPrice());
             groupAmount = groupAmount.add(goodsSellDto.getGoodsGroupPrice());
         }
@@ -320,35 +368,59 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         BigDecimal totalPrice = BigDecimal.ZERO;
         
         if(goodsSellDtos != null){
-        	String goodsIdsStr = goodsSellDtos.stream().map(t -> t.getGoodsId().toString()).collect(Collectors.joining(","));
+        	//String goodsIdsStr = goodsSellDtos.stream().map(t -> t.getGoodsId().toString()).collect(Collectors.joining(","));
+        	
+        	StringBuffer sb = new StringBuffer();
+        	for(GoodsSellDto goodsSellDto : goodsSellDtos){
+        		for(int i = 0; i < goodsSellDto.getGoodsNum() ; i++){
+        			sb.append(goodsSellDto.getGoodsId()).append(",");
+        		}
+        	}
+        	
+        	String goodsIdsStr = StringUtils.removeEnd(sb.toString(), ",");
+        	
         	List<Goods> goodsList = goodsService.findGoodsByIds(goodsIdsStr);
-        	BigDecimal goodsPrice = goodsList.stream().map(t -> t.getGroupPurchasePrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
-        	for(Goods g : goodsList){
-        		studentPaymentOrderDetail = new StudentPaymentOrderDetail();
-                studentPaymentOrderDetail.setCreateTime(date);
-                if(g.getType() == GoodsType.INSTRUMENT){
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
-                }else if(g.getType() == GoodsType.ACCESSORIES){
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
-                }else if(g.getType() == GoodsType.TEACHING || g.getType() == GoodsType.STAFF){
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
-                }else{
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
-                }
-                studentPaymentOrderDetail.setGoodsIdList(g.getId() + "");
-                
-                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().multiply(g.getGroupPurchasePrice()).divide(goodsPrice, RoundingMode.UP);
-                
-                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0){
-                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
-                }else{
-                	studentPaymentOrderDetail.setPrice(tempPrice);
-                }
-                studentPaymentOrderDetail.setUpdateTime(date);
-                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
-                
-                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
-                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+        	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+        	BigDecimal totalGroupPurchasePrice = BigDecimal.ZERO;
+        	
+        	for(String goodsIdStr : goodsIdsStr.split(",")){
+        		totalGroupPurchasePrice = totalGroupPurchasePrice.add(goodsMap.get(Integer.parseInt(goodsIdStr)).getGroupPurchasePrice());
+        	}
+        	
+        	Goods goods = null;
+        	for(String goodsIdStr : goodsIdsStr.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				
+				if(goods != null){
+					studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+	                studentPaymentOrderDetail.setCreateTime(date);
+	                if(goods.getType() == GoodsType.INSTRUMENT){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
+	                }else if(goods.getType() == GoodsType.ACCESSORIES){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
+	                }else if(goods.getType() == GoodsType.TEACHING || goods.getType() == GoodsType.STAFF){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
+	                }else{
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
+	                }
+	                studentPaymentOrderDetail.setGoodsIdList(goodsIdStr);
+	                
+	                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().multiply(goods.getGroupPurchasePrice()).divide(totalGroupPurchasePrice, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN);
+	                
+	                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0 || goodsIdsStr.split(",").length == studentPaymentOrderDetailList.size() + 1){
+	                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
+	                }else{
+	                	studentPaymentOrderDetail.setPrice(tempPrice);
+	                }
+	                studentPaymentOrderDetail.setUpdateTime(date);
+	                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+	                
+	                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
+	                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+				}
         	}
         }
         
@@ -560,7 +632,6 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         repairInfo.setUpdateTime(date);
         
         List<RepairGoodsDto> repairGoodsDtos = null;
-        String goodIdList = null;
         		
         String goodsJson = repairInfo.getGoodsJson();
         if (StringUtils.isNotEmpty(goodsJson)) {
@@ -571,7 +642,6 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                 e.setGroupPurchasePrice(map.get(e.getId()));
             });
             repairInfo.setGoodsJson(JSONObject.toJSONString(repairGoodsDtos));
-            goodIdList = repairGoodsDtos.stream().map(t -> t.getId().toString()).collect(Collectors.joining(","));
         }
 
         BigDecimal goodsPrice = BigDecimal.ZERO;
@@ -639,32 +709,52 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         if(StringUtils.isNotBlank(repairInfo.getGoodsJson()) && repairGoodsDtos != null){
         	String goodsIds = repairGoodsDtos.stream().map(t -> t.getId().toString()).collect(Collectors.joining(","));
         	List<Goods> goodsList = goodsService.findGoodsByIds(goodsIds);
-        	for(Goods g : goodsList){
-        		studentPaymentOrderDetail = new StudentPaymentOrderDetail();
-                studentPaymentOrderDetail.setCreateTime(date);
-                if(g.getType() == GoodsType.INSTRUMENT){
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
-                }else if(g.getType() == GoodsType.ACCESSORIES){
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
-                }else if(g.getType() == GoodsType.TEACHING || g.getType() == GoodsType.STAFF){
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
-                }else{
-                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
-                }
-                studentPaymentOrderDetail.setGoodsIdList(g.getId() + "");
-                
-                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().subtract(repairPrice).multiply(g.getGroupPurchasePrice()).divide(goodsPrice, RoundingMode.UP);
-                
-                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0){
-                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
-                }else{
-                	studentPaymentOrderDetail.setPrice(tempPrice);
-                }
-                studentPaymentOrderDetail.setUpdateTime(date);
-                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
-                
-                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
-                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+        	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+        	Goods goods = null;
+        	BigDecimal totalGroupPurchasePrice = BigDecimal.ZERO;
+        	for(String goodsIdStr : goodsIds.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				
+				if(goods != null){
+					totalGroupPurchasePrice = totalGroupPurchasePrice.add(goods.getGroupPurchasePrice());
+				}
+        	}
+        	for(String goodsIdStr : goodsIds.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				
+				if(goods != null){
+					studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+	                studentPaymentOrderDetail.setCreateTime(date);
+	                if(goods.getType() == GoodsType.INSTRUMENT){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
+	                }else if(goods.getType() == GoodsType.ACCESSORIES){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
+	                }else if(goods.getType() == GoodsType.TEACHING || goods.getType() == GoodsType.STAFF){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
+	                }else{
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
+	                }
+	                studentPaymentOrderDetail.setGoodsIdList(goodsIdStr);
+	                
+	                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().subtract(repairPrice).multiply(goods.getGroupPurchasePrice()).divide(totalGroupPurchasePrice, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN);
+	                
+	                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0 || goodsIds.split(",").length == studentPaymentOrderDetailList.size()){
+	                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
+	                }else{
+	                	studentPaymentOrderDetail.setPrice(tempPrice);
+	                }
+	                studentPaymentOrderDetail.setUpdateTime(date);
+	                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+	                
+	                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
+	                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+				}
         	}
         }
         
@@ -791,18 +881,32 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         ) {
             throw new BizException("邮寄信息必填");
         }
+        
+        List<RepairGoodsDto> repairGoodsDtos = null;
         Date date = new Date();
         StudentRepair studentRepair = studentRepairDao.get(repairInfo.getId());
         studentDao.lockUser(studentRepair.getStudentId());
         BigDecimal amount = studentRepair.getAmount();
         String goodsJson = studentRepair.getGoodsJson();
         if (StringUtils.isNotEmpty(goodsJson)) {
-            List<RepairGoodsDto> repairGoodsDtos = JSONObject.parseArray(goodsJson, RepairGoodsDto.class);
+            repairGoodsDtos = JSONObject.parseArray(goodsJson, RepairGoodsDto.class);
             BigDecimal reduce = repairGoodsDtos.stream().map(e -> e.getGroupPurchasePrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
             amount = amount.add(reduce);
         }
         amount = amount.subtract(studentRepair.getExemptionAmount());
 
+        BigDecimal goodsPrice = BigDecimal.ZERO;
+        if (StringUtils.isNoneBlank(goodsJson)) {
+            JSONArray goods = JSON.parseArray(goodsJson);
+            for (Object good : goods) {
+                JSONObject goodObject = (JSONObject) good;
+                BigDecimal groupPurchasePrice = goodObject.getBigDecimal("groupPurchasePrice");
+                if (Objects.nonNull(groupPurchasePrice)) {
+                	goodsPrice = goodsPrice.add(groupPurchasePrice);
+                }
+            }
+        }
+
         String orderNo = idGeneratorService.generatorId("payment") + "";
         studentRepair.setUseBalancePayment(repairInfo.getUseBalancePayment());
         studentRepair.setTransNo(orderNo);
@@ -835,6 +939,77 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 
         studentPaymentOrderService.insert(studentPaymentOrder);
         studentPaymentOrder.setVersion(0);
+        
+        List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = new ArrayList<StudentPaymentOrderDetail>();
+        // 添加studentPaymentOrderDetail
+        StudentPaymentOrderDetail studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+        studentPaymentOrderDetail.setCreateTime(date);
+        studentPaymentOrderDetail.setType(OrderDetailTypeEnum.REPAIR);
+        studentPaymentOrderDetail.setGoodsIdList(null);
+        studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().multiply(studentRepair.getAmount()).divide(goodsPrice.add(studentRepair.getAmount()), RoundingMode.UP));
+        studentPaymentOrderDetail.setUpdateTime(date);
+        studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+        
+        studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+
+        BigDecimal totalPrice = studentPaymentOrderDetail.getPrice();
+        BigDecimal repairPrice = studentPaymentOrderDetail.getPrice();
+        
+        if(StringUtils.isNotBlank(studentRepair.getGoodsJson()) && repairGoodsDtos != null){
+        	String goodsIds = repairGoodsDtos.stream().map(t -> t.getId().toString()).collect(Collectors.joining(","));
+        	List<Goods> goodsList = goodsService.findGoodsByIds(goodsIds);
+        	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+        	Goods goods = null;
+        	BigDecimal totalGroupPurchasePrice = BigDecimal.ZERO;
+        	for(String goodsIdStr : goodsIds.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				
+				if(goods != null){
+					totalGroupPurchasePrice = totalGroupPurchasePrice.add(goods.getGroupPurchasePrice());
+				}
+        	}
+        	for(String goodsIdStr : goodsIds.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				
+				if(goods != null){
+					studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+	                studentPaymentOrderDetail.setCreateTime(date);
+	                if(goods.getType() == GoodsType.INSTRUMENT){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
+	                }else if(goods.getType() == GoodsType.ACCESSORIES){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
+	                }else if(goods.getType() == GoodsType.TEACHING || goods.getType() == GoodsType.STAFF){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
+	                }else{
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
+	                }
+	                studentPaymentOrderDetail.setGoodsIdList(goodsIdStr);
+	                
+	                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().subtract(repairPrice).multiply(goods.getGroupPurchasePrice()).divide(totalGroupPurchasePrice, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN);
+	                
+	                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0 || goodsIds.split(",").length == studentPaymentOrderDetailList.size()){
+	                	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
+	                }else{
+	                	studentPaymentOrderDetail.setPrice(tempPrice);
+	                }
+	                studentPaymentOrderDetail.setUpdateTime(date);
+	                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+	                
+	                totalPrice = totalPrice.add(studentPaymentOrderDetail.getPrice());
+	                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+				}
+        	}
+        }
+        
+        if(studentPaymentOrderDetailList.size() > 0){
+        	studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);
+        }
 
         BigDecimal balance = BigDecimal.ZERO;
         if (studentRepair.getUseBalancePayment() && amount.compareTo(BigDecimal.ZERO) > 0) {
@@ -957,36 +1132,51 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             saveSellOrder(studentPaymentOrder.getOrderNo());
         } else if (studentPaymentOrder.getStatus() == DealStatusEnum.CLOSE || studentPaymentOrder.getStatus() == DealStatusEnum.FAILED) {
             
-			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
-            //增加商品库存
+        	//增加商品库存
             List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
             String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
             if(StringUtils.isNotBlank(goodsIds)){
             	GoodsProcurement goodsProcurement = null;
-            	List<GoodsProcurement> goodsProcurementList = new ArrayList<GoodsProcurement>();
             	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
-            	for(Goods subGoods : goodsList){
-					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
-					subGoods.setSellCount(new AtomicInteger(subGoods.getSellCount()).decrementAndGet());
-					subGoods.setUpdateTime(nowDate);
-					batchUpdateGoodsList.add(subGoods);
+            	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+            	Goods goods = null;
+    			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+    			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+            	
+            	for(String goodsIdStr : goodsIds.split(",")){
+            		if(StringUtils.isBlank(goodsIdStr)){
+						continue;
+					}
+					goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+					if(batchUpdateGoodsMap.get(goods.getId()) != null){
+						goods = batchUpdateGoodsMap.get(goods.getId());
+					}
+					goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+					goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+					goods.setUpdateTime(nowDate);
+					
+					batchUpdateGoodsMap.put(goods.getId(), goods);
 					
 					// 进货清单
-					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(subGoods.getId());
+					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
 					if(goodsProcurement != null){
+						if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+							goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+						}
 						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
 						goodsProcurement.setUpdateTime(nowDate);
-						goodsProcurementList.add(goodsProcurement);
+						goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
 					}
+					
             	}
             	
-            	if(goodsProcurementList.size() > 0){
-            		goodsProcurementDao.batchUpdate(goodsProcurementList);
+            	if(goodsProcurementMap.size() > 0){
+            		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
             	}
-            }
-            
-            if(batchUpdateGoodsList.size() > 0){
-            	goodsService.batchUpdate(batchUpdateGoodsList);
+                
+                if(batchUpdateGoodsMap.size() > 0){
+                	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+                }
             }
         	
         	if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
@@ -1369,36 +1559,51 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                 throw new BizException("维修单更新失败");
             }
             
-			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
             //增加商品库存
             List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
             String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
             if(StringUtils.isNotBlank(goodsIds)){
             	GoodsProcurement goodsProcurement = null;
-            	List<GoodsProcurement> goodsProcurementList = new ArrayList<GoodsProcurement>();
             	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
-            	for(Goods subGoods : goodsList){
-					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
-					subGoods.setSellCount(new AtomicInteger(subGoods.getSellCount()).decrementAndGet());
-					subGoods.setUpdateTime(nowDate);
-					batchUpdateGoodsList.add(subGoods);
+            	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+            	Goods goods = null;
+    			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+    			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+            	
+            	for(String goodsIdStr : goodsIds.split(",")){
+            		if(StringUtils.isBlank(goodsIdStr)){
+						continue;
+					}
+					goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+					if(batchUpdateGoodsMap.get(goods.getId()) != null){
+						goods = batchUpdateGoodsMap.get(goods.getId());
+					}
+					goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+					goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+					goods.setUpdateTime(nowDate);
+					
+					batchUpdateGoodsMap.put(goods.getId(), goods);
 					
 					// 进货清单
-					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(subGoods.getId());
+					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
 					if(goodsProcurement != null){
+						if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+							goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+						}
 						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
 						goodsProcurement.setUpdateTime(nowDate);
-						goodsProcurementList.add(goodsProcurement);
+						goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
 					}
+					
             	}
             	
-            	if(goodsProcurementList.size() > 0){
-            		goodsProcurementDao.batchUpdate(goodsProcurementList);
+            	if(goodsProcurementMap.size() > 0){
+            		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
             	}
-            }
-            
-            if(batchUpdateGoodsList.size() > 0){
-            	goodsService.batchUpdate(batchUpdateGoodsList);
+                
+                if(batchUpdateGoodsMap.size() > 0){
+                	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+                }
             }
             
             if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {

+ 315 - 76
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SubjectChangeServiceImpl.java

@@ -4,7 +4,6 @@ import static com.ym.mec.biz.dal.enums.DealStatusEnum.SUCCESS;
 import static com.ym.mec.biz.dal.enums.GroupType.SUBJECT_CHANGE;
 
 import java.math.BigDecimal;
-import java.math.RoundingMode;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
@@ -16,7 +15,6 @@ import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
-import org.apache.commons.collections.ListUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -215,6 +213,52 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                 studentPaymentOrder.setUpdateTime(date);
                 studentPaymentOrderService.update(studentPaymentOrder);
                 subjectChange.setStatus(SubjectChangeStatusEnum.WAIT_PAY);
+                //增加商品库存
+                List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
+                String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+                if(StringUtils.isNotBlank(goodsIds)){
+                	GoodsProcurement goodsProcurement = null;
+                	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+                	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+                	Goods goods = null;
+        			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+        			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+                	
+                	for(String goodsIdStr : goodsIds.split(",")){
+                		if(StringUtils.isBlank(goodsIdStr)){
+        					continue;
+        				}
+        				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+        				if(batchUpdateGoodsMap.get(goods.getId()) != null){
+        					goods = batchUpdateGoodsMap.get(goods.getId());
+        				}
+        				goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+        				goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+        				goods.setUpdateTime(date);
+        				
+        				batchUpdateGoodsMap.put(goods.getId(), goods);
+        				
+        				// 进货清单
+        				goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
+        				if(goodsProcurement != null){
+        					if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+        						goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+        					}
+        					goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
+        					goodsProcurement.setUpdateTime(date);
+        					goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
+        				}
+        				
+                	}
+                	
+                	if(goodsProcurementMap.size() > 0){
+                		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
+                	}
+                    
+                    if(batchUpdateGoodsMap.size() > 0){
+                    	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+                    }
+                }
             }
         }
 
@@ -300,44 +344,55 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
 
         BigDecimal totalPrice = BigDecimal.ZERO;
         
-        List<String> changeAccessoriesList = null;
+        List<String> changeGoodsList = new ArrayList<String>();
         
         if(StringUtils.isNotBlank(subjectChange.getChangeAccessories())){
-        	changeAccessoriesList = Arrays.asList(subjectChange.getChangeAccessories().split(","));
-        	
-        	List<String> originalAccessories = new ArrayList<String>();
-        	if(StringUtils.isNotBlank(subjectChange.getOriginalAccessories())){
-        		originalAccessories = Arrays.asList(subjectChange.getOriginalAccessories().split(","));
+        	for(String id : subjectChange.getChangeAccessories().split(",")){
+        		changeGoodsList.add(id);
         	}
-        	//changeAccessoriesList.removeAll(originalAccessories);
-        	changeAccessoriesList = ListUtils.subtract(changeAccessoriesList, originalAccessories);
         	
         	if(subjectChange.getChangeMusical() != subjectChange.getOriginalMusical()){
-        		changeAccessoriesList.add(subjectChange.getChangeMusical()+"");
+        		changeGoodsList.add(subjectChange.getChangeMusical()+"");
         	}
         }
         
-        if(changeAccessoriesList != null){
-            String goodsIdsStr = changeAccessoriesList.stream().map(t -> t.toString()).collect(Collectors.joining(","));
+        if(changeGoodsList != null){
+            String goodsIdsStr = changeGoodsList.stream().map(t -> t.toString()).collect(Collectors.joining(","));
         	List<Goods> goodsList = goodsService.findGoodsByIds(goodsIdsStr);
-        	BigDecimal goodsPrice = goodsList.stream().map(t -> t.getGroupPurchasePrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
-        	for(Goods g : goodsList){
-        		studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+        	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+        	Goods goods = null;
+        	BigDecimal goodsTotalGroupPurchasePrice = BigDecimal.ZERO;
+        	
+        	for(String goodsIdStr : goodsIdsStr.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				goodsTotalGroupPurchasePrice = goodsTotalGroupPurchasePrice.add(goods.getGroupPurchasePrice());
+        	}
+        	
+        	for(String goodsIdStr : goodsIdsStr.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				
+				studentPaymentOrderDetail = new StudentPaymentOrderDetail();
                 studentPaymentOrderDetail.setCreateTime(date);
-                if(g.getType() == GoodsType.INSTRUMENT){
+                if(goods.getType() == GoodsType.INSTRUMENT){
                 	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
-                }else if(g.getType() == GoodsType.ACCESSORIES){
+                }else if(goods.getType() == GoodsType.ACCESSORIES){
                 	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
-                }else if(g.getType() == GoodsType.TEACHING || g.getType() == GoodsType.STAFF){
+                }else if(goods.getType() == GoodsType.TEACHING || goods.getType() == GoodsType.STAFF){
                 	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
                 }else{
                 	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
                 }
-                studentPaymentOrderDetail.setGoodsIdList(g.getId() + "");
+                studentPaymentOrderDetail.setGoodsIdList(goods.getId() + "");
                 
-                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().multiply(g.getGroupPurchasePrice()).divide(goodsPrice, RoundingMode.UP);
+                BigDecimal tempPrice = studentPaymentOrder.getExpectAmount().multiply(goods.getGroupPurchasePrice()).divide(goodsTotalGroupPurchasePrice, BigDecimal.ROUND_DOWN).setScale(2, BigDecimal.ROUND_DOWN);
                 
-                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0){
+                if(totalPrice.add(tempPrice).compareTo(studentPaymentOrder.getExpectAmount()) > 0 || goodsIdsStr.split(",").length == studentPaymentOrderDetailList.size() + 1){
                 	studentPaymentOrderDetail.setPrice(studentPaymentOrder.getExpectAmount().subtract(totalPrice));
                 }else{
                 	studentPaymentOrderDetail.setPrice(tempPrice);
@@ -460,6 +515,9 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
         for (Goods goods : goodies) {
             goodsPrice = goodsPrice.add(goods.getDiscountPrice());
         }
+        
+        Map<Integer,Goods> goodsMap = goodies.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+        
         subjectChange.setChangeCost(goodsPrice);
         subjectChange.setCostMargin(subjectChange.getChangeCost().subtract(subjectChange.getOriginalCost()));
         //差价 <= 0
@@ -512,6 +570,177 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
             subjectChange.setOrderNo(orderNo);
             subjectChangeDao.update(subjectChange);
 
+            //添加订单详情
+            List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = new ArrayList<StudentPaymentOrderDetail>();
+            StudentPaymentOrderDetail studentPaymentOrderDetail = null;
+        	Goods goods = null;
+			List<Integer> minuendStockGoodsIdList = null;
+			GoodsProcurement goodsProcurement = null;
+			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+			
+            for(String goodsIdStr : goodsIds.split(",")){
+        		if(StringUtils.isBlank(goodsIdStr)){
+					continue;
+				}
+				goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+				
+				if(goods != null){
+					studentPaymentOrderDetail = new StudentPaymentOrderDetail();
+	                studentPaymentOrderDetail.setCreateTime(nowDate);
+	                if(goods.getType() == GoodsType.INSTRUMENT){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.MUSICAL);
+	                }else if(goods.getType() == GoodsType.ACCESSORIES){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.ACCESSORIES);
+	                }else if(goods.getType() == GoodsType.TEACHING || goods.getType() == GoodsType.STAFF){
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.TEACHING);
+	                }else{
+	                	studentPaymentOrderDetail.setType(OrderDetailTypeEnum.OTHER);
+	                }
+	                studentPaymentOrderDetail.setGoodsIdList(goodsIdStr);
+                	studentPaymentOrderDetail.setPrice(BigDecimal.ZERO);
+	                studentPaymentOrderDetail.setUpdateTime(nowDate);
+	                studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
+	              
+	                //忽略的分部
+	        		List<Integer> ignoreOrganList = Arrays.asList(4, 55, 59);
+	        		
+	        		//查询商品收款账户配置
+	                String goodsSellReceiptMerNo = sysConfigDao.findConfigValue("goodsSellReceiptMerNo");
+	                
+	                if(!ignoreOrganList.contains(subjectChange.getOrganId()) && StringUtils.isNotBlank(goodsSellReceiptMerNo)){
+	                	//扣减库存
+						
+						BigDecimal totalGroupPurchaseAmount = BigDecimal.ZERO;
+						
+						BigDecimal groupPurchaseAmount = BigDecimal.ZERO;
+						
+						minuendStockGoodsIdList = new ArrayList<Integer>();
+						
+						// 是否是组合商品
+						if(StringUtils.isNotBlank(goods.getComplementGoodsIdList())){
+							List<Goods> goodsList = goodsService.getGoodsWithLocked(goods.getComplementGoodsIdList());
+							totalGroupPurchaseAmount =  totalGroupPurchaseAmount.add(goodsList.stream().map(Goods :: getGroupPurchasePrice).reduce(BigDecimal.ZERO,BigDecimal :: add));
+							
+							for(Goods subGoods : goodsList){
+								
+								if(batchUpdateGoodsMap.get(subGoods.getId()) != null){
+									subGoods = batchUpdateGoodsMap.get(subGoods.getId());
+								}
+								//判断是否有内部库存
+								if(subGoods.getStockCount() > 0){
+									
+									groupPurchaseAmount = groupPurchaseAmount.add(subGoods.getGroupPurchasePrice());
+									subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).decrementAndGet());
+									subGoods.setSellCount(new AtomicInteger(subGoods.getSellCount()).incrementAndGet());
+									subGoods.setUpdateTime(nowDate);
+									
+									batchUpdateGoodsMap.put(subGoods.getId(), subGoods);
+									
+									minuendStockGoodsIdList.add(subGoods.getId());
+									
+									goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(subGoods.getId());
+									if(goodsProcurement != null){
+										if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+											goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+										}
+										goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).incrementAndGet());
+										goodsProcurement.setUpdateTime(nowDate);
+										goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
+									}
+								}
+							}
+						}else{
+							totalGroupPurchaseAmount = totalGroupPurchaseAmount.add(goods.getGroupPurchasePrice());
+							
+							if(batchUpdateGoodsMap.get(goods.getId()) != null){
+								goods = batchUpdateGoodsMap.get(goods.getId());
+							}
+							
+							//判断是否有内部库存
+							if(goods.getStockCount() > 0){
+								
+								groupPurchaseAmount = groupPurchaseAmount.add(goods.getGroupPurchasePrice());
+								goods.setStockCount(new AtomicInteger(goods.getStockCount()).decrementAndGet());
+								goods.setSellCount(new AtomicInteger(goods.getSellCount()).incrementAndGet());
+								goods.setUpdateTime(nowDate);
+								batchUpdateGoodsMap.put(goods.getId(), goods);
+								
+								minuendStockGoodsIdList.add(goods.getId());
+								
+								goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
+								if(goodsProcurement != null){
+									if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+										goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+									}
+									goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).incrementAndGet());
+									goodsProcurement.setUpdateTime(nowDate);
+									goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
+								}
+							}
+						}
+						
+						if (minuendStockGoodsIdList.size() > 0) {
+							studentPaymentOrderDetail.setIncomeItem(OrderDetailTypeEnum.CLOUD_TEACHER.name());
+							studentPaymentOrderDetail.setIncome(BigDecimal.ZERO);
+							studentPaymentOrderDetail.setBalanceIncome(BigDecimal.ZERO);
+							if(minuendStockGoodsIdList.size() > 0){
+								studentPaymentOrderDetail.setMinuendStockGoodsIdList(minuendStockGoodsIdList.stream().map(t -> t.toString()).collect(Collectors.joining(",")));
+							}
+						}
+	                }
+	                
+	                studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
+				}
+        	}
+            
+            //原始订单的库存要归还
+            List<StudentPaymentOrderDetail> originalStudentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(subjectChange.getOriginalOrderId().longValue());
+            String originalGoodsIds = originalStudentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+            if(StringUtils.isNotBlank(originalGoodsIds)){
+            	List<Goods> goodsList = goodsService.getGoodsWithLocked(originalGoodsIds);
+            	goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+            	
+            	for(String goodsIdStr : originalGoodsIds.split(",")){
+            		if(StringUtils.isBlank(goodsIdStr)){
+						continue;
+					}
+					goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+					if(batchUpdateGoodsMap.get(goods.getId()) != null){
+						goods = batchUpdateGoodsMap.get(goods.getId());
+					}
+					goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+					goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+					goods.setUpdateTime(nowDate);
+					
+					batchUpdateGoodsMap.put(goods.getId(), goods);
+					
+					// 进货清单
+					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
+					if(goodsProcurement != null){
+						if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+							goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+						}
+						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
+						goodsProcurement.setUpdateTime(nowDate);
+						goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
+					}
+					
+            	}
+            }
+            
+            if(studentPaymentOrderDetailList.size() > 0){
+            	studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);
+            }
+			
+			if(goodsProcurementMap.size() > 0){
+				goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
+			}
+    		
+    		if(batchUpdateGoodsMap.size() > 0){
+    			goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+    		}
+
             //退原订单商品
             sellOrderService.refundByOrderId(subjectChange.getOriginalOrderId().longValue(), false);
 
@@ -827,56 +1056,51 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
             }
             sysUserCashAccountDetailService.insert(paymentDetail);
             
-			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
-			
-			List<String> changeAccessoriesList = null;
-			List<String> originalAccessories = new ArrayList<String>();
-	        
-	        if(StringUtils.isNotBlank(subjectChange.getOriginalAccessories())){
-	        	changeAccessoriesList = Arrays.asList(subjectChange.getOriginalAccessories().split(","));
-	        	
-	        	if(StringUtils.isNotBlank(subjectChange.getChangeAccessories())){
-	        		originalAccessories = Arrays.asList(subjectChange.getChangeAccessories().split(","));
-	        	}
-	        	changeAccessoriesList.removeAll(originalAccessories);
-	        	
-	        	if(subjectChange.getChangeMusical() != subjectChange.getOriginalMusical()){
-	        		changeAccessoriesList.add(subjectChange.getOriginalMusical()+"");
-	        	}
-	        }
-	        List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(subjectChange.getOriginalOrderId().longValue());
-	        String goodsIdsStr = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
-	        originalAccessories = Arrays.asList(goodsIdsStr.split(","));
-	        originalAccessories.removeAll(changeAccessoriesList);
-	        
-	        goodsIdsStr = originalAccessories.stream().map(t -> t.toString()).collect(Collectors.joining(","));
             //增加商品库存
-            if(StringUtils.isNotBlank(goodsIdsStr)){
+            List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(subjectChange.getOriginalOrderId().longValue());
+            goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
+            if(StringUtils.isNotBlank(goodsIds)){
             	GoodsProcurement goodsProcurement = null;
-            	List<GoodsProcurement> goodsProcurementList = new ArrayList<GoodsProcurement>();
-            	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIdsStr);
-            	for(Goods subGoods : goodsList){
-					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
-					subGoods.setSellCount(new AtomicInteger(subGoods.getSellCount()).decrementAndGet());
-					subGoods.setUpdateTime(nowDate);
-					batchUpdateGoodsList.add(subGoods);
+            	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
+            	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+            	Goods goods = null;
+    			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+    			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+            	
+            	for(String goodsIdStr : goodsIds.split(",")){
+            		if(StringUtils.isBlank(goodsIdStr)){
+						continue;
+					}
+					goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+					if(batchUpdateGoodsMap.get(goods.getId()) != null){
+						goods = batchUpdateGoodsMap.get(goods.getId());
+					}
+					goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+					goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+					goods.setUpdateTime(nowDate);
+					
+					batchUpdateGoodsMap.put(goods.getId(), goods);
 					
 					// 进货清单
-					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(subGoods.getId());
+					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
 					if(goodsProcurement != null){
+						if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+							goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+						}
 						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
 						goodsProcurement.setUpdateTime(nowDate);
-						goodsProcurementList.add(goodsProcurement);
+						goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
 					}
+					
             	}
             	
-            	if(goodsProcurementList.size() > 0){
-            		goodsProcurementDao.batchUpdate(goodsProcurementList);
+            	if(goodsProcurementMap.size() > 0){
+            		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
             	}
-            }
-            
-            if(batchUpdateGoodsList.size() > 0){
-            	goodsService.batchUpdate(batchUpdateGoodsList);
+                
+                if(batchUpdateGoodsMap.size() > 0){
+                	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+                }
             }
         }
 
@@ -886,36 +1110,51 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                 throw new BizException("声部更改订单更新失败");
             }
             
-			List<Goods> batchUpdateGoodsList = new ArrayList<Goods>();
             //增加商品库存
             List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = studentPaymentOrderDetailService.queryOrderDetail(studentPaymentOrder.getId());
             String goodsIds = studentPaymentOrderDetailList.stream().filter(t -> StringUtils.isNotBlank(t.getMinuendStockGoodsIdList())).map(t -> t.getMinuendStockGoodsIdList()).collect(Collectors.joining(","));
             if(StringUtils.isNotBlank(goodsIds)){
             	GoodsProcurement goodsProcurement = null;
-            	List<GoodsProcurement> goodsProcurementList = new ArrayList<GoodsProcurement>();
             	List<Goods> goodsList = goodsService.getGoodsWithLocked(goodsIds);
-            	for(Goods subGoods : goodsList){
-					subGoods.setStockCount(new AtomicInteger(subGoods.getStockCount()).incrementAndGet());
-					subGoods.setSellCount(new AtomicInteger(subGoods.getSellCount()).decrementAndGet());
-					subGoods.setUpdateTime(nowDate);
-					batchUpdateGoodsList.add(subGoods);
+            	Map<Integer,Goods> goodsMap = goodsList.stream().collect(Collectors.toMap(Goods :: getId, t -> t));
+            	Goods goods = null;
+    			Map<Integer, Goods> batchUpdateGoodsMap = new HashMap<Integer, Goods>();
+    			Map<Long, GoodsProcurement> goodsProcurementMap = new HashMap<Long, GoodsProcurement>();
+            	
+            	for(String goodsIdStr : goodsIds.split(",")){
+            		if(StringUtils.isBlank(goodsIdStr)){
+						continue;
+					}
+					goods = goodsMap.get(Integer.parseInt(goodsIdStr));
+					if(batchUpdateGoodsMap.get(goods.getId()) != null){
+						goods = batchUpdateGoodsMap.get(goods.getId());
+					}
+					goods.setStockCount(new AtomicInteger(goods.getStockCount()).incrementAndGet());
+					goods.setSellCount(new AtomicInteger(goods.getSellCount()).decrementAndGet());
+					goods.setUpdateTime(nowDate);
+					
+					batchUpdateGoodsMap.put(goods.getId(), goods);
 					
 					// 进货清单
-					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(subGoods.getId());
+					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
 					if(goodsProcurement != null){
+						if(goodsProcurementMap.get(goodsProcurement.getId()) != null){
+							goodsProcurement = goodsProcurementMap.get(goodsProcurement.getId());
+						}
 						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).decrementAndGet());
 						goodsProcurement.setUpdateTime(nowDate);
-						goodsProcurementList.add(goodsProcurement);
+						goodsProcurementMap.put(goodsProcurement.getId(), goodsProcurement);
 					}
+					
             	}
             	
-            	if(goodsProcurementList.size() > 0){
-            		goodsProcurementDao.batchUpdate(goodsProcurementList);
+            	if(goodsProcurementMap.size() > 0){
+            		goodsProcurementDao.batchUpdate(new ArrayList<GoodsProcurement>(goodsProcurementMap.values()));
             	}
-            }
-            
-            if(batchUpdateGoodsList.size() > 0){
-            	goodsService.batchUpdate(batchUpdateGoodsList);
+                
+                if(batchUpdateGoodsMap.size() > 0){
+                	goodsService.batchUpdate(new ArrayList<Goods>(batchUpdateGoodsMap.values()));
+                }
             }
             
             if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {

+ 11 - 4
mec-biz/src/main/resources/config/mybatis/StudentPaymentOrderDetailMapper.xml

@@ -21,6 +21,7 @@
         <result column="is_renew_" property="isRenew"/>
         <result column="income_item_" property="incomeItem"/>
         <result column="income_" property="income"/>
+        <result column="balance_income_" property="balanceIncome"/>
         <result column="minuend_stock_goods_id_list_" property="minuendStockGoodsIdList"/>
         <result column="user_id_" property="userId"/>
         <collection property="goodsList" ofType="com.ym.mec.biz.dal.entity.Goods">
@@ -65,10 +66,10 @@
             keyColumn="id" keyProperty="id">
         INSERT INTO student_payment_order_detail
         (type_,goods_id_list_,price_,create_time_,update_time_,payment_order_id_,kit_group_purchase_type_,
-         student_instrument_id_,is_renew_,income_item_,income_,minuend_stock_goods_id_list_,tenant_id_,remit_fee_)
+         student_instrument_id_,is_renew_,income_item_,income_,balance_income_,minuend_stock_goods_id_list_,tenant_id_,remit_fee_)
         VALUES(#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{goodsIdList},#{price},now(),now(),
         #{paymentOrderId},#{kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
-               #{studentInstrumentId},#{isRenew},#{incomeItem},#{income},#{minuendStockGoodsIdList},#{tenantId},#{remitFee})
+               #{studentInstrumentId},#{isRenew},#{incomeItem},#{income},#{balanceIncome},#{minuendStockGoodsIdList},#{tenantId},#{remitFee})
     </insert>
 
     <!-- 根据主键查询一条记录 -->
@@ -103,6 +104,9 @@
             <if test="income != null">
                 income_ = #{income},
             </if>
+            <if test="balanceIncome != null">
+                balance_income_ = #{balanceIncome},
+            </if>
             <if test="minuendStockGoodsIdList != null">
                 minuend_stock_goods_id_list_ = #{minuendStockGoodsIdList},
             </if>
@@ -142,6 +146,9 @@
 	            <if test="item.income != null">
 	                income_ = #{item.income},
 	            </if>
+	            <if test="item.balanceIncome != null">
+	                balance_income_ = #{item.balanceIncome},
+	            </if>
 	            <if test="item.minuendStockGoodsIdList != null">
 	                minuend_stock_goods_id_list_ = #{item.minuendStockGoodsIdList},
 	            </if>
@@ -172,13 +179,13 @@
     <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
         INSERT INTO student_payment_order_detail
         (type_,goods_id_list_,price_,remit_fee_,create_time_,update_time_,payment_order_id_,
-         kit_group_purchase_type_,student_instrument_id_,is_renew_,income_item_,income_,minuend_stock_goods_id_list_,tenant_id_)
+         kit_group_purchase_type_,student_instrument_id_,is_renew_,income_item_,income_,balance_income_,minuend_stock_goods_id_list_,tenant_id_)
         VALUE
         <foreach collection="studentPaymentOrderDetailList" item="orderDetail" separator=",">
             (#{orderDetail.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
             #{orderDetail.goodsIdList},#{orderDetail.price},#{orderDetail.remitFee},now(),now(),#{orderDetail.paymentOrderId},
             #{orderDetail.kitGroupPurchaseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
-            #{orderDetail.studentInstrumentId},#{orderDetail.isRenew},#{orderDetail.incomeItem},#{orderDetail.income},#{orderDetail.minuendStockGoodsIdList},#{orderDetail.tenantId})
+            #{orderDetail.studentInstrumentId},#{orderDetail.isRenew},#{orderDetail.incomeItem},#{orderDetail.income},#{orderDetail.balanceIncome},#{orderDetail.minuendStockGoodsIdList},#{orderDetail.tenantId})
         </foreach>
     </insert>
 

+ 0 - 1
mec-im/src/main/java/com/ym/service/Impl/LiveRoomServiceImpl.java

@@ -265,7 +265,6 @@ public class LiveRoomServiceImpl implements LiveRoomService {
             log.error("获取sessionId失败 roomId : {} returnResult:{}", roomId, resultObject);
             throw new BizException("获取sessionId失败");
         }
-
         return sessionId;
     }