package xtools.boot.api.model.dto.page;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import xtools.core.extend.PagingUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * <p>Title : PageResp</p>
 * <p>Description : PageResp</p>
 * <p>DevelopTools : Idea_x64_v2026.1</p>
 * <p>DevelopSystem : macOS Sequoia 15.7.5</p>
 * <p>Company : org.xujun</p>
 *
 * @author : XuJun
 * @version : 5.0.0
 * @date : 2025/12/22 14:58
 */
@Data
@NoArgsConstructor
public final class PageResp<T> implements Serializable {

    /**
     * 当前页面
     */
    @Schema(description = "当前页面")
    private Integer currentPage;

    /**
     * 页面数据条数
     */
    @Schema(description = "页面数据条数")
    private Integer pageSize;

    /**
     * 数据条数
     */
    @Schema(description = "数据条数")
    private Long total;

    /**
     * 总页数
     */
    @Schema(description = "总页数")
    private Long pageCount;

    /**
     * 结果数据集
     */
    @Schema(description = "结果数据集")
    private List<T> data;

    /**
     * 构造方法
     *
     * @param req 分页请求
     */
    public PageResp(PageReq<?> req) {
        this.currentPage = req.getCurrentPage();
        this.pageSize = req.getPageSize();
    }

    /**
     * 构造方法
     *
     * @param req 分页请求
     */
    public PageResp(PageReq<?> req, Long total, List<T> data) {
        this.currentPage = req.getCurrentPage();
        this.pageSize = req.getPageSize();
        this.total = total;
        this.data = data;
    }

    /**
     * 获取总页数
     *
     * @return 总页数
     */
    public Long getPageCount() {
        if (Objects.isNull(this.pageCount)) {
            this.pageCount = PagingUtils.getLastPage(this.total, this.pageSize);
        }
        return this.pageCount;
    }
}
