Procházet zdrojové kódy

Merge remote-tracking branch 'origin/Joburgess' into Joburgess

周箭河 před 5 roky
rodič
revize
74dca8a137

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

@@ -1,5 +1,6 @@
 package com.ym.mec.biz.dal.dao;
 
+import com.ym.mec.biz.dal.dto.GoodsSellDto;
 import com.ym.mec.biz.dal.entity.Goods;
 import com.ym.mec.common.dal.BaseDAO;
 
@@ -102,4 +103,15 @@ public interface GoodsDao extends BaseDAO<Integer, Goods> {
     String getInnerRepertoryWarnName(@Param("innerRepertoryWarnNum") String innerRepertoryWarnNum);
 
     String getOuterRepertoryWarnName(@Param("outerRepertoryWarnNum") String outerRepertoryWarnNum);
+
+    /**
+     * @describe 获取商品辅件列表
+     * @apiNote 时光荏苒,认真工作的时间总是过得很快,而我、享受这一刻!
+     * @author zouxuan
+     * @date 2020/10/13
+     * @time 15:09
+     * @param goodsId:
+     * @return java.util.List<com.ym.mec.biz.dal.dto.GoodsSellDto>
+     */
+    List<GoodsSellDto> queryGoodsSellDtos(@Param("goodsId") String goodsId);
 }

+ 10 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/SellOrderDao.java

@@ -13,6 +13,8 @@ public interface SellOrderDao extends BaseDAO<Integer, SellOrder> {
 
     int batchInsert(@Param("sellOrders") List<SellOrder> sellOrders);
 
+    int batchUpdate(@Param("sellOrders") List<SellOrder> sellOrders);
+
     /**
      * 获取订单的销售列表
      *
@@ -138,6 +140,14 @@ public interface SellOrderDao extends BaseDAO<Integer, SellOrder> {
      */
     List<SellOrder> getSellOrders(@Param("sellOrderIds") List<Integer> sellOrderIds);
 
+    /**
+     * @describe 获取无批次号商品销售记录
+     * @author Joburgess
+     * @date 2020.10.13
+     * @return java.util.List<com.ym.mec.biz.dal.entity.SellOrder>
+     */
+    List<SellOrder> getNoneBatchNoSellOrders();
+
 
     /**
      * 获取订单组合商品的子商品

+ 23 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dto/GoodsSellDto.java

@@ -3,6 +3,7 @@ package com.ym.mec.biz.dal.dto;
 import io.swagger.annotations.ApiModelProperty;
 
 import java.math.BigDecimal;
+import java.util.List;
 
 public class GoodsSellDto{
 	@ApiModelProperty(value = "商品数量", required = false)
@@ -26,6 +27,28 @@ public class GoodsSellDto{
 	@ApiModelProperty(value = "商品销售总价", required = false)
 	private BigDecimal totalGoodsPrice = BigDecimal.ZERO;
 
+    @ApiModelProperty(value = "辅件商品列表", required = false)
+    private String complementGoodsIdList;
+
+    public String getComplementGoodsIdList() {
+        return complementGoodsIdList;
+    }
+
+    public void setComplementGoodsIdList(String complementGoodsIdList) {
+        this.complementGoodsIdList = complementGoodsIdList;
+    }
+
+	@ApiModelProperty(value = "组合商品子商品", required = false)
+	private List<GoodsSellDto> goodsSellDtos;
+
+	public List<GoodsSellDto> getGoodsSellDtos() {
+		return goodsSellDtos;
+	}
+
+	public void setGoodsSellDtos(List<GoodsSellDto> goodsSellDtos) {
+		this.goodsSellDtos = goodsSellDtos;
+	}
+
 	public String getGoodsType() {
 		return goodsType;
 	}

+ 1 - 1
mec-biz/src/main/java/com/ym/mec/biz/dal/enums/StockType.java

@@ -25,6 +25,6 @@ public enum StockType implements BaseEnum<String, StockType> {
 
     @Override
     public String getCode() {
-        return null;
+        return code;
     }
 }

+ 14 - 4
mec-biz/src/main/java/com/ym/mec/biz/service/GoodsService.java

@@ -80,13 +80,12 @@ public interface GoodsService extends BaseService<Integer, Goods> {
     void repertoryWarn();
 
     /**
-     * @describe 商品批次号分配
+     * @describe 给超售商品分配批次
      * @author Joburgess
      * @date 2020.10.13
-     * @param goods:
-     * @return java.util.List<com.ym.mec.biz.dal.entity.GoodsProcurement>
+     * @return void
      */
-    List<SellOrder> goodsBatchNoAllot(Goods goods);
+    void sellOrderBatchNoAllot();
 
     /**
      * @describe 扣减商品库存
@@ -106,4 +105,15 @@ public interface GoodsService extends BaseService<Integer, Goods> {
      * @return void
      */
     void increaseStock(List<SellOrder> sellOrderIds, AccountType accountType);
+
+    /**
+     * @describe 获取辅件商品列表
+     * @apiNote 时光荏苒,认真工作的时间总是过得很快,而我、享受这一刻!
+     * @author zouxuan
+     * @date 2020/10/13
+     * @time 15:07
+     * @param goodsId:
+     * @return java.util.List<com.ym.mec.biz.dal.dto.GoodsSellDto>
+     */
+    List<GoodsSellDto> queryGoodsSellDtos(String goodsId);
 }

