package com.sunyard.chsm.model;

import lombok.Data;

/**
 * @author liulu
 * @since 2024/10/17
 */
@Data
public class R<T> {

    /**
     * 成功标志
     */
    private boolean success;

    /**
     * 失败消息
     */
    private String message;

    /**
     * 返回代码 200-代表成功，
     */
    private Integer code;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 结果对象
     */
    private T result;


    public static <T> R<T> ok() {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setMessage("success");
        r.setCode(200);
        return r;
    }

    public static <T> R<T> ok(String msg) {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setMessage(msg);
        r.setCode(200);
        return r;
    }

    public static <T> R<T> data(T value) {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setResult(value);
        r.setCode(200);
        return r;
    }

    public static <T> R<T> data(T value, String msg) {
        R<T> r = new R<>();
        r.setSuccess(true);
        r.setResult(value);
        r.setMessage(msg);
        r.setCode(200);
        return r;
    }

    public static <T> R<T> error(String msg) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setMessage(msg);
        r.setCode(400);
        return r;
    }

    public static <T> R<T> error(int code, String msg) {
        R<T> r = new R<>();
        r.setSuccess(false);
        r.setMessage(msg);
        r.setCode(code);
        return r;
    }

}
