|  | @@ -10,18 +10,21 @@ import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
 | 
	
		
			
				|  |  |  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.StudentPaymentRouteOrderService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.dal.BaseDAO;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.exception.BizException;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.IdGeneratorService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.impl.BaseServiceImpl;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.collection.MapUtil;
 | 
	
		
			
				|  |  | +import org.apache.ibatis.javassist.expr.NewArray;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  |  import java.util.*;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, StudentPaymentRouteOrder> implements StudentPaymentRouteOrderService {
 | 
	
	
		
			
				|  | @@ -38,6 +41,8 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
 | 
	
		
			
				|  |  |      private SellOrderDao sellOrderDao;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private GoodsDao goodsDao;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private GoodsService goodsService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public BaseDAO<Long, StudentPaymentRouteOrder> getDAO() {
 | 
	
	
		
			
				|  | @@ -107,10 +112,15 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
 | 
	
		
			
				|  |  |              BigDecimal hasRouteAmount = BigDecimal.ZERO;
 | 
	
		
			
				|  |  |              BigDecimal goodsTotalPrice = BigDecimal.ZERO;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +            List<Integer> goodsIdList = new ArrayList<>();
 | 
	
		
			
				|  |  |              for (Map.Entry<Integer, Integer> goodsMap : studentPaymentRouteOrderDto.getGoodies().entrySet()) {
 | 
	
		
			
				|  |  |                  Goods goods = goodsDao.get(goodsMap.getKey());
 | 
	
		
			
				|  |  |                  goodsTotalPrice = goodsTotalPrice.add(goods.getGroupPurchasePrice().multiply(new BigDecimal(goodsMap.getValue())));
 | 
	
		
			
				|  |  | +                for (int i = 0; i < goodsMap.getValue(); i++) {
 | 
	
		
			
				|  |  | +                    goodsIdList.add(goodsMap.getKey());
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | +            List<SellOrder> sellOrderList = goodsService.subtractStock(goodsIdList, AccountType.INTERNAL);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              int i = 1;
 | 
	
		
			
				|  |  |              for (Map.Entry<Integer, Integer> goodsMap : studentPaymentRouteOrderDto.getGoodies().entrySet()) {
 | 
	
	
		
			
				|  | @@ -126,45 +136,49 @@ public class StudentPaymentRouteOrderServiceImpl extends BaseServiceImpl<Long, S
 | 
	
		
			
				|  |  |                  hasRouteAmount = hasRouteAmount.add(actualAmount);
 | 
	
		
			
				|  |  |                  i++;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                Map<String, BigDecimal> CostMap = new HashMap<>();
 | 
	
		
			
				|  |  | -                CostMap.put("sellCost", goods.getDiscountPrice());
 | 
	
		
			
				|  |  | -                if (goods.getAgreeCostPrice() != null) {
 | 
	
		
			
				|  |  | -                    CostMap.put("SellCost2", goods.getAgreeCostPrice());
 | 
	
		
			
				|  |  | +                int complementNum = goods.getComplementGoodsIdList() == null ? 1 : goods.getComplementGoodsIdList().split(",").length;
 | 
	
		
			
				|  |  | +                int goodsNum = goodsMap.getValue() * complementNum;
 | 
	
		
			
				|  |  | +                BigDecimal complementPrice = goods.getGroupPurchasePrice().multiply(new BigDecimal(goodsMap.getValue()));
 | 
	
		
			
				|  |  | +                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).multiply(new BigDecimal(goodsMap.getValue()));
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -                SellOrder sellOrder = new SellOrder();
 | 
	
		
			
				|  |  | -                sellOrder.setSellTime(studentPaymentRouteOrderDto.getPayTime());
 | 
	
		
			
				|  |  | -                sellOrder.setOrganId(studentPaymentRouteOrderDto.getOrganId());
 | 
	
		
			
				|  |  | -                sellOrder.setCooperationOrganId(studentPaymentRouteOrderDto.getSchoolId());
 | 
	
		
			
				|  |  | -                sellOrder.setTransNo(studentPaymentRouteOrderDto.getTransNo());
 | 
	
		
			
				|  |  | -                sellOrder.setOrderId(studentPaymentRouteOrderDto.getId());
 | 
	
		
			
				|  |  | -                sellOrder.setOrderNo(orderNo);
 | 
	
		
			
				|  |  | -                sellOrder.setGoodsId(goods.getId());
 | 
	
		
			
				|  |  | -                sellOrder.setGoodsName(goods.getName());
 | 
	
		
			
				|  |  | -                sellOrder.setActualAmount(actualAmount);
 | 
	
		
			
				|  |  | -                sellOrder.setBalanceAmount(BigDecimal.ZERO);
 | 
	
		
			
				|  |  | -                sellOrder.setExpectAmount(actualAmount);
 | 
	
		
			
				|  |  | -                sellOrder.setSellCost(goods.getDiscountPrice());
 | 
	
		
			
				|  |  | -                sellOrder.setSellCost2(JSON.toJSONString(CostMap));
 | 
	
		
			
				|  |  | -                sellOrder.setNum(goodsMap.getValue());
 | 
	
		
			
				|  |  | -                sellOrder.setUserId(studentPaymentRouteOrderDto.getUserId());
 | 
	
		
			
				|  |  | -                sellOrder.setPaymentChannel(studentPaymentRouteOrderDto.getPaymentChannel());
 | 
	
		
			
				|  |  | -                sellOrder.setMerNo(studentPaymentRouteOrderDto.getMerNos());
 | 
	
		
			
				|  |  | -                sellOrder.setAccountType(AccountType.INTERNAL);
 | 
	
		
			
				|  |  | -                sellOrder.setSellTime(studentPaymentRouteOrderDto.getCreateTime());
 | 
	
		
			
				|  |  | -                sellOrder.setCreateIme(nowDate);
 | 
	
		
			
				|  |  | -                sellOrder.setUpdateTime(nowDate);
 | 
	
		
			
				|  |  | -                sellOrder.setType(SellTypeEnum.SCHOOL_BUY);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                //库存类型
 | 
	
		
			
				|  |  | -                if(goods.getStockType().equals(StockType.ALL)){
 | 
	
		
			
				|  |  | -                    sellOrder.setStockType(StockType.INTERNAL);
 | 
	
		
			
				|  |  | -                }else {
 | 
	
		
			
				|  |  | -                    sellOrder.setStockType(goods.getStockType());
 | 
	
		
			
				|  |  | +                BigDecimal hasRouteSellOrderActualAmount = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | +                for (SellOrder sellOrder : sellOrderList) {
 | 
	
		
			
				|  |  | +                    if (!goodsMap.getKey().equals(sellOrder.getParentGoodsId()) && !goodsMap.getKey().equals(sellOrder.getParentGoodsId())) {
 | 
	
		
			
				|  |  | +                        continue;
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    BigDecimal sellOrderActualAmount = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | +                    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);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    sellOrder.setSellTime(studentPaymentRouteOrderDto.getPayTime());
 | 
	
		
			
				|  |  | +                    sellOrder.setOrganId(studentPaymentRouteOrderDto.getOrganId());
 | 
	
		
			
				|  |  | +                    sellOrder.setCooperationOrganId(studentPaymentRouteOrderDto.getSchoolId());
 | 
	
		
			
				|  |  | +                    sellOrder.setTransNo(studentPaymentRouteOrderDto.getTransNo());
 | 
	
		
			
				|  |  | +                    sellOrder.setOrderId(studentPaymentRouteOrderDto.getId());
 | 
	
		
			
				|  |  | +                    sellOrder.setOrderNo(orderNo);
 | 
	
		
			
				|  |  | +                    sellOrder.setActualAmount(sellOrderActualAmount);
 | 
	
		
			
				|  |  | +                    sellOrder.setBalanceAmount(BigDecimal.ZERO);
 | 
	
		
			
				|  |  | +                    sellOrder.setExpectAmount(sellOrderActualAmount);
 | 
	
		
			
				|  |  | +                    sellOrder.setUserId(studentPaymentRouteOrderDto.getUserId());
 | 
	
		
			
				|  |  | +                    sellOrder.setPaymentChannel(studentPaymentRouteOrderDto.getPaymentChannel());
 | 
	
		
			
				|  |  | +                    sellOrder.setMerNo(studentPaymentRouteOrderDto.getMerNos());
 | 
	
		
			
				|  |  | +                    sellOrder.setSellTime(studentPaymentRouteOrderDto.getCreateTime());
 | 
	
		
			
				|  |  | +                    sellOrder.setCreateIme(nowDate);
 | 
	
		
			
				|  |  | +                    sellOrder.setUpdateTime(nowDate);
 | 
	
		
			
				|  |  | +                    sellOrder.setType(SellTypeEnum.SCHOOL_BUY);
 | 
	
		
			
				|  |  | +                    sellOrders.add(sellOrder);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  | -                //批次号 TODO
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -                sellOrders.add(sellOrder);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              sellOrderDao.batchInsert(sellOrders);
 | 
	
		
			
				|  |  |          }
 |