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

import com.bocloud.common.model.*;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.horizon.VmwareHorizonVcenter;
import com.bocloud.ims.interfaces.resource.horizon.VmwareHorizonVcenterService;
import com.bocloud.ims.repository.resource.horizon.VmwareHorizonVcenterRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * horizon vcenter
 *
 * @author wangyu
 * @version 1.0
 * @since 2020年2月27日
 */
@Service("vmwareHorizonVcenterService")
public class VmwareHorizonVcenterServiceImpl implements VmwareHorizonVcenterService {

    private static Logger logger = LoggerFactory.getLogger(VmwareHorizonVcenterServiceImpl.class);
    @Autowired
    private VmwareHorizonVcenterRepository repository;

    @Override
    public BocloudResult<GridBean<VmwareHorizonVcenter>> list(Pager pager, RequestContext context) {
        List<VmwareHorizonVcenter> list;
        int total;
        GridBean gridBean;
        try {
            pager.getSorter().put("gmtCreate", Common.ONE);
            total = repository.count(pager.getParams());
            if (pager.getSimple()) {
                List<SimpleBean> beans = repository.list(pager.getParams(), pager.getSorter());
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                list = repository.list(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter());
                gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, list);
            }
            return new BocloudResult<>(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("Get error message：{}", e);
            return new BocloudResult<>(false, "查询失败");
        }
    }

}
