package cn.hmsoft.jdbc.core;

import java.util.List;
import java.util.Map;

import cn.hmsoft.exception.develop.GenericNotDefineException;
import cn.hmsoft.helper.ClassHelper;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.jdbc.entity.QueryOrder;
import cn.hmsoft.jdbc.entity.ValueCompareType;

public class PlatformDaoSupport<T> extends AbstractDaoSupport {
	private Class<T> clazz;

	@SuppressWarnings("unchecked")
	public PlatformDaoSupport() {
		try {
			// 获取类的泛型参数,第一个泛型
			this.clazz = (Class<T>) ClassHelper.getGenericsClass(this.getClass());
		} catch (Exception e) {
			throw new GenericNotDefineException(this.getClass());
		}
	}

	/*******************************************
	 * 统计某张表的所有数据
	 * 
	 * @return 表的总记录数
	 */
	public int count() {
		return this.count(clazz);
	}

	/***************************
	 * 统计数据
	 */
	public int count(String columnName, Object columnValue) {
		return count(clazz, columnName, columnValue, ValueCompareType.Equal);
	}

	/***************************
	 * 统计数据
	 */
	public int count(String columnName, Object columnValue, ValueCompareType compareType) {
		return count(clazz, columnName, columnValue, compareType);
	}

	/******************************************************
	 * 根据实现定义的主键删除记录
	 */
	public int delete(Object keyValue) {
		return this.delete(clazz, keyValue);
	}

	/***************************************************************
	 * 根据某个字段的值删除符合条件的记录
	 */
	public int delete(String columnName, Object columnValue) {
		return this.delete(clazz, columnName, columnValue, ValueCompareType.Equal);
	}

	/***************************************************************
	 * 根据某个字段的值删除符合条件的记录
	 */
	public int delete(String columnName, Object columnValue, ValueCompareType compareType) {
		return this.delete(clazz, columnName, columnValue, compareType);
	}

	/***************************************************************
	 * 根据map修改符合条件的记录
	 * 
	 */
	public int updateByMap(Map<String, Object> columnMap, Map<String, Object> conditionMap) {
		return this.updateByMap(clazz, columnMap, conditionMap);
	}

	/*************************
	 * 删除某张表所有数据
	 */
	public int deleteAll() {
		return this.deleteAll(clazz);
	}

	/*****
	 * 根据sql查询单个简单值，如String Integer
	 */
	public T findObject(String sql, Object... args) {
		return this.findObject(clazz, sql, args);
	}

	/***************************************
	 * 根据sql语句查询符合记录的数据
	 */
	public List<T> listBySql(String sql, Object... args) {
		return this.listBySql(clazz, sql, args);
	}

	/***************************************
	 * 根据sql语句查询符合记录的数据
	 */
	public List<T> listBySql(QueryOrder order, String sql, Object... args) {
		return this.listBySql(clazz, order, sql, args);
	}

	/***************************************
	 * 根据sql语句查询符合记录的数据
	 */
	public List<T> listBySql(int start, int limit, String sql, Object... args) {
		return this.listBySql(clazz, start, limit, sql, args);
	}

	/***************************************
	 * 根据sql语句查询符合记录的数据
	 */
	public List<T> listBySql(QueryOrder order, int start, int limit, String sql, Object... args) {
		return this.listBySql(clazz, order, start, limit, sql, args);
	}

	/*****************************************
	 * 查询符合某条件的记录
	 */
	public List<T> list(String columnName, Object columnValue) {
		return this.list(clazz, columnName, columnValue, ValueCompareType.Equal);
	}

	/*****************************************
	 * 查询符合某条件的记录
	 */
	public List<T> list(String columnName, Object columnValue, QueryOrder queryOrder) {
		return this.list(clazz, columnName, columnValue, ValueCompareType.Equal, queryOrder);
	}