+ 78 - 12
mec-biz/src/main/java/com/ym/mec/biz/service/impl/GoodsServiceImpl.java

@@ -39,6 +39,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
@@ -266,6 +267,8 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		}
 		goodsProcurementDao.batchInsert(goodsProcurements);
 
+		sellOrderBatchNoAllot();
+
 		return goodsList;
 	}
 
@@ -304,9 +307,36 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 	}
 
 	@Override
-	public List<SellOrder> goodsBatchNoAllot(Goods goods) {
+	public void sellOrderBatchNoAllot() {
+		List<SellOrder> noneBatchNoSellOrders = sellOrderDao.getNoneBatchNoSellOrders();
+		if(CollectionUtils.isEmpty(noneBatchNoSellOrders)){
+			return;
+		}
+		List<SellOrder> updateSellOrders = new ArrayList<>();
+		for (SellOrder noneBatchNoSellOrder : noneBatchNoSellOrders) {
+			GoodsProcurement goodsProcurement = null;
+			if(StockType.INTERNAL.equals(noneBatchNoSellOrder.getStockType())||(StockType.ALL.equals(noneBatchNoSellOrder.getStockType())&&AccountType.INTERNAL.equals(noneBatchNoSellOrder.getAccountType()))){
+				goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(noneBatchNoSellOrder.getGoodsId());
+				if(Objects.nonNull(goodsProcurement)){
+					goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).incrementAndGet());
+				}
+			}else if(StockType.EXTERNAL.equals(noneBatchNoSellOrder.getStockType())||(StockType.ALL.equals(noneBatchNoSellOrder.getStockType())&&AccountType.EXTERNAL.equals(noneBatchNoSellOrder.getAccountType()))){
+				goodsProcurement = goodsProcurementDao.getWithTaxStockSurplusProcurement(noneBatchNoSellOrder.getGoodsId());
+				if(Objects.nonNull(goodsProcurement)){
+					goodsProcurement.setTaxStockSoldNum(new AtomicInteger(goodsProcurement.getTaxStockSoldNum()).incrementAndGet());
+				}
+			}
 
-		return null;
+			if(Objects.nonNull(goodsProcurement)){
+				goodsProcurementDao.update(goodsProcurement);
+				noneBatchNoSellOrder.setBatchNo(goodsProcurement.getBatchNo());
+				noneBatchNoSellOrder.setSellCost(goodsProcurement.getDiscountPrice());
+				updateSellOrders.add(noneBatchNoSellOrder);
+			}
+		}
+		if(!CollectionUtils.isEmpty(updateSellOrders)){
+			sellOrderDao.batchUpdate(updateSellOrders);
+		}
 	}
 
 	@Override
@@ -320,7 +350,7 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 
 		List<Goods> tempGoodsList = goodsDao.lockGoods(new ArrayList<>(goodsIds));
 		Map<Integer, Goods> idTempGoodsMap = tempGoodsList.stream().collect(Collectors.toMap(Goods::getId, g -> g));
