package com.xydl.cac.task;

import com.xydl.cac.entity.WarnRule;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.WarnRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class CacheTask {

    @Resource
    WarnRuleService ruleService;

    @Scheduled(cron = "0 0 1 * * ?")
    private void clearCache1() {
        StaticVariable.unit_Cache.clear();
        StaticVariable.modevType_Cache = null;
        StaticVariable.jg_Cache = null;
        StaticVariable.zsb_Cache = null;
        StaticVariable.rule_Cache.clear();
        StaticVariable.paramRelationMap.clear();
    }

    @Scheduled(cron = "0 30 9 * * ?")
    private void clearCache9() {
        StaticVariable.doneWarnMap.clear();
    }

    @Scheduled(initialDelay = 30 * 1000, fixedDelay = 60 * 1000)
    private void refreshRule() {
        try {
            List<WarnRule> list = ruleService.listAll(null);
            for (WarnRule item : list) {
                WarnRule rule = StaticVariable.rule_Cache.get(item.getId());
                if (rule == null) {
                    item.initComparator(item.getComparator(), item.getOperator());
                    StaticVariable.rule_Cache.put(item.getId(), item);
                }
            }
        } catch (Exception e) {
            log.error("CacheTask.refreshRule error.", e);
        }
    }
}
