Переглянути джерело

Merge branch 'master' of http://git.dayaedu.com/yonge/mec

zouxuan 5 роки тому
батько
коміт
7386540376

+ 1 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/GoodsDao.java

@@ -45,7 +45,7 @@ public interface GoodsDao extends BaseDAO<Integer, Goods> {
      * @date 2020/9/4
      * @time 14:39
      */
-    void batchInsert(@Param("goodsList") List<Goods> goodsList);
+    void batchInsert(List<Goods> goodsList);
 
     /**
      * @describe 批量更新商品

+ 74 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -200,6 +200,33 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 						}
 						continue;
 					}
+					if (columnValue.equals("stockType")) {
+						for (StockType stockType : StockType.values()) {
+							if (stockType.getMsg().equals(row.get(s).toString())) {
+								objectMap.put(columnValue, stockType);
+								break;
+							}
+						}
+						continue;
+					}
+					if (columnValue.equals("clientShow")) {
+						for (YesOrNoEnum yesOrNoEnum : YesOrNoEnum.values()) {
+							if (yesOrNoEnum.getMsg().equals(row.get(s).toString())) {
+								objectMap.put(columnValue, yesOrNoEnum);
+								break;
+							}
+						}
+						continue;
+					}
+					if (columnValue.equals("stockWarning")) {
+						for (YesOrNoEnum yesOrNoEnum : YesOrNoEnum.values()) {
+							if (yesOrNoEnum.getMsg().equals(row.get(s).toString())) {
+								objectMap.put(columnValue, yesOrNoEnum);
+								break;
+							}
+						}
+						continue;
+					}
 					if (columnValue.equals("goodsCategoryName")) {
 						Integer integer = map.get(row.get(s));
 						if(integer == null){
@@ -250,6 +277,43 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			int taxStockCount = goods.stream().mapToInt(Goods::getTaxStockCount).sum();
 			existsGood.setStockCount((Objects.isNull(existsGood.getStockCount())?0:existsGood.getStockCount()) + stockCount);
 			existsGood.setTaxStockCount((Objects.isNull(existsGood.getTaxStockCount())?0:existsGood.getTaxStockCount()) + taxStockCount);
+			Goods newGoods = goods.get(0);
+			if(Objects.nonNull(newGoods.getGoodsCategoryId())&&!newGoods.getGoodsCategoryId().equals(existsGood.getGoodsCategoryId())){
+				existsGood.setGoodsCategoryId(newGoods.getGoodsCategoryId());
+			}
+			if(Objects.nonNull(newGoods.getName())&&!newGoods.getName().equals(existsGood.getName())){
+				existsGood.setName(newGoods.getName());
+			}
+			if(Objects.nonNull(newGoods.getType())&&!newGoods.getType().equals(existsGood.getType())){
+				existsGood.setType(newGoods.getType());
+			}
+			if(Objects.nonNull(newGoods.getSpecification())&&!newGoods.getSpecification().equals(existsGood.getSpecification())){
+				existsGood.setSpecification(newGoods.getSpecification());
+			}
+			if(Objects.nonNull(newGoods.getMarketPrice())&&!newGoods.getMarketPrice().equals(existsGood.getMarketPrice())){
+				existsGood.setMarketPrice(newGoods.getMarketPrice());
+			}
+			if(Objects.nonNull(newGoods.getDiscountPrice())&&!newGoods.getDiscountPrice().equals(existsGood.getDiscountPrice())){
+				existsGood.setDiscountPrice(newGoods.getDiscountPrice());
+			}
+			if(Objects.nonNull(newGoods.getGroupPurchasePrice())&&!newGoods.getGroupPurchasePrice().equals(existsGood.getGroupPurchasePrice())){
+				existsGood.setGroupPurchasePrice(newGoods.getGroupPurchasePrice());
+			}
+			if(Objects.nonNull(newGoods.getImage())&&!newGoods.getImage().equals(existsGood.getImage())){
+				existsGood.setImage(newGoods.getImage());
+			}
+			if(Objects.nonNull(newGoods.getStockType())&&!newGoods.getStockType().equals(existsGood.getStockType())){
+				existsGood.setStockType(newGoods.getStockType());
+			}
+			if(Objects.nonNull(newGoods.getBrief())&&!newGoods.getBrief().equals(existsGood.getBrief())){
+				existsGood.setBrief(newGoods.getBrief());
+			}
+			if(Objects.nonNull(newGoods.getClientShow())&&!newGoods.getClientShow().equals(existsGood.getClientShow())){
+				existsGood.setClientShow(newGoods.getClientShow());
+			}
+			if(Objects.nonNull(newGoods.getStockWarning())&&!newGoods.getStockWarning().equals(existsGood.getStockWarning())){
+				existsGood.setStockWarning(newGoods.getStockWarning());
+			}
 		}
 
 		if(!CollectionUtils.isEmpty(existsGoods)){
@@ -271,8 +335,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			gp.setGoodsId(existsSnIdMap.get(goods.getSn()));
 			gp.setGoodsCategoryId(goods.getGoodsCategoryId());
 			gp.setSupplyChannel(goods.getSupplyChannel());
-			gp.setDiscountPrice(goods.getCostPrice()
-			);
+			gp.setDiscountPrice(goods.getCostPrice());
 			gp.setAgreeCostPrice(goods.getAgreeCostPrice());
 			gp.setStockCount(goods.getStockCount());
 			gp.setTaxStockCount(goods.getTaxStockCount());
@@ -346,6 +409,13 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 				goodsProcurementDao.update(goodsProcurement);
 				noneBatchNoSellOrder.setBatchNo(goodsProcurement.getBatchNo());
 				noneBatchNoSellOrder.setSellCost(goodsProcurement.getDiscountPrice());
+				noneBatchNoSellOrder.setSellCost(goodsProcurement.getDiscountPrice());
+				Map<String, BigDecimal> CostMap = new HashMap<>();
+				CostMap.put("sellCost", goodsProcurement.getDiscountPrice());
+				if (Objects.nonNull(goodsProcurement.getAgreeCostPrice())) {
+					CostMap.put("SellCost2", goodsProcurement.getAgreeCostPrice());
+				}
+				noneBatchNoSellOrder.setSellCost2(JSON.toJSONString(CostMap));
 				updateSellOrders.add(noneBatchNoSellOrder);
 			}
 		}
@@ -418,6 +488,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					sellOrder.setGoodsId(goodsNumMapEntry.getKey());
 					sellOrder.setNum(goodsNumMapEntry.getValue().intValue());
 					sellOrder.setStockType(goods.getStockType());
+					sellOrder.setGoodsName(goods.getName());
 					sellOrder.setAccountType(accountType);
 					sellOrder.setBatchNo(batchNoGoodsIdMapEntry.getKey());
 					GoodsProcurement goodsProcurement = goodsProcurementDao.getWithGoodsAndBatchNo(sellOrder.getGoodsId(), sellOrder.getBatchNo());
@@ -442,6 +513,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 				sellOrder.setGoodsId(goodsProcurement.getGoodsId());
 				sellOrder.setNum(1);
 				sellOrder.setStockType(goods.getStockType());
+				sellOrder.setGoodsName(goods.getName());
 				sellOrder.setAccountType(accountType);
 				sellOrder.setBatchNo(goodsProcurement.getBatchNo());
 				if(Objects.nonNull(goodsProcurement.getBatchNo())){

+ 123 - 109
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SellOrderServiceImpl.java

@@ -19,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> implements SellOrderService {
@@ -71,15 +72,11 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
 
         BigDecimal hasRouteBalance = BigDecimal.ZERO;
         BigDecimal goodsTotalBalance = goodsTotalPrice.multiply(balance).divide(totalAmount, 2, BigDecimal.ROUND_HALF_UP);
-        List<SellOrder> sellOrders = new ArrayList<>();
-
         AccountType accountType = sysPaymentConfigService.checkAccountType(PaymentChannelEnum.valueOf(order.getPaymentChannel()), order.getMerNos());
 
-        //批次号 TODO
         List<SellOrder> sellOrderList = goodsService.subtractStock(goodsIds, accountType);
 
         for (Integer goodsId : goodsIds) {
-            SellOrder sellOrder = new SellOrder();
             BigDecimal goodsPrice = BigDecimal.ZERO;
             Goods nowGoods = new Goods();
             for (Goods goods : goodies) {
@@ -95,63 +92,64 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
                 goodsBalance = goodsTotalBalance.subtract(hasRouteBalance);
             }
             hasRouteBalance = hasRouteBalance.add(goodsBalance);
-            sellOrder.setGoodsId(goodsId);
-            sellOrder.setGoodsName(nowGoods.getName());
-            sellOrder.setExpectAmount(goodsPrice);
-            sellOrder.setBalanceAmount(goodsBalance);
-            sellOrder.setActualAmount(goodsPrice.subtract(goodsBalance));
-            Map<String, BigDecimal> CostMap = new HashMap<>();
-            CostMap.put("sellCost", nowGoods.getDiscountPrice());
-            if (nowGoods.getAgreeCostPrice() != null) {
-                CostMap.put("SellCost2", nowGoods.getAgreeCostPrice());
-            }
-            sellOrder.setSellCost2(JSON.toJSONString(CostMap));
-
-            sellOrder.setOrganId(order.getOrganId());
-            sellOrder.setCooperationOrganId(musicGroup.getCooperationOrganId());
-            sellOrder.setTransNo(order.getTransNo());
-            sellOrder.setOrderId(order.getId());
-            sellOrder.setOrderNo(order.getOrderNo());
-            sellOrder.setSellCost(nowGoods.getDiscountPrice());
-            sellOrder.setNum(1);
-            sellOrder.setUserId(order.getUserId());
-            sellOrder.setPaymentChannel(order.getPaymentChannel());
-            sellOrder.setMerNo(order.getMerNos());
-            sellOrder.setAccountType(accountType);
-            sellOrder.setSellTime(order.getCreateTime());
-            sellOrder.setCreateIme(new Date());
-            sellOrder.setUpdateTime(new Date());
-            if (nowGoods.getType().equals(GoodsType.INSTRUMENT)) {
-                sellOrder.setType(SellTypeEnum.INSTRUMENT);
-            } else if (nowGoods.getType().equals(GoodsType.ACCESSORIES)) {
-                sellOrder.setType(SellTypeEnum.ACCESSORIES);
-            } else {
-                sellOrder.setType(SellTypeEnum.OTHER);
+            i++;
+
+            int complementGoodsNum = nowGoods.getComplementGoodsIdList() == null ? 1 : nowGoods.getComplementGoodsIdList().split(",").length;
+            BigDecimal complementPrice = nowGoods.getGroupPurchasePrice();
+            Map<Integer, BigDecimal> complementGoodsPrice = new HashMap<>();
+            complementGoodsPrice.put(nowGoods.getId(), nowGoods.getGroupPurchasePrice());
+            if (nowGoods.getComplementGoodsIdList() != null) {
+                List<Goods> complementGoodies = goodsDao.findGoodsByIds(nowGoods.getComplementGoodsIdList());
+                complementGoodsPrice = complementGoodies.stream().collect(Collectors.toMap(Goods::getId, Goods::getGroupPurchasePrice));
+                complementPrice = complementGoodies.stream().map(Goods::getGroupPurchasePrice).reduce(BigDecimal.ZERO, BigDecimal::add);
             }
 
-            StockType goodsStockType = StockType.INTERNAL;
-            for (Goods goods : goodies) {
-                if (goods.getId().equals(goodsId)) {
-                    goodsStockType = goods.getStockType();
-                    break;
+            BigDecimal hasRouteSellOrderActualAmount = BigDecimal.ZERO;
+            BigDecimal hasRouteSellOrderBalance = BigDecimal.ZERO;
+            for (SellOrder sellOrder : sellOrderList) {
+                if (!goodsId.equals(sellOrder.getParentGoodsId()) && !goodsId.equals(sellOrder.getParentGoodsId())) {
+                    continue;
+                }
+                BigDecimal sellOrderActualAmount = BigDecimal.ZERO;
+                BigDecimal sellOrderBalance = BigDecimal.ZERO;
+                if (complementPrice.compareTo(BigDecimal.ZERO) > 0) {
+                    sellOrderActualAmount = complementGoodsPrice.get(sellOrder.getGoodsId()).multiply(new BigDecimal(sellOrder.getNum())).multiply(goodsPrice).divide(complementPrice, 2, BigDecimal.ROUND_HALF_UP);
+                    sellOrderBalance = complementGoodsPrice.get(sellOrder.getGoodsId()).multiply(new BigDecimal(sellOrder.getNum())).multiply(goodsBalance).divide(complementPrice, 2, BigDecimal.ROUND_HALF_UP);
                 }
-            }
-            //库存类型
-            if (goodsStockType.equals(StockType.ALL) && accountType.equals(AccountType.INTERNAL)) {
-                sellOrder.setStockType(StockType.INTERNAL);
-            } else if (goodsStockType.equals(StockType.ALL) && accountType.equals(AccountType.EXTERNAL)) {
-                sellOrder.setStockType(StockType.EXTERNAL);
-            } else {
-                sellOrder.setStockType(goodsStockType);
-            }
-            //批次号 TODO
-            //goodsService.subtractStock();
 
-            sellOrders.add(sellOrder);
-            i++;
+                complementGoodsNum = complementGoodsNum - sellOrder.getNum();
+                if (complementGoodsNum <= 0) {
+                    sellOrderActualAmount = goodsPrice.subtract(hasRouteSellOrderActualAmount);
+                    sellOrderBalance = goodsBalance.subtract(hasRouteSellOrderActualAmount);
+                }
+                hasRouteSellOrderActualAmount = hasRouteSellOrderActualAmount.add(sellOrderActualAmount);
+                hasRouteSellOrderBalance = hasRouteSellOrderBalance.add(sellOrderBalance);
+
+                sellOrder.setExpectAmount(sellOrderActualAmount);
+                sellOrder.setBalanceAmount(sellOrderBalance);
+                sellOrder.setActualAmount(sellOrderActualAmount.subtract(sellOrderBalance));
+                sellOrder.setOrganId(order.getOrganId());
+                sellOrder.setCooperationOrganId(musicGroup.getCooperationOrganId());
+                sellOrder.setTransNo(order.getTransNo());
+                sellOrder.setOrderId(order.getId());
+                sellOrder.setOrderNo(order.getOrderNo());
+                sellOrder.setUserId(order.getUserId());
+                sellOrder.setPaymentChannel(order.getPaymentChannel());
+                sellOrder.setMerNo(order.getMerNos());
+                sellOrder.setSellTime(order.getCreateTime());
+                sellOrder.setCreateIme(new Date());
+                sellOrder.setUpdateTime(new Date());
+                if (nowGoods.getType().equals(GoodsType.INSTRUMENT)) {
+                    sellOrder.setType(SellTypeEnum.INSTRUMENT);
+                } else if (nowGoods.getType().equals(GoodsType.ACCESSORIES)) {
+                    sellOrder.setType(SellTypeEnum.ACCESSORIES);
+                } else {
+                    sellOrder.setType(SellTypeEnum.OTHER);
+                }
+            }
         }
-        sellOrderDao.batchInsert(sellOrders);
-        return sellOrders;
+        sellOrderDao.batchInsert(sellOrderList);
+        return sellOrderList;
     }
 
     @Override
@@ -185,6 +183,9 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
             BigDecimal routePrice = BigDecimal.ZERO;
             BigDecimal routeBalance = BigDecimal.ZERO;
             int j = 1;
+
+            List<Integer> goodsIds = orderDetail.getGoodsList().stream().map(Goods::getId).collect(Collectors.toList());
+            List<SellOrder> sellOrderList = goodsService.subtractStock(goodsIds, accountType);
             for (Goods goods : orderDetail.getGoodsList()) {
                 BigDecimal expectAmount = BigDecimal.ZERO;
                 BigDecimal balance = BigDecimal.ZERO;
@@ -200,62 +201,75 @@ public class SellOrderServiceImpl extends BaseServiceImpl<Integer, SellOrder> im
                 routeBalance = routeBalance.add(balance);
                 j++;
 
-                SellOrder sellOrder = new SellOrder();
-                Map<String, BigDecimal> CostMap = new HashMap<>();
-                CostMap.put("sellCost", goods.getDiscountPrice());
-                if (goods.getAgreeCostPrice() != null) {
-                    CostMap.put("SellCost2", goods.getAgreeCostPrice());
-                }
-                sellOrder.setOrganId(studentPaymentOrder.getOrganId());
-                sellOrder.setTransNo(studentPaymentOrder.getTransNo());
-                sellOrder.setOrderId(studentPaymentOrder.getId());
-                if (musicGroup != null) {
-                    sellOrder.setCooperationOrganId(musicGroup.getCooperationOrganId());
-                    sellOrder.setEduTeacherId(musicGroup.getEducationalTeacherId());
+                int complementGoodsNum = goods.getComplementGoodsIdList() == null ? 1 : goods.getComplementGoodsIdList().split(",").length;
+                BigDecimal complementPrice = goods.getGroupPurchasePrice();
+                Map<Integer, BigDecimal> complementGoodsPrice = new HashMap<>();
+                complementGoodsPrice.put(goods.getId(), goods.getGroupPurchasePrice());
+                if (goods.getComplementGoodsIdList() != null) {
+                    List<Goods> complementGoodies = goodsDao.findGoodsByIds(goods.getComplementGoodsIdList());
+                    complementGoodsPrice = complementGoodies.stream().collect(Collectors.toMap(Goods::getId, Goods::getGroupPurchasePrice));
+                    complementPrice = complementGoodies.stream().map(Goods::getGroupPurchasePrice).reduce(BigDecimal.ZERO, BigDecimal::add);
                 }
-                sellOrder.setOrderNo(studentPaymentOrder.getOrderNo());
-                sellOrder.setActualAmount(expectAmount.subtract(balance));
-                sellOrder.setBalanceAmount(balance);
-                sellOrder.setExpectAmount(expectAmount);
-                sellOrder.setSellCost(goods.getDiscountPrice());
-                sellOrder.setSellCost2(JSON.toJSONString(CostMap));
-                sellOrder.setGoodsId(goods.getId());
-                sellOrder.setGoodsName(goods.getName());
-                sellOrder.setNum(1);
-                sellOrder.setUserId(studentPaymentOrder.getUserId());
-                sellOrder.setPaymentChannel(studentPaymentOrder.getPaymentChannel());
-                sellOrder.setMerNo(studentPaymentOrder.getMerNos());
-                sellOrder.setAccountType(accountType);
-                sellOrder.setSellTime(studentPaymentOrder.getCreateTime());
-                sellOrder.setCreateIme(new Date());
-                sellOrder.setUpdateTime(new Date());
-                if (goods.getType().equals(GoodsType.INSTRUMENT)) {
-                    sellOrder.setType(SellTypeEnum.INSTRUMENT);
-                } else if (goods.getType().equals(GoodsType.ACCESSORIES)) {
-                    sellOrder.setType(SellTypeEnum.ACCESSORIES);
-                } else {
-                    sellOrder.setType(SellTypeEnum.OTHER);
-                }
-                //租赁、免费的没有销售收入,有销售成本
-                if (orderDetail.getType().equals(OrderDetailTypeEnum.MUSICAL) &&
-                        (orderDetail.getKitGroupPurchaseType().equals(KitGroupPurchaseTypeEnum.LEASE) ||
-                                orderDetail.getKitGroupPurchaseType().equals(KitGroupPurchaseTypeEnum.FREE)
-                        )) {
-                    sellOrder.setActualAmount(BigDecimal.ZERO);
-                    sellOrder.setBalanceAmount(BigDecimal.ZERO);
-                    sellOrder.setExpectAmount(BigDecimal.ZERO);
-                }
-                //库存类型
-                if (goods.getStockType().equals(StockType.ALL) && accountType.equals(AccountType.INTERNAL)) {
-                    sellOrder.setStockType(StockType.INTERNAL);
-                } else if (goods.getStockType().equals(StockType.ALL) && accountType.equals(AccountType.EXTERNAL)) {
-                    sellOrder.setStockType(StockType.EXTERNAL);
-                } else {
-                    sellOrder.setStockType(goods.getStockType());
+
+                BigDecimal hasRouteSellOrderActualAmount = BigDecimal.ZERO;
+                BigDecimal hasRouteSellOrderBalance = BigDecimal.ZERO;
+                for (SellOrder sellOrder : sellOrderList) {
+                    if (!goods.getId().equals(sellOrder.getParentGoodsId()) && !goods.getId().equals(sellOrder.getParentGoodsId())) {
+                        continue;
+                    }
+                    BigDecimal sellOrderActualAmount = BigDecimal.ZERO;
+                    BigDecimal sellOrderBalance = BigDecimal.ZERO;
+                    if (complementPrice.compareTo(BigDecimal.ZERO) > 0) {
+                        sellOrderActualAmount = complementGoodsPrice.get(sellOrder.getGoodsId()).multiply(new BigDecimal(sellOrder.getNum())).multiply(expectAmount).divide(complementPrice, 2, BigDecimal.ROUND_HALF_UP);
+                        sellOrderBalance = complementGoodsPrice.get(sellOrder.getGoodsId()).multiply(new BigDecimal(sellOrder.getNum())).multiply(balance).divide(complementPrice, 2, BigDecimal.ROUND_HALF_UP);
+                    }
+                     complementGoodsNum = complementGoodsNum - sellOrder.getNum();
+                    if (complementGoodsNum <= 0) {
+                        sellOrderActualAmount = expectAmount.subtract(hasRouteSellOrderActualAmount);
+                        sellOrderBalance = balance.subtract(hasRouteSellOrderActualAmount);
+                    }
+                    hasRouteSellOrderActualAmount = hasRouteSellOrderActualAmount.add(sellOrderActualAmount);
+                    hasRouteSellOrderBalance = hasRouteSellOrderBalance.add(sellOrderBalance);
+
+                    sellOrder.setOrganId(studentPaymentOrder.getOrganId());
+                    sellOrder.setTransNo(studentPaymentOrder.getTransNo());
+                    sellOrder.setOrderId(studentPaymentOrder.getId());
+                    if (musicGroup != null) {
+                        sellOrder.setCooperationOrganId(musicGroup.getCooperationOrganId());
+                        sellOrder.setEduTeacherId(musicGroup.getEducationalTeacherId());
+                    }
+                    sellOrder.setOrderNo(studentPaymentOrder.getOrderNo());
+                    sellOrder.setActualAmount(sellOrderActualAmount.subtract(sellOrderBalance));
+                    sellOrder.setBalanceAmount(sellOrderBalance);
+                    sellOrder.setExpectAmount(sellOrderActualAmount);
+                    sellOrder.setNum(1);
+                    sellOrder.setUserId(studentPaymentOrder.getUserId());
+                    sellOrder.setPaymentChannel(studentPaymentOrder.getPaymentChannel());
+                    sellOrder.setMerNo(studentPaymentOrder.getMerNos());
+                    sellOrder.setAccountType(accountType);
+                    sellOrder.setSellTime(studentPaymentOrder.getCreateTime());
+                    sellOrder.setCreateIme(new Date());
+                    sellOrder.setUpdateTime(new Date());
+                    if (goods.getType().equals(GoodsType.INSTRUMENT)) {
+                        sellOrder.setType(SellTypeEnum.INSTRUMENT);
+                    } else if (goods.getType().equals(GoodsType.ACCESSORIES)) {
+                        sellOrder.setType(SellTypeEnum.ACCESSORIES);
+                    } else {
+                        sellOrder.setType(SellTypeEnum.OTHER);
+                    }
+                    //租赁、免费的没有销售收入,有销售成本
+                    if (orderDetail.getType().equals(OrderDetailTypeEnum.MUSICAL) &&
+                            (orderDetail.getKitGroupPurchaseType().equals(KitGroupPurchaseTypeEnum.LEASE) ||
+                                    orderDetail.getKitGroupPurchaseType().equals(KitGroupPurchaseTypeEnum.FREE)
+                            )) {
+                        sellOrder.setActualAmount(BigDecimal.ZERO);
+                        sellOrder.setBalanceAmount(BigDecimal.ZERO);
+                        sellOrder.setExpectAmount(BigDecimal.ZERO);
+                    }
                 }
-                //批次号 TODO
 
-                sellOrders.add(sellOrder);
+                sellOrders.addAll(sellOrderList);
+
             }
         }
         if (sellOrders.size() > 0) {

+ 11 - 6
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentPaymentRouteOrderServiceImpl.java

@@ -11,6 +11,7 @@ import com.ym.mec.biz.dal.entity.StudentPaymentRouteOrder;
 import com.ym.mec.biz.dal.enums.*;
 import com.ym.mec.biz.dal.page.StudentPaymentOrderQueryInfo;
 import com.ym.mec.biz.service.GoodsService;
+import com.ym.mec.biz.service.SellOrderService;
 import com.ym.mec.biz.service.StudentPaymentRouteOrderService;
 import com.ym.mec.common.dal.BaseDAO;
 import com.ym.mec.common.exception.BizException;
@@ -43,6 +44,8 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
     private GoodsDao goodsDao;
     @Autowired
     private GoodsService goodsService;
+    @Autowired
+    private SellOrderService sellOrderService;
 
     @Override
     public BaseDAO<Long, StudentPaymentRouteOrder> getDAO() {
@@ -108,7 +111,6 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
         studentPaymentRouteOrderDao.insert(studentPaymentRouteOrder);
 
         if (studentPaymentRouteOrderDto.getGoodies() != null && studentPaymentRouteOrderDto.getGoodies().size() > 0) {
-            List<SellOrder> sellOrders = new ArrayList<>();
             BigDecimal hasRouteAmount = BigDecimal.ZERO;
             BigDecimal goodsTotalPrice = BigDecimal.ZERO;
 
@@ -156,11 +158,12 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
                     if (complementPrice.compareTo(BigDecimal.ZERO) > 0) {
                         sellOrderActualAmount = complementGoodsPrice.get(sellOrder.getGoodsId()).multiply(new BigDecimal(sellOrder.getNum())).multiply(actualAmount).divide(complementPrice, 2, BigDecimal.ROUND_HALF_UP);
                     }
-                    hasRouteSellOrderActualAmount = hasRouteSellOrderActualAmount.add(sellOrderActualAmount);
                     goodsNum = goodsNum - sellOrder.getNum();
                     if (goodsNum <= 0) {
                         sellOrderActualAmount = actualAmount.subtract(hasRouteSellOrderActualAmount);
                     }
+                    hasRouteSellOrderActualAmount = hasRouteSellOrderActualAmount.add(sellOrderActualAmount);
+
                     sellOrder.setSellTime(studentPaymentRouteOrderDto.getPayTime());
                     sellOrder.setOrganId(studentPaymentRouteOrderDto.getOrganId());
                     sellOrder.setCooperationOrganId(studentPaymentRouteOrderDto.getSchoolId());
@@ -177,10 +180,9 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
                     sellOrder.setCreateIme(nowDate);
                     sellOrder.setUpdateTime(nowDate);
                     sellOrder.setType(SellTypeEnum.SCHOOL_BUY);
-                    sellOrders.add(sellOrder);
                 }
             }
-            sellOrderDao.batchInsert(sellOrders);
+            sellOrderDao.batchInsert(sellOrderList);
         }
         return studentPaymentRouteOrderDto;
     }
@@ -208,8 +210,11 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
         }
         studentPaymentOrderDao.delete(orderId);
         studentPaymentRouteOrderDao.deleteByOrderNo(studentPaymentOrder.getOrderNo());
-        sellOrderDao.deleteByOrderId(orderId);
-        //归还库存 TODO
+        //归还库存
+        List<SellOrder> sellOrders = sellOrderService.getSellOrderByParentGoodsId(orderId, null);
+        if (sellOrders.size() > 0) {
+            sellOrderDao.deleteByOrderId(orderId);
+        }
         return true;
     }
 

+ 66 - 56
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SubjectChangeServiceImpl.java

@@ -70,6 +70,8 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
     private StudentPaymentOrderDao studentPaymentOrderDao;
     @Autowired
     private SellOrderService sellOrderService;
+    @Autowired
+    private GoodsService goodsService;
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -570,13 +572,12 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
         }
 
         BigDecimal hasRouteBalance = BigDecimal.ZERO;
-        BigDecimal goodsTotalBalance = balance == null ? BigDecimal.ZERO : balance;
-        List<SellOrder> sellOrders = new ArrayList<>();
+        BigDecimal hasRouteExpectAmount = BigDecimal.ZERO;
 
         AccountType accountType = sysPaymentConfigService.checkAccountType(PaymentChannelEnum.valueOf(order.getPaymentChannel()), order.getMerNos());
+        List<SellOrder> sellOrderList = goodsService.subtractStock(goodsIds, accountType);
 
         for (Integer goodsId : goodsIds) {
-            SellOrder sellOrder = new SellOrder();
             BigDecimal goodsPrice = BigDecimal.ZERO;
             Goods nowGoods = new Goods();
             for (Goods goods : goodies) {
@@ -586,67 +587,76 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                 }
             }
             goodsPrice = nowGoods.getGroupPurchasePrice();
+            BigDecimal expectAmount = BigDecimal.ZERO;
+            BigDecimal goodsBalance = BigDecimal.ZERO;
 
-            BigDecimal goodsBalance = goodsTotalBalance.multiply(goodsPrice).divide(goodsTotalPrice, 2, BigDecimal.ROUND_HALF_UP);
+            if (goodsTotalPrice.compareTo(BigDecimal.ZERO) > 0) {
+                goodsBalance = balance.multiply(goodsPrice).divide(goodsTotalPrice, 2, BigDecimal.ROUND_HALF_UP);
+                expectAmount = totalAmount.multiply(goodsPrice).divide(goodsTotalPrice, 2, BigDecimal.ROUND_HALF_UP);
+            }
             if (i == goodsNum) {
-                goodsBalance = goodsTotalBalance.subtract(hasRouteBalance);
+                goodsBalance = balance.subtract(hasRouteBalance);
+                expectAmount = totalAmount.subtract(hasRouteExpectAmount);
             }
             hasRouteBalance = hasRouteBalance.add(goodsBalance);
-            sellOrder.setGoodsId(goodsId);
-            sellOrder.setGoodsName(nowGoods.getName());
-            sellOrder.setExpectAmount(goodsPrice);
-            sellOrder.setBalanceAmount(goodsBalance);
-            sellOrder.setActualAmount(goodsPrice.subtract(goodsBalance));
-            Map<String, BigDecimal> CostMap = new HashMap<>();
-            CostMap.put("sellCost", nowGoods.getDiscountPrice());
-            if (nowGoods.getAgreeCostPrice() != null) {
-                CostMap.put("SellCost2", nowGoods.getAgreeCostPrice());
-            }
-            sellOrder.setSellCost2(JSON.toJSONString(CostMap));
-
-            sellOrder.setOrganId(order.getOrganId());
-            sellOrder.setCooperationOrganId(musicGroup.getCooperationOrganId());
-            sellOrder.setTransNo(order.getTransNo());
-            sellOrder.setOrderId(order.getId());
-            sellOrder.setOrderNo(order.getOrderNo());
-            sellOrder.setSellCost(nowGoods.getDiscountPrice());
-            sellOrder.setNum(1);
-            sellOrder.setUserId(order.getUserId());
-            sellOrder.setPaymentChannel(order.getPaymentChannel());
-            sellOrder.setMerNo(order.getMerNos());
-            sellOrder.setAccountType(accountType);
-            sellOrder.setSellTime(order.getCreateTime());
-            sellOrder.setCreateIme(new Date());
-            sellOrder.setUpdateTime(new Date());
-            if (nowGoods.getType().equals(GoodsType.INSTRUMENT)) {
-                sellOrder.setType(SellTypeEnum.INSTRUMENT);
-            } else if (nowGoods.getType().equals(GoodsType.ACCESSORIES)) {
-                sellOrder.setType(SellTypeEnum.ACCESSORIES);
-            } else {
-                sellOrder.setType(SellTypeEnum.OTHER);
+            hasRouteExpectAmount = hasRouteExpectAmount.add(expectAmount);
+            i++;
+
+            int complementGoodsNum = nowGoods.getComplementGoodsIdList() == null ? 1 : nowGoods.getComplementGoodsIdList().split(",").length;
+            BigDecimal complementPrice = nowGoods.getGroupPurchasePrice();
+            Map<Integer, BigDecimal> complementGoodsPrice = new HashMap<>();
+            complementGoodsPrice.put(nowGoods.getId(), nowGoods.getGroupPurchasePrice());
+            if (nowGoods.getComplementGoodsIdList() != null) {
+                List<Goods> complementGoodies = goodsDao.findGoodsByIds(nowGoods.getComplementGoodsIdList());
+                complementGoodsPrice = complementGoodies.stream().collect(Collectors.toMap(Goods::getId, Goods::getGroupPurchasePrice));
+                complementPrice = complementGoodies.stream().map(Goods::getGroupPurchasePrice).reduce(BigDecimal.ZERO, BigDecimal::add);
             }
 
-            StockType goodsStockType = StockType.INTERNAL;
-            for (Goods goods : goodies) {
-                if (goods.getId().equals(goodsId)) {
-                    goodsStockType = goods.getStockType();
-                    break;
+            BigDecimal hasRouteSellOrderExpectAmount = BigDecimal.ZERO;
+            BigDecimal hasRouteSellOrderBalance = BigDecimal.ZERO;
+            for (SellOrder sellOrder : sellOrderList) {
+                if (!goodsId.equals(sellOrder.getParentGoodsId()) && !goodsId.equals(sellOrder.getParentGoodsId())) {
+                    continue;
+                }
+                BigDecimal sellOrderExpectAmount = BigDecimal.ZERO;
+                BigDecimal sellOrderBalance = BigDecimal.ZERO;
+                if (complementPrice.compareTo(BigDecimal.ZERO) > 0) {
+                    sellOrderExpectAmount = complementGoodsPrice.get(sellOrder.getGoodsId()).multiply(new BigDecimal(sellOrder.getNum())).multiply(expectAmount).divide(complementPrice, 2, BigDecimal.ROUND_HALF_UP);
+                    sellOrderBalance = complementGoodsPrice.get(sellOrder.getGoodsId()).multiply(new BigDecimal(sellOrder.getNum())).multiply(goodsBalance).divide(complementPrice, 2, BigDecimal.ROUND_HALF_UP);
+                }
+                complementGoodsNum = complementGoodsNum - sellOrder.getNum();
+                if (complementGoodsNum <= 0) {
+                    sellOrderExpectAmount = expectAmount.subtract(hasRouteSellOrderExpectAmount);
+                    sellOrderBalance = goodsBalance.subtract(hasRouteSellOrderBalance);
+                }
+                hasRouteSellOrderExpectAmount = hasRouteSellOrderExpectAmount.add(sellOrderExpectAmount);
+                hasRouteSellOrderBalance = hasRouteSellOrderBalance.add(sellOrderBalance);
+
+                sellOrder.setExpectAmount(sellOrderExpectAmount);
+                sellOrder.setBalanceAmount(sellOrderBalance);
+                sellOrder.setActualAmount(sellOrderExpectAmount.subtract(sellOrderBalance));
+                sellOrder.setOrganId(order.getOrganId());
+                sellOrder.setCooperationOrganId(musicGroup.getCooperationOrganId());
+                sellOrder.setTransNo(order.getTransNo());
+                sellOrder.setOrderId(order.getId());
+                sellOrder.setOrderNo(order.getOrderNo());
+                sellOrder.setSellCost(nowGoods.getDiscountPrice());
+                sellOrder.setUserId(order.getUserId());
+                sellOrder.setPaymentChannel(order.getPaymentChannel());
+                sellOrder.setMerNo(order.getMerNos());
+                sellOrder.setSellTime(order.getCreateTime());
+                sellOrder.setCreateIme(new Date());
+                sellOrder.setUpdateTime(new Date());
+                if (nowGoods.getType().equals(GoodsType.INSTRUMENT)) {
+                    sellOrder.setType(SellTypeEnum.INSTRUMENT);
+                } else if (nowGoods.getType().equals(GoodsType.ACCESSORIES)) {
+                    sellOrder.setType(SellTypeEnum.ACCESSORIES);
+                } else {
+                    sellOrder.setType(SellTypeEnum.OTHER);
                 }
             }
-            //库存类型
-            if (goodsStockType.equals(StockType.ALL) && accountType.equals(AccountType.INTERNAL)) {
-                sellOrder.setStockType(StockType.INTERNAL);
-            } else if (goodsStockType.equals(StockType.ALL) && accountType.equals(AccountType.EXTERNAL)) {
-                sellOrder.setStockType(StockType.EXTERNAL);
-            } else {
-                sellOrder.setStockType(goodsStockType);
-            }
-            //批次号 TODO
-
-            sellOrders.add(sellOrder);
-            i++;
         }
-        sellOrderDao.batchInsert(sellOrders);
-        return sellOrders;
+        sellOrderDao.batchInsert(sellOrderList);
+        return sellOrderList;
     }
 }

+ 2 - 2
mec-biz/src/main/resources/config/mybatis/GoodsMapper.xml

@@ -63,13 +63,13 @@
         #{clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{stockWarning,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
         #{stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler})
     </insert>
-    <insert id="batchInsert">
+    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
         INSERT INTO goods
         (goods_category_id_,name_,brand_,specification_,image_,market_price_,
         discount_price_,group_purchase_price_,brief_,desc_,update_time_,create_time_,type_,agree_cost_price_,sn_,
         stock_count_,tax_stock_count_,client_show_,stock_warning_,stock_type_)
         VALUES
-        <foreach collection="goodsList" separator="," item="goods">
+        <foreach collection="list" separator="," item="goods">
             (#{goods.goodsCategoryId},#{goods.name},#{goods.brand},#{goods.specification},#{goods.image},#{goods.marketPrice},
             #{goods.discountPrice},#{goods.groupPurchasePrice},#{goods.brief},#{goods.desc},now(),now(),
             #{goods.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{goods.agreeCostPrice},#{goods.sn},

+ 1 - 1
mec-web/src/main/resources/columnMapper.ini

@@ -10,7 +10,7 @@
 商品团购价(元) = groupPurchasePrice
 商品采购价1(元) = costPrice
 商品图片(插入一张图片) = image
-商品明细 = desc
+商品明细 = brief
 商品采购价2(元) = agreeCostPrice
 内部库存 = stockCount
 税务库存 = taxStockCount