package cn.orm.util.builder;

import java.sql.SQLException;
import java.util.*;
import org.apache.commons.lang3.StringUtils;
import cn.config.SysConfig;
import cn.constant.CharsetKit;
import cn.orm.util.builder.base.SQLAssemblyUtil;
import cn.orm.util.builder.base.SQLCondition;
import cn.orm.util.builder.base.SQLDelete;
import cn.orm.util.builder.base.SQLInsertion;
import cn.orm.util.builder.base.SQLQuery;
import cn.orm.util.builder.base.SQLUpdate;
import cn.orm.util.metadata.DBMetaData;
import cn.orm.util.metadata.base.Field;
import cn.service.util.UnifyFieldsUtil;
import cn.util.core.IsKit;
import cn.util.core.TypeKit;
import cn.util.string.NamingKit;
import cn.util.string.StringKit;


public class BuildSQL
{
	/** 静态代码块获取静态注入变量值*/
	private final static boolean humpConversion = new SysConfig().isHumpConversion();
	private final static boolean isUnifyFields = new SysConfig().isUnifyFields();


	/**
	 * record：记录中的主键及对应的取值
	 * 返回：只保留表中存在的字段及对应的值
	 */
	private static Map<String, Object> tableFieldFilter(String tableName,
			Map<String, Object> mapRecord,Boolean genKey)
	{	
		if( IsKit.isNullOrEmpty(tableName) || IsKit.isNullOrEmpty(mapRecord)) { 
			return null;
		}
		//从数据库中获取表中所有字段
		String[] tableFields = DBMetaData.getColumnsName(tableName);
		if(IsKit.isNullOrEmpty(tableFields)) {
			return null;
		}
		if(humpConversion) {
			mapRecord = NamingKit.camelToUnderline(mapRecord);
		}		

		//获取用户提交记录里所有字段名的集合
		Object[] fields = (mapRecord.keySet()).toArray();
		//确保mapRecord只保留有效(表中的)的字段
		for (Object field : fields) {
			if (!IsKit.isInFields(tableFields, TypeKit.objToStr(field))) {
				//移除无效字段，查看记录中的字段在表中是否存在，如果不存在，则移除到
				mapRecord.remove(TypeKit.objToStr(field));
			}
		}
		//主键字段的处理
		String[] keys = DBMetaData.getKeys(tableName);
		for(String key:keys) 
		{
			//剔除所有主键字段
			if(genKey) {
				mapRecord.remove(key);
			}
			//剔除空值的主键字段
			else {
				Object keyValue = mapRecord.get(key);
				if(IsKit.isNullOrEmpty(keyValue)) {
					mapRecord.remove(key);
				}
			}
		}
		return mapRecord;
	}

