package com.energy.manage.common.base;

import org.springframework.util.CollectionUtils;

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

public class Page<T> implements Serializable {
  private Integer pageNum = 1;
  private Integer pageSize = 10;
  private Long totalSize;
  private Integer pages;
  private List<T> list;

  public Integer getPageNum() {
    return pageNum;
  }

  public void setPageNum(Integer pageNum) {
    this.pageNum = pageNum;
  }

  public Integer getPageSize() {
    return pageSize;
  }

  public void setPageSize(Integer pageSize) {
    this.pageSize = pageSize;
  }

  public Long getTotalSize() {
    return totalSize;
  }

  public void setTotalSize(Long totalSize) {
    this.totalSize = totalSize;
  }

  public Integer getPages() {
    pages = (int) (totalSize / pageSize);
    int mod = (int) (totalSize % pageSize);
    if (mod > 0) {
      pages += 1;
    }
    return pages;
  }

  public void setPages(Integer pages) {
    this.pages = pages;
  }

  public List<T> getList() {
    return list;
  }

  public void setList(List<T> list) {
    if (CollectionUtils.isEmpty(list)) {
      this.list = Collections.emptyList();
    } else {
      this.list = list;
    }
  }

  public Page() {
  }

  /**
   * 构建分页数据
   *
   * @param pageNum
   * @param pageSize
   * @param totalSize
   * @param list
   * @param <T>
   * @return
   */
  public static <T> Page<T> build(int pageNum, int pageSize, long totalSize, List<T> list) {
    Page<T> page = new Page<>();
    page.setPageNum(pageNum);
    page.setPageSize(pageSize);
    page.setTotalSize(totalSize);
    page.setList(list);
    return page;
  }

  /**
   * Page Redis List 分页工具，获取分页起始数据
   *
   * @param pageNum
   * @param pageSize
   * @return
   */
  public static int getPageDataStart(int pageNum, int pageSize) {
    pageNum = pageNum <= 0 ? 1 : pageNum;
    pageSize = pageSize <= 0 ? 10 : pageSize;
    return (pageNum - 1) * pageSize;
  }

  /**
   * Page Redis List 分页工具，获取分页结束数据
   *
   * @param pageNum
   * @param pageSize
   * @return
   */
  public static int getPageDataEnd(int pageNum, int pageSize) {
    pageNum = pageNum <= 0 ? 1 : pageNum;
    pageSize = pageSize <= 0 ? 10 : pageSize;
    return pageNum * pageSize - 1;
  }
}
