package com.mortals.xhx.module.api.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mortals.xhx.module.api.RspCode;
import io.swagger.annotations.ApiModel;

import java.io.Serializable;

@ApiModel
public class BaseRsp<D extends Object> implements Serializable {

	private static final long serialVersionUID = -308663256626340163L;
	/** 结果编码 0代表成功,其它代表失败,具体参看结果编码章节 */
	private int code;
	/** 结果描述 业务失败原因 */
	private String msg;
	/** 业务数据 */
	private D data;

	public BaseRsp() {
		super();
	}

	public BaseRsp(int code, String msg, D data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public static <D extends Object> BaseRsp<D> buildSuccRsp(D d) {
		return new BaseRsp<D>(RspCode.SUCCESS.getValue(), null, d);
	}

	public static <D extends Object> BaseRsp<D> buildSuccRsp() {
		return new BaseRsp<D>(RspCode.SUCCESS.getValue(), null, null);
	}

	public static <D extends Object> BaseRsp<D> buildFailRsp(int code, String msg) {
		return new BaseRsp<D>(code, msg, null);
	}

	@JSONField(serialize = false)
	@JsonIgnore
	public boolean isSucc() {
		return RspCode.SUCCESS.getValue() == this.code;
	}

	@JSONField(serialize = false) // 使用fastjson时有效
	@JsonIgnore // 使用jackson时有效
	public boolean isFail() {
		return !isSucc();
	}

	public int getCode() {
		return code;
	}

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

	public String getMsg() {
		return msg;
	}

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

	public D getData() {
		return data;
	}

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

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