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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class SecurityRiskAssessmentSummaryInit {

    @Resource
    private PlatformInfoService platformInfoService;

    @Resource
    private SecurityRiskAssessmentSummaryService securityRiskAssessmentSummaryService;


    /**
     * 初始化安全风险评估汇总表
     */
    @Scheduled(cron = "0 0 1 1 * ?")
    public void executeTask() {
        try {
            log.info("Executing task on New Year's Day at 1:00 AM");
            log.info("新年快乐=_=");

            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.setEvaluationType("1");
                securityRiskAssessmentSummaries.add(securityRiskAssessmentSummary);
                //自评
                securityRiskAssessmentSummary.setEvaluationType("2");
                securityRiskAssessmentSummaries.add(securityRiskAssessmentSummary);
            }
            securityRiskAssessmentSummaryService.saveBatch(securityRiskAssessmentSummaries);
        }catch (Exception e){
            log.error("SecurityRiskAssessmentSummaryInit executeTask error",e);
        }

    }
}
