package net.northking.cctp.common.db;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title:
 * @Description:
 * @Company: 京北方信息技术股份有限公司
 * @Author: 谢秀松
 * @CreateDate: 2018/3/19
 * @UpdateUser:
 * @Version: 1.0
 */
@JsonSerialize
public class DefaultPagination<E> implements Pagination<E>
{
  private int pageNo = 1;
  private int pageSize = 10;
  private long recordCount = 0L;
  @JsonSerialize
  private List<E> records = new ArrayList<>();

  public DefaultPagination()
  {
  }

  public DefaultPagination(int pageNo, int pageSize)
  {
    this.pageNo = pageNo;
    this.pageSize = pageSize;
  }

  public DefaultPagination(int pageNo, int pageSize, long recordCount, List<E> records)
  {
    this.pageNo = pageNo;
    this.pageSize = pageSize;
    this.recordCount = recordCount;
    this.records = records;
  }

  public int getPageSize()
  {
    return pageSize;
  }

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

  public int getPageCount()
  {
    return (int) ((this.recordCount + this.pageSize - 1) / this.pageSize);
  }

  public long getRecordCount()
  {
    return recordCount;
  }

  public void setRecordCount(long recordCount)
  {
    this.recordCount = recordCount;
  }

  public int getPageNo()
  {
    return pageNo;
  }

  public void setPageNo(int curPage)
  {
    this.pageNo = curPage;
  }

  public long getStartRecordNo()
  {
    return (long) this.pageSize * (this.pageNo - 1);
  }

  public long getEndRecordNo()
  {
    return getStartRecordNo() + this.records.size();
  }

  public int getFirstPageNo()
  {
    return 1;
  }

  public int getPrevPageNo()
  {
    if (this.pageNo > 1)
    {
      return this.pageNo - 1;
    } else
    {
      return getFirstPageNo();
    }
  }

  public int getNextPageNo()
  {
    if (this.pageNo < this.getLastPageNo())
    {
      return this.pageNo + 1;
    } else
    {
      return getLastPageNo();
    }
  }

  public int getLastPageNo()
  {
    return this.getPageCount();
  }

  public List<E> getRecords()
  {
    return records;
  }

  public void setRecords(List<E> data)
  {
    this.records = data;
  }

  @Override
  public boolean hasNextPage()
  {
    return this.pageNo < this.getNextPageNo();
  }
}
