package com.ruoyi.common.dev.mybatis;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class SQLStatement
{
    public final String sql;
    public final String arguments;

    public SQLStatement(String sql, String arguments)
    {
        this.sql = sql;
        this.arguments = arguments;
    }

    public String Parse()
    {
        String str;
        if(null == arguments || arguments.isEmpty())
            str = sql;
        else
        {
            String[] args = ParseArguments();
            str = ReplacePlaceholder(args);
        }
        return Format(str);
    }

    private String[] ParseArguments()
    {
        String[] split = (arguments + ", ").split("\\(\\w+\\), ");
        return split;
    }

    private String ReplacePlaceholder(String[] args)
    {
        try
        {
            int lastIndex = 0;
            int i = 0;
            StringBuilder sb = new StringBuilder();
            int length = sql.length();
            while(lastIndex < length)
            {
                int index = sql.indexOf('?', lastIndex);
                if(i < args.length && index >= 0)
                {
                    String str = sql.substring(lastIndex, index);
                    sb.append(str);
                    sb.append(ToStr(args[i++]));
                    lastIndex = index + 1;
                }
                else
                {
                    String str = sql.substring(lastIndex);
                    sb.append(str);
                    lastIndex = length;
                }
            }
            return sb.toString();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    private String ReplacePlaceholder_rev(String[] args)
    {
        try
        {
            int lastIndex = sql.length() - 1;
            int i = args.length - 1;
            StringBuilder sb = new StringBuilder();
            while(lastIndex >= 0)
            {
                int index = sql.lastIndexOf('?', lastIndex);
                if(i >= 0 && index >= 0)
                {
                    String str = sql.substring(index + 1, lastIndex + 1);
                    sb.insert(0, str);
                    sb.insert(0, "'" + args[i--] + "'");
                    lastIndex = index - 1;
                }
                else
                {
                    String str = sql.substring(0, lastIndex + 1);
                    sb.insert(0, str);
                    lastIndex = -1;
                }
            }
            return sb.toString();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    private String Format(String sql)
    {
        if(null == sql)
            return null;
        sql = sql.trim();
        if(!sql.endsWith(";"))
            sql = sql + ";";
        return sql;
    }

    private static String ToStr(String arg)
    {
        if(arg == null)
            return "null";
        else if(arg.isEmpty())
            return "''";
        else if(IsNum(arg))
            return arg;
        else
            return "'" + arg + "'";
    }

    // 10
    private static boolean IsNum(String str)
    {
        if(null == str || str.isEmpty())
            return false;
        try
        {
            boolean matches = str.matches("^[\\d.-]*$");
            if(!matches)
                return false;
            Double.parseDouble(str);
            return true;
        }
        catch(Exception e)
        {
            return false;
        }
    }
}
