package cn.sh.stc.sict.cloud.common.core.util;

import cn.sh.stc.sict.cloud.common.core.constant.Constant;
import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serializable;

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

	@Getter
	@Setter
	private byte code = Constant.BYTE_YES;

	@Getter
	@Setter
	private Integer bizCode = Constant.INT_YES;

	@Getter
	@Setter
	private String msg = "操作成功";


	@Getter
	@Setter
	private T data;

	public R() {
		super();
	}

	public R(T data) {
		super();
		this.data = data;
	}

	public R(T data, String msg) {
		super();
		this.data = data;
		this.msg = msg;
	}

	public R success(T data, String msg) {
		return R.builder().code(Constant.BYTE_YES).data(data).msg(msg).build();
	}

	public R success(T data) {
		return R.builder().code(Constant.BYTE_YES).data(data).msg("操作成功").build();
	}

	public R error(String msg) {
		return this.error(null, msg);
	}

	public R error(T data, String msg) {
		return R.builder().code(Constant.BYTE_NO).data(data).msg("操作失败：" + msg).build();
	}

	public R(Throwable e) {
		super();
		this.msg = e.getMessage();
		this.code = Constant.BYTE_NO;
	}
}
