package com.lutao.cloud.oil.runner;

import com.google.common.collect.Lists;
import com.lutao.cloud.oil.service.PlatformInfoService;
import com.lutao.cloud.oil.service.SecurityRiskAssessmentSummaryService;
import com.lutao.entity.PlatformInfo;
import com.lutao.entity.SecurityRiskAssessmentSummary;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SecurityRiskAssessmentSummaryInit {

    @Resource
    private PlatformInfoService platformInfoService;

    @Resource
    private SecurityRiskAssessmentSummaryService securityRiskAssessmentSummaryService;

    @Resource(name = "redisTemplateAlias")
    private RedisTemplate redisTemplate;

    private static final String LOCK_KEY = "scheduled_task_lock:SecurityRiskAssessmentSummaryInit";

    private boolean acquireLock() {
        // 尝试获取锁，设置超时时间为 1 分钟
        return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(LOCK_KEY, "locked", 1, TimeUnit.MINUTES));
    }

    private void releaseLock() {
        // 释放锁
        redisTemplate.delete(LOCK_KEY);
    }


    /**
     * 初始化安全风险评估汇总表
     */
    @Scheduled(cron = "0 0 0 1 1 ?")
    public void executeTask() {
      if (acquireLock()){
          try {
              log.info("Executing task on New Year's Day at 1:00 AM");
              ArrayList<SecurityRiskAssessmentSummary> securityRiskAssessmentSummaries = Lists.newArrayList();

              //获取当年所有平台
              List<PlatformInfo> platformInfos = platformInfoService.list();
              for (PlatformInfo platformInfo : platformInfos) {
                  SecurityRiskAssessmentSummary securityRiskAssessmentSummary = new SecurityRiskAssessmentSummary();
                  securityRiskAssessmentSummary.setPlatformCode(platformInfo.getPlatformCode());
                  securityRiskAssessmentSummary.setAppId(platformInfo.getAppId());
                  securityRiskAssessmentSummary.setEvaluationYear(String.valueOf(LocalDate.now().getYear()));
                  securityRiskAssessmentSummary.setPlatformType(String.valueOf(platformInfo.getPlatformType()));
                  //深评
                  securityRiskAssessmentSummary.setEvaluationType("1");
                  securityRiskAssessmentSummaries.add(securityRiskAssessmentSummary);
                  //自评
                  securityRiskAssessmentSummary.setEvaluationType("2");
                  securityRiskAssessmentSummaries.add(securityRiskAssessmentSummary);

              }
              securityRiskAssessmentSummaryService.saveBatch(securityRiskAssessmentSummaries);
          }catch (Exception e){
              log.error("SecurityRiskAssessmentSummaryInit executeTask error",e);
          }finally {
              releaseLock();
          }
      }

    }
}
