smart-switch-java/smart-switch-service/src/main/java/com/ruoyi/task/bill/PayTask.java
2024-05-06 15:07:24 +08:00

79 lines
3.0 KiB
Java

package com.ruoyi.task.bill;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ss.transactionBill.domain.TransactionBillQuery;
import com.ruoyi.ss.transactionBill.domain.TransactionBillVo;
import com.ruoyi.ss.transactionBill.domain.TransactionBill;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillStatus;
import com.ruoyi.ss.transactionBill.domain.enums.TransactionBillType;
import com.ruoyi.ss.transactionBill.service.TransactionBillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* @author wjh
* 2024/4/10
*/
@Component("payTask")
@Slf4j
public class PayTask implements ApplicationRunner {
@Autowired
private TransactionBillService transactionBillService;
/**
* 更新支付结果
*/
@Transactional
public void refreshPayResult() {
// 查询十分钟以内支付中的充值订单
Date now = new Date();
Date past = DateUtils.addMinutes(now, -10);
TransactionBillQuery dto = new TransactionBillQuery();
dto.setStartDate(past);
dto.setType(TransactionBillType.RECHARGE.getType());
dto.setStatus(TransactionBillStatus.PAYING.getStatus());
List<TransactionBillVo> billList = transactionBillService.selectSmTransactionBillList(dto);
if (CollectionUtils.isEmpty(billList)) {
log.info("十分钟以内没有支付中的订单");
return;
}
transactionBillService.refreshPayResult(billList.stream().map(TransactionBill::getBillId).collect(Collectors.toList()));
}
@Override
public void run(ApplicationArguments args) throws Exception {
// 查询支付中的充值订单,将其加入到延迟队列
log.info("查询支付中的充值订单,将其加入到延迟队列");
TransactionBillQuery dto = new TransactionBillQuery();
dto.setStatus(TransactionBillStatus.PAYING.getStatus());
dto.setType(TransactionBillType.RECHARGE.getType());
List<TransactionBillVo> list = transactionBillService.selectSmTransactionBillList(dto);
if (CollectionUtils.isEmpty(list)) {
log.info("没有支付中的订单");
return;
}
log.info("查询到{}个正在支付的订单", list.size());
for (TransactionBillVo bill : list) {
try {
transactionBillService.refreshPayResultBeforeExpire(bill.getBillNo(), 20, TimeUnit.SECONDS);
} catch (Exception e) {
log.error("加入延迟队列异常:{} ", bill.getBillNo());
}
}
}
}