package com.bocloud.ims.service.scheduler.cloudalarm;

import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.service.scheduler.cloudalarm.azure.AzureAlarmUtils;
import com.bocloud.service.core.CurrentService;
import org.apache.commons.collections.CollectionUtils;
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.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: AutoMateSyncCloudAlarm
 * @Description:公有云同步告警
 * @author: zhenpingLi/李振平
 * @date: 2021/1/18 13:56
 */
@Component
public class AutoMateSyncCloudAlarm {

    private static Logger logger = LoggerFactory.getLogger(AutoMateSyncCloudAlarm.class);
    @Autowired
    private CloudVendorRepository cloudVendorRepository;
    @Autowired
    private CurrentService service;
    @Autowired
    private AzureAlarmUtils azureAlarmUtils;

    @Scheduled(cron = "0 */15 * * * ?")
    protected void execute() {
        if (service.isLeader()) {
            autoMateAlarmExecute();
        }
    }

    private void autoMateAlarmExecute() {
        logger.info("开始执行同步公有云警报信息......");
        List<CloudVendor> vendors = cloudVendorRepository.listAll();
        Map<String, List<CloudVendor>> vendorsMap = vendors.stream().collect(Collectors.groupingBy(CloudVendor::getType));
        for (String vendorType : vendorsMap.keySet()) {

            List<CloudVendor> typeVendors = vendorsMap.get(vendorType);
            if (CollectionUtils.isEmpty(typeVendors)) {
                continue;
            }

            CloudProvider cloudProvider = CloudProvider.valueOf(vendorType);
            switch (cloudProvider) {
                case AZURE:
                    azureAlarmUtils.alarm(typeVendors);
                    break;
                default:

                    break;
            }
        }
        logger.info("同步公有云警报信息结束");

    }


}