|  | @@ -5,8 +5,8 @@ import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  |  import com.ym.mec.auth.api.client.SysUserFeignService;
 | 
	
		
			
				|  |  |  import com.ym.mec.auth.api.entity.SysUser;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.dao.SellOrderDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.StudentDao;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.dao.StudentGoodsSellDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.StudentRepairDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dao.SysConfigDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dto.BasicUserDto;
 | 
	
	
		
			
				|  | @@ -28,6 +28,7 @@ import org.slf4j.Logger;
 | 
	
		
			
				|  |  |  import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +import org.springframework.transaction.annotation.Isolation;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Propagation;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -61,15 +62,15 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private StudentDao studentDao;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  | -    private GoodsService goodsService;
 | 
	
		
			
				|  |  | -    @Autowired
 | 
	
		
			
				|  |  |      private StudentPaymentRouteOrderService studentPaymentRouteOrderService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private SellOrderService sellOrderService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  | -    private SellOrderDao sellOrderDao;
 | 
	
		
			
				|  |  | +    private StudentGoodsSellDao studentGoodsSellDao;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private ContractService contractService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private GoodsService goodsService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private final Logger logger = LoggerFactory.getLogger(this.getClass());
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -106,12 +107,12 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | -    public Map addGoodsSellOrder(GoodsSellDto goodsSellDto) throws Exception {
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class,isolation = Isolation.SERIALIZABLE)
 | 
	
		
			
				|  |  | +    public Map addGoodsSellOrder(StudentGoodsSell studentGoodsSell) throws Exception {
 | 
	
		
			
				|  |  |          SysUser sysUser = sysUserFeignService.queryUserInfo();
 | 
	
		
			
				|  |  |          Integer studentId = sysUser.getId();
 | 
	
		
			
				|  |  | -        String goodsId = goodsSellDto.getGoodsId();
 | 
	
		
			
				|  |  | -        if (StringUtils.isEmpty(goodsId)) {
 | 
	
		
			
				|  |  | +        List<GoodsSellDto> goodsSellDtos = studentGoodsSell.getGoodsSellDtos();
 | 
	
		
			
				|  |  | +        if(goodsSellDtos == null || goodsSellDtos.size() == 0){
 | 
	
		
			
				|  |  |              throw new BizException("请选择需要购买的商品");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          if (studentId == null) {
 | 
	
	
		
			
				|  | @@ -120,14 +121,42 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |          studentDao.lockUser(studentId);
 | 
	
		
			
				|  |  |          SysUser student = sysUserFeignService.queryUserById(studentId);
 | 
	
		
			
				|  |  |          String orderNo = idGeneratorService.generatorId("payment") + "";
 | 
	
		
			
				|  |  | +        studentGoodsSell.setOrderNo(orderNo);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        String[] goodsIds = goodsId.split(",");
 | 
	
		
			
				|  |  | -        Map<String, BigDecimal> map = getMap("goods", "id_", "group_purchase_price_", goodsId, String.class, BigDecimal.class);
 | 
	
		
			
				|  |  | +        List<Integer> goodsIds = goodsSellDtos.stream().map(e -> e.getGoodsId()).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//        String[] goodsIds = goodsJson.split(",");
 | 
	
		
			
				|  |  | +        Map<Integer, BigDecimal> map = getMap("goods", "id_", "market_price_", goodsIds, Integer.class, BigDecimal.class);
 | 
	
		
			
				|  |  | +        for (GoodsSellDto goodsSellDto : goodsSellDtos) {
 | 
	
		
			
				|  |  | +            goodsSellDto.setGoodsPrice(map.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));
 | 
	
		
			
				|  |  |          BigDecimal amount = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | -        for (String id : goodsIds) {
 | 
	
		
			
				|  |  | -            amount.add(map.get(id));
 | 
	
		
			
				|  |  | +        for (Integer id : goodsIds) {
 | 
	
		
			
				|  |  | +            GoodsSellDto goodsSellDto = goodsMap.get(id).get(0);
 | 
	
		
			
				|  |  | +            amount = amount.add(goodsSellDto.getTotalGoodsPrice());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        amount = amount.subtract(studentGoodsSell.getMarketAmount());
 | 
	
		
			
				|  |  | +        if(amount.signum() < 0){
 | 
	
		
			
				|  |  | +            throw new BizException("操作失败:订单金额异常");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        studentGoodsSell.setOrganId(sysUser.getOrganId());
 | 
	
		
			
				|  |  | +        studentGoodsSell.setTotalAmount(amount);
 | 
	
		
			
				|  |  | +        studentGoodsSell.setGoodsJson(JSONObject.toJSONString(goodsSellDtos));
 | 
	
		
			
				|  |  | +        if(studentGoodsSell.getId() == null){
 | 
	
		
			
				|  |  | +            studentGoodsSellDao.insert(studentGoodsSell);
 | 
	
		
			
				|  |  | +        }else {
 | 
	
		
			
				|  |  | +            studentGoodsSellDao.update(studentGoodsSell);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        List<Goods> goods = goodsService.findGoodsByIds(goodsId);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (studentGoodsSell.getType() == 1) {
 | 
	
		
			
				|  |  | +            Map<String, Object> repairInfoMap = new HashMap<>();
 | 
	
		
			
				|  |  | +            MapUtil.populateMap(repairInfoMap, studentGoodsSell);
 | 
	
		
			
				|  |  | +            return repairInfoMap;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +//        List<Goods> goods = goodsService.findGoodsByIds(StringUtils.join(goodsIds,","));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
 | 
	
		
			
				|  |  |          studentPaymentOrder.setUserId(studentId);
 | 
	
	
		
			
				|  | @@ -141,7 +170,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |          studentPaymentOrder.setRoutingOrganId(student.getOrganId());
 | 
	
		
			
				|  |  |          studentPaymentOrderService.insert(studentPaymentOrder);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        Map<Integer, List<Goods>> collect = goods.stream().collect(Collectors.groupingBy(Goods::getId));
 | 
	
		
			
				|  |  | +        /*Map<Integer, List<Goods>> collect = goods.stream().collect(Collectors.groupingBy(Goods::getId));
 | 
	
		
			
				|  |  |          List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = new ArrayList<>();
 | 
	
		
			
				|  |  |          for (String id : goodsIds) {
 | 
	
		
			
				|  |  |              Goods e = collect.get(id).get(0);
 | 
	
	
		
			
				|  | @@ -162,11 +191,11 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |              studentPaymentOrderDetail.setKitGroupPurchaseType(KitGroupPurchaseTypeEnum.GROUP);
 | 
	
		
			
				|  |  |              studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);
 | 
	
		
			
				|  |  | +        studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);*/
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          studentPaymentOrder.setVersion(0);
 | 
	
		
			
				|  |  |          BigDecimal balance = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | -        if (goodsSellDto.getIsUseBalancePayment() && amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +        if (studentGoodsSell.getIsUseBalancePayment() && amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  |              SysUserCashAccount userCashAccount = sysUserCashAccountService.getLocked(studentId);
 | 
	
		
			
				|  |  |              if (userCashAccount == null) {
 | 
	
		
			
				|  |  |                  throw new BizException("用户账户不存在");
 | 
	
	
		
			
				|  | @@ -509,7 +538,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | -    public void goodsSellorderCallback(StudentPaymentOrder studentPaymentOrder) {
 | 
	
		
			
				|  |  | +    public void goodsSellOrderCallback(StudentPaymentOrder studentPaymentOrder) {
 | 
	
		
			
				|  |  |          Date nowDate = new Date();
 | 
	
		
			
				|  |  |          //更新订单信息
 | 
	
		
			
				|  |  |          studentPaymentOrder.setUpdateTime(nowDate);
 | 
	
	
		
			
				|  | @@ -555,11 +584,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |                  rechargeDetail.setPerAmount(studentPaymentOrder.getPerAmount().negate());
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              sysUserCashAccountDetailService.insert(paymentDetail);
 | 
	
		
			
				|  |  | -            //销售订单详情
 | 
	
		
			
				|  |  | -            List<StudentPaymentOrderDetail> orderDetails = studentPaymentOrderDetailService.getOrderDetail(studentPaymentOrder.getId());
 | 
	
		
			
				|  |  | -            if (orderDetails.size() > 0) {
 | 
	
		
			
				|  |  | -                sellOrderService.addOrderDetail2SellOrder(orderDetails, studentPaymentOrder, null);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | +            saveSellOrder(studentPaymentOrder.getOrderNo());
 | 
	
		
			
				|  |  |          } else if (studentPaymentOrder.getStatus() == DealStatusEnum.CLOSE || studentPaymentOrder.getStatus() == DealStatusEnum.FAILED) {
 | 
	
		
			
				|  |  |              if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  |                  sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐器维修支付失败");
 | 
	
	
		
			
				|  | @@ -567,6 +592,50 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    private void saveSellOrder(String orderNo){
 | 
	
		
			
				|  |  | +        StudentPaymentOrder orderByOrderNo = studentPaymentOrderService.findOrderByOrderNo(orderNo);
 | 
	
		
			
				|  |  | +        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());
 | 
	
		
			
				|  |  | +        List<Goods> goodsByIds = goodsService.findGoodsByIds(StringUtils.join(goodsId, ","));
 | 
	
		
			
				|  |  | +        Map<Integer, List<Goods>> goodsMap = goodsByIds.stream().collect(Collectors.groupingBy(Goods::getId));
 | 
	
		
			
				|  |  | +        Map<String, BigDecimal> costMap = new HashMap<>(2);
 | 
	
		
			
				|  |  | +        List<SellOrder> sellOrders = new ArrayList<>();
 | 
	
		
			
				|  |  | +        goodsId.forEach(e->{
 | 
	
		
			
				|  |  | +            GoodsSellDto goodsSellDto = collect.get(e).get(0);
 | 
	
		
			
				|  |  | +            Goods goods = goodsMap.get(e).get(0);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            costMap.put("sellCost", goods.getDiscountPrice());
 | 
	
		
			
				|  |  | +            if (goods.getAgreeCostPrice() != null) {
 | 
	
		
			
				|  |  | +                costMap.put("SellCost2", goods.getAgreeCostPrice());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            SellOrder sellOrder = new SellOrder();
 | 
	
		
			
				|  |  | +            sellOrder.setOrganId(orderByOrderNo.getOrganId());
 | 
	
		
			
				|  |  | +            sellOrder.setTransNo(orderByOrderNo.getTransNo());
 | 
	
		
			
				|  |  | +            sellOrder.setOrderId(orderByOrderNo.getId());
 | 
	
		
			
				|  |  | +            sellOrder.setOrderNo(orderByOrderNo.getOrderNo());
 | 
	
		
			
				|  |  | +            sellOrder.setExpectAmount(orderByOrderNo.getExpectAmount());
 | 
	
		
			
				|  |  | +            sellOrder.setActualAmount(orderByOrderNo.getActualAmount());
 | 
	
		
			
				|  |  | +            sellOrder.setBalanceAmount(orderByOrderNo.getBalancePaymentAmount());
 | 
	
		
			
				|  |  | +            sellOrder.setSellCost(goods.getDiscountPrice());
 | 
	
		
			
				|  |  | +            sellOrder.setSellCost2(JSONObject.toJSONString(costMap));
 | 
	
		
			
				|  |  | +            sellOrder.setType(SellTypeEnum.valueOf(goods.getType().getCode()));
 | 
	
		
			
				|  |  | +            sellOrder.setGoodsId(e);
 | 
	
		
			
				|  |  | +            sellOrder.setGoodsName(goods.getName());
 | 
	
		
			
				|  |  | +            sellOrder.setNum(goodsSellDto.getGoodsNum());
 | 
	
		
			
				|  |  | +            sellOrder.setUserId(studentGoodsSell.getUserId());
 | 
	
		
			
				|  |  | +            sellOrder.setPaymentChannel(orderByOrderNo.getPaymentChannel());
 | 
	
		
			
				|  |  | +            sellOrder.setMerNo(orderByOrderNo.getMerNos());
 | 
	
		
			
				|  |  | +            sellOrder.setSellTime(orderByOrderNo.getPayTime());
 | 
	
		
			
				|  |  | +            sellOrders.add(sellOrder);
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        if(sellOrders.size() > 0){
 | 
	
		
			
				|  |  | +            sellOrderService.batchInsert(sellOrders);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |      public Boolean orderCallback(StudentPaymentOrder studentPaymentOrder) {
 |