package com.bocloud.ims.service.resource.azure.db;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.azure.db.CloudDatabaseServerLogDriver;
import com.bocloud.cmp.model.azure.db.AzureDatabaseServerLogModel;
import com.bocloud.cmp.model.azure.db.AzureDatabaseServerModel;
import com.bocloud.common.model.*;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.ResourceGroup;
import com.bocloud.ims.entity.resource.azure.db.AzureDatabaseServer;
import com.bocloud.ims.entity.resource.azure.db.AzureDatabaseServerLog;
import com.bocloud.ims.interfaces.resource.azure.db.CloudDatabaseServerLogService;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.ResourceGroupRepository;
import com.bocloud.ims.repository.resource.azure.db.CloudDatabaseServerLogRepository;
import com.bocloud.ims.repository.resource.azure.db.CloudDatabaseServerRepository;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: CloudDatabaseServerLogServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/2/4 15:23
 */
@Service("cloudDatabaseServerLogService")
public class CloudDatabaseServerLogServiceImpl implements CloudDatabaseServerLogService {

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

    @Autowired
    private CloudDatabaseServerLogRepository azureDatabaseServerLogRepository;
    @Autowired
    private CloudVendorRepository vendorRepository;
    @Autowired
    private CloudDatabaseServerLogDriver cloudDatabaseServerLogDriver;
    @Autowired
    private ResourceGroupRepository resourceGroupRepository;
    @Autowired
    private CloudDatabaseServerRepository cloudDatabaseServerRepository;


    @Override
    public BocloudResult<List<AzureDatabaseServerLogModel>> listApi(Long id) {
        try {
            AzureDatabaseServer entityParams = cloudDatabaseServerRepository.query(id);
            if (ObjectUtils.isEmpty(entityParams)) {
                logger.error("get azureDatabaseServer failure");
                return new BocloudResult<>(false, "数据不存在");
            }
            CloudVendor vendor = vendorRepository.query(entityParams.getVendorId());
            if (ObjectUtils.isEmpty(vendor)) {
                logger.error("get cloudVendor failure");
                return new BocloudResult<>(false, "平台验证失败");
            }
            ResourceGroup resourceGroup = resourceGroupRepository.query(entityParams.getResourceGroupId());
            if (ObjectUtils.isEmpty(resourceGroup)) {
                logger.error("get resourceGroup failure");
                return new BocloudResult<>(false, "资源组不能为空");
            }
            AzureDatabaseServerModel model = new AzureDatabaseServerModel();
            model.setName(entityParams.getName());
            model.setResourceGroupName(resourceGroup.getName());
            BocloudResult result = cloudDatabaseServerLogDriver.list(vendor.getUuid(), entityParams.getRegionId(), model);
            if (result.isFailed()) {
                return new BocloudResult<>(false, "查询失败");
            }
            List<AzureDatabaseServerLogModel> logModels = JSONArray.parseArray(JSONObject.toJSONString(result.getData()), AzureDatabaseServerLogModel.class);
            return new BocloudResult<>(true, logModels, "查询成功");
        } catch (Exception e) {
            logger.error("list AzureDatabaseLog error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }
    }

    @Override
    public BocloudResult<GridBean<AzureDatabaseServerLog>> list(Pager pager, RequestContext context) {
        List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
        Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
        Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
        Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
        Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);

        try {
            int total = azureDatabaseServerLogRepository.count(params);
            List<AzureDatabaseServerLog> list = this.azureDatabaseServerLogRepository.list(page, rows, params, sorter);
            GridBean<AzureDatabaseServerLog> gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("list AzureDatabaseLog error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }

    }


}