package com.bocloud.ims.service.resource.powervc;

import com.bocloud.cmp.driver.compute.StorageConnGroupDriver;
import com.bocloud.cmp.model.powervc.PowervcViosGroupModel;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.interfaces.resource.powervc.ViosGroupService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ViosGroupServiceImpl implements ViosGroupService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StorageConnGroupDriver storageConnGroupDriver;
    @Autowired
    private CloudVendorRepository cloudVendorRepository;

    @Override
    public BocloudResult<List<PowervcViosGroupModel>> list(long vendorId) {
        try {
            CloudVendor vendor = cloudVendorRepository.query(vendorId);
            if (vendor == null) {
                return new BocloudResult(false, "未找到平台：" + vendorId);
            }
            BocloudResult bocloudResult = storageConnGroupDriver.list(vendor.getUuid());
            if (bocloudResult.isFailed()) {
                return bocloudResult;
            }
            List<PowervcViosGroupModel> groups = (List<PowervcViosGroupModel>) bocloudResult.getData();
            groups.removeIf(model -> model.getEnabled() == null || model.getEnabled().equals(false) ||
                    model.getBootHealth() == null || !"OK".equals(model.getBootHealth()) ||
                    model.getDataHealth() == null || !"OK".equals(model.getDataHealth())
            );
            return new BocloudResult(true, groups, "查询成功");
        } catch (Exception e) {
            logger.error("查询失败：", e);
            return new BocloudResult(false, "查询失败");
        }
    }
}
