package com.ruoyi.sensitive.config;

import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.ruoyi.sensitive.cache.LastUpdateTimeCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class MySensitiveWordScheduleRefresh {

    @Autowired
    private SensitiveWordBs sensitiveWordBs;
    @Autowired
    private LastUpdateTimeCache cache;

    @Scheduled(fixedDelay = 20,timeUnit = TimeUnit.SECONDS)
    public void job(){
        try {
            LocalDateTime time = cache.getUpdateTime();
            LocalDateTime now = LocalDateTime.now();
            if(time == null || time.isBefore(now)){
                return;
            }
            refresh();
        } catch (Exception e) {
            log.error("MySensitiveWordScheduleRefresh meet ex", e);
        }
    }

    @PostConstruct
    public void init() {
    }

    private void refresh() {
        sensitiveWordBs.init();
        cache.setRefreshTime();
    }

}