	/*****************************************
	 * 查询符合某条件的记录
	 */
	public List<T> list(String columnName, Object columnValue, ValueCompareType compareType) {
		return this.list(clazz, columnName, columnValue, compareType, null);
	}

	/*****************************************
	 * 查询符合某条件的记录
	 */
	public List<T> list(String columnName, Object columnValue, ValueCompareType compareType, QueryOrder queryOrder) {
		return this.list(clazz, columnName, columnValue, compareType, queryOrder);
	}

	/*****************************************
	 * 查询符合某条件的记录
	 */
	public List<T> list(String columnName, Object columnValue, int start, int limit) {
		return this.list(clazz, columnName, columnValue, start, limit);
	}

	public List<T> list(String columnName, Object columnValue, QueryOrder order, int start, int limit) {
		return this.list(clazz, columnName, columnValue, order, start, limit);

	}

	public List<T> list(String columnName, Object columnValue, ValueCompareType compareType, int start, int limit,
			QueryOrder queryOrder) {
		return this.list(clazz, columnName, columnValue, compareType, start, limit, queryOrder);

	}

	public List<T> all() {
		return this.all(clazz);
	}

	public List<T> all(QueryOrder order) {
		return this.all(clazz, order);
	}

	public List<T> all(int start, int limit) {
		return this.all(clazz, start, limit);
	}

	public List<T> all(int start, int limit, QueryOrder order) {
		return this.all(clazz, start, limit, order);
	}

	public Pager page(int start, int limit) {
		return this.page(clazz, start, limit);
	}

	/*****
	 * 分页查询一张表的所有记录
	 */
	public Pager page(int start, int limit, QueryOrder queryOrder) {
		return this.page(clazz, start, limit, queryOrder);
	}

	/*****
	 * 根据单一字段分页查询记录
	 */
	public Pager pageByColumn(String columnName, Object columnValue, int start, int limit) {
		return this.pageByColumn(clazz, columnName, columnValue, ValueCompareType.Equal, start, limit, null);
	}

	/*****
	 * 根据单一字段分页查询记录
	 */
	public Pager pageByColumn(String columnName, Object columnValue, int start, int limit, QueryOrder queryOrder) {
		return this.pageByColumn(clazz, columnName, columnValue, ValueCompareType.Equal, start, limit, queryOrder);
	}

	/*****
	 * 根据单一字段分页查询记录
	 */
	public Pager pageByColumn(String columnName, Object columnValue, ValueCompareType compareType, int start, int limit,
			QueryOrder queryOrder) {
		return this.pageByColumn(clazz, columnName, columnValue, compareType, start, limit, queryOrder);
	}

	/*****
	 * 根据sql分页查询记录
	 */
	public Pager pageBySql(int start, int limit, String sql, Object... args) {
		return this.pageBySql(clazz, start, limit, sql, args);
	}

	public Pager pageBySql(QueryOrder order, int start, int limit, String sql, Object... args) {
		return this.pageBySql(clazz, order, start, limit, sql, args);
	}


	/*************************
	 * 根据主键值查找记录
	 */
	public T find(Object keyValue) {
		return this.find(clazz, keyValue);
	}

	/*****
	 * 根据某一个字段值，读取第一行数据
	 */
	public T find(String columnName, Object columnValue) {
		return this.find(clazz, columnName, columnValue, ValueCompareType.Equal, null);
	}

	public T find(String columnName, Object columnValue, QueryOrder queryOrder) {
		return this.find(clazz, columnName, columnValue, ValueCompareType.Equal, queryOrder);
	}

	public T find(String columnName, Object columnValue, ValueCompareType compareType, QueryOrder queryOrder) {
		return this.find(clazz, columnName, columnValue, compareType, queryOrder);
	}

	/*****
	 * 根据sql语句获取符合条件的第一条记录
	 */
	public T findBySql(String sql, Object... args) {
		return this.findBySql(clazz, sql, args);
	}

	
}
