package com.storlead.framework.common.result;

import com.storlead.framework.common.enums.ErrorMsgCode;
import com.storlead.framework.common.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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 = "成功标志")
	public final static String successStr = "OK";
	/**
	 * 成功标志
	 */
	@ApiModelProperty(value = "成功标志")
	private boolean success = true;

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

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

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

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

	public Result() {
	  this.timestamp = System.currentTimeMillis();
	}

	public T getResult(){
		return result;
	}

	 public void setResult(T result) {
	  this.result = result;
	 }
	 public Result<T> success(String message) {
		this.message = message;
		this.code = ResultCode.SUCCESS.getCode();
		this.success = true;
		this.timestamp = System.currentTimeMillis();
		return this;
	}


	public static Result<Object> ok() {
		Result<Object> r = new Result<Object>();
		r.setSuccess(true);
		r.setCode(ResultCode.SUCCESS.getCode());
		r.setMessage("成功");
		r.setTimestamp(System.currentTimeMillis());
		return r;
	}

	public static <T> Result<T> ok(String msg) {
		Result<T> r = new Result<T>();
		r.setSuccess(true);
		r.setCode(ResultCode.SUCCESS.getCode());
		r.setMessage(msg);
		r.setTimestamp(System.currentTimeMillis());
		return r;
	}

	public static <T> Result<T> ok(T result) {
		Result<T> r = new Result<T>();
		r.setSuccess(true);
		r.setCode(ResultCode.SUCCESS.getCode());
		r.setResult(result);
		r.setTimestamp(System.currentTimeMillis());
		return r;
	}

	public static <T> Result<T> result(T result) {
		Result<T> r = new Result<T>();
		r.setSuccess(true);
		r.setCode(ResultCode.SUCCESS.getCode());
		r.setResult(result);
		r.setTimestamp(System.currentTimeMillis());
		return r;
	}

//	public static <T> Result<T> date(T result) {
//		Result<T> r = new Result<T>();
//		r.setSuccess(true);
//		r.setCode(ResultCode.SC_OK_200.getCode());
//		r.setData(result);
//		r.setTimestamp(System.currentTimeMillis());
//		return r;
//	}

	public static <T> Result<T> error(T errorData) {
		return error(ResultCode.RULES_FAILED.getCode(), errorData);
	}

	public static <T> Result<T> error(T errorData,String msg) {
		return error(ResultCode.RULES_FAILED.getCode(), errorData,msg);
	}

	public static Result<Object> error(String msg) {
		return error(ResultCode.ERROR_500.getCode(), msg);
	}

	public static Result<Object> error(ErrorMsgCode msgCode) {
		return error(msgCode.getCode(), msgCode.getMsg());
	}

	public static Result<Object> error(ResultCode code) {
		return error(code.getCode(), code.getMsg());
	}

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

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

	public static <T>Result<T> error(int code,T errorData) {
		Result<T> r = new Result<T>();
		r.setCode(code);
		r.setMessage("");
		r.setResult(errorData);
		r.setSuccess(false);
		r.setTimestamp(System.currentTimeMillis());
		return r;
	}

	public Result<T> error500(String message) {
		this.message = message;
		this.code = ResultCode.ERROR_500.getCode();
		this.success = false;
		this.timestamp = System.currentTimeMillis();
		return this;
	}
	/**
	 * 无权限访问返回结果
	 */
	public static Result<Object> noauth(String msg) {
		return error(ResultCode.FORBIDDEN.getCode(), msg);
	}

}
