package com.platform.plugs.common.rpc;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author TONG
 * @Date 2019/2/22 15:06
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RpcResponse<T> {

    public static String ERROR_STATUS = "500";

    public static String SUCCESS_STATUS = "200";

    private String status = SUCCESS_STATUS;

    private ResponseResult result = ResponseResult.SUCCESS;

    private String data;

    private String errorCode;

    private String errorMsg;

    public RpcResponse(String status, ResponseResult result, String errorCode, String errorMsg) {
        this.status = status;
        this.result = result;
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public RpcResponse(String status, ResponseResult result, String data) {
        this.status = status;
        this.result = result;
        this.data = data;
    }

    public String getErrorMsg() {
        if(errorCode == null || errorCode.trim().length() <= 0)
            return errorMsg;
        if(ErrorCode.BussinessError.toString().equals(errorCode))
            return errorMsg;
        else {
            String result = ErrorCodeMsg.getErrorMsg(errorCode);
            if(result != null && result.trim().length() > 0)
                return result;
            return errorMsg;
        }
    }

    private T response;

    public RpcResponse(String status, ResponseResult result) {
        this.status = status;
        this.result = result;
    }

    public T getResponse() {
        return response;
    }
}
