package com.smppw.common.pojo.vo;

import com.smppw.common.pojo.enums.status.ResultCode;
import com.smppw.common.pojo.enums.status.StatusCode;
import lombok.Data;

/**
 * @author lzj
 * @param <T>   数据对象类型
 */
@Data
public class ResultVo<T> {
    private int code;
    private String msg;
    private T data;

    /**
     * 手动设置返回vo
     * @param code  状态
     * @param msg   返回信息
     * @param data  数据对象
     */
    public ResultVo(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 默认返回成功状态码，数据对象
     * @param data  数据对象
     */
    public ResultVo(T data) {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
        this.data = data;
    }

    /**
     * 返回指定状态码，数据对象
     * @param statusCode    状态枚举
     * @param data  数据对象
     */
    public ResultVo(StatusCode statusCode, T data) {
        this.code = statusCode.getCode();
        this.msg = statusCode.getMsg();
        this.data = data;
    }

    /**
     * 只返回状态码
     * @param statusCode    状态枚举
     */
    public ResultVo(StatusCode statusCode) {
        this.code = statusCode.getCode();
        this.msg = statusCode.getMsg();
        this.data = null;
    }

    private static <T> ResultVo<T> createResult(int code, String msg, T data) {
        return new ResultVo<>(code, msg, data);
    }

    private static <T> ResultVo<T> createResult(StatusCode statusCode, T data) {
        return createResult(statusCode.getCode(), statusCode.getMsg(), data);
    }

    private static <T> ResultVo<T> createResult(StatusCode statusCode) {
        return createResult(statusCode.getCode(), statusCode.getMsg(), null);
    }

    public static <T> ResultVo<T> ok(T data) {
        return createResult(ResultCode.SUCCESS, data);
    }

    public static <T> ResultVo<T> ok(StatusCode statusCode, T data) {
        return createResult(statusCode, data);
    }

    public static <T> ResultVo<T> ok(int code, String msg, T data) {
        return createResult(code, msg, data);
    }

    public static <T> ResultVo<T> fail() {
        return createResult(ResultCode.COMMON_ERROR);
    }

    public static <T> ResultVo<T> fail(StatusCode statusCode) {
        return createResult(statusCode);
    }

    public static <T> ResultVo<T> fail(int code, String msg) {
        return createResult(code, msg, null);
    }
}
