package org.xyzh.common.core.page;

import java.io.Serializable;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @description    分页参数
 * @filename    分页参数
 * @author    yslg
 * @copyright yslg
 * @since 2025-11-02
 */
@Data
@NoArgsConstructor
public class PageParam implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 页码
     * @author    yslg
     * @since 2025-11-02
     */
    private int page;
    /**
     * 每页条数
     * @author    yslg
     * @since 2025-11-02
     */
    private int pageSize;
    
    private int total;
    /**
     * 总页数
     * @author    yslg
     * @since 2025-11-02
     */
    private int totalPages;
    
    private int offset;

    public PageParam(int page, int pageSize) {
        if (page <= 0) {
            throw new IllegalArgumentException("页码必须大于0");
        }
        if (pageSize <= 0) {
            throw new IllegalArgumentException("每页条数必须大于0");
        }
        this.page = page;
        this.pageSize = pageSize;
        this.offset = (page - 1) * pageSize;
    }

    public void setPage(int page){
        if (page <= 0) {
            throw new IllegalArgumentException("页码必须大于0");
        }
        this.page = page;
        if (this.pageSize <= 0) {
            this.pageSize = 10;
        }
        this.offset = (page - 1) * this.pageSize;
    }

    public void setPageSize(int pageSize){
        if (pageSize <= 0) {
            throw new IllegalArgumentException("每页条数必须大于0");
        }
        this.pageSize = pageSize;
        if (this.page <= 0) {
            this.page = 1;
        }
        this.offset = (this.page - 1) * this.pageSize;
    }

    public void setTotal(int total){
        this.total = total;
        if (this.pageSize > 0) {
            this.totalPages = (int) Math.ceil((double) total / pageSize);
        }
    }
}
