|  | @@ -49,6 +49,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 | 
	
		
			
				|  |  |  import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import static com.ym.mec.biz.dal.enums.DealStatusEnum.CLOSE;
 | 
	
		
			
				|  |  | +import static com.ym.mec.biz.dal.enums.DealStatusEnum.SUCCESS;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, StudentPaymentOrder> implements StudentPaymentOrderService {
 | 
	
	
		
			
				|  | @@ -1113,12 +1114,45 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
 | 
	
		
			
				|  |  |          studentPaymentOrder.setOrderNo(model.getOrderNo());
 | 
	
		
			
				|  |  |          studentPaymentOrder.setCreateTime(date);
 | 
	
		
			
				|  |  |          studentPaymentOrder.setUpdateTime(date);
 | 
	
		
			
				|  |  | -        studentPaymentOrder.setPaymentChannel("ADAPAY");
 | 
	
		
			
				|  |  | +        if (model.isUseBalance()) {
 | 
	
		
			
				|  |  | +            studentPaymentOrder.setPaymentChannel("BALANCE");
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            studentPaymentOrder.setPaymentChannel("ADAPAY");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |          studentPaymentOrder.setOrganId(student.getOrganId());
 | 
	
		
			
				|  |  |          studentPaymentOrder.setRoutingOrganId(student.getOrganId());
 | 
	
		
			
				|  |  |          studentPaymentOrder.setTenantId(student.getTenantId());
 | 
	
		
			
				|  |  |          studentPaymentOrderService.insert(studentPaymentOrder);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        // 余额支付
 | 
	
		
			
				|  |  | +        BigDecimal amount = studentPaymentOrder.getActualAmount();
 | 
	
		
			
				|  |  | +        BigDecimal balance = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | +        if (model.isUseBalance() && amount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +            SysUserCashAccount userCashAccount = sysUserCashAccountService.getLocked(model.getUserId());
 | 
	
		
			
				|  |  | +            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(model.getUserId(), balance.negate(), PlatformCashAccountDetailTypeEnum.PAY_FEE, "商品销售");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        studentPaymentOrderService.update(studentPaymentOrder);
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setVersion(studentPaymentOrder.getVersion() + 1);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (amount.compareTo(BigDecimal.ZERO) == 0) {
 | 
	
		
			
				|  |  | +            studentPaymentRouteOrderService.addRouteOrder(model.getOrderNo(), student.getOrganId(), balance);
 | 
	
		
			
				|  |  | +            Map<String, String> notifyMap = new HashMap<>();
 | 
	
		
			
				|  |  | +            notifyMap.put("tradeState", "1");
 | 
	
		
			
				|  |  | +            notifyMap.put("merOrderNo", studentPaymentOrder.getOrderNo());
 | 
	
		
			
				|  |  | +            studentPaymentOrderService.updateOrder(notifyMap);
 | 
	
		
			
				|  |  | +            notifyMap.put("orderNo", model.getOrderNo());
 | 
	
		
			
				|  |  | +            return notifyMap;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          studentGoodsSell.setCouponMarketAmount(studentPaymentOrder.getCouponRemitFee());
 | 
	
		
			
				|  |  |          studentGoodsSell.setOrganId(student.getOrganId());
 | 
	
		
			
				|  |  |          studentGoodsSell.setTotalAmount(studentPaymentOrder.getActualAmount());
 | 
	
	
		
			
				|  | @@ -1228,13 +1262,27 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
 | 
	
		
			
				|  |  |      public OrderCancelModel cancelOrder(StudentPaymentOrder order) {
 | 
	
		
			
				|  |  |          OrderCancelModel model = new OrderCancelModel();
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  | +            HfMerchantConfig hfMerchantConfig = hfMerchantConfigService.queryByTenantId(order.getTenantId());
 | 
	
		
			
				|  |  | +            if(hfMerchantConfig == null){
 | 
	
		
			
				|  |  | +                throw new BizException("机构[{}]汇付商户信息找不到", order.getTenantId());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            Map<String, Object> payment = Payment.queryPayment(order.getTransNo(), hfMerchantConfig.getMerKey());
 | 
	
		
			
				|  |  | +            LOGGER.info("订单[{}]交易主动查询接口返回:{}", order.getTransNo(), payment);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            model.setPayChannel((String) payment.get("pay_channel"));
 | 
	
		
			
				|  |  | +            String status = (String) payment.get("status");
 | 
	
		
			
				|  |  | +            if (status.equals("succeeded")) {
 | 
	
		
			
				|  |  | +                model.setStatus(true);
 | 
	
		
			
				|  |  | +                order.setStatus(SUCCESS);
 | 
	
		
			
				|  |  | +            } else if (status.equals("failed") ) {
 | 
	
		
			
				|  |  | +                model.setStatus(false);
 | 
	
		
			
				|  |  | +                order.setStatus(CLOSE);
 | 
	
		
			
				|  |  | +                order.setMemo("超时未支付关闭");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |              TenantContextHolder.setTenantId(order.getTenantId());
 | 
	
		
			
				|  |  | -            order.setStatus(CLOSE);
 | 
	
		
			
				|  |  | -            order.setMemo("超时未支付关闭");
 | 
	
		
			
				|  |  |              callOrderCallBack(order);
 | 
	
		
			
				|  |  |              TenantContextHolder.clearTenantId();
 | 
	
		
			
				|  |  |              model.setSuccess(true);
 | 
	
		
			
				|  |  | -            model.setStatus(false);
 | 
	
		
			
				|  |  |          } catch (Exception e) {
 | 
	
		
			
				|  |  |              model.setSuccess(false);
 | 
	
		
			
				|  |  |              e.printStackTrace();
 | 
	
	
		
			
				|  | @@ -1269,4 +1317,22 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
 | 
	
		
			
				|  |  |          payMap.put("mallStatus",1);
 | 
	
		
			
				|  |  |          return payMap;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void receive(String orderNo, boolean autoReceive) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        StudentPaymentOrder orderByOrderNo = studentPaymentOrderService.findOrderByOrderNo(orderNo);
 | 
	
		
			
				|  |  | +        if (orderByOrderNo == null) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // MANUAL_RECEIVE 手动确认,AUTO_RECEIVE 自动确认
 | 
	
		
			
				|  |  | +        StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
 | 
	
		
			
				|  |  | +        studentPaymentOrder.setId(orderByOrderNo.getId());
 | 
	
		
			
				|  |  | +        if (autoReceive) {
 | 
	
		
			
				|  |  | +            studentPaymentOrder.setReceiveStatus("AUTO_RECEIVE");
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            studentPaymentOrder.setReceiveStatus("MANUAL_RECEIVE");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        studentPaymentOrderDao.update(studentPaymentOrder);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |  }
 |