|  | @@ -130,8 +130,6 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          studentDao.lockUser(studentId);
 | 
	
		
			
				|  |  |          SysUser student = sysUserFeignService.queryUserById(studentId);
 | 
	
		
			
				|  |  | -        String orderNo = idGeneratorService.generatorId("payment") + "";
 | 
	
		
			
				|  |  | -        studentGoodsSell.setOrderNo(orderNo);
 | 
	
		
			
				|  |  |          if(studentGoodsSell.getTeacherId() == null){
 | 
	
		
			
				|  |  |              //获取学员第一个教务老师
 | 
	
		
			
				|  |  |              studentGoodsSell.setTeacherId(musicGroupDao.getFirstEduTeacherId(studentGoodsSell.getUserId()));
 | 
	
	
		
			
				|  | @@ -164,9 +162,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |              MapUtil.populateMap(repairInfoMap, studentGoodsSell);
 | 
	
		
			
				|  |  |              return repairInfoMap;
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -//        List<Goods> goods = goodsService.findGoodsByIds(StringUtils.join(goodsIds,","));
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +        String orderNo = idGeneratorService.generatorId("payment") + "";
 | 
	
		
			
				|  |  |          StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
 | 
	
		
			
				|  |  |          studentPaymentOrder.setUserId(studentId);
 | 
	
		
			
				|  |  |          studentPaymentOrder.setGroupType(GroupType.GOODS_SELL);
 | 
	
	
		
			
				|  | @@ -179,28 +175,113 @@ 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));
 | 
	
		
			
				|  |  | -        List<StudentPaymentOrderDetail> studentPaymentOrderDetailList = new ArrayList<>();
 | 
	
		
			
				|  |  | -        for (String id : goodsIds) {
 | 
	
		
			
				|  |  | -            Goods e = collect.get(id).get(0);
 | 
	
		
			
				|  |  | -            StudentPaymentOrderDetail studentPaymentOrderDetail = new StudentPaymentOrderDetail();
 | 
	
		
			
				|  |  | -            studentPaymentOrderDetail.setRemitFee(BigDecimal.ZERO);
 | 
	
		
			
				|  |  | -            OrderDetailTypeEnum type = null;
 | 
	
		
			
				|  |  | -            if (e.getType() == GoodsType.INSTRUMENT) {
 | 
	
		
			
				|  |  | -                type = OrderDetailTypeEnum.MUSICAL;
 | 
	
		
			
				|  |  | -            } else if (e.getType() == GoodsType.ACCESSORIES) {
 | 
	
		
			
				|  |  | -                type = OrderDetailTypeEnum.ACCESSORIES;
 | 
	
		
			
				|  |  | -            } else if (e.getType() == GoodsType.OTHER) {
 | 
	
		
			
				|  |  | -                type = OrderDetailTypeEnum.TEACHING;
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setVersion(0);
 | 
	
		
			
				|  |  | +        BigDecimal balance = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | +        if (studentGoodsSell.getIsUseBalancePayment() && amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +            SysUserCashAccount userCashAccount = sysUserCashAccountService.getLocked(studentId);
 | 
	
		
			
				|  |  | +            if (userCashAccount == null) {
 | 
	
		
			
				|  |  | +                throw new BizException("用户账户不存在");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (userCashAccount.getBalance() != null && userCashAccount.getBalance().compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +                balance = amount.compareTo(userCashAccount.getBalance()) >= 0 ? userCashAccount.getBalance() : amount;
 | 
	
		
			
				|  |  | +                amount = amount.subtract(balance);
 | 
	
		
			
				|  |  | +                studentPaymentOrder.setActualAmount(amount);
 | 
	
		
			
				|  |  | +                studentPaymentOrder.setBalancePaymentAmount(balance);
 | 
	
		
			
				|  |  | +                sysUserCashAccountService.updateBalance(studentId, balance.negate(), PlatformCashAccountDetailTypeEnum.PAY_FEE, "商品销售");
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | -            studentPaymentOrderDetail.setType(type);
 | 
	
		
			
				|  |  | -            studentPaymentOrderDetail.setPrice(e.getGroupPurchasePrice());
 | 
	
		
			
				|  |  | -            studentPaymentOrderDetail.setGoodsIdList(id);
 | 
	
		
			
				|  |  | -            studentPaymentOrderDetail.setPaymentOrderId(studentPaymentOrder.getId());
 | 
	
		
			
				|  |  | -            studentPaymentOrderDetail.setKitGroupPurchaseType(KitGroupPurchaseTypeEnum.GROUP);
 | 
	
		
			
				|  |  | -            studentPaymentOrderDetailList.add(studentPaymentOrderDetail);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);*/
 | 
	
		
			
				|  |  | +        studentPaymentOrderService.update(studentPaymentOrder);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setVersion(studentPaymentOrder.getVersion() + 1);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (amount.compareTo(BigDecimal.ZERO) == 0) {
 | 
	
		
			
				|  |  | +            studentPaymentRouteOrderService.addRouteOrder(orderNo, student.getOrganId(), balance);
 | 
	
		
			
				|  |  | +            Map<String, String> notifyMap = new HashMap<>();
 | 
	
		
			
				|  |  | +            notifyMap.put("tradeState", "1");
 | 
	
		
			
				|  |  | +            notifyMap.put("merOrderNo", studentPaymentOrder.getOrderNo());
 | 
	
		
			
				|  |  | +            studentPaymentOrderService.updateOrder(notifyMap);
 | 
	
		
			
				|  |  | +            notifyMap.put("orderNo", orderNo);
 | 
	
		
			
				|  |  | +            return notifyMap;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String baseApiUrl = sysConfigDao.findConfigValue(SysConfigService.BASE_API_URL);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map payMap = payService.getPayMap(
 | 
	
		
			
				|  |  | +                amount,
 | 
	
		
			
				|  |  | +                balance,
 | 
	
		
			
				|  |  | +                orderNo,
 | 
	
		
			
				|  |  | +                baseApiUrl + "/api-student/studentOrder/notify",
 | 
	
		
			
				|  |  | +                baseApiUrl + "/api-student/studentOrder/paymentResult?type=edu&orderNo=" + orderNo,
 | 
	
		
			
				|  |  | +                "商品销售",
 | 
	
		
			
				|  |  | +                "商品销售",
 | 
	
		
			
				|  |  | +                student.getOrganId(),
 | 
	
		
			
				|  |  | +                "goodsSell"
 | 
	
		
			
				|  |  | +        );
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setMerNos((String) payMap.get("routingMerNos"));
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setPaymentChannel((String) payMap.get("type"));
 | 
	
		
			
				|  |  | +        studentPaymentOrderService.update(studentPaymentOrder);
 | 
	
		
			
				|  |  | +        return payMap;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class,isolation = Isolation.SERIALIZABLE)
 | 
	
		
			
				|  |  | +    public Map studentPaymentGoodsOrder(Integer goodsSellId) throws Exception {
 | 
	
		
			
				|  |  | +        StudentGoodsSell studentGoodsSell = studentGoodsSellDao.get(goodsSellId);
 | 
	
		
			
				|  |  | +        Integer studentId = studentGoodsSell.getUserId();
 | 
	
		
			
				|  |  | +        studentDao.lockUser(studentId);
 | 
	
		
			
				|  |  | +//        SysUser student = sysUserFeignService.queryUserById(studentId);
 | 
	
		
			
				|  |  | +        List<GoodsSellDto> goodsSellDtos = JSONObject.parseArray(studentGoodsSell.getGoodsJson(),GoodsSellDto.class);
 | 
	
		
			
				|  |  | +        List<Integer> goodsIds = goodsSellDtos.stream().map(e -> e.getGoodsId()).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        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 (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("操作失败:订单金额异常");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        String orderNo1 = studentGoodsSell.getOrderNo();
 | 
	
		
			
				|  |  | +        String orderNo = idGeneratorService.generatorId("payment") + "";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        studentGoodsSell.setOrderNo(orderNo);
 | 
	
		
			
				|  |  | +        StudentPaymentOrder studentPaymentOrder = null;
 | 
	
		
			
				|  |  | +        if(StringUtils.isNotEmpty(orderNo1)){
 | 
	
		
			
				|  |  | +            studentPaymentOrder = studentPaymentOrderService.findOrderByOrderNo(orderNo1);
 | 
	
		
			
				|  |  | +            if(studentPaymentOrder.getStatus() == DealStatusEnum.SUCCESS){
 | 
	
		
			
				|  |  | +                throw new BizException("该订单已支付");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            //关闭老订单
 | 
	
		
			
				|  |  | +            if(studentPaymentOrder.getBalancePaymentAmount() != null){
 | 
	
		
			
				|  |  | +                studentGoodsSell.setIsUseBalancePayment(true);
 | 
	
		
			
				|  |  | +            }else {
 | 
	
		
			
				|  |  | +                studentGoodsSell.setIsUseBalancePayment(false);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            studentPaymentOrder.setStatus(DealStatusEnum.CLOSE);
 | 
	
		
			
				|  |  | +            studentPaymentOrderService.update(studentPaymentOrder);
 | 
	
		
			
				|  |  | +            if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +                sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "关闭订单");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        studentGoodsSellDao.update(studentGoodsSell);
 | 
	
		
			
				|  |  | +        studentPaymentOrder = new StudentPaymentOrder();
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setUserId(studentId);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setGroupType(GroupType.GOODS_SELL);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setOrderNo(orderNo);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setType(OrderTypeEnum.GOODS_SELL);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setExpectAmount(amount);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setActualAmount(amount);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setStatus(DealStatusEnum.ING);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setOrganId(studentGoodsSell.getOrganId());
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setRoutingOrganId(studentGoodsSell.getOrganId());
 | 
	
		
			
				|  |  | +        studentPaymentOrderService.insert(studentPaymentOrder);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          studentPaymentOrder.setVersion(0);
 | 
	
		
			
				|  |  |          BigDecimal balance = BigDecimal.ZERO;
 | 
	
	
		
			
				|  | @@ -221,7 +302,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |          studentPaymentOrder.setVersion(studentPaymentOrder.getVersion() + 1);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          if (amount.compareTo(BigDecimal.ZERO) == 0) {
 | 
	
		
			
				|  |  | -            studentPaymentRouteOrderService.addRouteOrder(orderNo, student.getOrganId(), balance);
 | 
	
		
			
				|  |  | +            studentPaymentRouteOrderService.addRouteOrder(orderNo, studentGoodsSell.getOrganId(), balance);
 | 
	
		
			
				|  |  |              Map<String, String> notifyMap = new HashMap<>();
 | 
	
		
			
				|  |  |              notifyMap.put("tradeState", "1");
 | 
	
		
			
				|  |  |              notifyMap.put("merOrderNo", studentPaymentOrder.getOrderNo());
 | 
	
	
		
			
				|  | @@ -240,7 +321,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 | 
	
		
			
				|  |  |                  baseApiUrl + "/api-student/studentOrder/paymentResult?type=edu&orderNo=" + orderNo,
 | 
	
		
			
				|  |  |                  "商品销售",
 | 
	
		
			
				|  |  |                  "商品销售",
 | 
	
		
			
				|  |  | -                student.getOrganId(),
 | 
	
		
			
				|  |  | +                studentGoodsSell.getOrganId(),
 | 
	
		
			
				|  |  |                  "goodsSell"
 | 
	
		
			
				|  |  |          );
 | 
	
		
			
				|  |  |  
 |