package com.engine.htsc.payment.util;

import cn.hutool.core.lang.Assert;
import org.apache.commons.collections4.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.*;

/**
 * 所有模块都可以使用此接口简化开发
 *
 * @author ruan4261
 */
public final class CommonAPI {

    /**
     * 返回首个字段<br>
     * RecordSet结果集下标从1开始
     *
     * @since 1.1.11
     */
    public static String querySingleField(String sql) {
        Assert.notEmpty(sql, "sql");
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        rs.next();
        return rs.getString(1);
    }

    /**
     * 通过一条sql语句查询一个字段
     * 查询不到则返回空字符串
     */
    public static String querySingleField(final String sql, final String field) {
        Assert.notEmpty(sql, "execute sql");
        Assert.notEmpty(sql, "field");
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        rs.next();
        return Util.null2String(rs.getString(field));
    }

    /**
     * 通过一条sql语句查询一个字段
     * 查询不到则返回空字符串
     */
    public static String querySingleFieldWithPlaceholder(final String sql, final String field, Object... params) {
        Assert.notEmpty(sql, "execute sql");
        Assert.notEmpty(sql, "field");
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql, params);
        rs.next();
        return Util.null2String(rs.getString(field));
    }

    /**
     * 返回一条记录<br>
     * RecordSet结果集下标从1开始
     *
     * @since 1.1.11
     */
    public static Map<String, String> querySingleRow(String sql) {
        Assert.notEmpty(sql, "sql");
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return mapFromRecordRow(rs);
    }

    public static Map<String, String> querySingleRow(String sql, Object... params) {
        Assert.notEmpty(sql, "sql");
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql, params);
        rs.next();
        return mapFromRecordRow(rs);
    }

    /**
     * 从RecordSet当前行读取一张键值映射表
     * <hr>
     * 所有键值将会使用小写!!
     */
    public static Map<String, String> mapFromRecordRow(RecordSet rs) {
        Assert.notNull(rs, "RecordSet");
        String[] cols = rs.getColumnName();
        Map<String, String> result = new HashMap<String, String>(cols.length + (cols.length >> 1));
        for (String key : cols) {
            if (key != null && !key.equals("")) {
                String value = Util.null2String(rs.getString(key));
                result.put(key.toLowerCase(), value);
            }
        }
        return result;
    }

    /**
     * @param table      目标表名
     * @param fields     目标字段名, 为空时默认获取所有字段
     * @param conditions 查询字符串: xx='xx' 自动用anid连接
     */
    public static List<Map<String, String>> query(String table, String fields, String... conditions) {
        Assert.notEmpty(table);
        if (fields == null || fields.equals(""))
            fields = "*";

        StringBuilder builder = null;
        if (conditions != null && conditions.length > 0) {
            builder = new StringBuilder(" where ");
            for (int i = 0; i < conditions.length; i++) {
                if (i > 0)
                    builder.append(" and ");

                builder.append(conditions[i]);
            }
        }

        return query("select " + fields + " from " + table + (builder == null ? "" : builder.toString()));
    }

    /**
     * @param table      目标表名
     * @param fields     目标字段名, 为空时默认获取所有字段
     * @param conditions 查询字符串: xx='xx' 自动用and连接
     */
    public static List<Map<String, String>> queryWithPlaceholder(String table, String fields, String conditions, Object... params) {
        Assert.notEmpty(table);
        if (fields == null || fields.equals(""))
            fields = "*";

        StringBuilder builder = null;
        List<String> conditionList = Arrays.asList(conditions.split(","));
        if (CollectionUtils.isNotEmpty(conditionList)) {
            builder = new StringBuilder(" where ");
            for (int i = 0; i < conditionList.size(); i++) {
                if (i > 0) {
                    builder.append(" and ");
                }
                builder.append(conditionList.get(i)).append("=?");
            }
        }

        return queryWithPlaceholder("select " + fields + " from " + table + (builder == null ? "" : builder.toString()), params);
    }

    public static List<Map<String, String>> query(String sql) {
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        return query(rs);
    }

    public static List<Map<String, String>> queryWithOrder(String sql) {
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        return query(rs);
    }

    public static List<Map<String, String>> queryWithPlaceholder(String sql, Object... conditions) {
        RecordSet rs = new RecordSet();
        if (conditions != null && conditions.length > 0) {
            rs.executeQuery(sql, conditions);
        } else {
            rs.executeQuery(sql);
        }

        return query(rs);
    }

    public static List<Map<String, String>> query(RecordSet rs) {
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        while (rs.next()) {
            result.add(mapFromRecordRow(rs));
        }

        return result;
    }

    public static List<Map<String, String>> queryWithOrder(RecordSet rs) {
        List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        while (rs.next()) {
            result.add(mapFromRecordRow(rs));
        }

        return result;
    }

}
