ChannelGoodsOrderTask.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.qs.mp.quartz.task;
  2. import cn.hutool.core.date.LocalDateTimeUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  5. import com.qs.mp.channel.domain.ChannelGoodsOrder;
  6. import com.qs.mp.channel.domain.ChannelOrder;
  7. import com.qs.mp.channel.service.IChannelGoodsOrderService;
  8. import com.qs.mp.common.enums.ChannelGoodsOrderStatusEnum;
  9. import com.qs.mp.common.enums.ChannelOrderStatusEnum;
  10. import com.qs.mp.common.utils.DateUtils;
  11. import com.qs.mp.common.utils.LogUtil;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Component;
  16. import java.time.LocalDateTime;
  17. import java.time.temporal.ChronoUnit;
  18. import java.util.List;
  19. /**
  20. * @author zhangkaikai
  21. * @create 2023-06-08 9:54 PM
  22. **/
  23. @Component("channelGoodsOrderTask")
  24. public class ChannelGoodsOrderTask {
  25. protected final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
  26. @Autowired
  27. private IChannelGoodsOrderService channelGoodsOrderService;
  28. private final int PAGE_SIZE = 500;
  29. /**
  30. * x天前的待收货订单自动确认收货任务
  31. */
  32. public void confirm(Integer day) {
  33. LogUtil.info(logger, "...渠道商采购商品订单自动确认收货任务开始...");
  34. LocalDateTime now = LocalDateTimeUtil.now();
  35. LocalDateTime deliveryTime = LocalDateTimeUtil.offset(now, -day, ChronoUnit.DAYS);
  36. List<ChannelGoodsOrder> channelOrderList = channelGoodsOrderService.list(new LambdaQueryWrapper<ChannelGoodsOrder>()
  37. .eq(ChannelGoodsOrder::getStatus, ChannelGoodsOrderStatusEnum.NOT_CONFIRM.getValue())
  38. .le(ChannelGoodsOrder::getDeliveryTime, deliveryTime));
  39. if (CollectionUtils.isNotEmpty(channelOrderList)) {
  40. for (ChannelGoodsOrder channelGoodsOrder : channelOrderList) {
  41. channelGoodsOrderService.confirmOrder(channelGoodsOrder);
  42. }
  43. }
  44. LogUtil.info(logger, "...渠道商采购商品订单自动确认收货任务结束...");
  45. }
  46. /**
  47. * 订单取消到期任务
  48. */
  49. public void cancel() {
  50. LogUtil.info(logger, "...渠道未支付采购商品订单定时取消任务开始...");
  51. int total = PAGE_SIZE;
  52. while (total == PAGE_SIZE) {
  53. // 捞取30分钟前未支付订单
  54. List<ChannelGoodsOrder> channelGoodsOrderList = channelGoodsOrderService.list(
  55. new LambdaQueryWrapper<ChannelGoodsOrder>()
  56. .eq(ChannelGoodsOrder::getStatus, ChannelOrderStatusEnum.NOT_PAY)
  57. .lt(ChannelGoodsOrder::getCreatedTime, DateUtils.addMinutes(DateUtils.getNowDate(), -30))
  58. .last("limit " + PAGE_SIZE));
  59. total = channelGoodsOrderList.size();
  60. for (ChannelGoodsOrder channelGoodsOrder : channelGoodsOrderList) {
  61. try {
  62. channelGoodsOrderService.cancelOrder(channelGoodsOrder.getChannelId(), channelGoodsOrder.getOrderId());
  63. } catch (Exception e) {
  64. LogUtil.error(logger, e, "定时取消未支付渠道采购商品订单异常。orderId:{0}", channelGoodsOrder.getOrderId());
  65. }
  66. }
  67. }
  68. LogUtil.info(logger, "...渠道未支付采购商品订单定时取消任务结束...");
  69. }
  70. }