package com.zhiwei.manage.bean;

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

/**
 * Json 统一返回消息类
 *
 * @author zzm
 */
public class JsonResult implements Serializable {
	private static final long serialVersionUID = -1491499610244557029L;
	private static List<String> NOOP = new ArrayList<>();

	private int code; // 处理状态：0: 成功
	private String message;
	private Object data; // 返回数据
	private boolean state;

	public JsonResult(boolean state, int code, String message, Object data) {
		this.state = state;
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public JsonResult(boolean state, int code, String message) {
		this.state = state;
		this.code = code;
		this.message = message;
	}
	/**
	 * 处理成功，并返回数据
	 *
	 * @param data
	 *            数据对象
	 * @return data
	 */
	public static JsonResult success(Object data) {
		return new JsonResult(true, 200, "操作成功", data);
	}

	/**
	 * 处理成功
	 *
	 * @param message
	 *            消息
	 * @return data
	 */
	public static JsonResult success(String message) {
		return new JsonResult(true, 200, message, NOOP);
	}

	public static JsonResult failure(int code, String message, Object data) {
		return new JsonResult(false, code, message, data);
	}

	/**
	 * 处理失败，并返回数据（一般为错误信息）
	 *
	 * @param code
	 *            错误代码
	 * @param message
	 *            消息
	 * @return data
	 */
	public static JsonResult failure(int code, String message) {
		return failure(code, message, NOOP);
	}

	/**
	 * 处理失败
	 *
	 * @param message
	 *            消息
	 * @return data
	 */
	public static JsonResult failure(String message) {
		return failure(210, message);
	}

	public static JsonResult failure() {
		return failure("操作失败");
	}

	/**
	 * 系统异常
	 *
	 * @param code
	 *            错误代码
	 * @param message
	 *            消息
	 * @return data
	 */
	public static JsonResult error(int code, String message) {
		return failure(code, message, NOOP);
	}

	/**
	 * 系统异常
	 *
	 * @param message
	 *            消息
	 * @return data
	 */
	public static JsonResult error(String message) {
		return failure(210, message);
	}

	public int getCode() {
		return code;
	}

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

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getData() {
		return data;
	}

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

	public boolean isState() {
		return state;
	}

	public void setState(boolean state) {
		this.state = state;
	}

	@Override
	public String toString() {
		return "JsonResult [code=" + code + ", message=" + message + ", data=" + data + "]";
	}

}