-		List<SellOrder> goodsBatchNoDtos = new ArrayList<>();
+		List<GoodsProcurement> goodsProcurements = new ArrayList<>();
 		for (Integer goodsId : goodsIds) {
 			Goods tempGoods = idTempGoodsMap.get(goodsId);
 			List<Goods> childGoods = new ArrayList<>();
@@ -337,14 +367,12 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					goodsProcurement = goodsProcurementDao.getWithStockSurplusProcurement(goods.getId());
 					goods.setStockCount(new AtomicInteger(goods.getStockCount()).decrementAndGet());
 					if(Objects.nonNull(goodsProcurement)){
-						goodsProcurement.setStockCount(new AtomicInteger(goodsProcurement.getStockCount()).decrementAndGet());
 						goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).incrementAndGet());
 					}
 				}else if(StockType.EXTERNAL.equals(goods.getStockType())||(StockType.ALL.equals(goods.getStockType())&&AccountType.EXTERNAL.equals(accountType))){
 					goodsProcurement = goodsProcurementDao.getWithTaxStockSurplusProcurement(goods.getId());
 					goods.setTaxStockCount(new AtomicInteger(goods.getTaxStockCount()).decrementAndGet());
 					if(Objects.nonNull(goodsProcurement)){
-						goodsProcurement.setTaxStockCount(new AtomicInteger(goodsProcurement.getTaxStockCount()).decrementAndGet());
 						goodsProcurement.setTaxStockSoldNum(new AtomicInteger(goodsProcurement.getTaxStockSoldNum()).incrementAndGet());
 					}
 				}
@@ -359,12 +387,47 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 					goodsProcurement.setParentGoodsId(tempGoods.getId());
 				}
 
-//				goodsBatchNoDtos.add(goodsProcurement);
+				goodsProcurements.add(goodsProcurement);
 			}
 //			goodsDao.update(tempGoods);
 		}
 		goodsDao.batchUpdate(tempGoodsList);
-		return goodsBatchNoDtos;
+
+		List<SellOrder> sellOrders = new ArrayList<>();
+
+		List<GoodsProcurement> singleGoodsList = goodsProcurements.stream().filter(g -> Objects.isNull(g.getParentGoodsId())&&Objects.nonNull(g.getBatchNo())).collect(Collectors.toList());
+		if(!CollectionUtils.isEmpty(singleGoodsList)){
+			Map<String, List<Integer>> batchNoGoodsIdMap = singleGoodsList.stream().collect(Collectors.groupingBy(GoodsProcurement::getBatchNo, Collectors.mapping(GoodsProcurement::getGoodsId, Collectors.toList())));
+			for (Map.Entry<String, List<Integer>> batchNoGoodsIdMapEntry : batchNoGoodsIdMap.entrySet()) {
+				Map<Integer, Long> goodsNumMap = batchNoGoodsIdMapEntry.getValue().stream().collect(Collectors.groupingBy(gid -> gid, Collectors.counting()));
+				for (Map.Entry<Integer, Long> goodsNumMapEntry : goodsNumMap.entrySet()) {
+					SellOrder sellOrder = new SellOrder();
+					sellOrder.setGoodsId(goodsNumMapEntry.getKey());
+					sellOrder.setNum(goodsNumMapEntry.getValue().intValue());
+					sellOrder.setBatchNo(batchNoGoodsIdMapEntry.getKey());
+					GoodsProcurement goodsProcurement = goodsProcurementDao.getWithGoodsAndBatchNo(sellOrder.getGoodsId(), sellOrder.getBatchNo());
+					sellOrder.setSellCost(goodsProcurement.getDiscountPrice().multiply(new BigDecimal(sellOrder.getNum())));
+					sellOrders.add(sellOrder);
+				}
+			}
+		}
+
+		List<GoodsProcurement> groupGoodsList = goodsProcurements.stream().filter(g -> Objects.nonNull(g.getParentGoodsId())||Objects.isNull(g.getBatchNo())).collect(Collectors.toList());
+		if(!CollectionUtils.isEmpty(groupGoodsList)){
+			for (GoodsProcurement goodsProcurement : groupGoodsList) {
+				SellOrder sellOrder = new SellOrder();
+				sellOrder.setParentGoodsId(goodsProcurement.getParentGoodsId());
+				sellOrder.setGoodsId(goodsProcurement.getGoodsId());
+				sellOrder.setNum(1);
+				sellOrder.setBatchNo(goodsProcurement.getBatchNo());
+				if(Objects.nonNull(goodsProcurement.getBatchNo())){
+					sellOrder.setSellCost(goodsProcurement.getDiscountPrice().multiply(new BigDecimal(sellOrder.getNum())));
+				}
+				sellOrders.add(sellOrder);
+			}
+		}
+
+		return sellOrders;
 	}
 
 	@Override
