package com.jy.framework.model.base;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.Setter;


/**
 * 统一返回结果的类
 */
@Getter
@Setter
public class R {

    /**
     * 是否成功
     */
    private Boolean success;

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

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

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

    private R(Boolean success, Integer code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    private R(Boolean success, Integer code, String message, Object data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static R success() {
        return new R(true, HttpServletResponse.SC_OK, "成功");
    }

    public static R success(Object data) {
        return new R(true, HttpServletResponse.SC_OK, "成功", data);
    }

    public static R fail(String errorMsg, Object... params) {
        return new R(false, HttpServletResponse.SC_BAD_REQUEST, StrUtil.format(errorMsg, params));
    }

    public static R fail(Integer errorCode, String errorMsg, Object... params) {
        return new R(false, errorCode, StrUtil.format(errorMsg, params));
    }

}
