package com.fdage.util;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 通用返回类
 *
 * @author
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -1491499610244557029L;
    public static final String SUCCESS_MSG = "操作成功";
    public static int CODE_SUCCESS = 0;
    public static int CODE_FAILURE = -1;
    public static String[] NOOP = new String[]{};
    public static LocalDateTime dataTime = LocalDateTime.now();

    /**
     * 处理状态：0: 成功, 1: 失败
     */
    @ApiModelProperty(value = "处理状态：0: 成功, -1: 失败", name = "code")
    private int code;
    /**
     * 消息
     */
    @ApiModelProperty(value = "消息", name = "msg")
    private String msg;
    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据", name = "data")
    private T data;

    @ApiModelProperty(value = "时间戳", name = "timestamp")
    private LocalDateTime timestamp;
    /**
     * 处理成功，并返回数据
     *
     * @param data 数据对象
     * @return data
     */
    public static Result success(Object data) {
        return new Result(CODE_SUCCESS, SUCCESS_MSG, data, dataTime);
    }
    /**
     * 处理成功
     *
     * @return data
     */
    public static Result success() {
        return new Result(CODE_SUCCESS, SUCCESS_MSG, NOOP, dataTime);
    }
    /**
     * 处理成功
     *
     * @param msg 消息
     * @return data
     */
    public static Result success(String msg) {
        return new Result(CODE_SUCCESS, msg, NOOP, dataTime);
    }
    /**
     * 处理成功
     *
     * @param msg  消息
     * @param data 数据对象
     * @return data
     */
    public static Result success(String msg, Object data) {
        return new Result(CODE_SUCCESS, msg, data, dataTime);
    }
    /**
     * 处理失败，并返回数据（一般为错误信息）
     *
     * @param code 错误代码
     * @param msg  消息
     * @return data
     */
    public static Result failure(int code, String msg) {
        return new Result(code, msg, NOOP , dataTime);
    }
    /**
     * 处理失败
     *
     * @param msg 消息
     * @return data
     */
    public static Result failure(String msg) {
        return failure(CODE_FAILURE, msg);
    }

    @Override
    public String toString() {
        return "JsonResult [code=" + code + ", msg=" + msg + ", data="
                + data + ", timestamp="+ dataTime + "]";
    }
}
