package com.wechat.global.base.dao.impl;

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

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.dao.DataAccessException;

import com.wechat.global.base.dao.IBaseSqlDao;
import com.wechat.global.base.dao.Pagination;
import com.wechat.global.base.dao.PagingResult;

/** 
 * @ClassName: BaseSqlDaoImpl
 * @Description: 数据库操作基本实现类
 * @author shensuoyao
 * @date 2014年7月10日 下午3:52:03
 */
public class BaseSqlDaoImpl extends SqlSessionDaoSupport implements IBaseSqlDao {

	/** (非 Javadoc) 
	* Title: insert
	* Description:
	* @param statementName
	* @param entity
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#insert(java.lang.String, java.lang.Object)
	*/ 
	@Override
	public <T> int insert(String statementName, T entity) throws RuntimeException {
		try {
			return this.getSqlSession().insert(statementName, entity);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: update
	* Description:
	* @param statementName
	* @param entity
	* @return
	* @throws RuntimeException
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#update(java.lang.String, java.lang.Object)
	*/ 
	@Override
	public <T> int update(String statementName, T entity) throws RuntimeException {
		try {
			return this.getSqlSession().update(statementName, entity);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: updateParam
	* Description:
	* @param statementName
	* @param param
	* @return
	* @throws RuntimeException
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#updateParam(java.lang.String, java.util.Map)
	*/ 
	@Override
	public int updateParam(String statementName, Map<?, ?> param) throws RuntimeException {
		try {
			return this.getSqlSession().update(statementName, param);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: delete
	* Description:
	* @param primaryKey
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#delete(java.io.Serializable)
	*/ 
	@Override
	public <PK extends Serializable> int delete(String statementName, PK primaryKey) throws RuntimeException {
		try {
			return this.getSqlSession().delete(statementName, primaryKey);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: deleteParam
	* Description:
	* @param param
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#deleteParam(java.util.Map)
	*/ 
	@Override
	public int deleteParam(String statementName, Map<?, ?> param) throws RuntimeException {
		try {
			return this.getSqlSession().delete(statementName, param);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: truncate
	* Description:
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#truncate()
	*/ 
	@Override
	public int truncate(String statementName) throws RuntimeException {
		try {
			return this.getSqlSession().delete(statementName);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: count
	* Description:
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#count()
	*/ 
	@Override
	public int count(String statementName) throws RuntimeException {
		try {
			return this.getSqlSession().selectOne(statementName);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: count
	* Description:
	* @param param
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#count(java.lang.Object)
	*/ 
	@Override
	public int count(String statementName, Object param) throws RuntimeException {
		try {
			return this.getSqlSession().selectOne(statementName, param);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: get
	* Description:
	* @param primaryKey
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#get(java.io.Serializable)
	*/ 
	@Override
	public <T, PK extends Serializable> T get(String statementName, PK primaryKey) throws RuntimeException {
		try {
			return this.getSqlSession().selectOne(statementName, primaryKey);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: selectOne
	* Description:
	* @param statementName
	* @param obj
	* @return
	* @throws RuntimeException
	* @see com.wechat.global.dao.test.core.base.dao.IBaseSqlDao#selectOne(java.lang.String, java.lang.Object)
	*/ 
	@Override
	public <T> T selectOne(String statementName, Object obj) throws RuntimeException {
		try {
			return this.getSqlSession().selectOne(statementName, obj);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: select
	* Description:
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#select()
	*/ 
	@Override
	public <T> List<T> select(String statementName) throws RuntimeException {
		try {
			return this.getSqlSession().selectList(statementName);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}
	

	/** (非 Javadoc) 
	* Title: select
	* Description:
	* @param statementName
	* @param obj
	* @return
	* @throws RuntimeException
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#select(java.lang.String, java.lang.Object)
	*/ 
	@Override
	public <T> List<T> select(String statementName, Object obj) throws RuntimeException {
		try {
			return this.getSqlSession().selectList(statementName, obj);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: selectParam
	* Description:
	* @param param
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#selectParam(java.util.Map)
	*/ 
	@Override
	public <T> List<T> selectParam(String statementName, Map<?, ?> param) throws RuntimeException {
		try {
			return this.getSqlSession().selectList(statementName, param);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: selectPagination
	* Description:
	* @param pagination
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#selectPagination(com.wechat.global.dao.tiangou.me.core.support.Pagination)
	*/ 
	@Override
	public <T> PagingResult<T> selectPagination(String statementName, String countStatementName, Pagination pagination)
			throws RuntimeException {
		try {
			// 默认为第一页
			int page = pagination.getPage();
			// 默认每页15个
			int size = pagination.getSize();
			
			RowBounds rowBounds = new RowBounds((page - 1) * size, size);

			Map<Object, Object> param = pagination.getParams();
			if (param != null) {
				param.put("orderColumn", pagination.getOrderColumn());
			} else {
				param = new HashMap<Object, Object>();
				param.put("orderColumn", pagination.getOrderColumn());
			}

			List<T> resultList = this.getSqlSession().selectList(statementName, param, rowBounds);
			int totalSize = count(countStatementName, pagination.getParams());
			
			PagingResult<T> pagingResult = new PagingResult<T>();
			pagingResult.setCurrentPage(page);
			pagingResult.setTotalSize(totalSize);
			pagingResult.setPageSize(size);
			pagingResult.setResultList(resultList);
			
			return pagingResult;
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: insertBatch
	* Description:
	* @param list
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#insertBatch(java.util.List)
	*/ 
	@Override
	public <T> int insertBatch(String statementName, List<T> list) throws RuntimeException {
		try {
			return this.getSqlSession().insert(statementName, list);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: updateBatch
	* Description:
	* @param list
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#updateBatch(java.util.List)
	*/ 
	@Override
	public <T> int updateBatch(String statementName, List<T> list) throws RuntimeException {
		try {
			int rows = 0;
			for(T t : list) {
				rows += this.getSqlSession().update(statementName, t);
			}
			return rows;
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

	/** (非 Javadoc) 
	* Title: deleteBatch
	* Description:
	* @param list
	* @return
	* @see com.wechat.global.dao.tiangou.me.core.support.IBaseSqlDao#deleteBatch(java.util.List)
	*/ 
	@Override
	public <PK extends Serializable> int deleteBatch(String statementName, List<PK> list) throws RuntimeException {
		try {
			return this.getSqlSession().delete(statementName, list);
		} catch (DataAccessException e) {
			throw new RuntimeException(e);
		}
	}

}
