

package com.lideeyunji.tool.framework.tenant.core.util;


import com.lideeyunji.tool.framework.tenant.core.parse.lideeYunJiTenantParse;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.update.Update;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 租户工具类
 */
public class LideeYunJiTenantUtils {





    private static String TMP_PARAM="lideeyunji_param_mjkj_tmp_";


    private static String fomatSql(String sql) {
        try{
            sql=sql.replaceAll(",jdbcType=","_lideeyunji_jdbcType_");
            String patternString = "#\\{([^}]+)\\}"; // 正则表达式字符串
            Pattern pattern = Pattern.compile(patternString);
            Matcher matcher = pattern.matcher(sql);

            StringBuffer sb = new StringBuffer();
            while (matcher.find()) {
                String variableName = matcher.group(1); // 获取#{}内的变量名
                matcher.appendReplacement(sb, TMP_PARAM + variableName);
            }
            matcher.appendTail(sb);

            sql = sb.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return sql;
    }

    private static String resetSql(String sql){
        try{
            sql=sql.replaceAll("_lideeyunji_jdbcType_",",jdbcType=");
            String patternString = TMP_PARAM+"([a-zA-Z0-9_.]+)(?:,(\\s*jdbcType\\s*=\\s*[a-zA-Z]+))?"; // 正则表达式字符串
            Pattern pattern = Pattern.compile(patternString);
            Matcher matcher = pattern.matcher(sql);

            StringBuffer sb = new StringBuffer();
            while (matcher.find()) {
                String variableName = matcher.group(1); // 获取括号内的变量名
                String jdbcType = matcher.group(2); // 获取括号内的变量名
                if(FuncBase.isEmpty(jdbcType)){
                    matcher.appendReplacement(sb, "#{" + variableName + "}");
                }else{
                    matcher.appendReplacement(sb, "#{" + variableName+","+jdbcType + "}");
                }
            }
            matcher.appendTail(sb);

            return sb.toString();
        }catch (Exception e){
            e.printStackTrace();
        }
        return sql;
    }

    /**
     * 解析多租户sql
     *
     * @param sql
     * @return
     */
    public static String parseTenantSql(String sql) throws JSQLParserException {
        Statement statement = CCJSqlParserUtil.parse(fomatSql(sql));
        if (statement instanceof Select) {//查询
            Select select = (Select) statement;
            lideeYunJiTenantParse.processSelect(select);
        } else if (statement instanceof Update) {//修改
            Update update = (Update) statement;
            lideeYunJiTenantParse.processUpdate(update);
        } else if (statement instanceof Delete) {//删除
            Delete delete = (Delete) statement;
            lideeYunJiTenantParse.processDelete(delete);
        } else if (statement instanceof Insert) {//新增
            Insert insert = (Insert) statement;
            lideeYunJiTenantParse.processInsert(insert);
        }
        String new_sql = statement.toString();
        return resetSql(new_sql);
    }






}
