package com.crm.rely.backend.core.dto.base;

import com.crm.rely.backend.core.entity.base.BaseSearchPageEntity;
import com.crm.rely.backend.core.entity.base.PageEntity;
import lombok.Data;

@Data
public class PageDto {

    /**
     * 当前页
     */
    private int current;

    private int row;

    private int pageTotal;

    private int rowTotal;

    public PageDto() {

    }

    /**
     * 格式化分页 返回分页dto
     *
     * @param current  单前页
     * @param row      每页显示条数
     * @param rowTotal 总条数
     * @return
     */
    public static PageDto format(int current, int row, int rowTotal) {

        int pageTotal = (rowTotal % row) == 0 ? (rowTotal / row) : (rowTotal / row) + 1;

        if (current > pageTotal) {
            current = pageTotal;
        }
        return format(current, row, pageTotal, rowTotal);
    }

    /**
     * 格式化分页 返回分页dto
     *
     * @param page     page com.crm.rely.backend.entity(current未处理过 错误数据)
     * @param rowTotal 总条数
     * @param <T>
     * @return
     */
    public static <T extends PageEntity> PageDto format(T page, Integer rowTotal) {
        if (rowTotal == null) {
            rowTotal = 0;
        }

        return format(page, (int) rowTotal);
    }
    public static <T extends BaseSearchPageEntity> PageDto format(T searchPageEntity, Integer rowTotal) {

        if (rowTotal == null) {
            rowTotal = 0;
        }

        return format(searchPageEntity, (int) rowTotal);
    }
    public static <T extends BaseSearchPageEntity> PageDto format(T searchPageEntity, int rowTotal) {

        return format(searchPageEntity.getPage(), rowTotal);
    }

    /**
     * 格式化分页 返回分页dto
     *
     * @param page     分页参数
     * @param rowTotal 总条数
     * @param <T>
     * @return
     */
    public static <T extends PageEntity> PageDto format(T page, int rowTotal) {

        int pageTotal = 0;
        int current = page.getCurrent();

        if (rowTotal == 0) {
            pageTotal = 1;
        } else {
            pageTotal = (rowTotal % page.getRow()) == 0 ? (rowTotal / page.getRow()) : (rowTotal / page.getRow()) + 1;
        }

        if (current > pageTotal) {
            current = pageTotal;
        }

        return format(current, page.getRow(), pageTotal, rowTotal);
    }

    /**
     * 格式化 并返回分页dto
     *
     * @param page
     * @param rowTotal
     * @param <T>
     * @return
     */
    public static <T extends PageEntity> PageDto format(T page, Long rowTotal) {
        if (rowTotal == null) {
            rowTotal = 0L;
        }

        return format(page, (long) rowTotal);
    }

    /**
     * 格式化 并返回分页dto
     *
     * @param page
     * @param rowTotal
     * @param <T>
     * @return
     */
    public static <T extends PageEntity> PageDto format(T page, long rowTotal) {

        return format(page, (int) rowTotal);
    }

    /**
     * 格式化 并返回分页dto
     *
     * @param current   当前页
     * @param row       每页显示条数
     * @param pageTotal 总页数
     * @param rowTotal  总条数
     * @return
     */
    private static PageDto format(int current, int row, int pageTotal, int rowTotal) {
        PageDto page = new PageDto();
        page.setCurrent(current);
        page.setRow(row);
        page.setPageTotal(pageTotal);
        page.setRowTotal(rowTotal);
        return page;
    }
}
