Result.java 820 Bytes
package com.xly.erp.common.response;

import lombok.Getter;

/**
 * 统一响应包装。
 * docs/04 § 1.3。
 */
@Getter
public class Result<T> {
    private final int code;
    private final String message;
    private final T data;
    private final long timestamp;

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(ErrorCode.OK, "操作成功", data);
    }

    public static Result<Void> ok() {
        return new Result<>(ErrorCode.OK, "操作成功", null);
    }

    public static <T> Result<T> fail(int code, String message) {
        return new Result<>(code, message, null);
    }
}