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

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.common.utils.MapUtils;
import com.bocloud.ims.entity.resource.SangforAz;
import com.bocloud.ims.interfaces.resource.sangfor.SangforAzService;
import com.bocloud.ims.repository.resource.SangforAzRepository;
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.Assert;

import java.util.*;

/**
 * 深信服资源池服务实现类
 *
 * @author sxp
 * @since 2023/6/1
 */
@Service
public class SangforAzServiceImpl implements SangforAzService {
    private static final Logger logger = LoggerFactory.getLogger(SangforAzServiceImpl.class);

    @Autowired
    private SangforAzRepository azRepository;

    @Override
    public BocloudResult<GridBean<SangforAz>> list(Pager pager, RequestContext context) {
        try {
            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);
            sorter.put("id", Common.ZERO);

            Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
            Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
            List<SangforAz> list;
            List<SimpleBean> beans;
            int total;
            GridBean gridBean;

            total = azRepository.count(params);
            if (simple) {
                beans = this.azRepository.list(params, sorter);
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                list = azRepository.list(page, rows, params, sorter);
                gridBean = GridHelper.getBean(page, rows, total, list);
            }

            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("list cloudVendor fail:", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

    @Override
    public BocloudResult<SangforAz> detail(String uuid) {
        try {
            SangforAz sangforAz = azRepository.query(uuid);
            Assert.notNull(sangforAz, "云平台不存在");
            return new BocloudResult<>(true,sangforAz,"查询详情成功");
        }catch (Exception e){
            logger.error("query sangforAz detail fail:",e);
            return new BocloudResult<>(false,"查询失败");
        }
    }

    @Override
    public Map<String, String> getAllAzUuidNameMap() {
        List<SangforAz> azList = azRepository.listByVendor(null);
        if (azList == null || azList.isEmpty()) {
            return new HashMap<>(0);
        }

        Map<String, String> azUuidNameMap = new HashMap<>(MapUtils.capacity(azList.size()));
        for (SangforAz az : azList) {
            azUuidNameMap.put(az.getUuid(), az.getName());
        }

        return azUuidNameMap;
    }

    @Override
    public Map<String, SangforAz> getAllAzUuidMap() {
        List<SangforAz> azList = azRepository.listByVendor(null);
        if (azList == null || azList.isEmpty()) {
            return new HashMap<>(0);
        }

        Map<String, SangforAz> azUuidMap = new HashMap<>(MapUtils.capacity(azList.size()));
        for (SangforAz az : azList) {
            azUuidMap.put(az.getUuid(), az);
        }

        return azUuidMap;
    }
}
