package com.sys.core.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.TableMapping;

/**
 * 用于生成JFinal的SQL查询语句<br>
 * 类名称：Conditions<br>
 * 创建人：yangxp<br>
 * 创建时间：2014-1-19 上午11:22:26<br>
 * 
 * @version v1.0.9
 * 
 */
public class Conditions {
    static Logger log = Logger.getLogger(Conditions.class);

    public static final String EQUAL = "EQUAL"; // 相等

    public static final String NOT_EQUAL = "NOT_EQUAL"; // 不相等

    public static final String LESS_THEN = "LESS_THEN"; // 小于

    public static final String LESS_EQUAL = "LESS_EQUAL"; // 小于等于

    public static final String GREATER_EQUAL = "GREATER_EQUAL"; // 大于等于

    public static final String GREATER_THEN = "GREATER_THEN"; // 大于

    public static final String FUZZY = "FUZZY"; // 模糊匹配 %xxx%

    public static final String FUZZY_LEFT = "FUZZY_LEFT"; // 左模糊 %xxx

    public static final String FUZZY_RIGHT = "FUZZY_RIGHT"; // 右模糊 xxx%

    public static final String NOT_EMPTY = "NOT_EMPTY"; // 不为空值的情况

    public static final String EMPTY = "EMPTY"; // 空值的情况

    public static final String IN = "IN"; // 在范围内

    public static final String NOT_IN = "NOT_IN"; // 不在范围内

    // 用于接收SQL语句
    private ThreadLocal<String> sql = new ThreadLocal<String>();

    private String tableName = "";
    // 用于接收参数数组
    private ThreadLocal<ArrayList<Object>> paramList = new ThreadLocal<ArrayList<Object>>();

    // 用于存放设置的条件
    private ThreadLocal<Map<String, Object[]>> conditionMap = new ThreadLocal<Map<String, Object[]>>();

    // 用于存放需要排除的字段
    private ThreadLocal<Map<String, String>> excludeFieldMap = new ThreadLocal<Map<String, String>>();

    // 构造方法(表示没有设置查询类型的字段全部按照等于来处理)
    public Conditions() {
        conditionMap.set(new HashMap<String, Object[]>());
        excludeFieldMap.set(new HashMap<String, String>());
    }

    // 构造方法(设置后表示字段所有的查询方式按照设置类型来处理，除非后面针对字段的重新设置)
    public Conditions(String type) {
        Map<String, Object[]> map = new HashMap<String, Object[]>();
        map.put("GLOBALTYPE", new String[] { type });
        conditionMap.set(map);
        excludeFieldMap.set(new HashMap<String, String>());
    }

    /***************************************************************************
     * 设置字段的查询类型
     * 
     * @param QueryType
     *            查询类型
     * @param fieldName
     *            字段名称数组
     */
    public void setFiledQuery(String QueryType, String... filedName) {
        if (!StringUtils.isEmpty (QueryType) && !Conditions.isNullOrEmpty(filedName)) {
            Map<String, Object[]> map = conditionMap.get();
            String[] str = new String[filedName.length];
            for(int i =0;i<filedName.length;i++){
            	str[i] = filedName[i].toLowerCase();
            }
            map.put(QueryType, str);
            conditionMap.set(map);
        }
    }

    /***************************************************************************
     * 设置需要排除的字段
     * 
     * setexcludeField<br>
     * 
     * @param 参数说明
     * @return 返回对象
     * @Exception 异常对象
     * 
     */
    public void setExcludeField(String... filedName) {
        if (!Conditions.isNullOrEmpty(filedName)) {
            Map<String, String> map = excludeFieldMap.get();
            for (String str : filedName) {
                map.put(str, str);
            }
            excludeFieldMap.set(map);
        }
    }

