package com.dashitech.utils;

import java.io.Serializable;


import net.sf.json.JSONObject;

public class JsonResponse implements Serializable{
	private static final long serialVersionUID = 2275655193720587417L;
	
	/**
	 * 消息
	 */
	private String msg;
	/**
	 * 状态
	 */
	private Integer status;
	/**
	 * 数据
	 */
	private Object data;
	
	public String getMsg() {
		return msg;
	}

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

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public JsonResponse() {
		
	}
	
	public JsonResponse(String msg, Integer status, Object data) {
		super();
		this.msg = msg;
		this.status = status;
		this.data = data;
	}
	
	public static JsonResponse success(Object data) {
		JsonResponse bean = new JsonResponse();
		bean.setData(data);
		bean.setMsg("成功！");
		bean.setStatus(200);
		return bean;
	}
	
	public static JsonResponse error(String msg) {
		JsonResponse bean = new JsonResponse();
		bean.setMsg("服务出错！");
		bean.setStatus(500);
		return bean;
	}
	
	public static JsonResponse forbidden() {
		JsonResponse bean = new JsonResponse();
		bean.setMsg("没有权限！");
		bean.setStatus(403);
		return bean;
	}
	
	public static JsonResponse forbidden(String msg) {
		JsonResponse bean = new JsonResponse();
		bean.setMsg(msg);
		bean.setStatus(403);
		return bean;
	}

	@Override
	public String toString() {
		try {
			JSONObject json = JsonUtil.toJSONObject(this);
			return json.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return error("json转换错误！").toString();
	}
	
	
}
