package com.steering.comm.tools;

import java.util.regex.Matcher;

import CoreFS.SA08.CoreXmlSqlParsersFactory;

/**
 * SQL构造器
 * @author Chenxi
 *
 */
public class SqlBuilder {
	private String sql = "";
	
	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public static final int Empty = 1;
	public static final int IsNull = 2;
	
	/**
	 * 构造函数
	 * @param sqlId SQL语句ID
	 */
	public SqlBuilder(String sqlId){
		sql = CoreXmlSqlParsersFactory.getSql(sqlId);
	}
	
	public String GetSql(){
		return sql;
	}
	
	public String toString(){
		return sql;
	}
	
	/**
	 * 为井号参数填充值
	 * @param parm 井号参数
	 * @param value 值
	 */
	public void Fill(String parm, String value){
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		parm = "#" + parm + "#";
		sql = sql.replaceAll(parm, value);
	}
	
	
	public void Replace(String parm, String value){
		parm = "#" + parm + "#";
		sql = sql.replaceAll(parm, value);
	}
	
	/**
	 * 构造查询条件（等号）
	 * @param conditionfield 条件（字段）
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void Build(String conditonField, String value, int sqlBuilderType){
		String field = conditonField;
		String conditon = "#" + conditonField + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, field +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, field +" = '" + value + "'");
    	}
	}
	
	/**
	 * 构造查询条件（等号）
	 * @param conditon 条件
	 * @param field 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void Build(String conditon, String field, String value, int sqlBuilderType){
		conditon = "#" + conditon + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, field +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, field +" = '" + value + "'");
    	}
	}
	
	/**
	 * 构造查询条件（Like）
	 * @param conditonField 条件（字段）
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildLike(String conditonField, String value, int sqlBuilderType){
		String field = conditonField;
		String conditon = "#" + conditonField + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, field +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon,"LOWER(" + field +") LIKE LOWER('%" + value + "%')");
    	}
	}
	
	/**
	 * 构造查询条件（Like）
	 * @param conditon 条件
	 * @param fieldName 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildLike(String conditon, String fieldName, String value, int sqlBuilderType){
		conditon = "#" + conditon + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, fieldName +" IS NULL");
					break;
			}
		}else{
			sql = sql.replaceAll(conditon,"LOWER(" + fieldName +") LIKE LOWER('%" + value + "%')");
    	}
	}
	
	/**
	 * 构造查询条件（大于）
	 * @param conditonField 条件(字段)
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildGreat(String conditonField, String value, int sqlBuilderType){
		String field = conditonField;
		String conditon = "#" + conditonField + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, field +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, field + " > " + value);
    	}
	}
	
	/**
	 * 构造查询条件（大于）
	 * @param conditon 条件
	 * @param fieldName 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildGreat(String conditon, String fieldName, String value, int sqlBuilderType){
		conditon = "#" + conditon + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, fieldName +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, fieldName + " > " + value);
    	}
	}
	
	/**
	 * 构造查询条件（大于等于）
	 * @param conditonField 条件(字段)
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildGreatEqual(String conditonField, String value, int sqlBuilderType){
		String field = conditonField;
		String conditon = "#" + conditonField + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, field +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, field + " >= " + value);
    	}
	}
	
	/**
	 * 构造查询条件（大于等于）
	 * @param conditon 条件
	 * @param fieldName 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildGreatEqual(String conditon, String fieldName, String value, int sqlBuilderType){
		conditon = "#" + conditon + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, fieldName +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, fieldName + " >= " + value);
    	}
	}
	
	/**
	 * 构造查询条件（小于）
	 * @param conditonField 条件 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildLess(String conditonField, String value, int sqlBuilderType){
		String field = conditonField;
		String conditon = "#" + conditonField + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, field +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, field + " < " + value);
    	}
	}
	
	/**
	 * 构造查询条件（小于）
	 * @param conditon 条件
	 * @param fieldName 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildLess(String conditon, String fieldName, String value, int sqlBuilderType){
		conditon = "#" + conditon + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, fieldName +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, fieldName + " < " + value);
    	}
	}
	
	/**
	 * 构造查询条件（小于等于）
	 * @param conditonField 条件 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildLessEqual(String conditonField, String value, int sqlBuilderType){
		String field = conditonField;
		String conditon = "#" + conditonField + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, field +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, field + " <= " + value);
    	}
	}
	
	/**
	 * 构造查询条件（小于等于）
	 * @param conditon 条件
	 * @param fieldName 字段
	 * @param value 值
	 * @param sqlBuilderType 构造类型
	 */
	public void BuildLessEqual(String conditon, String fieldName, String value, int sqlBuilderType){
		conditon = "#" + conditon + "#";
		value = value.replaceAll("'", "''");
		value = value.replaceAll("&", "' || '&' || '");
		value = Matcher.quoteReplacement(value);
		if(value.equals("")){
			switch(sqlBuilderType)
			{
				case Empty :
			    		sql = sql.replaceAll(conditon, "1 = 1");
					break;
				case IsNull:
			    		sql = sql.replaceAll(conditon, fieldName +" IS NULL");
					break;
			}
		}else{
    		sql = sql.replaceAll(conditon, fieldName + " <= " + value);
    	}
	}
	
	/**
	 * 追加查询条件
	 * @param sql 查询年条件
	 */
	public SqlBuilder Append(String sql){
		this.sql += sql;
		return this;
	}
	
	/**
	 * 追加查询条件
	 * @param sql 查询条件
	 * @param condition 追加条件
	 */
	public SqlBuilder Append(String sql, boolean condition){
		if(condition){
			this.sql += sql;
		}
		return this;
	}
	
	/**
	 * 追加查询条件
	 * @param sql 查询年条件
	 */
	public SqlBuilder AppendLn(String sql){
		this.sql += "\r\n" + sql;
		return this;
	}
	
	/**
	 * 追加查询条件
	 * @param sql 查询条件
	 * @param condition 追加条件
	 */
	public SqlBuilder AppendLn(String sql, boolean condition){
		if(condition){
			this.sql += "\r\n" + sql;
		}
		return this;
	}
	
	/**
	 * 填充参数 （参数替代符： {0}, {1}, {2} ....）
	 * @param parms 参数
	 */
	public SqlBuilder Fill(String[] parms){
		for(int i = 0; i < parms.length; i++){
			String parm = parms[i];
			parm = parm.replaceAll("'", "''");
			parm = parm.replaceAll("&", "' || '&' || '");
			parm = "'" + parm + "'";
			//parm = Matcher.quoteReplacement(parm);
			
			this.sql = sql.replace("{" + i + "}", parm);
		}
		
		return this;
	}
	
	public SqlBuilder Fill(Object[] parms){
		for(int i = 0; i < parms.length; i++){
			Object obj = parms[i];
			if(obj instanceof String[]){
				String[] strs = (String[])obj;
				StringBuilder sb = new StringBuilder();
				sb.append("('");
				for(String str : strs){
					str = str.replaceAll("'", "''");
					str = str.replaceAll("&", "' || '&' || '");
					if(sb.length() == 2){
						sb.append(str);
					}else{
						sb.append("','");
						sb.append(str);
					}
				}
				sb.append("')");
				this.sql = sql.replace("{" + i + "}", sb.toString());
			}else{
				String parm = obj.toString();
				parm = parm.replaceAll("'", "''");
				parm = parm.replaceAll("&", "' || '&' || '");
				parm = "'" + parm + "'";
				this.sql = sql.replace("{" + i + "}", parm);
			}
		}
		
		return this;
	}
}
