package com.mortals.xhx.module.api.feign.portal;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.ToString;

import java.io.Serializable;
import java.util.Collections;
import java.util.Map;

@ToString
public  class ApiResponse<T> implements Serializable{

	private static final long serialVersionUID = 9068810954983382980L;

	private int code;

	private String message;

	private T data;
	// 字典数据
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private Map<String, Object> dict;
		
	public static ApiResponse success(Object data, Map<String, Object> dict) {
		ApiResponse r = success(data);
		r.setDict(dict);
		return  r;
	}

	public static ApiResponse fail(String msg) {
		ApiResponse r = newResp(-1, msg, null);
		return  r;
	}

	public static ApiResponse success() {
		return success(Collections.emptyMap());
	}

	public static ApiResponse success(Object data) {
		return newResp(ApiResponseCode.SUCCESS, data);
	}
	
	public static ApiResponse newResp(ApiResponseCode code, Object data) {
		return newResp(code.getCode(), code.getDesc(), data);
	}
	
	public static ApiResponse newResp(int code, String message, Object data) {
		return new ApiResponse(code, message, data);
	}

	public ApiResponse(){}

	public ApiResponse(int code, String message, T data) {
		super();
		this.code = code;
		this.message = message;
		this.data = data;
	}

	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 T getData() {
		return data;
	}

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

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

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

	public String getMsg() {
		return message;
	}

 	public boolean isSuccess() {
		return code == ApiResponseCode.SUCCESS.getCode();
	}

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