package com.storlead.framework.mybatis.interceptor;

import cn.hutool.core.util.StrUtil;

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

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-04-22 16:22
 */
public class SqlToJsonParser {

    public static Map<String, String> parseSqlToWhereAndSet(String sql) {

        String updateStatement = sql;
        String regexPattern = "SET\\s+(.+?)\\s+WHERE\\s+(.+)";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regexPattern);
        Matcher matcher = pattern.matcher(updateStatement);

        // 如果匹配到了set后面的键值对
        if (matcher.find()) {
            // 获取匹配到的键值对字符串
            String keyValuePairsStr = matcher.group(1);
            String whereConditionStr = matcher.group(2);

            // 使用正则表达式提取键值对并转换为JSON格式
            Map<String, String> jsonMap = new HashMap<>();
            Pattern keyValuePattern = Pattern.compile("(\\w+)\\s*=\\s*(?:'([^']+)'|(\\d+))");
            Matcher keyValueMatcher = keyValuePattern.matcher(keyValuePairsStr);
            while (keyValueMatcher.find()) {
                String key = keyValueMatcher.group(1);
                String value = keyValueMatcher.group(2);
                if (value == null) {
                    // 如果值不是字符串，则获取数字值
                    value = keyValueMatcher.group(3);
                }
                String camelCaseKey = StrUtil.toCamelCase(key);
                jsonMap.put(camelCaseKey, value);
            }

            Pattern wherePattern = Pattern.compile("(\\w+)\\s*=\\s*(?:'([^']+)'|(\\d+))");
            Matcher whereMatcher = wherePattern.matcher(whereConditionStr);
            while (whereMatcher.find()) {
                String key = whereMatcher.group(1);
                String value = whereMatcher.group(2);
                if (value == null) {
                    // 如果值不是字符串，则获取数字值
                    value = whereMatcher.group(3);
                }
                if ("id".equals(key)) {
                    jsonMap.put(key, value);
                }
            }
            // 打印转换后的JSON格式数据
            return jsonMap;
        } else {
            System.out.println("No key-value pairs found after SET.");
            return null;
        }
    }

    public static Map<String, String> parseInsertSqlToMap(String sql) {

        Map<String, String> jsonMap = new HashMap<>();
        // 匹配 INSERT INTO 语句的正则表达式
        String regex = "INSERT INTO\\s+(\\w+)\\s*\\(([^)]+)\\)\\s*VALUES\\s*\\(([^)]+)\\)";
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(sql);

        if (matcher.find()) {
            String tableName = matcher.group(1);
            String columnsPart = matcher.group(2);
            String valuesPart = matcher.group(3);

            // 处理列名和对应的值
            String[] columns = columnsPart.split("\\s*,\\s*");
            String[] values = valuesPart.split("\\s*,\\s*");

            // 打印结果
            Map<String, String> columnValueMap = new HashMap<>();
            for (int i = 0; i < columns.length; i++) {
                columnValueMap.put(columns[i], values[i]);
            }
            return columnValueMap;
        } else {
            System.out.println("The SQL statement is not in the correct format.");
            return null;
        }
    }
}
