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
package com.xiaopeng.operation.utils.task;
 
import com.xiaopeng.api.model.operation.XpBanner;
import com.xiaopeng.api.vo.operation.XpLogMessage;
import com.xiaopeng.common.core.constant.CacheConstants;
import com.xiaopeng.operation.service.XpBannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Set;
 
/**
 * @author 无关风月
 * @date 2026/2/26 9:49
 */
@Slf4j
@Component
public class BannerTaskUtil {
 
    @Autowired
    private XpBannerService bannerService;
 
    @Resource
    private MongoTemplate mongoTemplate;
 
 
    /**
     * 每隔一分钟去处理的定时任务
     */
    @Scheduled(fixedRate = 60000)
    public void taskMinute() {
        try {
            log.info("处理的banner状态定时任务开始执行,当前时间:{}", LocalDateTime.now());
            // 根据banner开始、结束时间修改banner状态
            List<XpBanner> banners = bannerService.list();
            for (XpBanner banner : banners) {
                // 如果当前时间小于开始时间 那么状态为未开始 当前时间大于结束时间状态为已结束 否则为进行中
                if (banner.getStartTime().isAfter(LocalDateTime.now())) {
                    banner.setStatus(0);
                }else if (banner.getEndTime().isBefore(LocalDateTime.now())){
                    banner.setStatus(2);
                }else{
                    banner.setStatus(1);
                }
            }
            bannerService.updateBatchById(banners);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 删除移动端日志
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void removeLogMessage() {
        //删除大于2天的日志记录
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime twoDayAgo = now.minusDays(2);
        mongoTemplate.remove(new Query(Criteria.where("timestamp").lt(twoDayAgo.toEpochSecond(ZoneOffset.of("+8")))), XpLogMessage.class);
    }
}