@@ -392,13 +455,11 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 			if(StockType.INTERNAL.equals(goods.getStockType())||(StockType.ALL.equals(goods.getStockType())&&AccountType.INTERNAL.equals(accountType))){
 				goods.setStockCount(new AtomicInteger(goods.getStockCount()).addAndGet(sellOrder.getNum()));
 				if(Objects.nonNull(goodsProcurement)){
-					goodsProcurement.setStockCount(new AtomicInteger(goodsProcurement.getStockCount()).addAndGet(sellOrder.getNum()));
 					goodsProcurement.setStockSoldNum(new AtomicInteger(goodsProcurement.getStockSoldNum()).addAndGet(-sellOrder.getNum()));
 				}
 			}else if(StockType.EXTERNAL.equals(goods.getStockType())||(StockType.ALL.equals(goods.getStockType())&&AccountType.EXTERNAL.equals(accountType))){
 				goods.setTaxStockCount(new AtomicInteger(goods.getTaxStockCount()).addAndGet(sellOrder.getNum()));
 				if(Objects.nonNull(goodsProcurement)){
-					goodsProcurement.setTaxStockCount(new AtomicInteger(goodsProcurement.getTaxStockCount()).addAndGet(sellOrder.getNum()));
 					goodsProcurement.setTaxStockSoldNum(new AtomicInteger(goodsProcurement.getTaxStockSoldNum()).addAndGet(-sellOrder.getNum()));
 				}
 			}
@@ -410,13 +471,18 @@ public class GoodsServiceImpl extends BaseServiceImpl<Integer, Goods>  implement
 		}
 
 		//处理组合商品
-		Map<Integer, Long> goodsNumMap = sellOrders.stream().filter(so -> Objects.nonNull(so.getParentGoodsId())).collect(Collectors.groupingBy(SellOrder::getId, Collectors.counting()));
+		Map<Integer, Long> goodsNumMap = sellOrders.stream().filter(so -> Objects.nonNull(so.getParentGoodsId())).collect(Collectors.groupingBy(SellOrder::getParentGoodsId, Collectors.counting()));
 		for (Map.Entry<Integer, Long> goodsIdNumMapEntry : goodsNumMap.entrySet()) {
 			Goods goods = idGoodsMap.get(goodsIdNumMapEntry.getKey());
-			int complementGoodsSize = goods.getComplementGoodsIdList().split(",").length;
-			int sellNum = (int) (goodsIdNumMapEntry.getValue()/complementGoodsSize);
+			int goodSize = (int) sellOrders.stream().filter(so -> goodsIdNumMapEntry.getKey().equals(so.getParentGoodsId())).count();
+			int sellNum = (int) (goodSize/goodsIdNumMapEntry.getValue());
 			goods.setSellCount(new AtomicInteger(goods.getSellCount()).addAndGet(-sellNum));
 			goodsDao.update(goods);
 		}
 	}
+
+	@Override
+	public List<GoodsSellDto> queryGoodsSellDtos(String goodsId) {
+		return goodsDao.queryGoodsSellDtos(goodsId);
+	}
 }

+ 91 - 43
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRepairServiceImpl.java

@@ -50,6 +50,8 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
     @Autowired
     private StudentPaymentOrderService studentPaymentOrderService;
     @Autowired
+    private SysPaymentConfigService sysPaymentConfigService;
+    @Autowired
     private MusicGroupDao musicGroupDao;
     @Autowired
     private SysUserCashAccountService sysUserCashAccountService;
@@ -134,6 +136,12 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         if (studentId == null) {
             throw new BizException("请指定学员");
         }
+
+        goodsSellDtos.forEach(e->{
+            if(StringUtils.isNotEmpty(e.getComplementGoodsIdList())){
+                e.setGoodsSellDtos(goodsService.queryGoodsSellDtos(e.getComplementGoodsIdList()));
+            }
+        });
         studentDao.lockUser(studentId);
         SysUser student = sysUserFeignService.queryUserById(studentId);
         //如果教务老师为空,代表学员自己创建的订单
@@ -738,6 +746,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         StudentGoodsSell studentGoodsSell = studentGoodsSellDao.findByOrderNo(orderNo);
         String goodsJson = studentGoodsSell.getGoodsJson();
         List<GoodsSellDto> goodsSellDtos = JSONObject.parseArray(goodsJson, GoodsSellDto.class);
+        //用户选择的商品
         Map<Integer, List<GoodsSellDto>> collect = goodsSellDtos.stream().collect(Collectors.groupingBy(GoodsSellDto::getGoodsId));
         List<Integer> goodsId = goodsSellDtos.stream().map(e -> e.getGoodsId()).collect(Collectors.toList());
 
@@ -747,34 +756,10 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                 goodsIds.add(e.getGoodsId());
             }
         });
-        List<GoodsSellDto> goodsSellDtoList = new ArrayList<>();
-        //扣减库存
-        List<SellOrder> goodsBatchNoDtos = goodsService.subtractStock(goodsIds, AccountType.EXTERNAL);
-
-        Map<String, List<SellOrder>> listMap = goodsBatchNoDtos.stream().collect(Collectors.groupingBy(SellOrder::getBatchNo));
-        Set<String> listMapKeys = listMap.keySet();
-        listMapKeys.forEach(e->{
-            listMap.get(e).forEach(z->{
-                GoodsSellDto goodsSellDto1 = collect.get(z.getGoodsId()).get(0);
-                GoodsSellDto goodsSellDto = new GoodsSellDto();
-                if(StringUtils.isEmpty(z.getBatchNo())){
-                    goodsSellDto.setGoodsNum(1);
-                    goodsSellDto.setTotalGoodsPrice(goodsSellDto1.getGoodsPrice());
-                    goodsSellDto.setGoodsPrice(goodsSellDto1.getGoodsPrice());
-                    goodsSellDto.setGoodsId(goodsSellDto1.getGoodsId());
-                    goodsSellDto.setGoodsName(goodsSellDto1.getGoodsName());
-                    goodsSellDto.setGoodsType(goodsSellDto1.getGoodsType());
-                    goodsSellDto.setImage(goodsSellDto1.getImage());
-                }else {
-
-                }
-            });
-        });
-
-        List<Goods> goodsByIds = goodsService.findGoodsByIds(StringUtils.join(goodsId, ","));
-        Map<Integer, List<Goods>> goodsMap = goodsByIds.stream().collect(Collectors.groupingBy(Goods::getId));
+        AccountType accountType = sysPaymentConfigService.checkAccountType(PaymentChannelEnum.valueOf(orderByOrderNo.getPaymentChannel()), orderByOrderNo.getMerNos());
+        //实际扣减的库存商品
+        List<SellOrder> sellOrderList = goodsService.subtractStock(goodsIds, accountType);
         Map<String, BigDecimal> costMap = new HashMap<>(2);
-        List<SellOrder> sellOrders = new ArrayList<>();
         BigDecimal balancePaymentAmount = orderByOrderNo.getBalancePaymentAmount();
         //可用余额
         BigDecimal usableBalance = balancePaymentAmount;
@@ -783,6 +768,10 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         BigDecimal usableMarketAmount = marketAmount;
         //包含减免余额的总金额
         BigDecimal totalAmount = goodsSellDtos.stream().map(e -> e.getTotalGoodsPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);
+        //组合商品减免金额
+        Map<Integer,BigDecimal> parentGoodsCutTotalPriceMap = new HashMap<>();
+        //组合商品附加金额
+        Map<Integer,BigDecimal> parentGoodsAddTotalPriceMap = new HashMap<>();
         //应付总额
         BigDecimal totalActual = totalAmount.subtract(marketAmount);
         //可用应付
@@ -791,23 +780,83 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         BigDecimal realityAmount = totalActual.subtract(usableBalance);
         //可用实付总额
         BigDecimal usableAmount = realityAmount;
