package com.bocloud.ims.service.resource;

import com.bocloud.common.model.*;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.resource.ScsiLun;
import com.bocloud.ims.interfaces.resource.ScsiLunService;
import com.bocloud.ims.repository.resource.ScsiLunRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("scsiLunService")
public class ScsiLunServiceImpl implements ScsiLunService {

	private static Logger logger = LoggerFactory.getLogger(ScsiLunServiceImpl.class);
	@Autowired
	private ScsiLunRepository scsiLunRepository;

	@Override
	public BocloudResult<GridBean<ScsiLun>> list(Pager pager) {
		List<ScsiLun> list = null;
		int total = 0;
		GridBean gridBean = null;
		try {

			int page = pager.getPage();
			int rows = pager.getRows();
			List<Param> params = pager.getParams();
			Map<String, String> sorter = pager.getSorter();
			boolean simple = pager.getSimple();

			params = null == params ? new ArrayList<>() : params;
			sorter = null == sorter ? new HashMap<>() : sorter;
			total = scsiLunRepository.count(params);
			if (simple) {
				List<SimpleBean> beans = scsiLunRepository.list(params, sorter);
				gridBean = new GridBean(1, 1, total, beans);
			} else {
				list = scsiLunRepository.list(page, rows, params, sorter);
				gridBean = GridHelper.getBean(page, rows, total, list);
			}
			return new BocloudResult<>(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("Get error message：{}", e);
			return new BocloudResult<>(false, "查询失败");
		}
	}
}
