package com.bocloud.ims.service.topology;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.GridHelper;
import com.bocloud.ims.entity.topology.Category;
import com.bocloud.ims.interfaces.topology.CategoryService;
import com.bocloud.ims.repository.topology.CategoryRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
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.List;
import java.util.Map;

/**
 * CiCategory service实现层
 * 
 * @author lyy
 * @Version 1.0
 * @since 2017年9月13日
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
	private static final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
	@Autowired
	private CategoryRepository categoryRepository;

	@Override
	public BocloudResult list(int page, int rows, List<Param> params, Map<String, String> sorter, Boolean simple) {
		GridBean gridBean = null;
		try {
			if (null == params) {
				params = Lists.newArrayList();
			}
			if (null == sorter) {
				sorter = Maps.newHashMap();
			}
			int total = this.categoryRepository.count(params);
			List<Category> list = this.categoryRepository.list(page, rows, params, sorter);
			gridBean = GridHelper.getBean(page, rows, total, list);
			return new BocloudResult(true, gridBean, "查询成功");
		} catch (Exception e) {
			logger.error("Query notice Category list fail:", e);
			return new BocloudResult(false, "查询失败");
		}
	}

	@Override
	public BocloudResult query(Long id) {
		Category queryCategory;
		try {
			queryCategory = this.categoryRepository.query(id);
		} catch (Exception e) {
			logger.error("Query notice Category list fail:", e);
			return new BocloudResult(false, "查询失败");
		}
		ArrayList<Category> list = Lists.newArrayList();
		list.add(queryCategory);
		return new BocloudResult(true, list, "查询成功");
	}

	@Override
	public BocloudResult list() {
		try {
			List<Category> categories = categoryRepository.list();
			return new BocloudResult(true, categories, "查询成功");
		} catch (Exception e) {
			logger.error("Query relationCategorie list error:", e);
			return new BocloudResult(false, "查询失败");
		}
	}

}
