package com.zcxk.core.common.pojo;


import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Andy
 * @description: 带分页数据返回
 * @date 2021/7/16
 * @version V1.0
 **/
@Data
@ApiModel(value = "带分页数据返回")
public class PageResult<T> {

    @ApiModelProperty(value = "当前页码", name = "currentPage")
    private Integer currentPage;

    @ApiModelProperty(value = "每页多少数据", name = "pageSize")
    private Integer pageSize;

    @ApiModelProperty(value = "总页数", name = "totalPages")
    private Integer totalPages;

    @ApiModelProperty(value = "总条数", name = "totalSize")
    private Integer total;

    @ApiModelProperty(value = "数据", name = "rows")
    private List<T> list;

    private PageResult() {

    }

    private PageResult<T> pageResult(List<T> list, int start, int size, int totalSize) {
        PageResult<T> pageResult = new PageResult<>();
        pageResult.setCurrentPage(start);
        pageResult.setPageSize(size);
        if (BigDecimal.ZERO.intValue() != totalSize) {
            pageResult
                .setTotalPages(totalSize % size == 0 ? totalSize / size : (totalSize / size) + 1);
        } else {
            pageResult.setTotalPages(BigDecimal.ZERO.intValue());
        }

        pageResult.setTotal(totalSize);
        pageResult.setList(list);
        return pageResult;
    }

    public static <T> PageResult<T> builder(List<T> list, int start, int size,
        int totalSize) {
        return new PageResult<T>().pageResult(list, start, size, totalSize);
    }

    public static <T> PageResult<T> builderEmpty() {
        return new PageResult<T>().pageResult(Lists.newArrayList(), 0, 0, 0);
    }

}
