package com.sckw.payment.api.model.dto.common;

import lombok.*;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author xcq
 * @date 2022年06月14日 13:41
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = -2087972616778698265L;
    /**
     * 1-成功 0-失败
     */
    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private T data;
    @Getter
    @Setter
    private Boolean status;

    public static <T> R<T> ok() {
        return restResult(null, 1, null, true);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, 1, null, true);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, 1, msg, true);
    }

    public static <T> R<T> failed() {
        return restResult(null, 0, null, false);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, 0, msg, false);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, 0, null, false);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, 0, msg, false);
    }

    public static <T> R<T> failed(T data, String msg, int code) {
        return restResult(data, code, msg, false);
    }

    private static <T> R<T> restResult(T data, int code, String msg, Boolean status) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setStatus(status);
        return apiResult;
    }
}