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

import com.bocloud.common.model.*;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.hmc.HmcIoAdapter;
import com.bocloud.ims.interfaces.resource.hmc.HmcIoAdapterService;
import com.bocloud.ims.repository.resource.hmc.HmcIoAdapterRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * HMC输入输出适配器服务
 * 
 * @author sxp
 * @since Jun 19, 2018
 */
@Service("hmcIoAdapterService")
public class HmcIoAdapterServiceImpl implements HmcIoAdapterService {
    private static Logger logger = LoggerFactory.getLogger(HmcIoAdapterServiceImpl.class);

    @Autowired
    private HmcIoAdapterRepository hmcIoAdapterRepository;

    @Override
    public BocloudResult<GridBean<HmcIoAdapter>> list(Pager pager) {
        List<HmcIoAdapter> list;
        List<SimpleBean> beans;
        GridBean gridBean;
        try {
            int page = pager.getPage();
            int rows = pager.getRows();
            List<Param> params = pager.getParams();
            Map<String, String> sorter = pager.getSorter();
            boolean simple = pager.getSimple();

            int total = this.hmcIoAdapterRepository.count(params);
            if (simple) {
                beans = this.hmcIoAdapterRepository.list(params, sorter);
                gridBean = new GridBean(1, 1, total, beans);
            } else {
                list = this.hmcIoAdapterRepository.list(page, rows, params, sorter);
                gridBean = GridHelper.getBean(page, rows, total, list);
            }
            return new BocloudResult(true, gridBean, "查询成功");
        } catch (Exception e) {
            logger.error("Query HMC io adapter list fail:", e);
            return new BocloudResult(false, "查询失败," + e.getMessage());
        }
    }

}
