package com.boman.common.core.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

import static com.boman.common.core.utils.obj.ObjectUtils.escapeStr;
import static com.boman.common.core.utils.obj.ObjectUtils.isEmpty;
import static com.boman.domain.constant.FormDataConstant.*;

public class SqlProviderUtils {

    /**
     * 功能描述: 判断传过来的值是否需要转义
     *
     * @param packCondition 封装的条件
     * @param wholeSql      sql
     */
    public static void packCondition(JSONObject packCondition, StringBuilder wholeSql) {
        if (isEmpty(packCondition)) {
            return;
        }

        wholeSql.append(" where ");
        StringBuilder conditionSql = new StringBuilder();
        for (Map.Entry<String, Object> entry : packCondition.entrySet()) {
            String key = entry.getKey();
            Object valueObj = entry.getValue();
            List<String> types = ((List<String>) valueObj);
            /** {@link com.boman.web.core.utils.ColumnUtils.packColCondition} 这里是拼参数的地方 **/
            Object value = types.get(0);
            String queryType = types.get(1);
            String columnType = types.get(2);
            conditionSql.append(key).append(covert(queryType, columnType, key, value)).append(" and ");
        }

        wholeSql.append(StringUtils.substringBeforeLast(conditionSql.toString(), " and"));
    }

    /**
     * 功能描述: 仅此用来拼装查询条件，不做他用
     *
     * @param queryType  like > < =
     * @param columnType varchar char textarea timestamp
     * @param key        key
     * @param valueObj   valueObj
     * @return java.lang.String
     */
    public static String covert(String queryType, String columnType, String key, Object valueObj) {
        // false 不需要转义
        boolean needEscape = columnType.contains(VARCHAR) || columnType.contains(CHAR) || columnType.contains(DATETIME) || columnType.contains(TIMESTAMP);
        Object value;
        switch (queryType) {
            case EQ:
                value = needEscape ? escapeStr(String.valueOf(valueObj)) : valueObj;
                return " = " + value;
            case LIKE:
                return " like concat('%', #{condition." + key + "}, '%')";
            case NE:
                value = needEscape ? escapeStr(String.valueOf(valueObj)) : valueObj;
                return " != " + value;
            case GT:
                value = needEscape ? escapeStr(String.valueOf(valueObj)) : valueObj;
                return " > " + value;
            case GTE:
                value = needEscape ? escapeStr(String.valueOf(valueObj)) : valueObj;
                return " >= " + value;
            case LT:
                value = needEscape ? escapeStr(String.valueOf(valueObj)) : valueObj;
                return " < " + value;
            case LTE:
                value = needEscape ? escapeStr(String.valueOf(valueObj)) : valueObj;
                return " <= " + value;
            default:
                // in
                List<Object> list  = new ArrayList<>();
                if(valueObj instanceof List) {
                    list = ((List<Object>) valueObj);
                }else{
                    Collections.addAll(list, valueObj);
                }
                return " in (" + joinList(list) + ")";
        }
    }

    /**
     * 连接字符串数组集合
     * eg: System.out.println(joinList(Lists.newArrayList(1, 3, 2, 5))); 输出 "'1', '3', '2', '5'"
     *
     * @param iterable 集合
     * @return 连接结果
     */
    public static <T> String joinList(Iterable<T> iterable) {
        Iterator<T> iterator = iterable.iterator();
        StringBuilder stringBuilder = new StringBuilder();
        while (iterator.hasNext()) {
            T next = iterator.next();
            String s = "'" + next + "', ";
            stringBuilder.append(s);
        }
        String result = stringBuilder.toString();
        return result.substring(0, result.length() - 2);
    }

}
