Showing
1 changed file
with
50 additions
and
0 deletions
| 1 | +package com.tianting.infoloop.task; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.date.LocalDateTimeUtil; | ||
| 4 | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ||
| 5 | +import com.baomidou.mybatisplus.core.toolkit.Wrappers; | ||
| 6 | +import com.infoloop.tianting.clientcustomerorderservice.OrderStatusEnum; | ||
| 7 | +import com.tianting.infoloop.mapper.OrderDbMapper; | ||
| 8 | +import com.tianting.infoloop.model.db.OrderDb; | ||
| 9 | +import com.tianting.infoloop.service.OrderDbService; | ||
| 10 | +import java.time.LocalDate; | ||
| 11 | +import java.time.LocalDateTime; | ||
| 12 | +import lombok.RequiredArgsConstructor; | ||
| 13 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 14 | +import org.springframework.scheduling.annotation.EnableScheduling; | ||
| 15 | +import org.springframework.scheduling.annotation.Scheduled; | ||
| 16 | +import org.springframework.stereotype.Component; | ||
| 17 | + | ||
| 18 | +@Component | ||
| 19 | +@EnableScheduling | ||
| 20 | +@RequiredArgsConstructor(onConstructor_ = @Autowired) | ||
| 21 | +public class ScheduledTask { | ||
| 22 | + private final OrderDbService orderDbService; | ||
| 23 | + private final OrderDbMapper orderDbMapper; | ||
| 24 | + | ||
| 25 | + // 每5秒执行一次的定时任务 | ||
| 26 | +// @Scheduled(fixedRate = 5000) | ||
| 27 | +// public void task1() { | ||
| 28 | +// System.out.println("定时任务1:每5秒执行一次,当前时间:" + System.currentTimeMillis()); | ||
| 29 | +// } | ||
| 30 | + | ||
| 31 | + // 每天凌晨2点执行一次的定时任务 | ||
| 32 | + @Scheduled(cron = "0 0 1 * * *") | ||
| 33 | + public void taskForUpdateOrderStatus() { | ||
| 34 | + LocalDate today = LocalDate.now(); | ||
| 35 | + LocalDateTime todayZero = today.atStartOfDay(); | ||
| 36 | + LocalDate tomorrow = today.plusDays(1); | ||
| 37 | + LocalDateTime tomorrowZero = tomorrow.atStartOfDay(); | ||
| 38 | + | ||
| 39 | + LambdaQueryWrapper<OrderDb> queryWrapper = Wrappers.lambdaQuery(); | ||
| 40 | + queryWrapper.eq(OrderDb::getStatus, OrderStatusEnum.TO_PREPARE.getNumber()); | ||
| 41 | + queryWrapper.ge(OrderDb::getMealTime, LocalDateTimeUtil.of(todayZero)); | ||
| 42 | + queryWrapper.le(OrderDb::getMealTime, LocalDateTimeUtil.of(tomorrowZero)); | ||
| 43 | + | ||
| 44 | + final var list = orderDbService.list(queryWrapper); | ||
| 45 | + for( OrderDb orderDb: list) { | ||
| 46 | + orderDb.setStatus(OrderStatusEnum.PREPARING_VALUE); | ||
| 47 | + orderDbMapper.updateById(orderDb); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment