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.VmwareHorizonDesktopPool;
import com.bocloud.ims.entity.resource.horizon.VmwareHorizonVcenter;
import com.bocloud.ims.interfaces.resource.horizon.VmwareHorizonDesktopPoolService;
import com.bocloud.ims.repository.resource.horizon.VmwareHorizonDesktopPoolRepository;
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 桌面池
 *
 * @author wangyu
 * @version 1.0
 * @since 2020年2月27日
 */
@Service("vmwareHorizonDesktopPoolService")
public class VmwareHorizonDesktopPoolServiceImpl implements VmwareHorizonDesktopPoolService {

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

    @Override
    public BocloudResult<GridBean<VmwareHorizonDesktopPool>> list(Pager pager, RequestContext context) {
        List<VmwareHorizonDesktopPool> 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());
                for (VmwareHorizonDesktopPool pool : list) {
                    if (null != pool.getHorizonVcenterId()) {
                        VmwareHorizonVcenter vcenter = vcenterRepository.query(pool.getHorizonVcenterId());
                        pool.setVcenter(vcenter);
                    }
                }
                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, "查询失败");
        }
    }


}