	/**删除全部记录，构造对应的SQL语句*/
	public static String removeAll(String tableName)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		return new SQLDelete().fromTable(tableName).create();
	}
	/**用于组装对多个库(tableName逗号分割)进行删除的SQL语句. 删除条件一样:paramMap**/
	public static ArrayList<String> deletes(String tableName,Map<String,Object> paramMap)
	{
		if( IsKit.isNullOrEmpty(paramMap) || IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		String where = BuildWhere.genQueryWrapper(paramMap);
		if( IsKit.isNullOrEmpty(where)) { 
			return null;
		}
		ArrayList<String> sqlList = new ArrayList<>();
		String[] tableNames = tableName.split(",");
		for (String name : tableNames) {
			String sql = new SQLDelete().fromTable(name).where(where).create();
			sqlList.add(sql);
		}

		return sqlList;
	}
	/**单库：
	 * paramMap用于组装删除语句的条件where
	 * 默认：and 关系
	 * */
	public static String delete(String tableName,Map<String,Object> paramMap)
	{
		if( IsKit.isNullOrEmpty(paramMap) || IsKit.isNullOrEmpty(tableName)) { 
			return null;
		} 
		SQLCondition sqlWhere = new SQLCondition().andWhereAttribute(paramMap);
		if( IsKit.isNullOrEmpty(sqlWhere)) { 
			return null;
		}
		return new SQLDelete().fromTable(tableName).where(sqlWhere).create();
	}

	/**依据删除条件的字段field和对应的值values，构造对应的SQL语句*/
	public static String delete(String tableName,String field, Object values)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		return new SQLDelete()
				.fromTable(tableName)
				.where(new SQLCondition()
						.whereAttribute(field)
						.equalsTo(TypeKit.objToStr(values)))
				.create();
	}
	public static String delete(String tableName,String field,List<String> values)
	{
		if(IsKit.isNullOrEmpty(tableName) || IsKit.isNullOrEmpty(field) || IsKit.isNull(values)){ 
			return null;
		}
		return new SQLDelete()
				.fromTable(tableName)
				.where(new SQLCondition()
						.whereAttribute(field)
						.inSet(TypeKit.listToStr(values))
						).create();
	}
	public static String delete(String tableName,String field,String[] values) 
	{
		if(IsKit.isNullOrEmpty(values)){ 
			return null;
		}
		List<String> listStr = Arrays.asList(values);
		return delete(tableName,field,listStr);
	}
	/**用于组装一条delete语句*/
	public static String delete(String tableName,String ids)
	{
		if( IsKit.isNullOrEmpty(ids)) { 
			return null;
		}
		String where = BuildWhere.genQueryWrapper(tableName,ids);
		if( IsKit.isNullOrEmpty(where)) { 
			return null;
		}
		return new SQLDelete()
				.fromTable(tableName)
				.where(where)
				.create();
	}

	/*条件参数paramsMap: 
	 * {
	 *  "ipaddr":"*11*","userName":"*22*","status":"1",
	 *  "params":{"beginTime":"2022-01-05T16:00:00.000Z","endTime":"2022-02-13T16:00:00.000Z"}
	 * }
	 * 其中：params={}是区间取值参数比如时间；rangeField对应的表中字段，默认为beginTime和endTime
	 */
	public static String query(String tableName,Map<String, Object> paramMap,String rangeField)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		String item = genItem(tableName);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		String where = BuildWhere.genQueryWrapper(tableName,paramMap,rangeField);
		if(IsKit.isNotNullOrEmpty(where)) {
			query.where(where);	
		}
		return query.create();
	}
	public static String query(String tableName,String field, Object value)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		String item = genItem(tableName);
		return new SQLQuery().select(item).from(tableName)
				.where(new SQLCondition()
						.whereAttribute(field)
						.equalsTo(TypeKit.objToStr(value)))
				.create();
	}
	/**组装一次检索出若干条记录的SQL*/
	public static String query(String tableName,String field,Object[] values)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		String item = genItem(tableName);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		SQLCondition where = new SQLCondition();
		if(IsKit.isNull(values)) {
			for (Object value : values) {
				if (IsKit.isNull(where)) {
					where.whereAttribute(field)
							.equalsTo(TypeKit.objToStr(value));
				} else {
					where.orAttribute(field)
							.equalsTo(TypeKit.objToStr(value));
				}
			}
		}
		if(IsKit.isNotEmpty(where)) {
			query.where(where);
		}
		return query.create();
	}
	public static String query(String tableName)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		String item = genItem(tableName);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		return query.create();
	}

	public static String query(String tableName,Map<String, Object> whereMap)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		String item = genItem(tableName);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		String where = BuildWhere.genQueryWrapper(whereMap);
		if(IsKit.isNotEmpty(where)) {
			query.where(where);
		}
		return query.create();
	}	
	public static String queryOne(String tableName,Map<String, Object> whereMap)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		SQLQuery query = new SQLQuery().select("COUNT(1)").from(tableName);
		String where = BuildWhere.genQueryWrapper(whereMap);
		if(IsKit.isNotEmpty(where)) {
			query.where(where);	
		}
		query.limit("1");
		return query.create();
	}
	public static String getRecordNum(String tableName,Map<String, Object> mapRecord)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		SQLQuery query = new SQLQuery().select("COUNT(1)").from(tableName);
		String where = BuildWhere.genQueryWrapper(mapRecord);
		if(IsKit.isNotEmpty(where)) {
			query.where(where);	
		}
		return query.create();
	}

	public static String getList(String tableName,String item){
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		if(IsKit.isNullOrEmpty(item)) {
			item = genItem(tableName);
		}
		return new SQLQuery().select(item).from(tableName).create();
	}
	public static String queryByItem(String tableName,String item,Map<String, Object> paramMap,String rangeField)
	{
		if(IsKit.isNullOrEmpty(tableName)) {
			return null;
		}
		item = genItem(tableName,item);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		String where = BuildWhere.genQueryWrapper(tableName,paramMap,rangeField);
		if(IsKit.isNotNullOrEmpty(where)) {
			query.where(where);
		}
		return query.create();
	}
	public static String queryByItem(String tableName,String item,Map<String, Object> criteria)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		item = genItem(tableName,item);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		String where = BuildWhere.genQueryWrapper(criteria);
		if(IsKit.isNotNullOrEmpty(where)) {
			query.where(where);	
		}
		return query.create();
	}
	/**用于组装一条SELECT语句
	 * 查询条件criteria：格式："键|值,键|值"
	 */
	public static String queryByItem(String tableName,String item,String criteria)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		//查询条件
		String where = BuildWhere.genQueryWrapper(criteria); 
		item = genItem(tableName,item);
		SQLQuery query = new SQLQuery().select(item).from(tableName);

		if(IsKit.isNotNullOrEmpty(where)) {
			query.where(where);	
		}
		return query.create();
	}

	/**组装insert语句*/
	private static String handleInsert(String tableName,Map<String, Object> mapRecord)
	{
		if( IsKit.isNullOrEmpty(mapRecord)) { 
			return null;
		}
		if(isUnifyFields) {
			UnifyFieldsUtil.setCreateField(mapRecord);
		}
		mapRecord = NamingKit.camelToUnderline(mapRecord);
		
		//过滤后的有效字段
		Object[] fields = (mapRecord.keySet()).toArray();
		if(IsKit.isNullOrEmpty(fields)) {	
			return null;
		}
		String insert = new SQLInsertion()
				.intoTable(tableName)
				.intoColumns(StringKit.objArrayToStrArray(fields))
				.create();

		StringBuilder values = new StringBuilder();
		//对对应的取值进行处理
		for (Object field : fields) {
			String key = TypeKit.objToStr(field);
			if (IsKit.isNullOrEmpty(key)) {
				continue;
			}
			Field f = DBMetaData.getFields(tableName, key);//字段映射表
			if (IsKit.isNullOrEmpty(f)) {
				continue;
			}
			Object value = mapRecord.get(key);//获取给定字段的值（用户传入）
			String className = f.getJavaTypeName();//字段类型对应的Java类的名称
			value = SQLAssemblyUtil.objTypeMappingToSqlType(className, value);
			if (value instanceof Boolean) {

			} else {
				value = "'" + value + "'";
			}
			if (values.length() != 0) {
				values.append(",").append(value);
			} else {
				values.append(value);
			}
		}
		return insert +" VALUES (" + values + ") ";
	}

	/**用于组装一条insert语句 genKey:true系统自动产生主键*/
	public static String insert(String tableName,Map<String, Object> mapRecord,Boolean genKey)
	{
		mapRecord = tableFieldFilter(tableName,mapRecord,genKey);
		return handleInsert(tableName,mapRecord);
	}
	/**用于组装一条insert语句*/
	public static String insert(String tableName,Map<String, Object> mapRecord)
	{
		mapRecord = tableFieldFilter(tableName,mapRecord,false);
		return handleInsert(tableName,mapRecord);
	}
	/**组装一批高效Insert语句*/
	public static String BatchInsert(String tableName, List<Map<String, Object>> listRecord)
	{
		if(IsKit.isNullOrEmpty(tableName)|| IsKit.isNullOrEmpty(listRecord)){	
			return null;
		}
		/*获取数据库表的有关信息*/
		String[] tableFields = DBMetaData.getColumnsName(tableName);//获取表中所有字段
		if(IsKit.isNullOrEmpty(tableFields)) { 
			return null;
		}
		//过滤、分组依据
		Map<String, Object> mapRecord = new HashMap<>();
		mapRecord = listRecord.get(0);
		if(isUnifyFields) {
			UnifyFieldsUtil.setCreateField(mapRecord);
		}
		mapRecord = NamingKit.camelToUnderline(mapRecord);
		Object[] recordFields = (mapRecord.keySet()).toArray(); //获取记录里的字段名的集合
		if(IsKit.isNotNullOrEmpty(recordFields))
		{
			for (Object recordField : recordFields) {
				if (IsKit.isNullOrEmpty(recordField)) {
					continue;
				}
				if (!IsKit.isInFields(tableFields, recordField.toString())) {
					mapRecord.remove(recordField.toString());//移除无效字段， 查看记录中的字段在表中是否存在，如果不存在，则移除到
				}
			}
		}
		Object[] fields = (mapRecord.keySet()).toArray();//所有有效字段
		String sql_field = TypeKit.arryToStr(fields, ",");
		String sqlPre = "INSERT INTO " + tableName + " (" + sql_field + ") VALUES ";
		String sqlValue = "";
		if(IsKit.isNotNullOrEmpty(fields))
		{
			int size = listRecord.size();
			for(int index = 0 ; index < size; index++)//所有记录
			{
				Object values = "";
				mapRecord = listRecord.get(index);
				if(isUnifyFields) {
					UnifyFieldsUtil.setCreateField(mapRecord);
				}
				mapRecord = NamingKit.camelToUnderline(mapRecord);
								
				for (int i = 0; i < fields.length; i++)//对对应的取值进行处理 /一条记录
				{
					String key = fields[i].toString();
					if (IsKit.isNullOrEmpty(key)) {
						continue;
					}
					Field f = DBMetaData.getFields(tableName,key);//字段映射表
					String className = f.getJavaTypeName();//字段类型对应的Java类的名称
					Object value = mapRecord.get(key);//获取给定字段的值（用户传入）
					value = SQLAssemblyUtil.objTypeMappingToSqlType(className,value);
					if(value instanceof java.lang.Boolean){

					}
					else {
						value = "'" + value + "'";
					}
					if (!"".equals(values)) {
						values += "," + value;
					}
					else { 
						values = value;
					}
				}//for
				if (sqlValue.isEmpty()) { 
					sqlValue =  "(" + values + ")";
				}
				else {
					sqlValue += ",(" + values + ")";
				}
			}//for
		}
		return sqlPre + sqlValue ;//+ ";";
	}


	/**组装出来的语句为insert 或 insert ... ON DUPLICATE KEY UPDATE */
	public static String insertUpdate(String tableName, Map<String, Object> mapRecord,
			String incField,Integer incValue) throws SQLException
	{

		String[] tableKeys = DBMetaData.getKeys(tableName);//表中的主键
		if(IsKit.isNullOrEmpty(tableKeys)) { 
			return null;
		}
		String[] tableFields = DBMetaData.getColumnsName(tableName);//表中的字段
		//处理前端传过来的记录，获取记录中的key。 验证key是否在表中存在对应的字段。 
		Map<String, Object> _m = new LinkedHashMap<String, Object>(mapRecord);

		Object[] recordFields = (_m.keySet()).toArray();
		//移除mapRecord中的无效字段。 即查看记录中的字段在表中是否存在，如果不存在，则移除到
		for (int i = 0; i < recordFields.length; i++) {
			if (!IsKit.isInFields(tableFields, recordFields[i].toString())) {
				_m.remove(recordFields[i].toString());
			}
		}
		String increStr = "";
		//将需要自动增加1的字段从 _m删除
		if (incField != null && incField.length() > 0) 
		{ 
			if (incValue >= 0) {
				increStr = incField + "=" + incField + "+" +incValue;// +
			}
			else {
				increStr = incField + "=" + incField + incValue;//-
			}

			if(_m.containsKey(incField)) {
				_m.remove(incField);
			}
		}
		//过滤后的有效字段，即库表中都存在的字段,但不包含需要进行自增1操作的字段
		Object[] fields = (_m.keySet()).toArray();

		String update = "";
		if(IsKit.isNotNullOrEmpty(increStr)) {
			update = ","+ increStr + " ON DUPLICATE KEY UPDATE " + increStr;
		}

		String insert = "INSERT INTO " + tableName + " SET ";
		Object setClause = "";
		if(IsKit.isNotNullOrEmpty(fields))
		{
			int loop = fields.length;
			for (int i = 0; i < loop; i++)//对对应的取值进行处理 
			{
				String key = TypeKit.objToStr(fields[i]);
				if(IsKit.isNullOrEmpty(key)) {
					continue;
				}
				Field f = DBMetaData.getFields(tableName,key);//字段映射表
				String className = f.getJavaTypeName();//字段类型对应的Java类的名称
				Object value = mapRecord.get(key);//获取给定字段的值（用户传入）
				value = SQLAssemblyUtil.objTypeMappingToSqlType(className,value);
				if(value instanceof java.lang.Boolean){
				}
				else {
					value = "'" + value + "'";
				}

				if (!"".equals(setClause)) { 
					setClause += "," + key +"=" + value;
				}
				else { 
					setClause = key +"=" + value;
				}
			}
		}
		String sql = insert + setClause + update;
		return sql;
	}

	/** 利用页面传来的Map自动组装一条sql语句
	 *  isUpdateKey = false; 不更新主键
	 * */
	public static String update(String tableName, Map<String, Object> mapRecord)
	{	
		return update(tableName, mapRecord, false);
	}
	/** 利用页面传来的Map，自动组装一条sql语句
	 *  isUpdateKey = false; 不更新主键
	 * */
	public static String update(String tableName, Map<String, Object> mapRecord, boolean isUpdateKey)
	{	
		if( IsKit.isNullOrEmpty(tableName)||IsKit.isNullOrEmpty(mapRecord)){
			return null;
		}
		if(isUnifyFields) {
			UnifyFieldsUtil.setUpdateField(mapRecord);
		}

		mapRecord = NamingKit.camelToUnderline(mapRecord);
		/**获取数据库表的有关信息； 即表中的主键*/
		String[] tableKeys = DBMetaData.getKeys(tableName);
		if(IsKit.isNullOrEmpty(tableKeys)) { 	
			return null;
		}
		/**获取表中所有字段*/
		String[] tableFields = DBMetaData.getColumnsName(tableName);

		/**过滤、分组依据*/ 
		//获取记录里的原始的字段名的集合
		Object[] recordFields = (mapRecord.keySet()).toArray();
		//移除mapRecord中无效字段，
		for (int i = 0; i < recordFields.length; i++) {
			if (!IsKit.isInFields(tableFields, recordFields[i].toString())) {
				mapRecord.remove(recordFields[i].toString());
			}
		}
		/**过滤后的有效字段，即mapRecord中字段都是表中存在的字段*/
		Object[] k0 = (mapRecord.keySet()).toArray(); 

		//keyMap保存表中主键;从mapRecord中移除主键
		Map<String, Object> keyMap = new LinkedHashMap<String, Object>(); 
		for (int i = 0; i < k0.length; i++) {
			if (IsKit.isInFields(tableKeys,TypeKit.objToStr(k0[i]))) {//记录中是否有主键
				keyMap.put(TypeKit.objToStr(k0[i]), mapRecord.remove(TypeKit.objToStr(k0[i])));//将记录中的主键移到key_m中；保证不对主键更新
			}
		}
		//记录中包含的主键 id
		Object[] keys =	(keyMap.keySet()).toArray();
		//组装where语句 
		String where = BuildWhere.genWhereWrapper(keyMap,keys,"AND");

		/**需要对主键更新的处理*/
		if (isUpdateKey ){
			mapRecord.putAll(keyMap);
		}
		Object[] fields = (mapRecord.keySet()).toArray();//所有有效字段
		//组装Update子句	
		String updateCols = "";
		if(IsKit.isNotNullOrEmpty(fields))
		{
			int loop = fields.length;
			//对对应的取值进行处理
			for (int i = 0; i < loop; i++){
				String key = fields[i].toString();	
				//不对主键进行更新 且key是主键的字段。判断字段名key是否在字段名的数组中keys
				if ( !isUpdateKey && IsKit.isInFields(keys,key)){
					continue;
				}
				Object value = mapRecord.get(key);//获取给定字段的值（用户传入）
				Field f = DBMetaData.getFields(tableName,key);//字段映射表
				String className = f.getJavaTypeName();//字段类型对应的Java类的名称
				value = SQLAssemblyUtil.objTypeMappingToSqlType(className,value);
				if(IsKit.isNullOrEmpty(value)){
					continue;
				}
				//数字类型				
				if ( "java.lang.Integer".equals(className) 
						|| "java.math.BigInteger".equals(className)
						|| "java.math.BigDecimal".equals(className)
						|| "java.lang.Long".equals(className)
						|| "java.lang.Short".equals(className)
						|| "java.lang.Float".equals(className)
						|| "java.lang.Double".equals(className)
						|| "java.lang.Boolean".equals(className))
				{/**无需添加引号*/}
				//其他数据类型
				else {
					value = "'" + value + "'";
				}

				if("".equals(updateCols)) {
					updateCols = key + "=" + value;
				}
				else {
					updateCols += "," + key + "=" + value;
				}
			}
		}
		String sqlPre = "";
		if(IsKit.isNotNullOrEmpty(updateCols)){
			sqlPre = "UPDATE " + tableName + " SET "  + updateCols ; 
			if(IsKit.isNotNullOrEmpty(where)) {
				sqlPre = sqlPre + " WHERE "+ where;
			}
		}
		return sqlPre;
	}
	/**依据更新条件字段field和对应的值values(whereMap)，
	 * 以及要更新的字段和值setMap，构造对应的SQL语句*/
	public static String update(String tableName,
			Map<String, Object> setMap,Map<String, Object> whereMap)
	{
		if(IsKit.isNullOrEmpty(tableName) ||IsKit.isNullOrEmpty(setMap)) { 
			return null;
		}
		setMap = NamingKit.camelToUnderline(setMap);
		SQLUpdate update = new SQLUpdate()
				.updateTable(tableName)
				.setAttributeTo(setMap);
		String where = BuildWhere.genQueryWrapper(whereMap);
		if(IsKit.isNotNullOrEmpty(where)) {
			update.where(where);	
		}
		return update.create();
	}	

	public static String update(String tableName,Map<String, Object> mapRecord,String upId)
	{	
		return update(tableName,mapRecord,upId,"UP");
	}
	/**依据给定条件upId（Key|vvalue）组装满足条件的update语句。更新字段及值：mapRecord
	 * 支持多个字段直接赋值(upFlag=UP)或在原有值基础上进行累加赋值(upFlag!=UP)
	 * */
	public static String update(String tableName,Map<String, Object> mapRecord,String upId,String upFlag)
	{	
		if(IsKit.isNullOrEmpty(tableName)||IsKit.isNullOrEmpty(mapRecord)){	
			return null;
		}
		String where = null;
		if(!("*".equals(upId))){//所有记录
			where = BuildWhere.genQueryWrapper(tableName,upId);
		}
		//组装Update子句
		StringBuilder updateCols = new StringBuilder(); //不保证线程安全

		mapRecord = NamingKit.camelToUnderline(mapRecord);
		for (Map.Entry<String, Object> entry : mapRecord.entrySet()) {
			if(updateCols.length() > 0 )	{
				updateCols.append(",");
			}
			String fieldName = entry.getKey().trim();
			updateCols.append(fieldName);//字段名
			updateCols.append("=");
			if(IsKit.isNotNullOrEmpty(upFlag) && "UP".equalsIgnoreCase(upFlag))
			{ //直接用新值fieldValue进行赋值
				updateCols.append("'");
				updateCols.append(entry.getValue());
				updateCols.append("'");
			}
			else
			{  //原字段的值与fieldValue求和后赋值
				updateCols.append(fieldName);
				updateCols.append("+(");
				updateCols.append(entry.getValue());
				updateCols.append(")");
			}
		}
		String sqlPre = "";
		if(IsKit.isNotNullOrEmpty(updateCols.toString()))
		{
			sqlPre = "UPDATE " + tableName + " SET "  + updateCols.toString(); 
			if(IsKit.isNotNullOrEmpty(where)) {
				sqlPre = sqlPre + " WHERE "+ where;
			}
		}
		return sqlPre;
	}

	/** 返回一条sql语句
	 *  isUpdateKey = false; 不更新主键
	 *  whereStr是用户直接给定的不含where的查询条件Id。
	 *  id格式： key|value,
	 *  如果id为空, 
	 * */
	public static String update(String tableName, Map<String, Object> mapRecord,
			String id, boolean isUpdateKey)
	{	
		if( IsKit.isNullOrEmpty(tableName)||IsKit.isNullOrEmpty(mapRecord)){
			return null;
		}

		/**获取数据库表的有关信息*/
		String[] tableKeys = DBMetaData.getKeys(tableName);//表中的主键
		if(IsKit.isNullOrEmpty(tableKeys)) { 	
			return null;
		}
		//获取表中所有字段
		String[] tableFields = DBMetaData.getColumnsName(tableName);
		/**过滤、分组依据*/ 
		//获取记录里的字段名的集合
		Object[] recordFields = (mapRecord.keySet()).toArray(); 
		for (int i = 0; i < recordFields.length; i++) {
			if (!IsKit.isInFields(tableFields, recordFields[i].toString())) {
				//移除无效字段，查看记录中的字段在表中是否存在，如果不存在，则移除到
				mapRecord.remove(recordFields[i].toString());
			}
		}
		//过滤后的有效字段
		Object[] k0 = (mapRecord.keySet()).toArray(); 
		//记录里的主键
		Map<String, Object> keyMap = new LinkedHashMap<String, Object>(); 
		//不对主键进行更新
		if (!isUpdateKey){
			if(IsKit.isNullOrEmpty(k0)) {	
				return null;
			}
			int loop = k0.length;
			for (int i = 0; i <loop; i++) {
				//记录中是否有主键
				if (IsKit.isInFields(tableKeys, k0[i].toString())) {
					//将记录中的主键移到key_m中；保证不对主键更新
					keyMap.put(TypeKit.objToStr(k0[i]), mapRecord.remove(k0[i].toString()));
				}
			}
		}
		String where = "";
		//原来的	where =BuildWhere.genWhere4SQL(id);
		if(IsKit.isNullOrEmpty(id)) {
			//用表主键构造where子句
			where = BuildWhere.genQueryWrapper(tableName,keyMap);
		}
		else {
			//用户给定查询条件id构造where子句
			where = BuildWhere.genQueryWrapper(tableName,id);
		}		

		Object[] keys = (keyMap.keySet()).toArray(); //记录中包含的主键
		Object[] fields = (mapRecord.keySet()).toArray();//所有有效字段
		//组装Update子句	
		String updateCols = "";
		if(IsKit.isNullOrEmpty(fields))
		{
			int loop = fields.length;
			for (int i = 0; i < loop; i++)//对对应的取值进行处理 
			{
				String key = fields[i].toString();	
				//不对主键进行更新 且key是主键的字段
				if ( !isUpdateKey && IsKit.isInFields(keys,key)){
					continue;
				}

				Object value = mapRecord.get(key);//获取给定字段的值（用户传入）
				Field f = DBMetaData.getFields(tableName,key);//字段映射表
				String className = f.getJavaTypeName();//字段类型对应的Java类的名称
				value = SQLAssemblyUtil.objTypeMappingToSqlType(className,value);
				if(value != null) {
					if(value instanceof java.lang.Boolean) {
					}
					else {
						value = "'" + value + "'";
					}
				}
				if("".equals(updateCols)) {
					updateCols = key + "=" + value;
				}
				else {
					updateCols += "," + key + "=" + value;
				}
			}
		}
		String sqlPre = "";
		if(IsKit.isNotNullOrEmpty(updateCols))
		{
			sqlPre = "UPDATE " + tableName + " SET "  + updateCols ; 
			if(IsKit.isNotNullOrEmpty(where)) {
				sqlPre = sqlPre + " WHERE "+ where;
			}
		}
		return sqlPre;
	}
	/** 返回一条sql语句
	 * 对某些字段进行增值操作:
	 * */
	public static String update_Field(
			String tableName,
			String fieldNameValues,//更新字段：格式：fieldName|fieldValue,...
			String upFlag,//原值+fieldValue操作(标志为:PLUS);赋值fieldValue操作(标志为:UP)
			String upId) //更新记录的条件upId：格式：key|value
	{	
		if(IsKit.isNullOrEmpty(tableName) || IsKit.isNullOrEmpty(fieldNameValues)){	
			return null;
		}
		//原来的	where =BuildWhere.genWhere4SQL(id);
		String where = BuildWhere.genQueryWrapper(tableName,upId);

		//组装Update子句
		StringBuilder updateCols = new StringBuilder(); //不保证线程安全
		if (IsKit.isNotNullOrEmpty(fieldNameValues)){
			String fieldValue = "";
			String fieldName = ""; 
			//第一个分割条件
			String[] idArry = TypeKit.strToArray(fieldNameValues,",");
			if(IsKit.isNotNullOrEmpty(idArry)){
				int loop = idArry.length;
				//idarray中的元素为：字段名|值
				for(int i = 0; i<loop; i++)	{
					String[] keyvalue = TypeKit.strToArray(idArry[i],"|");
					if(IsKit.isNotNullOrEmpty(keyvalue) && (keyvalue.length % 2 == 0))	{
						fieldValue = keyvalue[1].trim();//取值
						if(IsKit.isNullOrEmpty(fieldValue)){
							continue;
						}
						else{
							if(updateCols.length() > 0 )
							{//多个条件之间按照给定操作符and 或 OR等进行
								updateCols.append(",");
							}
							fieldName = keyvalue[0].trim();
							updateCols.append(fieldName);//字段名
							updateCols.append("=");
							if(IsKit.isNotNullOrEmpty(upFlag) && "UP".equalsIgnoreCase(upFlag))
							{//直接用新值fieldValue进行赋值
								updateCols.append("'");
								updateCols.append(fieldValue);
								updateCols.append("'");
							}
							else 
							{//原字段的值与fieldValue求和后赋值
								updateCols.append(fieldName);
								updateCols.append("+(");
								updateCols.append(fieldValue);
								updateCols.append(")");
							}
						}
					}
				}//for
			}
		}
		String sqlPre = "";
		if(IsKit.isNotNullOrEmpty(updateCols.toString()))
		{
			sqlPre = "UPDATE " + tableName + " SET "  + updateCols.toString(); 
			if(IsKit.isNotNullOrEmpty(where)) {
				sqlPre = sqlPre + " WHERE "+ where;
			}
		}
		return sqlPre;
	}
	/** 返回一条sql语句
	 *  whereStr是用户直接给定的不含where的查询条件。 
	 * */
	public static String updateByWhere(String tableName,Map<String, Object> mapRecord, String whereStr)
	{	
		//保留有效字段	
		mapRecord = tableFieldFilter(tableName,mapRecord,false);
		if( IsKit.isNullOrEmpty(mapRecord)) { 
			return null;
		}
		Object[] fields = (mapRecord.keySet()).toArray();//所有有效字段 
		//组装Update子句	
		String updateCols = "";
		if(IsKit.isNotNullOrEmpty(fields)) 
		{
			int loop = fields.length;
			for (int i = 0; i < loop; i++)//对对应的取值进行处理 
			{
				String key = TypeKit.objToStr(fields[i]);	
				Field f = DBMetaData.getFields(tableName,key);//字段映射表
				Object value = mapRecord.get(key);//获取给定字段的值（用户传入）
				String className = f.getJavaTypeName();//字段类型对应的Java类的名称
				value = SQLAssemblyUtil.objTypeMappingToSqlType(className,value);
				if(IsKit.isNotNullOrEmpty(value)){// true不空
					if(value instanceof java.lang.Boolean) {
					}
					else {
						value = "'" + value + "'";
					}
				}
				if("".equals(updateCols)) {
					updateCols = key + "=" + value;
				}
				else {
					updateCols += "," + key + "=" + value;
				}
			}
		}
		String sqlPre = "";
		if(IsKit.isNotNullOrEmpty(updateCols))
		{
			sqlPre = "UPDATE " + tableName + " SET "  + updateCols ; 
			if(IsKit.isNotNullOrEmpty(whereStr)) {
				sqlPre = sqlPre + " WHERE "+ whereStr;
			}
		}
		return sqlPre;
	}


	/** 返回若干条SQL语句
	 *  依据用户给定条件和数据，组装update语句
	 * 	where子句的字段whereField不一定是主键
	 *  listRecord中各记录的whereField字段的取值不一定相同。即
	 *  对listRecord中各记录whereField字段具有不同的取值
	 * */
	public static ArrayList<String> batchUpdate(String tableName,List<Map<String, Object>> listRecord, String whereField)
	{	
		if( IsKit.isNullOrEmpty(tableName) || IsKit.isNullOrEmpty(listRecord)) {
			return null;
		}
		String[] tableFields = DBMetaData.getColumnsName(tableName);//获取表中所有字段

		/**获取一条记录，作为过滤、分组依据。*/
		Map<String, Object> temp = listRecord.get(0);
		//_m中包含是用户传过来的一条记录的所有字段和其对应的值
		Map<String, Object> _m = new LinkedHashMap<String, Object>(temp);
		//获取记录里的字段名的集合
		Object[] recordFields = (_m.keySet()).toArray();
		for (int i = 0; i < recordFields.length; i++) {
			if (!IsKit.isInFields(tableFields, recordFields[i].toString())) {
				//移除无效字段即移除库表中不存在的字段，查看记录中的字段在表中是否存在，如果不存在，则移除
				_m.remove(recordFields[i].toString());
			}
		}
		//移除where条件字段
		if(IsKit.isNotNull(whereField)) {
			if(IsKit.isNotNull(_m.get(whereField))) {
				_m.remove(whereField);
			}
		}
		//记录中不包含WHERE条件字段的有效字段
		Object[] fields = (_m.keySet()).toArray(); 

		ArrayList<String> arrayList = new ArrayList<String>();
		String sqlPre = "UPDATE " + tableName + " SET " ;
		for (Map<String, Object> record : listRecord)
		{   //处理所有记录
			//获取每条记录where语句的字段名及取值		
			Map<String, Object> key_m = new LinkedHashMap<String, Object>();//记录库表的主键
			if(IsKit.isNotNull(whereField)) {
				if(IsKit.isNotNull(record.get(whereField))) {
					key_m.put(whereField, record.get(whereField));
				}
			}
			String where = BuildWhere.genQueryWrapper(tableName,key_m);
			if(IsKit.isNotNullOrEmpty(where)) {
				where = " WHERE "+ where;
			}

			String updateCols = "";
			//fields保存的是记录中不包含主键的有效字段；
			if(IsKit.isNotNull(fields))
			{
				//处理每条记录
				for (int i = 0; i < fields.length; i++){
					//字段名称
					String fieldName = TypeKit.objToStr(fields[i]);
					Field f = DBMetaData.getFields(tableName, fieldName);
					String className = f.getJavaTypeName();
					Object v = record.get(fieldName);//获取对应字段所对应的具体值
					Object value = SQLAssemblyUtil.objTypeMappingToSqlType(className,v);
					if(value instanceof java.lang.Boolean){

					}
					else {
						value = "'" + value + "'";
					}
					if(!"".equals(updateCols)) {
						updateCols +=",";	
					}
					updateCols += fieldName + "="+value;
				} //处理每条记录 
			}
			//记录下一条完整的SQL语句			
			String sql = sqlPre + updateCols + where;
			arrayList.add(sql);
		}//所有记录的处理
		return arrayList;
	}

	/** @param whereStr 是操作条件，插入时不起作用，更新时， 若where==null||"".equals(where)，
	 *                  则自动根据库表主键字段的键值对组合where条件语句;从LinkedHashMap参数中获取的值
	 * 	根据isUpdateKey对有效字段继续分组：!true时，再得主键字段组Object[] keys和非主键字段组Object[] fields
	 */
	public static ArrayList<String> batchUpdate(String tableName,List<Map<String, Object>> listRecord,boolean isUpdateKey, String whereStr)
	{
		if( IsKit.isNullOrEmpty(tableName)|| IsKit.isNullOrEmpty(listRecord)) {
			return null;
		}
		String[] tableKeys = DBMetaData.getKeys(tableName);//获取表中的主键
		if(tableKeys == null) return null;

		String[] tableFields = DBMetaData.getColumnsName(tableName);//获取表中所有字段	
		/**获取一条记录，作为过滤、分组依据。*/
		Map<String, Object> temp = listRecord.get(0);
		//_m中包含是用户传过来的一条记录的所有字段和其对应的值
		Map<String, Object> _m = new LinkedHashMap<String, Object>(temp);
		//获取记录里的字段名的集合
		Object[] recordFields = (_m.keySet()).toArray();
		for (int i = 0; i < recordFields.length; i++) {
			if (!IsKit.isInFields(tableFields, recordFields[i].toString())) {
				//移除无效字段即移除库表中不存在的字段，查看记录中的字段在表中是否存在，如果不存在，则移除
				_m.remove(recordFields[i].toString());
			}
		}
		Object[] k0 = (_m.keySet()).toArray(); //过滤后的有效字段,一般来讲：对应表的字段

		//记录表中的主键		
		Map<String, Object> key_m = new LinkedHashMap<String, Object>();//记录库表的主键
		if (!isUpdateKey) {//不更新主键
			if(IsKit.isNotNullOrEmpty(k0)) {
				int loop = k0.length;
				for (int i = 0; i <loop ; i++) {
					if (IsKit.isInFields(tableKeys, k0[i].toString())) {//记录中是否有主键
						key_m.put(k0[i].toString(), _m.remove(k0[i].toString()));//将记录中的主键移到key_m中；保证不对主键更新
					}
				}
			}
		}
		Object[] fields = (_m.keySet()).toArray(); //记录中不包含主键的有效字段
		Object[] keys = (key_m.keySet()).toArray(); //记录中包含的主键

		//如果不更新主键，则key_m为空，即keys为空，而tableKeys不空，所以执行如下代码时，if语句总执行。XBM
		/*if (isUpdateKey) {//要更新主键，但主键非法，则返回
				if (keys.length == 0 || keys.length != tableKeys.length) {
					return arrayList;
				}
			}*/

		ArrayList<String> arrayList = new ArrayList<String>();
		String sqlPre = "UPDATE " + tableName + " SET " ;
		String whereCols = "";
		for (Map<String, Object> record : listRecord) {//处理所有记录
			//默认为AND关系
			if(IsKit.isNullOrEmpty(whereStr)) {//等于空或null
				if (keys.length > 0 && IsKit.isNullOrEmpty(whereCols)) {
					whereCols = " WHERE " + BuildWhere.genWhereWrapper(record,keys,"AND");
				}
			}
			else {
				whereCols = " WHERE " + whereStr;
			}

			String updateCols = "";
			//fields保存的是记录中不包含主键的有效字段；
			for (int i = 0; i < fields.length; i++){//处理每条记录 
				String fieldName = fields[i].toString();//字段名称
				Field f = DBMetaData.getFields(tableName, fieldName);

				String className = f.getJavaTypeName();
				Object v = record.get(fieldName);//获取对应字段所对应的具体值
				Object value = SQLAssemblyUtil.objTypeMappingToSqlType(className,v);
				if(value instanceof java.lang.Boolean){
				}
				else {
					value = "'" + value + "'";
				}
				if( ! "".equals(updateCols)) {
					updateCols +=",";	
				}
				updateCols += fieldName + "="+value;

			} //处理每条记录 
			//记录下一条完整的SQL语句
			String sql =  sqlPre + updateCols + whereCols;
			arrayList.add(sql);
		}//所有记录的处理
		return arrayList;
	}

	/**返回一组sql语句
	 * 原则 "存在更新，不存在插入"使用duplicate语句 
	 * 不提倡使用duplicate
	 */
	public static ArrayList<String> batchUpdateInsert(String tableName, List<Map<String, Object>> listRecord)
	{
		if( IsKit.isNullOrEmpty(listRecord) || IsKit.isNullOrEmpty(tableName)){
			return null;
		}
		/**获取数据库表的有关信息*/
		String[] tableFields = DBMetaData.getColumnsName(tableName);//表中的字段名的集合
		if(IsKit.isNullOrEmpty(tableFields)) {	
			return null;
		}
		//表中的主键字段集
		String[] tableKeys = DBMetaData.getKeys(tableName);
		if(IsKit.isNullOrEmpty(tableKeys)) {	
			return null;
		}
		ArrayList<String> result = new ArrayList<String>();

		/**获取一条记录，作为过滤、分组依据。*/
		Map<String, Object> _m = new LinkedHashMap<String, Object>(listRecord.get(0));
		Object[] recordFields = (_m.keySet()).toArray(); //获取记录里的字段名的集合
		for (int i = 0; i < recordFields.length; i++) {
			if (!IsKit.isInFields(tableFields, recordFields[i].toString())) {
				_m.remove(recordFields[i].toString());//移除无效字段， 查看记录中的字段在表中是否存在，如果不存在，则移除
			}
		}
		Object[] k0 = (_m.keySet()).toArray(); //过滤后的有效字段. 即在数据库表中存在的字段

		String insertSQL = "INSERT INTO " + tableName; 
		String updateSQL = "ON DUPLICATE KEY UPDATE ";
		boolean writeFlag = true;
		String inserCols = "";
		String updateCols = "";
		String value = "";
		String tempName = "";
		Object tempValue = "";
		int size = listRecord.size();
		for(int i=0;i<size;i++)	{
			writeFlag = true;
			inserCols = "";
			updateCols = "";
			value = "";

			Map<String,Object> map = listRecord.get(i);
			for (Map.Entry<String,Object> entry : map.entrySet()) {
				tempName = entry.getKey();
				tempValue = entry.getValue();
				//如果主键没有值，则不处理
				if (IsKit.isInFields(tableKeys, tempName)) {
					if(IsKit.isNullOrEmpty(tempValue))	{
						writeFlag = false;
						break;//第2个for 列名
					}
				}  
				//如果某字段在数据库表中不存在， 系统不处理
				if (! IsKit.isInFields(k0, tempName)) {
					continue;
				}

				Field f = DBMetaData.getFields(tableName,tempName);//字段映射表
				String className = f.getJavaTypeName();//字段类型对应的Java类的名称
				tempValue = SQLAssemblyUtil.objTypeMappingToSqlType(className,tempValue);
				//if(ObjectUtil.isNullOrEmpty(tempValue))
				//tempValue =(GlobalConst.NULL_CHAR);
				//组装Insert子句
				if (!"".equals(inserCols)) {
					inserCols += ",";
					value += ",";
				}
				inserCols += tempName;
				value += "'" + tempValue + "'";

				//组装Update子句
				if( ! "".equals(updateCols)) {
					updateCols += ",";	
				}
				updateCols += tempName + "='" + tempValue + "'";
			}
			if(writeFlag){
				// SQL语句尾部有 ";"，executeBatch语句报错
				String sql =  insertSQL + " (" + inserCols + ") VALUES (" + value + ") " + updateSQL + updateCols;
				result.add(sql);
			}
		}
		return result;
	}

	//批量删除  目前仅支持AND 
	public static ArrayList<String> batchDelete(String tableName,List<Map<String, Object>> listRecord,String whereStr){
		if(IsKit.isNullOrEmpty(tableName)|| IsKit.isNullOrEmpty(listRecord)) { 
			return null;
		}
		String[] tableFields = DBMetaData.getColumnsName(tableName);//获取表中所有字段
		if(IsKit.isNullOrEmpty(tableFields)) { 
			return null;
		}
		Map<String, Object> temp = listRecord.get(0);
		//获取一条记录，作为过滤、分组依据 _m中包含是用户传过来的一条记录的所有字段和其对应的值
		Map<String, Object> _m = new LinkedHashMap<String, Object>(temp);
		Object[] recordFields = (_m.keySet()).toArray(); //获取记录里的字段名的集合
		for (int i = 0; i < recordFields.length; i++) {
			if (!IsKit.isInFields(tableFields, recordFields[i].toString())) {
				_m.remove(recordFields[i].toString());//移除无效字段即移除库表中不存在的字段， 查看记录中的字段在表中是否存在，如果不存在，则移除到
			}
		}
		Object[] fields = (_m.keySet()).toArray();//所有有效字段 


		ArrayList<String> arrayList = new ArrayList<String>();
		//String sqlPre = "DELETE FROM " + tableName + " ";
		String sqlPre = "DELETE FROM " + tableName + " WHERE ";

		if(IsKit.isNullOrEmpty(whereStr))//等于空或null
		{
			//sqlPre += " WHERE ";
			String whereCols = "";
			for (Map<String, Object> record : listRecord) {//处理所有记录
				whereCols = "";
				for (int i = 0; i < fields.length; i++){//处理每条记录   fields保存的是记录中不包含主键的有效字段； 
					String fieldName = fields[i].toString();//字段名称
					Field f = DBMetaData.getFields(tableName, fieldName);

					String className = f.getJavaTypeName();
					Object v = record.get(fieldName);//获取对应字段所对应的具体值
					Object value = SQLAssemblyUtil.objTypeMappingToSqlType(className,v);
					if(value instanceof java.lang.Boolean){
					}
					else {
						value = "'" + value + "'";
					}
					//组装where子句
					if (! "".equals(whereCols)) {
						whereCols += " AND ";
					}
					whereCols += "(" + fieldName + "="+value + ")";
				} //处理每条记录 

				String sql =  sqlPre + whereCols;
				arrayList.add(sql);
			}
		}
		else{
			arrayList.add(sqlPre + whereStr);
		}
		//记录下一条完整的SQL语句
		return arrayList;
	}

	private static String genItem(String tableName)
	{
		return genItem(tableName,"");
	}
	private static String genItem(String tableName,String item)
	{
		//用户没有提供Item, 或提供*(用表的字段替换"*");
		if(IsKit.isNullOrEmpty(item) || CharsetKit.SYMBOL_START.equals(item))
		{
			//从数据库中获取表中所有字段
			List<String> fieldList = DBMetaData.getColumnsNameList(tableName);
			if(IsKit.isNullOrEmpty(fieldList)) { 
				return "*";
			}
			String[] tableFields = fieldList.toArray(new String[fieldList.size()]);

			if(IsKit.isNullOrEmpty(tableFields)) { 
				return "*";
			}
			item = TypeKit.arryToStr(tableFields, ",");
		}

		return item;
	}

	public static String queryByWhere(String tableName,String item,String rField,String where)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		if(IsKit.isNullOrEmpty(item)) {
			item = genItem(tableName);
		}
		item = item + ", COUNT(DISTINCT " + rField +")";
		if(IsKit.isNullOrEmpty(where)) {
			return new SQLQuery().select(item).from(tableName).groupBy(rField).create();
		}
		else {
			return new SQLQuery().select(item).from(tableName).where(where).groupBy(rField).create();
		}		
	}

	public static String query_or(String tableName,Map<String, Object> mapRecord)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return null;
		}
		String item = genItem(tableName);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		String where = BuildWhere.genQueryWrapper_OR(mapRecord);
		if(IsKit.isNotEmpty(where)) {
			query.where(where);
		}
		return query.create();
	}
	public static String sql_query(String tableName,String field,List<?> values)
	{
		if(IsKit.isNullOrEmpty(tableName)) { 
			return "";
		}
		String item = genItem(tableName);
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		if(IsKit.isNotNullOrEmpty(values))
		{
			SQLCondition where = new SQLCondition()
					.whereAttribute(field)
					.inSet(StringUtils.join(values.toArray(), ","));
			query.where(where);
		}
		return query.create();
	}




	/*将满足条件的某个字段fiel以","拼接字符串作为结果返回*/
	public static String getFieldByGroup(String tableName,String field,Map<String, Object> criteria)
	{
		if(IsKit.isNullOrEmpty(tableName)||IsKit.isNullOrEmpty(field)) { 
			return null;
		}
		//查询条件
		String where = BuildWhere.genQueryWrapper(criteria);
		String item = "GROUP_CONCAT("+ field + " SEPARATOR ',') AS "+ field ;
		SQLQuery query = new SQLQuery().select(item).from(tableName);
		if(IsKit.isNotNullOrEmpty(where)) {
			query.where(where);	
		}
		return query.create();
	}

}