-        for (int i = 0; i < goodsId.size(); i++) {
-            Integer e = goodsId.get(i);
-            GoodsSellDto goodsSellDto = collect.get(e).get(0);
-            Goods goods = goodsMap.get(e).get(0);
+        //可用组合商品总减免金额
+        BigDecimal usableParentGoodsCutTotalPrice;
+        //可用组合商品总附加金额
+        BigDecimal usableParentGoodsAddTotalPrice;
+
+        //获取组合商品列表
+        List<SellOrder> orderList = sellOrderList.stream().filter(sellOrder -> sellOrder.getParentGoodsId() != null).collect(Collectors.toList());
+        for (SellOrder sellOrder : orderList) {
+            //获取组合商品销售总价
+            GoodsSellDto goodsSellDto = collect.get(sellOrder.getParentGoodsId()).get(0);
+            BigDecimal parentTotalGoodsPrice = goodsSellDto.getTotalGoodsPrice();
+            BigDecimal totalGoodsPrice = BigDecimal.ZERO;
+            //获取单个商品销售总价
+            for (GoodsSellDto sellDto : goodsSellDto.getGoodsSellDtos()) {
+                totalGoodsPrice = totalGoodsPrice.add(sellDto.getTotalGoodsPrice());
+            }
+            //如果组合价大于预计总价
+            if (parentTotalGoodsPrice.compareTo(totalGoodsPrice) == 1){
+                parentGoodsAddTotalPriceMap.put(sellOrder.getParentGoodsId(),parentTotalGoodsPrice.subtract(totalGoodsPrice));
+            }else if(parentTotalGoodsPrice.compareTo(totalGoodsPrice) == -1){//小于
+                parentGoodsCutTotalPriceMap.put(sellOrder.getParentGoodsId(),totalGoodsPrice.subtract(parentTotalGoodsPrice));
+            }
+        }
+        noAmount:for (SellOrder sellOrder : orderList) {
+            GoodsSellDto goodsSellDto = collect.get(sellOrder.getParentGoodsId()).get(0);
+            List<GoodsSellDto> goodsSellDtoList = goodsSellDto.getGoodsSellDtos();
+            BigDecimal addTotalPrice = parentGoodsAddTotalPriceMap.get(sellOrder.getParentGoodsId());
+            usableParentGoodsAddTotalPrice = addTotalPrice;
+            BigDecimal cutTotalPrice = parentGoodsCutTotalPriceMap.get(sellOrder.getParentGoodsId());
+            usableParentGoodsCutTotalPrice = cutTotalPrice;
+            for (int i = 0; i < goodsSellDtoList.size(); i++) {
+                GoodsSellDto sellDto = goodsSellDtoList.get(i);
+                //获取比例
+                BigDecimal ratioAmount = sellDto.getTotalGoodsPrice().divide(goodsSellDto.getTotalGoodsPrice(), 2, BigDecimal.ROUND_HALF_UP);
+
+                if(addTotalPrice.doubleValue() > 0l){
+                    //如果是最后一件商品
+                    if(i == goodsId.size() - 1){
+                        sellDto.setTotalGoodsPrice(sellDto.getTotalGoodsPrice().add(usableParentGoodsAddTotalPrice));
+                    }else {
+                        //获取分配的附加
+                        BigDecimal multiply = ratioAmount.multiply(addTotalPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
+                        sellDto.setTotalGoodsPrice(sellDto.getTotalGoodsPrice().add(multiply));
+                        usableParentGoodsAddTotalPrice = usableParentGoodsAddTotalPrice.subtract(multiply);
+                    }
+                }else if(cutTotalPrice.doubleValue() > 0l){
+                    //如果是最后一件商品
+                    if(i == goodsId.size() - 1){
+                        sellDto.setTotalGoodsPrice(sellDto.getTotalGoodsPrice().subtract(usableParentGoodsCutTotalPrice));
+                    }else {
+                        //获取分配的组合减免金额
+                        BigDecimal multiply = ratioAmount.multiply(cutTotalPrice).setScale(2,BigDecimal.ROUND_HALF_UP);
+                        sellDto.setTotalGoodsPrice(sellDto.getTotalGoodsPrice().subtract(multiply));
+                        usableParentGoodsCutTotalPrice = usableParentGoodsCutTotalPrice.subtract(multiply);
+                    }
+                }else {
+                    continue noAmount;
+                }
+            }
+        }
 
-            costMap.put("sellCost", goods.getDiscountPrice());
-            if (goods.getAgreeCostPrice() != null) {
-                costMap.put("SellCost2", goods.getAgreeCostPrice());
+        for (int i = 0; i < sellOrderList.size(); i++) {
+            SellOrder sellOrder = sellOrderList.get(i);
+            Integer e = null;
+            if(sellOrder.getParentGoodsId() != null){
+                e = sellOrder.getParentGoodsId();
+            }else {
+                e = sellOrder.getGoodsId();
             }
-            SellOrder sellOrder = new SellOrder();
+            GoodsSellDto goodsSellDto = collect.get(e).get(0);
             sellOrder.setOrganId(orderByOrderNo.getOrganId());
             sellOrder.setTransNo(orderByOrderNo.getTransNo());
             sellOrder.setOrderId(orderByOrderNo.getId());
             sellOrder.setOrderNo(orderByOrderNo.getOrderNo());
+            goodsSellDto.setTotalGoodsPrice(goodsSellDto.getGoodsPrice().multiply(new BigDecimal(sellOrder.getNum())));
             //获取比例
             BigDecimal ratioAmount = goodsSellDto.getTotalGoodsPrice().divide(totalAmount, 2, BigDecimal.ROUND_HALF_UP);
-
             //如果有减免金额
             if(marketAmount.doubleValue() > 0l){
                 //如果是最后一件商品
@@ -815,7 +864,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                     goodsSellDto.setTotalGoodsPrice(goodsSellDto.getTotalGoodsPrice().subtract(usableMarketAmount));
                 }else {
                     //获取分配的减免金额
-                    BigDecimal multiply = ratioAmount.multiply(usableMarketAmount).setScale(2,BigDecimal.ROUND_HALF_UP);
+                    BigDecimal multiply = ratioAmount.multiply(marketAmount).setScale(2,BigDecimal.ROUND_HALF_UP);
                     goodsSellDto.setTotalGoodsPrice(goodsSellDto.getTotalGoodsPrice().subtract(multiply));
                     usableMarketAmount = usableMarketAmount.subtract(multiply);
                 }
@@ -843,11 +892,11 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                 }
             }
             sellOrder.setExpectAmount(sellOrder.getActualAmount().add(sellOrder.getBalanceAmount()));
-            sellOrder.setSellCost(goods.getDiscountPrice());
+//            sellOrder.setSellCost(goods.getDiscountPrice());
             sellOrder.setSellCost2(JSONObject.toJSONString(costMap));
-            sellOrder.setType(SellTypeEnum.valueOf(goods.getType().getCode()));
+            sellOrder.setType(SellTypeEnum.valueOf(goodsSellDto.getGoodsType()));
             sellOrder.setGoodsId(e);
-            sellOrder.setGoodsName(goods.getName());
+            sellOrder.setGoodsName(goodsSellDto.getGoodsName());
             sellOrder.setNum(goodsSellDto.getGoodsNum());
             sellOrder.setUserId(studentGoodsSell.getUserId());
             sellOrder.setPaymentChannel(orderByOrderNo.getPaymentChannel());
@@ -855,10 +904,9 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             sellOrder.setSellTime(orderByOrderNo.getPayTime());
             sellOrder.setEduTeacherId(studentGoodsSell.getTeacherId());
             sellOrder.setCooperationOrganId(studentGoodsSell.getCooperationOrganId());
-            sellOrders.add(sellOrder);
         }
-        if(sellOrders.size() > 0){
-            sellOrderService.batchInsert(sellOrders);
+        if(sellOrderList.size() > 0){
+            sellOrderService.batchInsert(sellOrderList);
         }
     }
 

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

@@ -34,6 +34,7 @@
         <result column="complement_goods_id_list_" property="complementGoodsIdList"/>
         <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
         <result column="supply_channel_" property="supplyChannel" />
+        <result column="stock_type_" property="stockType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
         <result column="client_show_" property="clientShow" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
         <result column="stock_warning_" property="stockWarning" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
     </resultMap>
@@ -228,7 +229,7 @@
                 <if test="goods.clientShow != null">
                     client_show_ = #{goods.clientShow,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                 </if>
-                <if test="stockWarning != null">
+                <if test="goods.stockWarning != null">
                     stock_warning_ = #{goods.stockWarning,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                 </if>
                     update_time_ = NOW()
@@ -336,4 +337,15 @@
     <select id="getWithComplementGoodsAndStatus" resultMap="Goods">
         SELECT * FROM goods WHERE status_ = #{status} AND FIND_IN_SET(#{goodsId}, complement_goods_id_list_)
     </select>
+    <resultMap id="GoodsSellDtoMap" type="com.ym.mec.biz.dal.dto.GoodsSellDto">
+        <result property="goodsId" column="id_"/>
+        <result property="image" column="image_"/>
+        <result property="goodsName" column="name_"/>
+        <result property="goodsType" column="type_"/>
+        <result property="goodsPrice" column="market_price_"/>
+        <result property="totalGoodsPrice" column="market_price_"/>
+    </resultMap>
+    <select id="queryGoodsSellDtos" resultMap="GoodsSellDtoMap">
+        SELECT id_,image_,name_,type_,market_price_,market_price_ FROM goods WHERE FIND_IN_SET(id_,#{goodsId})
+    </select>
 </mapper>

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

@@ -134,11 +134,11 @@
 	</select>
 
 	<select id="getWithStockSurplusProcurement" resultMap="GoodsProcurement">
-		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND stock_count_>stock_sold_num_
+		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND stock_count_>stock_sold_num_ ORDER BY create_time_ LIMIT 1
 	</select>
 
 	<select id="getWithTaxStockSurplusProcurement" resultMap="GoodsProcurement">
-		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND tax_stock_count_>tax_stock_sold_num_
+		SELECT * FROM goods_procurement WHERE goods_id_ = #{goodsId} AND tax_stock_count_>tax_stock_sold_num_ ORDER BY create_time_ LIMIT 1
 	</select>
 
 	<select id="getWithGoodsAndBatchNo" resultMap="GoodsProcurement">

+ 91 - 0
mec-biz/src/main/resources/config/mybatis/SellOrderMapper.xml

@@ -35,6 +35,8 @@
         <result column="create_ime_" property="createIme"/>
         <result column="update_time_" property="updateTime"/>
         <result column="receive_status_" property="receiveStatus"/>
+        <result column="stock_type_" property="stockType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
+        <result column="account_type_" property="accountType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
     </resultMap>
     <sql id="Base_Column_List">
         <!--@mbg.generated-->
@@ -153,6 +155,91 @@
         where id_ = #{id}
     </update>
 
+    <update id="batchUpdate" parameterType="com.ym.mec.biz.dal.entity.SellOrder">
+        <foreach collection="sellOrders" item="sellOrder" separator=";">
+            update sell_order
+            <set>
+                <if test="sellOrder.eduTeacherId != null">
+                    edu_teacher_id_ = #{sellOrder.eduTeacherId},
+                </if>
+                <if test="sellOrder.organId != null">
+                    organ_id_ = #{sellOrder.organId},
+                </if>
+                <if test="sellOrder.cooperationOrganId != null">
+                    cooperation_organ_id_ = #{sellOrder.cooperationOrganId},
+                </if>
+                <if test="sellOrder.transNo != null">
+                    trans_no_ = #{sellOrder.transNo},
+                </if>
+                <if test="sellOrder.orderId != null">
+                    order_id_ = #{sellOrder.orderId},
+                </if>
+                <if test="sellOrder.orderNo != null">
+                    order_no_ = #{sellOrder.orderNo},
+                </if>
+                <if test="sellOrder.expectAmount != null">
+                    expect_amount_ = #{sellOrder.expectAmount},
+                </if>
+                <if test="sellOrder.actualAmount != null">
+                    actual_amount_ = #{sellOrder.actualAmount},
+                </if>
+                <if test="sellOrder.balanceAmount != null">
+                    balance_amount_ = #{sellOrder.balanceAmount},
+                </if>
+                <if test="sellOrder.type != null">
+                    type_ = #{sellOrder.type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
+                </if>
+                <if test="sellOrder.parentGoodsId != null">
+                    parent_goods_id_ = #{sellOrder.parentGoodsId},
+                </if>
+                <if test="sellOrder.goodsId != null">
+                    goods_id_ = #{sellOrder.goodsId},
+                </if>
+                <if test="sellOrder.goodsName != null">
+                    goods_name_ = #{sellOrder.goodsName},
+                </if>
+                <if test="sellOrder.sellCost != null">
+                    sell_cost_ = #{sellOrder.sellCost},
+                </if>
+                <if test="sellOrder.sellCost2 != null">
+                    sell_cost2_ = #{sellOrder.sellCost2},
+                </if>
+                <if test="sellOrder.num != null">
+                    num_ = #{sellOrder.num},
+                </if>
+                <if test="sellOrder.userId != null">
+                    user_id_ = #{sellOrder.userId},
+                </if>
+                <if test="sellOrder.paymentChannel != null">
+                    payment_channel_ = #{sellOrder.paymentChannel},
+                </if>
+                <if test="sellOrder.merNo != null">
+                    mer_no_ = #{sellOrder.merNo},
+                </if>
+                <if test="sellOrder.batchNo != null">
+                    batch_no_ = #{sellOrder.batchNo},
+                </if>
+                <if test="sellOrder.stockType != null">
+                    stock_type_ = #{sellOrder.stockType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
+                </if>
+                <if test="sellOrder.accountType != null">
+                    account_type_ = #{sellOrder.accountType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
+                </if>
+                <if test="sellOrder.status != null">
+                    status_ = #{sellOrder.status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
+                </if>
+                <if test="sellOrder.sellTime != null">
+                    sell_time_ = #{sellOrder.sellTime},
+                </if>
+                <if test="sellOrder.createIme != null">
+                    create_ime_ = #{sellOrder.createIme},
+                </if>
+                    update_time_ = NOW()
+            </set>
+            where id_ = #{sellOrder.id}
+        </foreach>
+    </update>
+
     <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
         insert into sell_order (organ_id_, cooperation_organ_id_, trans_no_,order_id_, order_no_, expect_amount_,
         actual_amount_,balance_amount_, type_, parent_goods_id_, goods_id_,goods_name_, sell_cost_, sell_cost2_, num_,
@@ -416,4 +503,8 @@
             AND parent_goods_id_ = #{parentGoodsId}
         </if>
     </select>
+
+    <select id="getNoneBatchNoSellOrders" resultMap="SellOrder">
+        SELECT * FROM sell_order WHERE status_ = 0 AND stock_type_ IS NOT NULL AND batch_no_ IS NULL
+    </select>
 </mapper>