package com.jkcredit.identity.model.vo;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author cislc
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class ApiResponse<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	@Getter
	@Setter
	private int code = 9999;

	@Getter
	@Setter
	private String msg;


	@Getter
	@Setter
	private T data;

	public static <T> ApiResponse<T> ok() {
		return restResult(null, 0, null);
	}

	public static <T> ApiResponse<T> ok(T data) {
		return restResult(data, 0, "success");
	}

	public static <T> ApiResponse<T> failed() {
		return restResult(null, 1, "failed");
	}

	public static <T> ApiResponse<T> failed(int code ,String msg) {
		return restResult(null, code, msg);
	}


	private static <T> ApiResponse<T> restResult(T data, int code, String msg) {
		ApiResponse<T> apiResult = new ApiResponse<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}
}
