package com.microservices.common.core.utils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.microservices.common.core.utils.sql.SqlUtil;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.core.web.page.PageDomain;
import com.microservices.common.core.web.page.TableSupport;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分页工具类
 *
 * @author microservices
 */
public class PageUtils extends PageHelper
{
    /**
     * 设置请求分页数据
     */
    public static void startPage()
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize > 1000) {
            pageSize = 1000;
        }
        PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    /**
     * 转换分页数据
     */
    public static <T, R> GenericsTableDataInfo<R> toPage(List<T> rows, Function<T, R> function) {
        rows = Optional.ofNullable(rows).orElse(Collections.emptyList());
        GenericsTableDataInfo<R> tableDataInfo = new GenericsTableDataInfo<>();
        tableDataInfo.setRows(rows.stream().map(function).collect(Collectors.toList()));
        tableDataInfo.setTotal(new PageInfo<>(rows).getTotal());
        return tableDataInfo;
    }

    /**
     * 将原始列表通过传入方法转换为对应类型，同时根据原始列表分页数据构建分页对象
     *
     * @param rows   原始列表（带分页）
     * @param toList Lambda表达式，对原始列表进行转换
     * @param <T>    原始列表
     * @param <R>    转换后列表
     * @return 转换后列表
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static <T, R> GenericsTableDataInfo<R> toListPage(List<T> rows, Page.Function<List<T>, List<R>> toList) {
        rows = Optional.ofNullable(rows).orElse(Collections.emptyList());
        GenericsTableDataInfo<R> tableDataInfo = new GenericsTableDataInfo<>();
        tableDataInfo.setRows(toList.apply(rows));
        tableDataInfo.setTotal(new PageInfo<>(rows).getTotal());
        return tableDataInfo;
    }


    /**
     * 转换分页数据
     */
    public static <T> GenericsTableDataInfo<T> toPage(List<T> rows) {
        rows = Optional.ofNullable(rows).orElse(Collections.emptyList());
        GenericsTableDataInfo<T> tableDataInfo = new GenericsTableDataInfo<>();
        tableDataInfo.setRows(rows);
        tableDataInfo.setTotal(new PageInfo<>(rows).getTotal());
        return tableDataInfo;
    }

    /**
     * 转换分页数据
     */
    public static <T> GenericsTableDataInfo<T> toPage(List<T> rows, Long total) {
        rows = Optional.ofNullable(rows).orElse(Collections.emptyList());
        GenericsTableDataInfo<T> tableDataInfo = new GenericsTableDataInfo<>();
        tableDataInfo.setRows(rows);
        tableDataInfo.setTotal(total);
        return tableDataInfo;
    }


    /**
     * 清理分页的线程变量
     */
    public static void clearPage()
    {
        PageHelper.clearPage();
    }
}
