package org.ta.pddserver.model.common;


import io.swagger.v3.oas.annotations.media.Schema;

public class PageTool {

    @Schema(
            description = "当前页码，最小1",
            example = "1",
            minLength = 0,
            maxLength = 20,
            defaultValue = "1",
            requiredMode = Schema.RequiredMode.REQUIRED // 必填
    )
    private Integer page;
    @Schema(
            description = "不需要传",
            example = "0",
            minLength = 0,
            maxLength = 20,
            defaultValue = "0",
            requiredMode = Schema.RequiredMode.NOT_REQUIRED // 必填
    )
    private Integer offset;
    @Schema(
            description = "分页数量",
            example = "20",
            minLength = 1,
            maxLength = 2,
            defaultValue = "20",
            requiredMode = Schema.RequiredMode.REQUIRED // 必填
    )
    private Integer size;

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public Integer getPage() {
        if (page == null || page < 1) {
            page = 1;
        }
        return page;
    }
    public void setPage(Integer page) {
        if (page == null || page < 1) {
            page = 1;
        }
        this.page = page;
    }

    public Integer getOffset() {
        if (offset == null || offset < 0) {
            offset = 0;
        }
        return offset;
    }

    public void setOffset(Integer offset) {
        if (offset == null || offset < 0) {
            offset = 0;
        }
        this.offset = offset;
    }

    public void checkPage(Integer count, Integer perPage) {
        if (this.size == null || this.size <= 0) {
            this.size = perPage;
        }
        Integer allPage = (count - 1) / this.size + 1;
        if (this.getPage() > allPage) {
            this.page = allPage;
        }
        this.offset = (this.page - 1) * this.size;
    }
}
