package com.microservices.dss.task;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.microservices.common.core.constant.CacheConstants;
import com.microservices.common.redis.service.RedisService;
import com.microservices.dss.projects.domain.vo.GlobalResourcesDataVo;
import com.microservices.dss.projects.service.IProjectGlobalResourcesService;
import com.microservices.dss.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.concurrent.TimeUnit;

import static com.microservices.dss.utils.DateUtil.getFormattedCurrentDate;

@Component
public class ProjectsGlobalResourcesTask {

    private static final Logger log = LoggerFactory.getLogger(ProjectsGlobalResourcesTask.class);

    @Autowired
    private IProjectGlobalResourcesService projectGlobalResourcesService;

    @Autowired
    private RedisService redisService;

    @Scheduled(cron = "0 0 2 * * ?")
    public void updateProjectsGlobalResources() throws ParseException, JsonProcessingException {
        log.info("开始更新近一年全局资源数据");
        String currentDate = getFormattedCurrentDate();
        String startTime = DateUtil.DateAddByYear(currentDate, -1);
        GlobalResourcesDataVo globalResourcesDataVo = projectGlobalResourcesService.getProjectGlobalResourcesData();
        String cacheKey = CacheConstants.getGlobalResourcesDataKey(startTime, currentDate);
        redisService.setCacheObject(cacheKey, globalResourcesDataVo, 24L, TimeUnit.HOURS);
    }
}