    /***************************************************************************
     * 查询空值或者不为空值的情况 setNullFieldQuery
     * 
     * @param 参数说明
     * @return 返回对象
     * @Exception 异常对象
     */
    public void setNullOrNotNullFieldQuery(String QueryType, String... filedName) {
        if (!StringUtils.isEmpty(QueryType) && !Conditions.isNullOrEmpty(filedName)) {
            if (!NOT_EMPTY.equals(QueryType) && !EMPTY.equals(QueryType)) {
                log.error("空值或者非空查询的类型只能为：EMPTY、NOT_EMPTY");
                throw new RuntimeException("空值或者非空查询的类型只能为：EMPTY、NOT_EMPTY");
            }
            Map<String, Object[]> map = conditionMap.get();
            map.put(QueryType, filedName);
            conditionMap.set(map);
        }
    }

    /***************************************************************************
     * <b>传值查询</b><br>
     * 注：如果QueryType为<b>in</b>或者<b>not in</b>那么filedValue必须为一个list对象
     * 
     * @param QueryType
     *            查询类型
     * @param fieldName
     *            字段名称
     * @param filedValue
     *            字段值
     */
    public void setValueQuery(String QueryType, String fieldName, Object filedValue) {
        if (!StringUtils.isEmpty(QueryType) && !StringUtils.isEmpty(fieldName) && !Conditions.isNullOrEmpty(filedValue)) {
            Object[] param = new Object[2];
            param[0] = fieldName; // 字段名
            param[1] = filedValue;// 字段值
            Map<String, Object[]> map = conditionMap.get();
            map.put(QueryType + "#" + fieldName, param);// 避免类型重复被覆盖掉就加上字段名
            conditionMap.set(map);
        }
    }

    /***************************************************************************
     * 用于生成SQL条件语句不带别名
     * 
     * @param modelClass
     *            必须继承于Model
     */
    public void modelToCondition(Model<?> modelClass) {
    	 modelToCondition(modelClass, null);
    }

    /***************************************************************************
     * 用于生成SQL条件语句不带别名
     * 
     * @param RecordClass
     *            必须是一个Record类
     */
    public void recordToCondition(Record recordClass) {
        recordToCondition(recordClass, null);
    }

    /***************************************************************************
     * 用于生成SQL条件语句带别名
     * 
     * @param modelClass
     *            必须继承于Model
     * @param alias
     *            别名
     */
    public void modelToCondition(Model<?> modelClass, String alias) {
        alias = !StringUtils.isEmpty(alias) ? alias + "." : "";
        Class c = modelClass.getClass();
        tableName = alias+TableMapping.me().getTable(c.getName().indexOf("$$EnhancerBy") == -1 ? c : c.getSuperclass()).getName();
       
        if (modelClass != null) {
            // 所有的字段
            String[] fieldNames = modelClass._getAttrNames();
            // 字段名和值的map集合
            Map<String, Object> valueMap = Conditions.modelToMap(modelClass);

            // 构建查询条件
            buildCondition(alias, fieldNames, valueMap);
        } else {
            if (!conditionMap.get().isEmpty()) {
                buildCondition(alias, new String[] {}, new HashMap<String, Object>());
            } else {
                sql.set("");
                paramList.set(new ArrayList<Object>());
            }
        }
    }

    /***************************************************************************
     * 用于生成SQL条件语句不带别名
     * 
     * @param RecordClass
     *            必须是一个Record类
     * @param alias
     *            别名
     */
    public void recordToCondition(Record recordClass, String alias) {
        // 别名
        alias = !StringUtils.isEmpty(alias) ? alias + "." : "";
        if (recordClass != null) {
            // 所有的字段
            String[] fieldNames = recordClass.getColumnNames();
            // 字段名和值的map集合
            Map<String, Object> valueMap = Conditions.recordToMap(recordClass);

            // 构建查询条件
            buildCondition(alias, fieldNames, valueMap);
        } else {
            if (!conditionMap.get().isEmpty()) {
                buildCondition(alias, new String[] {}, new HashMap<String, Object>());
            } else {
                sql.set("");
                paramList.set(new ArrayList<Object>());
            }
        }
    }

