liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.xiaopeng.order.utils;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xiaopeng.api.dto.account.UcUserInfo;
import com.xiaopeng.api.dto.operation.SmsDto;
import com.xiaopeng.api.feignClient.account.UserResourcesClient;
import com.xiaopeng.api.feignClient.operation.SmsClient;
import com.xiaopeng.api.model.order.XpOrder;
import com.xiaopeng.api.vo.operation.SmsVo;
import com.xiaopeng.common.core.enums.OrderStatusEnum;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.order.service.IXpOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
 
@Slf4j
@Component
public class TaskOrderSendSmsUtil {
 
    private static final int SMS_SENT = 1;
    private static final int BATCH_SIZE = 200;
 
    @Resource
    private IXpOrderService xpOrderService;
 
    @Resource
    private UserResourcesClient userResourcesClient;
 
    @Resource
    private SmsClient smsClient;
 
 
    @Value("${robotaxi.sms.app-id:1476153389173952512}")
    private String smsAppId;
 
//    @Value("${sms.appid:}")
//    private String smsAppId;
//
//    @Value("${sms.order-pay.template-code:RobotaxiOrderPaySmsTpl}")
//    private String orderPayTemplateCode;
//
//    @Value("${sms.order-pay.app-push-config-code:RobotaxiOrderPaySms}")
//    private String orderPayAppPushConfigCode;
 
    /**
     * 每分钟查询一次待支付超过24小时且未发送短信的订单。
     */
    @Scheduled(cron = "0 */1 * * * ?")
    public void sendPendingPaymentSms() {
        LocalDateTime deadline = LocalDateTime.now().minusHours(24);
        List<XpOrder> orders = xpOrderService.list(new LambdaQueryWrapper<XpOrder>()
                .eq(XpOrder::getStatus, OrderStatusEnum.APPROVAL_REJECTION.getCode())
                .le(XpOrder::getTripEnd, deadline)
                .and(wrapper -> wrapper.isNull(XpOrder::getSmsStatus).or().ne(XpOrder::getSmsStatus, SMS_SENT))
                .orderByAsc(XpOrder::getTripEnd)
                .last("LIMIT " + BATCH_SIZE));
 
        if (orders == null || orders.isEmpty()) {
            return;
        }
 
        for (XpOrder order : orders) {
            try {
                sendSms(order);
            } catch (Exception e) {
                log.error("待支付订单短信发送失败,订单ID: {}, 订单号: {}", order.getId(), order.getOrderNumber(), e);
            }
        }
    }
 
    private void sendSms(XpOrder order) {
        if (order.getAppUserId() == null) {
            log.warn("待支付订单短信发送跳过,用户ID为空,订单ID: {}", order.getId());
            return;
        }
        UcUserInfo userInfo = userResourcesClient.getBaseUserByUid(String.valueOf(order.getAppUserId()));
        if (userInfo == null || StringUtils.isBlank(userInfo.getMobile())) {
            log.warn("待支付订单短信发送跳过,手机号为空,订单ID: {}, 用户ID: {}", order.getId(), order.getAppUserId());
            return;
        }
 
        SmsVo smsVo = new SmsVo();
        smsVo.setAppId(smsAppId);
        smsVo.setPushType(1);
        smsVo.setReceivers(userInfo.getMobile());
        smsVo.setTemplateCode("RTOrderUnpaidTpl");
        smsVo.setAppPushConfigCode("RTOrderUnpaidSms");
        smsVo.setParams(new HashMap<>());
        smsVo.getParams().put("orderAmount", order.getOrderMoney().toString());
 
        SmsDto result = smsClient.pushSms(smsVo);
        if (result == null || result.getCode() == null || result.getCode() != 200) {
            log.warn("待支付订单短信发送未成功,订单ID: {}, 返回: {}", order.getId(), result);
            return;
        }
        order.setAppUserId(null);
        order.setSmsStatus(SMS_SENT);
        boolean updated = xpOrderService.updateById(order);
        if (!updated) {
            log.warn("待支付订单短信状态更新失败,订单ID: {}", order.getId());
        }
    }
}