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

import com.bocloud.common.model.*;
import com.bocloud.ims.entity.resource.fusionsphere.FusionSphereHostPort;
import com.bocloud.ims.interfaces.resource.fusionsphere.FusionSphereHostPortService;
import com.bocloud.ims.repository.resource.fusionsphere.FusionSphereHostPortRepository;
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;

/**
 * @ClassName: FusionSphereHostPortServiceImpl
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/11/2 14:15
 */
@Slf4j
@Service
public class FusionSphereHostPortServiceImpl implements FusionSphereHostPortService {

    @Autowired
    private FusionSphereHostPortRepository hostPortRepository;

    @Override
    public BocloudResult<GridBean<FusionSphereHostPort>> 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 = hostPortRepository.count(params);
            List<FusionSphereHostPort> list = this.hostPortRepository.list(page, rows, params, sorter);
            GridBean<FusionSphereHostPort> gridBean = new GridBean<>(page, rows, total, list);
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            log.error("list hostPort error!", e);
            return new BocloudResult<>(false, "查询失败:" + e.getMessage());
        }
    }
}