package com.sifang.modules.sys.util;

import com.jfinal.kit.JsonKit;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class LayuiPage<T> {

    private String code;    //0:正常返回
    private String msg;
    private Long count;      //总条数
    private List<T> data;
    private int page;       //当前页面
    private int limit;      //每页条数限制

    public LayuiPage(List<T> data, Long count) {
        this.count = count;
        this.data = data;
        this.code = "0";
        this.msg = "";
    }

    public LayuiPage(List<T> data) {
        this(data, Long.valueOf(data.size()));
    }

    public LayuiPage(HttpServletRequest request){
        this.code = "0";
        this.msg = "";
        try{
            this.page = Integer.valueOf(request.getParameter("page"));
            this.limit = Integer.valueOf(request.getParameter("limit"));
        }catch (Exception e){
          //  e.printStackTrace();
            this.page = 1;
            this.limit = 10;
        }
    }

    public String renderJson(){
        return JsonKit.toJson(this);
    }

    public String getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

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

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

    public int getPage() {
        return page;
    }

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

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
}
