package com.steering.comm.converter;

/**
 * CoreConverter.java 平台公共属性格式化处理类
 * @author siy 2015/04/21
 * @version v1.0
 * @modify
 */
public class CoreConverter {
    /**
     * 生成SQL where条件字符串 "and condition in('args[1],args[2],...')"  AND  IN 
     * @param column 条件列
     * @param args 条件参数
     * @return String sql条件字符串
     */
    public static String toSqlConditionString(String column, String[] args) {
        StringBuffer str = new StringBuffer();
        if (args != null && args.length > 0) {
            str.append(" AND ");
            str.append(column.toUpperCase());
            str.append(" IN (");
            for (String arg : args) {
                if (arg != null) {
                    str.append("'");
                    str.append(arg);
                    str.append("', ");
                }
            }
            str.deleteCharAt(str.lastIndexOf(","));
            str.append(")");
        }
        return str.toString();
    }


    public static String toSqlConditionString1000(String column, String[] args) {
        StringBuffer str = new StringBuffer();
        if (args != null && args.length > 0) {
            str.append(column.toUpperCase());
            str.append(" IN (");
            for (int i = 0; i < args.length; i++) {
                 if(args[i] != null) {
                     if(i % 999 == 0 && i > 0){
                         str.deleteCharAt(str.lastIndexOf(","));
                         str.append(") OR ").append(column.toUpperCase()+" IN (");
                     }
                    str.append("'");
                    str.append(args[i]);
                     str.append("', ");
                }
            }
            str.deleteCharAt(str.lastIndexOf(","));
            str.append(")");
        }
        return str.toString();
    }


    public static String toSqlConditionString1(String column, String[] args) {
        StringBuffer str = new StringBuffer();
        if (args != null && args.length > 0) {
            str.append(column.toUpperCase());
            str.append(" IN (");
            for (String arg : args) {
                if (arg != null) {
                    str.append("'");
                    str.append(arg);
                    str.append("', ");
                }
            }
            str.deleteCharAt(str.lastIndexOf(","));
            str.append(")");
        }
        return str.toString();
    }

    public static String toSqlConditionStringOr(String column, String[] args) {
        StringBuffer str = new StringBuffer();
        if (args != null && args.length > 0) {
            str.append(" AND (");
            //str.append(column.toUpperCase());
            //str.append(" OR "+column+" = ");
            
            for (int i = 0;i<args.length;i++) {
            	
            	if(i==0)
            	{
            		str.append(""+column+" = '"+args[i]+"'");
            	}else
            	{
            		str.append(" OR "+column+" = '"+args[i]+"'");
            	}
                
            }
            str.append(")");
        }
        return str.toString();
    }

    public static String toSqlConditionStringOrLike(String column, String[] args) {
        StringBuffer str = new StringBuffer();
        if (args != null && args.length > 0) {
            str.append(" AND (");
            //str.append(column.toUpperCase());
            //str.append(" OR "+column+" = ");

            for (int i = 0;i<args.length;i++) {

                if(i==0)
                {
                    str.append(""+column+" LIKE '%"+args[i]+"%'");
                }else
                {
                    str.append(" OR "+column+" LIKE '%"+args[i]+"%'");
                }

            }
            str.append(")");
        }
        return str.toString();
    }
    
    /**
     * 生成Where条件 AND  NOT IN
     * @param column  列名
     * @param args 参数
     * @return  SQL语句
     */
    public static String nOtoSqlConditionString(String column, String[] args) {
        StringBuffer str = new StringBuffer();
        if (args != null && args.length > 0) {
            str.append(" AND ");
            str.append(column.toUpperCase());
            str.append(" NOT IN(");
            for (String arg : args) {
                if (arg != null) {
                    str.append("'");
                    str.append(arg);
                    str.append("',");
                }
            }
            str.deleteCharAt(str.lastIndexOf(","));
            str.append(")");
        }
        return str.toString();
    }

    // public static void main(String[] args){
    // System.out.println(CoreConverter.toSqlConditionString("t1.name", new
    // String[]{"1","2","3","4","5"}));
    // }
}
