package com.dashitech.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

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


public class JsonParam implements Serializable {

	private static final long serialVersionUID = -4882852696761401880L;

	private Integer total;

	private Integer pageIndex = 1;

	private Integer pageSize = 20;

	private Integer totalPage = 0;

	/**
	 * 实体bean
	 */
	private Map<String, Object> model;
	/**
	 * 实体bean列表
	 */
	private List<Map<String,Object>> list;
	/**
	 * 其他参数
	 */
	private Map<String, Object> param;
	
	
	
	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(Integer pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Integer getPageSize() {
		return pageSize;
	}

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

	public Integer getTotalPage() {
		return totalPage;
	}

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

	public Map<String, Object> getModel() {
		return model;
	}

	public void setModel(Map<String, Object> model) {
		this.model = model;
	}

	public Map<String, Object> getParam() {
		return param;
	}

	public void setParam(Map<String, Object> param) {
		this.param = param;
	}
	
	public List<Map<String, Object>> getList() {
		return list;
	}

	public void setList(List<Map<String, Object>> list) {
		this.list = list;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public <T> T getModelBean(Class clazz) {
		JSONObject jsonObj = JsonUtil.toJSONObject(getModel());
		Object temp = JsonUtil.toObject(jsonObj.toString(), clazz);
		if(temp != null) {
			return (T)temp;
		}
		return null;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public <T> List<T> getModelListBean(Class clazz) {
		JSONArray jsonArr = JsonUtil.toJSONArray(getList());
		List<T> temp = JsonUtil.toList2(jsonArr.toString(), clazz);
		if(temp != null) {
			return temp;
		}
		return new ArrayList<T>();
	}

	@Override
	public String toString() {
		return "JsonParamBean [total=" + total + ", pageIndex=" + pageIndex + ", pageSize=" + pageSize + ", totalPage=" + totalPage + ", model=" + model + ", list=" + list + ", param=" + param + "]";
	}

}
