package com.bocloud.ims.repository.resource.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.database.core.BasicDao;

/**
 * 增强型的BaseRepository
 * 
 * @author sxp
 * @since May 23, 2018
 */
@Repository("enhancedBaseRepository")
public class EnhancedBaseRepository<T, Pk extends Serializable> extends BasicDao {

	@SuppressWarnings("unchecked")
	public T get(Class<T> clazz, Pk id) {
		Object obj = this.loadEntity(clazz, id);
		return (T) obj;
	}

	@SuppressWarnings("unchecked")
	public List<T> list(Class<T> clazz, String sql, Map<String, Object> params) {
		List<Object> list = queryForList(sql, params, clazz);
		List<T> result;
		int size;

		if (list == null || (size = list.size()) == 0) {
			result = new ArrayList<>(0);
		} else {
			result = new ArrayList<>(size);
			for (Object obj : list) {
				result.add((T) obj);
			}
		}

		return result;
	}

	public void save(T entity) {
		this.saveEntity(entity);
	}

	public boolean delete(T entity) {
		return this.baseDelete(entity);
	}
}
