| | |
| | | private final HuifuRefundService refundService; |
| | | private final MerchantPaymentReadinessService merchantPaymentReadinessService; |
| | | private final OrderFundBoundaryService orderFundBoundary; |
| | | private final PaymentFundConfirmationService fundConfirmation; |
| | | private final OrderVerificationService orderVerificationService; |
| | | |
| | | public OrderService(JdbcTemplate jdbcTemplate, HuifuRefundService refundService, MerchantPaymentReadinessService merchantPaymentReadinessService, OrderFundBoundaryService orderFundBoundary, PaymentFundConfirmationService fundConfirmation) { |
| | | public OrderService(JdbcTemplate jdbcTemplate, HuifuRefundService refundService, MerchantPaymentReadinessService merchantPaymentReadinessService, OrderFundBoundaryService orderFundBoundary, OrderVerificationService orderVerificationService) { |
| | | this.jdbcTemplate = jdbcTemplate; |
| | | this.refundService = refundService; |
| | | this.merchantPaymentReadinessService = merchantPaymentReadinessService; |
| | | this.orderFundBoundary = orderFundBoundary; |
| | | this.fundConfirmation = fundConfirmation; |
| | | this.orderVerificationService = orderVerificationService; |
| | | } |
| | | |
| | | @Transactional |
| | |
| | | public Map<String, Object> verify(String orderNo, OrderVerifyRequest request) { |
| | | Map<String, Object> order = detail(orderNo); |
| | | Long orderId = ((Number) order.get("id")).longValue(); |
| | | // 后台核销与商户核销、退款共用同一把订单行锁,重复核销只会产生一次有效结果。 |
| | | OrderFundBoundaryService.LockedOrder locked = orderFundBoundary.lock(orderId); |
| | | locked.requireVerifiable(); |
| | | Long orderMerchantId = order.get("merchantId") == null ? null : ((Number) order.get("merchantId")).longValue(); |
| | | if (request.merchantId() != null && !request.merchantId().equals(orderMerchantId)) { |
| | | throw new BusinessException(ErrorCode.BAD_REQUEST, "订单不属于该商户"); |
| | | } |
| | | String verifyNo = BusinessIdGenerator.next("V"); |
| | | jdbcTemplate.update(""" |
| | | insert into verify_record(verify_no, order_id, talent_id, card_id, benefit_id, merchant_id, |
| | | staff_id, verify_type, verify_amount, subsidy_amount, remark) |
| | | values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| | | """, |
| | | verifyNo, |
| | | order.get("id"), |
| | | order.get("talentId"), |
| | | order.get("cardId"), |
| | | order.get("benefitId"), |
| | | orderMerchantId, |
| | | request.staffId(), |
| | | Long storeId = order.get("storeId") == null ? null : ((Number) order.get("storeId")).longValue(); |
| | | // 后台核销同样走那份唯一实现:置已核销 + 写核销记录 + 确认延迟入账的资金。 |
| | | String verifyNo = orderVerificationService.verifyByMerchant(orderMerchantId, storeId, request.staffId(), orderId, |
| | | normalizeVerifyType(request.verifyType(), (String) order.get("orderType")), |
| | | order.get("payAmount"), |
| | | order.get("subsidyAmount"), |
| | | request.remark()); |
| | | int updated = jdbcTemplate.update("update benefit_order set verify_status = 1 where id = ? and verify_status = 0", orderId); |
| | | if (updated != 1) { |
| | | value(order.get("payAmount")), value(order.get("subsidyAmount")), request.remark(), null, null); |
| | | if (verifyNo.isBlank()) { |
| | | throw new BusinessException(ErrorCode.CONFLICT, "订单已核销"); |
| | | } |
| | | // 后台核销是第三条核销入口,同样要在核销后把延迟入账的钱确认给商户。 |
| | | fundConfirmation.confirmForOrder(orderId); |
| | | return detail(orderNo); |
| | | } |
| | | |
| | | private long value(Object value) { |
| | | return value instanceof Number number ? number.longValue() : 0L; |
| | | } |
| | | |
| | | public Map<String, Object> refund(String orderNo, OrderRefundRequest request) { |
| | | return refundService.request(orderNo, request); |
| | | } |