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.VmwareHorizonFarm;
import com.bocloud.ims.interfaces.resource.horizon.VmwareHorizonFarmService;
import com.bocloud.ims.repository.resource.horizon.VmwareHorizonFarmRepository;
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场
 *
 * @author wangyu
 * @version 1.0
 * @since 2020年2月27日
 */
@Service("vmwareHorizonFarmService")
public class VmwareHorizonFarmServiceImpl implements VmwareHorizonFarmService {

    private static Logger logger = LoggerFactory.getLogger(VmwareHorizonFarmServiceImpl.class);
    @Autowired
    private VmwareHorizonFarmRepository repository;

    @Override
    public BocloudResult<GridBean<VmwareHorizonFarm>> list(Pager pager, RequestContext context) {
        List<VmwareHorizonFarm> 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, "查询失败");
        }
    }


}
