| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- package com.ruoyi.app.order;
- import com.ruoyi.app.order.dto.OrderPushBodyDto;
- import com.ruoyi.app.push.BusinessPushProperties;
- import com.ruoyi.app.push.NativeRiderBatchPushService;
- import com.ruoyi.app.utils.PayPush;
- import com.ruoyi.app.utils.event.PushEventService;
- import com.ruoyi.framework.manager.AsyncManager;
- import com.ruoyi.system.domain.InfoUser;
- import com.ruoyi.system.domain.PosOrder;
- import com.ruoyi.system.domain.RiderPosition;
- import com.ruoyi.system.mapper.RiderPositionMapper;
- import com.ruoyi.system.service.IInfoUserService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.support.TransactionSynchronization;
- import org.springframework.transaction.support.TransactionSynchronizationManager;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Set;
- import java.util.TimerTask;
- /** 外送订单开放接单及取消后的推送。 */
- @Service
- public class DeliveryOrderNotificationService {
- private static final int RIDER_NOTIFICATION_LIMIT = 20;
- private final RiderPositionMapper riderPositionMapper;
- private final PushEventService pushEventService;
- private final IInfoUserService infoUserService;
- private final MerchantNotificationRouter merchantNotificationRouter;
- private final BusinessPushProperties pushProperties;
- private final NativeRiderBatchPushService nativeRiderBatch;
- public DeliveryOrderNotificationService(RiderPositionMapper riderPositionMapper,
- PushEventService pushEventService) {
- this(riderPositionMapper, pushEventService, null, null);
- }
- public DeliveryOrderNotificationService(RiderPositionMapper riderPositionMapper,
- PushEventService pushEventService,
- IInfoUserService infoUserService) {
- this(riderPositionMapper, pushEventService, infoUserService, null);
- }
- public DeliveryOrderNotificationService(RiderPositionMapper riderPositionMapper,
- PushEventService pushEventService,
- IInfoUserService infoUserService,
- MerchantNotificationRouter merchantNotificationRouter) {
- this(riderPositionMapper, pushEventService, infoUserService, merchantNotificationRouter, null, null);
- }
- @Autowired
- public DeliveryOrderNotificationService(RiderPositionMapper riderPositionMapper,
- PushEventService pushEventService,
- IInfoUserService infoUserService,
- MerchantNotificationRouter merchantNotificationRouter,
- BusinessPushProperties pushProperties,
- NativeRiderBatchPushService nativeRiderBatch) {
- this.riderPositionMapper = riderPositionMapper;
- this.pushEventService = pushEventService;
- this.infoUserService = infoUserService;
- this.merchantNotificationRouter = merchantNotificationRouter;
- this.pushProperties = pushProperties;
- this.nativeRiderBatch = nativeRiderBatch;
- }
- /**
- * 支付完成后的新外送订单通知:自配送订单通知商家,其余订单通知附近可接单骑手。
- * 所有下单、支付回调与线下转账确认入口复用这里,避免遗漏自配送分流。
- */
- public void notifyOrderAvailable(PosOrder order) {
- if (isAvailableForSelfDeliveryMerchant(order)) {
- notifySelfDeliveryOrderToMerchant(order);
- return;
- }
- if (!isAvailableForRider(order)) {
- return;
- }
- runAfterCommit(() -> {
- List<RiderPosition> riders = riderPositionMapper.getAcceptRiderList(
- order.getLongitude(), order.getLatitude(), RIDER_NOTIFICATION_LIMIT);
- if (riders != null) {
- if (pushProperties != null && pushProperties.getMode() == BusinessPushProperties.Mode.NATIVE
- && nativeRiderBatch != null) {
- String ddId = String.valueOf(order.getDdId());
- String body = OrderPushBodyDto.getJson(ddId, String.valueOf(order.getState()), 0);
- nativeRiderBatch.sendNewOrder(riders, "no.message.push.message",
- "no.message.push.new.food.order", body, ddId,
- rider -> sendAvailableOrderToRider(rider, order));
- } else {
- riders.forEach(rider -> sendAvailableOrderToRider(rider, order));
- }
- }
- });
- }
- /** 已分配骑手的外送订单取消后,通知除取消人外的相关方。 */
- public void notifyOrderCancelled(PosOrder order, Long operatorId) {
- if (order == null || !Long.valueOf(0L).equals(order.getType())
- || order.getQsId() == null || infoUserService == null) {
- return;
- }
- runAfterCommit(() -> {
- Set<Long> recipientIds = new LinkedHashSet<>();
- recipientIds.add(order.getUserId());
- recipientIds.add(order.getQsId());
- if (merchantNotificationRouter == null) {
- recipientIds.add(order.getShId());
- }
- recipientIds.remove(null);
- recipientIds.remove(operatorId);
- if (recipientIds.isEmpty()) {
- return;
- }
- List<InfoUser> recipients = infoUserService.listByIds(recipientIds);
- if (recipients != null) {
- recipients.forEach(recipient -> sendCancelledOrder(recipient, order));
- }
- if (merchantNotificationRouter != null && shouldNotifyMerchant(operatorId)) {
- String ddId = String.valueOf(order.getDdId());
- String body = OrderPushBodyDto.getJson(ddId, "4", 0);
- merchantNotificationRouter.sendStoreNotification(order.getMdId(), order.getShId(),
- "no.message.push.message", "no.message.push.order.cancelled", body, ddId);
- }
- });
- }
- private boolean shouldNotifyMerchant(Long operatorId) {
- if (operatorId == null || infoUserService == null) {
- return true;
- }
- InfoUser operator = infoUserService.getById(operatorId);
- return operator == null || "0".equals(operator.getUserType()) || "2".equals(operator.getUserType());
- }
- void sendAvailableOrderToRider(RiderPosition rider, PosOrder order) {
- if (rider == null || rider.getRiderId() == null) {
- return;
- }
- String ddId = String.valueOf(order.getDdId());
- String body = OrderPushBodyDto.getJson(ddId, String.valueOf(order.getState()), 0);
- AsyncManager.me().execute(new TimerTask() {
- @Override
- public void run() {
- PayPush.qsPushHandleLocal(new PayPush(), pushEventService, rider.getRiderId(), rider.getCid(),
- "no.message.push.message", "no.message.push.new.food.order", body, "", ddId);
- }
- });
- }
- void sendCancelledOrder(InfoUser recipient, PosOrder order) {
- if (recipient == null || recipient.getUserId() == null) {
- return;
- }
- String ddId = String.valueOf(order.getDdId());
- String body = OrderPushBodyDto.getJson(ddId, "4", 0);
- AsyncManager.me().execute(new TimerTask() {
- @Override
- public void run() {
- PayPush push = new PayPush();
- if (recipient.getUserId().equals(order.getQsId())) {
- PayPush.qsPushHandleLocal(push, pushEventService, recipient.getUserId(), recipient.getCid(),
- "no.message.push.message", "no.message.push.order.cancelled", body, "", ddId);
- } else if (recipient.getUserId().equals(order.getShId())) {
- PayPush.shPushHandleLocal(push, pushEventService, recipient.getUserId(), recipient.getCid(),
- "no.message.push.message", "no.message.push.order.cancelled", body, "", ddId);
- } else {
- PayPush.userPushHandleLocal(push, pushEventService, recipient.getUserId(), recipient.getCid(),
- "no.message.push.message", "no.message.push.order.cancelled", body, "", ddId);
- }
- }
- });
- }
- /** 自配送新单只允许通知实际门店及其归属商家,绝不进入骑手通知链路。 */
- private void notifySelfDeliveryOrderToMerchant(PosOrder order) {
- if (merchantNotificationRouter == null) {
- return;
- }
- runAfterCommit(() -> {
- String ddId = String.valueOf(order.getDdId());
- String body = OrderPushBodyDto.getJson(ddId, String.valueOf(order.getState()), 0);
- merchantNotificationRouter.sendStoreNotification(order.getMdId(), order.getShId(),
- "no.message.push.message", "no.message.push.new.order", body, ddId);
- });
- }
- private boolean isAvailableForSelfDeliveryMerchant(PosOrder order) {
- return isPaidUnassignedDeliveryOrder(order) && Integer.valueOf(1).equals(order.getSelfDelivery());
- }
- private boolean isAvailableForRider(PosOrder order) {
- return isPaidUnassignedDeliveryOrder(order) && !Integer.valueOf(1).equals(order.getSelfDelivery());
- }
- private boolean isPaidUnassignedDeliveryOrder(PosOrder order) {
- return order != null
- && Long.valueOf(0L).equals(order.getType())
- && Long.valueOf(0L).equals(order.getState())
- && Long.valueOf(0L).equals(order.getDeliveryStatus())
- && Long.valueOf(1L).equals(order.getPayStatus())
- && Long.valueOf(0L).equals(order.getAfterSaleStatus())
- && order.getQsId() == null;
- }
- private static void runAfterCommit(Runnable action) {
- if (!TransactionSynchronizationManager.isSynchronizationActive()) {
- action.run();
- return;
- }
- TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
- @Override
- public void afterCommit() {
- action.run();
- }
- });
- }
- }
|