package com.lutao.cloud.common.core.util;

import cn.hutool.json.JSONObject;
import com.lutao.cloud.common.core.constant.CommonConstants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>

 */
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "响应信息主体")
public class MessageInfo<T> implements Serializable{

	private static final long serialVersionUID = -8703188330984075284L;

	@Getter
	@Setter
	@ApiModelProperty(value = "返回标记：成功标记=200，失败标记=1")
	private int code;

	@Getter
	@Setter
	@ApiModelProperty(value = "返回信息")
	private String msg;

	@Getter
	@Setter
	@ApiModelProperty(value = "数据")
	private T data;

	public static <T> MessageInfo<T> ok(){
		return restResult(null, CommonConstants.SUCCESS, "success");
	}

	public static <T> MessageInfo<T> ok(T data){
		return restResult(data, CommonConstants.SUCCESS, "success");
	}

	public static <T> MessageInfo<T> ok(String data){
		JSONObject jsonObject = new JSONObject();
		jsonObject.putOnce("result",data);
		return (MessageInfo<T>) restResult(jsonObject, CommonConstants.SUCCESS, "success");
	}

	public static <T> MessageInfo<T> ok(T data, String msg){
		return restResult(data, CommonConstants.SUCCESS, msg);
	}

	public static <T> MessageInfo<T> failed(){
		return restResult(null, CommonConstants.FAIL, null);
	}

	public static <T> MessageInfo<T> failed(String msg){
		return restResult(null, CommonConstants.FAIL, msg);
	}

	public static <T> MessageInfo<T> failed(T data){
		return restResult(data, CommonConstants.FAIL, null);
	}

	public static <T> MessageInfo<T> failed(T data, String msg){
		return restResult(data, CommonConstants.FAIL, msg);
	}

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

}
