package com.gci.boot.common.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 成功标志
	 */
	@ApiModelProperty(value = "成功标志")
	private boolean success = true;

	/**
	 * 返回处理消息
	 */
	@ApiModelProperty(value = "返回处理消息")
	private String message = "操作成功！";

	/**
	 * 返回代码
	 */
	@ApiModelProperty(value = "返回代码")
	private Integer code = 0;

	/**
	 * 返回数据对象 data
	 */
	@ApiModelProperty(value = "返回数据对象")
	private T result;

	/**
	 * 时间戳
	 */
	@ApiModelProperty(value = "时间戳")
	private long timestamp = System.currentTimeMillis();

	public Result() {

	}

	public Result<T> success(String message) {
		this.message = message;
		this.code = HttpStatus.OK.value();
		this.success = true;
		return this;
	}

	public static <T> Result<T> ok() {
		Result<T> r = new Result<>();
		r.setSuccess(true);
		r.setCode(HttpStatus.OK.value());
		r.setMessage("成功");
		return r;
	}

	public static <T> Result<T> ok(String msg) {
		Result<T> r = new Result<>();
		r.setSuccess(true);
		r.setCode(HttpStatus.OK.value());
		r.setMessage(msg);
		return r;
	}

	public static <T> Result<T> ok(T data) {
		Result<T> r = new Result<>();
		r.setSuccess(true);
		r.setCode(HttpStatus.OK.value());
		r.setResult(data);
		return r;
	}

	public static <T> Result<T> error(String msg) {
		return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}

	public static <T> Result<T> error(int code, String msg) {
		Result<T> r = new Result<>();
		r.setCode(code);
		r.setMessage(msg);
		r.setSuccess(false);
		return r;
	}

	public Result<T> error500(String message) {
		this.message = message;
		this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
		this.success = false;
		return this;
	}

	/**
	 * 无权限访问返回结果
	 */
	public static <T> Result<T> noauth(String msg) {
		return error(HttpStatus.NON_AUTHORITATIVE_INFORMATION.value(), msg);
	}

	/**
	 * 无权限访问返回结果
	 */
	public static <T> Result<T> unauthorized(String msg) {
		return error(HttpStatus.UNAUTHORIZED.value(), msg);
	}
}