package cn.stylefeng.team.project.api.util;

import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;

import java.util.HashMap;

public class PageSqlCreateUtil {

    public static String finalPageSql(String originSql, int pageNo, int pageSize) {

        String sqlTemplate = "{originSql} limit {limitStart}, {limitEnd}";

        int limitStart = PageUtil.getStart(pageNo - 1, pageSize);
        int limitEnd = PageUtil.getEnd(pageNo - 1, pageSize);

        HashMap<String, Object> params = new HashMap<>();
        params.put("limitStart", limitStart);
        params.put("limitEnd", limitEnd);
        params.put("originSql", originSql);

        return StrUtil.format(sqlTemplate, params);
    }

    public static String finalCountSql(String originSql) {
        String sqlTemplate = "select count(*) as totalCount from ({originSql}) as temp";
        HashMap<String, Object> params = new HashMap<>();
        params.put("originSql", originSql);
        return StrUtil.format(sqlTemplate, params);
    }

}
