package com.uas.platform.b2b.manage.controller.support;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.data.domain.Page;

import java.io.*;
import java.util.List;

/**
 * 分页返回结果集
 *
 * @author hejq
 * @date 2018-09-07 11:44
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class PageResultBean<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 定义返回成功标识
     */
    private static final String SUCCESS = "success";

    /**
     * 成功状态码
     */
    private static final int SUCCESS_CODE = 0;

    /**
     * 失败状态码
     */
    public static final int FAIL_CODE = 1;

    /**
     * 消息
     */
    private String msg = SUCCESS;

    /**
     * 状态码
     */
    private int code = SUCCESS_CODE;

    /**
     * 总页数
     */
    private int totalPages;

    /**
     * 总条数
     */
    private long totalElements;

    /**
     * 页码
     */
    private int page;

    /**
     * 分页大小
     */
    private int size;

    /**
     * 首页
     */
    private boolean first;

    /**
     * 尾页
     */
    private boolean last;

    /**
     * 内容
     */
    private List<T> data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public long getTotalElements() {
        return totalElements;
    }

    public void setTotalElements(long totalElements) {
        this.totalElements = totalElements;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public boolean isFirst() {
        return first;
    }

    public void setFirst(boolean first) {
        this.first = first;
    }

    public boolean isLast() {
        return last;
    }

    public void setLast(boolean last) {
        this.last = last;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public PageResultBean() {
        super();
    }

    public PageResultBean(Throwable e) {
        super();
        this.msg = e.toString();
        this.code = FAIL_CODE;
    }

    public PageResultBean(Page<T> data) {
        super();
        this.data = data.getContent();
        this.totalElements = data.getTotalElements();
        this.totalPages = data.getTotalPages();
        this.first = data.isFirst();
        this.last = data.isLast();
        this.page = data.getNumber();
        this.size = data.getSize();
        this.code = SUCCESS_CODE;
        this.msg = SUCCESS;
    }
}
