package com.jsh.erp.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

@ApiModel("分页结果")
@Data
public final class PageResult<T> implements Serializable {

    @ApiModelProperty(value = "数据", required = true)
    private List<T> list;
    
    @ApiModelProperty(value = "总量", required = true)
    private Long total;
    
    private Integer pageSize;
    
    private Integer pageNumber;

    public PageResult() {
    }

    public PageResult(List<T> list, Long total) {
        this.list = list;
        this.total = total;
    }
    public PageResult(List<T> list, Long total, Integer pageSize, Integer pageNumber) {
        this.list = list;
        this.total = total;
        this.pageSize = pageSize;
        this.pageNumber = pageNumber;
    }
    
    public PageResult(List<T> list, PageResult oldResult) {
        this.list = list;
        this.total = oldResult.getTotal();
        this.pageSize = oldResult.getPageSize();
        this.pageNumber = oldResult.getPageNumber();
    }

    public PageResult(Long total) {
        this.list = new ArrayList<>();
        this.total = total;
    }

    public static <T> PageResult<T> empty() {
        return new PageResult<>(0L);
    }

    public static <T> PageResult<T> empty(Long total) {
        return new PageResult<>(total);
    }

    public static <T> PageResult<T> of(List<T> list, PageParam page) {
        return new PageResult<>(list, (long) list.size(), page.getPageSize(), page.getPageNo());
    }
    
    public static <T> PageResult<T> of(List<T> list, PageResult oldResult) {
        return new PageResult<>(list, oldResult);
    }
}
