package com.lunhan.xxx.common.orm2;

import com.lunhan.xxx.common.ConstantFactory;
import com.lunhan.xxx.common.util.CalendarUtil;
import com.lunhan.xxx.common.util.NumericUtil;

import java.math.BigDecimal;
import java.sql.Types;
import java.util.Date;

/**
 * 处理sql 相关工具类
 */
public final class SqlUtil {
    private SqlUtil() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 获取java类型对应的sql数据类型
     * @param value 参数值
     */
    public static Integer getParameterType(Object value) {
        Integer type = Types.VARCHAR;

        if (value instanceof Integer) {
            type = Types.INTEGER;
        } else if (value instanceof Long) {
            type = Types.BIGINT;
        } else if (value instanceof Float) {
            type = Types.DECIMAL;
        } else if (value instanceof Double) {
            type = Types.DECIMAL;
        } else if (value instanceof Boolean) {
            type = Types.BOOLEAN;
        } else if (value instanceof CalendarUtil) {
            type = Types.TIME;
        } else if (value instanceof Date) {
            type = Types.TIMESTAMP;
        } else if (value instanceof BigDecimal) {
            type = Types.DECIMAL;
        }
        return type;
    }

    /**
     * 构建in的where条件
     * @param inQuantity in条件个数
     */
    public static String getListInWhereSql(Integer inQuantity) {
        if(NumericUtil.tryParseInt(inQuantity).compareTo(0)<1) {
            return " 1!=1";
        }
        StringBuilder sbSql = new StringBuilder();
        sbSql.append("(");
        for (int i=0; i<inQuantity; i++) {
            if(i>0) {
                sbSql.append(ConstantFactory.SQL_STR_COMMA);
            }
            sbSql.append("?");
        }
        sbSql.append(")");
        return sbSql.toString();
    }
}