package com.rndchina.aisin.base.service.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.rndchina.aisin.base.dao.CommonDao;
import com.rndchina.aisin.base.service.CommonService;

public class CommonServiceImp<T> implements CommonService<T> {

	@Resource(name="commonDao")
	protected CommonDao commonDao;
	protected Class<T> clazz;

	@SuppressWarnings("unchecked")
	public CommonServiceImp() {
		ParameterizedType pt = (ParameterizedType) this.getClass()
				.getGenericSuperclass();
		clazz = (Class<T>) pt.getActualTypeArguments()[0];
	}

	@Override
	public void save(Object o) {
		commonDao.save(o);
	}

	@Override
	public void update(Object o) {
		commonDao.update(o);
	}

	@Override
	public void delete(Object o) {
		commonDao.delete(o);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T get(Serializable id) {
		return (T) commonDao.get(clazz, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findAll() {
		return (List<T>) commonDao.findAll(clazz);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findPart(int pageNo, int pageSize, String orderBy,
			String order, Map<String, Object> parameters) {
		return (List<T>) commonDao.findPart(clazz, pageNo, pageSize, orderBy,
				order, parameters);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findAll(String orderBy, String order) {
		return (List<T>) commonDao.findAll(clazz, orderBy, order);
	}

	@Override
	public long findCount() {
		return commonDao.findCount(clazz);
	}

	@Override
	public long getSumPage(int size) {
		return commonDao.getSumPage(clazz, size);
	}

	@Override
	public long findCount(Map<String, Object> parameters) {
		return commonDao.findCount(clazz, parameters);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> searchPart(int pageNo, int pageSize, String orderBy,
			String order, Map<String, String> conditions,
			Map<String, Object> parameters) {
		return (List<T>) commonDao.searchPart(clazz, pageNo, pageSize, orderBy,
				order, conditions, parameters);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findWithSql(int pageNo, int pageSize, String sql,
			List<?> parameters) {
		return (List<T>) commonDao.findWithSql(clazz, pageNo, pageSize, sql,
				parameters);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> findWithHql(int currentPage, int pageSize, String hql,
			Object[] parameters) {
		return (List<T>) commonDao.findWithHql(currentPage, pageSize, hql,
				parameters);
	}

	@Override
	public long findCountWithHql(String countHql) {
		return commonDao.findCountWithHql(countHql);
	}

	@Override
	public long findCountWithSql(String countSql) {
		return commonDao.findCountWithSql(countSql);
	}

	@Override
	public List<Object[]> findObjectArrayWithSql(String sql, int pageNo,
			int pageSize) {
		return commonDao.findObjectArrayWithSql(sql, pageNo, pageSize);
	}

	@Override
	public void batchDel(String ids, String idName) {
		commonDao.batchDel(clazz, ids, idName);
	}

}
