package com.cool.store.response;

import com.cool.store.constants.CommonConstants;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.ResponseCodeEnum;
import lombok.Data;
import lombok.ToString;
import org.slf4j.MDC;

import java.text.MessageFormat;

/**
 * 统一返回结果
 *
 * @author Aaron
 * @date 2019/12/20
 */
@Data
@ToString
public class ResponseResult<T> implements Result {
    private static final long serialVersionUID = -2217360460304088285L;

    public ResponseResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.requestId = MDC.get(CommonConstants.REQUEST_ID);
    }

    public ResponseResult(int code, String message, T data, String stackTrace) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.stackTrace = stackTrace;
        this.requestId = MDC.get(CommonConstants.REQUEST_ID);
    }

    public ResponseResult(int code, String message) {
        this.code = code;
        this.message = message;
        this.requestId = MDC.get(CommonConstants.REQUEST_ID);
    }

    /**
     * 返回码
     */
    private int code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 异常堆栈信息
     */
    private String stackTrace;

    private String requestId;

    public long getSystemCurrentTime() {
        return System.currentTimeMillis();
    }

    public static<T> ResponseResult<T> success(T data) {
        return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode(), "操作成功", data);
    }

    public static ResponseResult success() {
        return new ResponseResult(ResponseCodeEnum.SUCCESS.getCode(), "操作成功");
    }

    public static ResponseResult fail(int code, String msg) {
        return new ResponseResult(code, msg, false);
    }

    public static ResponseResult fail(int code, String msg, String stackTrace) {
        return new ResponseResult(code, msg, false, stackTrace);
    }

    public static ResponseResult fail(ErrorCodeEnum responseEnum){
        return new ResponseResult(responseEnum.getCode(), responseEnum.getMessage(), false);
    }

    public static ResponseResult fail(ErrorCodeEnum responseEnum, Object... objects){
        String message = MessageFormat.format(responseEnum.getMessage(), objects);
        return new ResponseResult(responseEnum.getCode(), message, false);
    }

}
