package com.platform.entity;


import lombok.Data;

import java.io.Serializable;

/**
 * 通用返回类
 *
 * @author
 */
@Data
public class Result implements Serializable {
    private static final long serialVersionUID = -1491499610244557029L;
    public static final String SUCCESS_MSG = "操作成功";
    public static Integer CODE_SUCCESS = 0;
    public static Integer CODE_FAILURE = -1;
    public static String[] NOOP = new String[]{};

    /**
     * 处理状态：0: 成功, 1: 失败
     */
    private Integer code;
    /**
     * 消息
     */
    private String msg;
    /**
     * 返回数据
     */

    private Object data;

    public Result(){

    }

    public Result(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 处理成功，并返回数据
     *
     * @param data 数据对象
     * @return data
     */
    public static Result success(Object data) {
        return new Result(CODE_SUCCESS, SUCCESS_MSG, data);
    }
    /**
     * 处理成功
     *
     * @return data
     */
    public static Result success() {
        return new Result(CODE_SUCCESS, SUCCESS_MSG, NOOP);
    }
    /**
     * 处理成功
     *
     * @param msg 消息
     * @return data
     */
    public static Result success(String msg) {
        return new Result(CODE_SUCCESS, msg, NOOP);
    }
    /**
     * 处理成功
     *
     * @param msg  消息
     * @param data 数据对象
     * @return data
     */
    public static Result success(String msg, Object data) {
        return new Result(CODE_SUCCESS, msg, data);
    }
    /**
     * 处理失败，并返回数据（一般为错误信息）
     *
     * @param code 错误代码
     * @param msg  消息
     * @return data
     */
    public static Result failure(Integer code, String msg) {
        return new Result(code, msg, NOOP);
    }
    /**
     * 处理失败
     *
     * @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 + "]";
    }
}
