package com.mortals.xhx.common.model;

import java.util.HashMap;
import java.util.Map;

import com.mortals.framework.exception.AppException;

/**
 * 请求结果
 * @author lyb
 *
 * @param <T> 响应结果数据
 */
public class RequestResult<T> {
	/**
	 * 响应码
	 */
	private Integer code;
	/**
	 * 响应码描述
	 */
	private String msg;
	/**
	 * 响应结果数据
	 */
	private T data;
	/**
	 * 字典信息
	 */
	private Map<String, Map<String, String>> dict;
	
	public RequestResult() {
		this(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg());
	}
	
	public RequestResult(ResponseCode code) {
		this(code.getCode(), code.getMsg());
	}
	
	public RequestResult(AppException e) {
		this(e.getCode(), e.getMessage());
	}
	
	public RequestResult(T data) {
		this(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMsg(), data);
	}
	
	public RequestResult(Integer code, String msg) {
		this(code, msg, null);
	}
	
	public RequestResult(Integer code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}
	
	public boolean isSuccess() {
		return ResponseCode.SUCCESS.getCode().equals(this.code);
	}
	
	public boolean isFailure() {
		return !isSuccess();
	}
	
	public void addDict(String name, Map<String, String> dict) {
		if (this.dict == null) {
			this.dict = new HashMap<String, Map<String, String>>();
		}
		this.dict.put(name, dict);
	}
	
	public Integer getCode() {
		return code;
	}

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

	public String getMsg() {
		return msg;
	}

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

	public T getData() {
		return data;
	}

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

	public Map<String, Map<String, String>> getDict() {
		return dict;
	}

	public void setDict(Map<String, Map<String, String>> dict) {
		this.dict = dict;
	}

	public static enum ResponseCode {
		/**
		 * 请求处理成功
		 */
		SUCCESS(1, "请求处理成功"),
		/**
		 * 请求处理失败
		 */
		FAILURE(-1, "请求处理失败");
		/**
		 * 响应码
		 */
		private Integer code;
		/**
		 * 响应码描述
		 */
		private String msg;
		
		private ResponseCode(Integer code, String msg) {
			this.code = code;
			this.msg = msg;
		}
		
		public Integer getCode() {
			return code;
		}
		
		public String getMsg() {
			return msg;
		}
	}
}