    /***************************************************************************
     * 构建条件语句
     * 
     * @param resultMap
     *            用于返回结果的map
     * @param alias
     *            别名
     * @param fieldNames
     *            所有查询的字段名称
     * @param valueMap
     *            所有的值的map
     */
    private void buildCondition(String alias, String[] fieldNames, Map<String, Object> valueMap) {
        try {
            // 构建条件前先清空变量
            sql.set("");
            paramList.set(new ArrayList<Object>());
            // 用于存放参数列表
            ArrayList<Object> paramArrayList = new ArrayList<Object>();
            StringBuilder sb = new StringBuilder();
            // 所有的字段名称
            Map<String, String> usedFieldMap = new HashMap<String, String>();
            if (!conditionMap.get().isEmpty()) {
                for (Entry<String, Object[]> map : conditionMap.get().entrySet()) {
                    String queryType = map.getKey();
                    Object[] array = map.getValue();
                    if (queryType.indexOf("#") > 0) {// 传值查询
                        String fieldQueryType = queryType.split("#")[0];
                        String fieldName = array[0] != null ? array[0].toString() : "";
                        Object fieldValue = array[1];

                        // 将设置过的字段保存到数组中
                        usedFieldMap.put(fieldName, fieldName);
                        // 构建SQL语句
                        buildSQL(sb, fieldQueryType, fieldName, fieldValue, alias, paramArrayList);
                    } else {// 字段查询
                        if (!"GLOBALTYPE".equals(queryType)) {
                            for (Object field : array) {
                                String filedName = field != null ? field.toString() : "";
                                if (!excludeFieldMap.get().containsKey(filedName)) {
                                    Object fieldValue = valueMap.get(filedName);
                                    // 将设置过的字段保存到数组中
                                    usedFieldMap.put(filedName, filedName);
                                    // 构建查询语句
                                    buildSQL(sb, queryType, filedName, fieldValue, alias, paramArrayList);
                                }
                            }
                        }
                    }
                }
            }
            // 对没有设置条件的字段进行查询类型设置
            String queryType = EQUAL;
            if (conditionMap.get().containsKey("GLOBALTYPE")) {
                String[] typeArray = (String[]) conditionMap.get().get("GLOBALTYPE");
                queryType = typeArray[0];
            }
            // 对未使用过的字段进行build
            for (String field : fieldNames) {
                if (!usedFieldMap.containsKey(field)) {
                    Object fieldValue = valueMap.get(field);
                    // 构建查询语句
                    buildSQL(sb, queryType, field, fieldValue, alias, paramArrayList);
                }
            }

            // 合并传入的参数到参数对象中
            sql.set(sb.toString());
            paramList.set(paramArrayList);
            conditionMap.set(new HashMap<String, Object[]>());// 清空本次的条件map
            excludeFieldMap.set(new HashMap<String, String>());// 清空本次的排除字段
        } catch (Exception e) {
            log.error("Conditions构建SQL语句出现错误,请仔细检查!",e);
            e.printStackTrace();
        }
    }

