package thyyxxk.webserver.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.Map;

public class WrapperUtils {
    public static String getFullSql(QueryWrapper<?> wrapper) {
        // 1. 获取SQL片段（带#{param}格式）
        String sqlSegment = wrapper.getSqlSegment();

        // 2. 获取参数映射
        Map<String, Object> paramMap = wrapper.getParamNameValuePairs();

        // 3. 替换参数
        for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
            String paramName = entry.getKey();
            Object value = entry.getValue();
            String placeholder = "#{ew.paramNameValuePairs." + paramName + "}";

            // 处理特殊值（字符串/日期需加引号）
            String replacement = (value instanceof CharSequence || value instanceof Date)
                    ? "'" + value + "'" : String.valueOf(value);

            sqlSegment = sqlSegment.replace(placeholder, replacement);
        }

        return sqlSegment;
    }
}
