package com.czg.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageParam;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import lombok.experimental.UtilityClass;

/**
 * 分页工具类
 *
 * @author tankaikai
 * @since 2025-02-11 16:56
 */
@UtilityClass
public class PageUtil {
    /**
     * 当前记录起始索引
     */
    public final String PAGE = "page";

    /**
     * 每页显示记录数
     */
    public final String SIZE = "size";

    /**
     * 排序方式，多个用逗号分隔，asc:升序,desc:降序
     */
    public final String ORDER_BY = "orderBy";

    /**
     * 当前记录起始索引 默认值
     */
    public final int DEFAULT_PAGE = 1;

    /**
     * 每页显示记录数 默认值 默认查全部
     */
    public final int DEFAULT_SIZE = 10;

    /**
     * 构造分页Page
     * <p>
     * 从param体获取 (url请求 ?拼接的参数)
     * page 默认1
     * size 默认10
     * <p>
     *
     * @return Page<T>
     */
    public <T> Page<T> buildPage() {
        HttpServletRequest request = ServletUtil.getRequest();
        Integer pageNum = Convert.toInt(request.getParameter(PAGE), DEFAULT_PAGE);
        Integer pageSize = Convert.toInt(request.getParameter(SIZE), DEFAULT_SIZE);
        if (pageNum <= 0) {
            pageNum = DEFAULT_PAGE;
        }
        return new Page<>(pageNum, pageSize);
    }

    /**
     * 获取空Page对象
     */
    public <T> Page<T> emptyPage() {
        return Page.of(1,10,0) ;
    }



    /**
     * 构造排序QueryWrapper
     * 从param体获取
     * <p>
     * orderField 排序字段列 驼峰命名 多个用逗号分隔
     * order排序规则 asc,desc 多个条件逗号分割
     *
     * @return QueryWrapper
     */
    public QueryWrapper buildSortQueryWrapper() {
        QueryWrapper queryWrapper = QueryWrapper.create();
        try {
            HttpServletRequest request = ServletUtil.getRequest();
            String orderBy = request.getParameter(ORDER_BY);
            if (StrUtil.isNotEmpty(orderBy)) {
                queryWrapper.orderBy(SqlUtil.escapeOrderBySql(orderBy));
            }
        } catch (Exception e) {
            //System.out.println("排序参数异常");
        }
        return queryWrapper;
    }

    /**
     * pageHelper 分页参数构造
     *
     * @return PageParam
     */
    public PageParam buildPageHelp() {
        HttpServletRequest request = ServletUtil.getRequest();
        Integer pageNum = Convert.toInt(request.getParameter(PAGE), DEFAULT_PAGE);
        Integer pageSize = Convert.toInt(request.getParameter(SIZE), DEFAULT_SIZE);
        String orderBy = ServletUtil.getRequest().getParameter(ORDER_BY);
        PageParam pageParam = new PageParam();
        pageParam.setPageNum(pageNum);
        pageParam.setPageSize(pageSize);
        if (StrUtil.isNotEmpty(orderBy)) {
            pageParam.setOrderBy(SqlUtil.escapeOrderBySql(orderBy));
        }
        return pageParam;
    }

    /**
     * pageHelper 转为 mybatisflex 分页
     */
    public static <T> Page<T> convert(PageInfo<T> page) {
        Page<T> page1 = new Page<>();
        page1.setPageNumber(page.getPageNum());
        page1.setPageSize(page.getPageSize());
        page1.setTotalRow(page.getTotal());
        page1.setTotalPage(page.getPages());
        page1.setRecords(page.getList());
        return page1;
    }
}