    /***************************************************************************
     * 构建SQL语句
     * 
     * @param sb
     *            用于拼接SQL语句
     * @param queryType
     *            查询类型
     * @param fieldName
     *            字段名称
     * @param fieldValue
     *            字段值
     * @param alias
     *            别名
     * @return
     */
    @SuppressWarnings("unchecked")
    private void buildSQL(StringBuilder sb, String queryType, String fieldName, Object fieldValue, String alias, ArrayList<Object> params) {
        // 非空的时候进行设置
        if (!Conditions.isNullOrEmpty(fieldValue) && !Conditions.isNullOrEmpty(fieldName)) {
            if (EQUAL.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " = ? ");
                params.add(fieldValue);
            } else if (NOT_EQUAL.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " <> ? ");
                params.add(fieldValue);
            } else if (LESS_THEN.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " < ? ");
                params.add(fieldValue);
            } else if (LESS_EQUAL.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " <= ? ");
                params.add(fieldValue);
            } else if (GREATER_THEN.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " > ? ");
                params.add(fieldValue);
            } else if (GREATER_EQUAL.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " >= ? ");
                params.add(fieldValue);
            } else if (FUZZY.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " like ? ");
                params.add("%" + fieldValue + "%");
            } else if (FUZZY_LEFT.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " like ? ");
                params.add("%" + fieldValue);
            } else if (FUZZY_RIGHT.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " like ? ");
                params.add(fieldValue + "%");
            } else if (IN.equals(queryType)) {
                try {
                    List list = (List) fieldValue;
                    StringBuffer instr = new StringBuffer();
                    sb.append(" and " + alias + fieldName + " in (");
                    for (Object obj : list) {
                        instr.append(!StringUtils.isEmpty(instr) ? ",?" : "?");
                        params.add(obj);
                    }
                    sb.append(instr + ") ");
                } catch (Exception e) {
                    throw new RuntimeException("使用IN条件的时候传入的值必须是个List对象，否则将会转换出错!例如将 in('1','2','3')中的'1','2','3'分为三个分别添加到List中做为值传入.",e);
                }
            } else if (NOT_IN.equals(queryType)) {
                try {
                    List list = (List) fieldValue;
                    StringBuffer instr = new StringBuffer();
                    sb.append(" and " + alias + fieldName + " not in (");
                    for (Object obj : list) {
                        instr.append(!StringUtils.isEmpty(instr) ? ",?" : "?");
                        params.add(obj);
                    }
                    sb.append(instr + ") ");
                } catch (Exception e) {
                    throw new RuntimeException("使用NOT IN条件的时候传入的值必须是个List对象，否则将会转换出错!例如将 not in('1','2','3')中的'1','2','3'分为三个分别添加到List中做为值传入.",e);
                }
            }
        } else {
            if (EMPTY.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " is null ");
            } else if (NOT_EMPTY.equals(queryType)) {
                sb.append(" and " + alias + fieldName + " is not null ");
            }
        }
    }

    public String getSql() {
        return sql.get();
    }
    
    public String getFullSql() {
    	  return "select * from  "+tableName+" where 1=1 "+sql.get();
    }
    
    public List<Object> getParamList() {
        return paramList.get();
    }
    
    public String getTableName() {
        return tableName;
    }
    
    public  SqlPara getSqlPara(){

		SqlPara sp = new SqlPara();
		sp.setSql(this.getFullSql());
		for(Object o:this.getParamList()){
			sp.addPara(o);
		}
		return sp;
	}
    
    public static Map<String, Object> modelToMap(Model<?> model) {
        Map<String, Object> map = new HashMap<String, Object>();
        String[] names = model._getAttrNames();
        for (String str : names) {
            map.put(str, model.get(str));
        }
        return map;
    }
    public static Map<String, Object> recordToMap(Record record) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (record != null) {
            String[] columns = record.getColumnNames();
            for (String col : columns) {
                map.put(col, record.get(col));
            }
        }
        return map;
    }
    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj instanceof String && (obj.equals(""))) {
            return true;
        } else if (obj instanceof Short && ((Short) obj).shortValue() == 0) {
            return true;
        } else if (obj instanceof Integer && ((Integer) obj).intValue() == 0) {
            return true;
        } else if (obj instanceof Double && ((Double) obj).doubleValue() == 0) {
            return true;
        } else if (obj instanceof Float && ((Float) obj).floatValue() == 0) {
            return true;
        } else if (obj instanceof Long && ((Long) obj).longValue() == 0) {
            return true;
        } else if (obj instanceof Boolean && !((Boolean) obj)) {
            return true;
        } else if (obj instanceof Collection && ((Collection) obj).isEmpty()) {
            return true;
        } else if (obj instanceof Map && ((Map) obj).isEmpty()) {
            return true;
        } else if (obj instanceof Object[] && ((Object[]) obj).length == 0) {
            return true;
        }
        return false;
    }
}