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

import com.bocloud.common.model.*;
import com.bocloud.ims.entity.resource.Vdc;
import com.bocloud.ims.interfaces.resource.fusionsphere.FusionSphereVdcService;
import com.bocloud.ims.repository.resource.VdcRepository;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: SiteServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/10/15 15:16
 */
@Slf4j
@Service
public class FusionSphereVdcServiceImpl implements FusionSphereVdcService {

    @Autowired
    private VdcRepository vdcRepository;

    @Override
    public BocloudResult<GridBean<Vdc>> 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());
        Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
        Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
        try {
            int total = vdcRepository.count(params);
            List<Vdc> list = this.vdcRepository.list(page, rows, params, sorter);
            GridBean<Vdc> gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            log.error("list Vdc error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }
    }
}