package com.yihu.hos.web.framework.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;

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

/**
 * 列表对象
 */
public class DataGridResult extends Result implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(DataGridResult.class);

    private int pageSize = 10;

    private int currPage = 1;

    private int totalPage;

    private int totalCount;

    private List detailModelList;

    public DataGridResult() {

    }

    public DataGridResult(int pageSize, int currPage) {
        this.pageSize = pageSize;
        this.currPage = currPage;
    }

    /**
     * Json�ַ���תʵ��
     *
     * @return
     */
    public static DataGridResult fromJson(String json) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(json, DataGridResult.class);
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    public String toJson() {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            logger.error(e.getMessage());

            return "";
        }
    }

    public int getPageSize() {
        return pageSize;
    }

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

    public int getCurrPage() {
        return currPage;
    }

    public void setCurrPage(int currPage) {
        this.currPage = currPage;
    }

    public int getTotalPage() {
        if (totalCount % pageSize == 0) {
            totalPage = totalCount / pageSize;
        } else {
            totalPage = totalCount / pageSize + 1;
        }
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStart() {
        if (currPage != 0) {
            return (currPage - 1) * pageSize;
        }
        return 0;
    }

    public int getEnd() {
        if (currPage != 0) {
            return currPage * pageSize;
        }
        return 0;
    }

    public int getTotalCount() {
        if (totalCount == 0 && detailModelList != null) {
            totalCount = detailModelList.size();
        }
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public List getDetailModelList() {
        return detailModelList;
    }

    public void setDetailModelList(List detailModelList) {
        this.detailModelList = detailModelList;
    }
}