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());
|
}
|
}
|
}
|