package com.zhiqiyun.open.mybatis.paginator.domain;

import lombok.Data;

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

/**
 * @author jtoms
 */
@Data
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -2857504782717596783L;

    private int pageSize;
    private int current;
    private int total;

    private List<T> data;

    public PageResult<T> setData(List<T> data) {
        this.data = data;
        return this;
    }

    public PageResult<T> setPageBounds(PageBounds bounds) {
        this.pageSize = bounds.getLimit();
        this.current = bounds.getCurrent();
        this.total = bounds.getTotal();
        return this;
    }

    public int getTotalPages() {
        if (this.getPageSize() == 0) {
            return 0;
        } else {
            int totalPages = this.getTotal() / this.getPageSize();
            if (this.getTotal() % this.getPageSize() != 0L) {
                ++totalPages;
            }
            return totalPages;
        }
    }
}
