package cn.hanghui.outapi.platform.common.entity.rest;

import cn.hanghui.outapi.platform.common.exception.CommonException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class Result<T> {

    @ApiModelProperty(value = "返回码，0为成功")
    private String errcode;

    @ApiModelProperty(value = "返回信息")
    private String errmsg;

    private T data;

    public Result(String code, String msg) {
        this.errcode = code;
        this.errmsg = msg;
    }

    public Result(String code, String msg, T data) {
        this.errcode = code;
        this.errmsg = msg;
        this.data = data;
    }

    public Result(CommonException e) {
        this.errcode = e.getErrCode();
        this.errmsg = e.getErrMsg();
    }

    public static Result<Void> ok() {
        return Result.ok(RespCodeEnum.SUCCESS.code, null);
    }

    public static <T> Result<T> ok(T data) {
        return Result.ok(RespCodeEnum.SUCCESS.msg, data);
    }

    public static <T> Result<T> ok(String msg, T data) {
        return new Result<>(RespCodeEnum.SUCCESS.code, msg, data);
    }

    public static Result<Void> error() {
        return Result.error(RespCodeEnum.SYS_ERROR.code, RespCodeEnum.SYS_ERROR.msg);
    }

    public static Result<Void> error(CommonException e) {
        return new Result<>(e.getErrCode(), e.getErrMsg());
    }

    public static Result<Void> error(String msg) {
        return Result.error(RespCodeEnum.SYS_ERROR.code, msg);
    }


    public static <T> Result<T> error(String msg, T data) {
        return new Result<>(RespCodeEnum.SYS_ERROR.code, msg, data);
    }

    public static Result<Void> error(String code, String msg) {
        return new Result<>(code, msg);
    }

}
