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

import com.bocloud.cmp.entity.enums.BocloudStatus;
import com.bocloud.cmp.model.installation.IpmiInfo;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.entity.resource.ServerConfig;
import com.bocloud.ims.repository.resource.ServerConfigRepository;
import com.bocloud.ims.repository.resource.ServerRepository;
import com.bocloud.ims.service.internal.worker.WorkerService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询服务器状态
 *
 * @author admin
 */
public class ServerTriggerTask implements Runnable {

    private static Logger logger = LoggerFactory.getLogger(ServerTriggerTask.class);

    private String assetCategory;
    private ServerRepository serverRepository;
    private ServerConfigRepository serverConfigRepository;
    private WorkerService workerService;

    public ServerTriggerTask() {
        super();
    }

    public ServerTriggerTask(String assetCategory, ServerRepository serverRepository,
                             ServerConfigRepository serverConfigRepository, WorkerService workerService) {
        super();
        this.assetCategory = assetCategory;
        this.serverRepository = serverRepository;
        this.serverConfigRepository = serverConfigRepository;
        this.workerService = workerService;
    }

    @Override
    public void run() {
        try {
            logger.error(assetCategory + " 同步开始！");
            // 获取服务器集合
            List<Server> serversByCategroy = serverRepository.getByAssetCategory(assetCategory);
            List<Server> servers = serversByCategroy.stream().filter(item -> null == item.getVendorId())
                    .collect(Collectors.toList());
            if (null != servers && servers.isEmpty()) {
                logger.error("未找到需要同步的【" + assetCategory + "】物理服务器！");
                return;
            }
            for (Server server : servers) {
                ServerConfig serverConfig = serverConfigRepository.getBySid(server.getId());
                String ipmiAddress = serverConfig.getIpmiAddress();
                String ipmiAccount = serverConfig.getIpmiAccount();
                String ipmiPassword = serverConfig.getIpmiPassword();
                Integer ipmiPort = serverConfig.getIpmiPort();
                if (null == ipmiPort) {
                    ipmiPort = 623;
                    serverConfig.setIpmiPort(ipmiPort);
                }
                if (StringUtils.isEmpty(ipmiAccount) || StringUtils.isEmpty(ipmiAddress)
                        || StringUtils.isEmpty(ipmiPassword)) {
                    logger.error(assetCategory + "【" + server.getName() + "】 IPMI信息不全，如需及时同步，请设置IMPI信息！");
                    continue;
                }

                IpmiInfo info = new IpmiInfo(ipmiAddress, ipmiAccount, ipmiPassword, ipmiPort);
                BocloudResult result = workerService.check(info);
                if (result.isFailed()) {
                    continue;
                }
                boolean contains = result.toString().contains("Chassis Power is on");
                if (contains) {
                    server.setStatus(BocloudStatus.Server.RUNNING.name());
                    server.setPowerState(BocloudStatus.Server.RUNNING.name());
                } else {
                    server.setStatus(BocloudStatus.Server.STOPPED.name());
                    server.setPowerState(BocloudStatus.Server.STOPPED.name());
                }
                serverRepository.update(server);
            }
            logger.error(assetCategory + " 同步结束！");
        } catch (Exception e) {
            logger.error("服务器状态获取失败：" + e.getMessage());
        }
    }

}