package com.nantian.cloud.dbcapp.unit;

import lombok.Data;

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

@Data
public class Pages implements Serializable {
	private static final long serialVersionUID = 1L;
	private long total; //总条数
	private long pages; //总页数
	private long size = 10;  //每页显示条数，默认10
	private long current = 1; //第几页，默认1
	private long offset; //偏移量
	private List<?> records;

	public Pages(long size, long current) {
		this.size = size;
		this.current = current;
		this.offset = getOffset();
	}

	public Pages(long total, long size, long current, List<?> records) {
		this.total = total;
		this.size = size;
		this.current = current;
		this.records = records;
		this.pages = getPages();
		this.offset = getOffset();
	}

	public long getOffset() {
		return this.getCurrent() > 0L ? (this.getCurrent() - 1L) * this.getSize() : 0L;
	}

	public long getPages() {
		if (this.getSize() == 0L) {
			return 0L;
		} else {
			long pages = this.getTotal() / this.getSize();
			if (this.getTotal() % this.getSize() != 0L) {
				++pages;
			}
			return pages;
		}
	}
}
