package com.zhgd.jeecg.common.api.vo;

import com.zhgd.jeecg.common.constant.CommonConstant;
import com.zhgd.xmgl.util.MessageUtil;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String message = MessageUtil.get("optSucess");

    /**
     * 返回代码
     */
    private Integer code = 200;

    /**
     * 返回数据对象 data
     */
    private T result;

    public Result() {

    }


    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public void error500(String message) {
        this.message = message;
        this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
    }

    public Result<T> successMsg(String message) {
        this.message = message;
        this.code = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }

    public static Result<Object> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }

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

    public static Result<Object> ok(String msg) {
        Result<Object> r = new Result<>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        return r;
    }


    public static <Y> Result<Y> success(Y data, String msg) {
        Result<Y> r = new Result<>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setResult(data);
        r.setMessage(msg);
        return r;
    }

    public static <Y> Result<Y> success(Y data) {
        Result<Y> r = new Result<>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setResult(data);
        return r;
    }

    public Result code(int code) {
        this.code = code;
        return this;
    }

    public Result msg(String message) {
        this.message = message;
        return this;
    }

    public Result data(T data) {
        this.result = data;
        return this;
    }

    public static Result ok() {
        return new Result().code(CommonConstant.SC_OK_200).msg(MessageUtil.get("optSucess"));
    }


}
