package com.kym.common;

import com.kym.common.constant.ResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.MDC;

import java.io.Serializable;

/**
 * 返回信息封装
 *
 * @param <T>
 * @author skyline
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private Integer code;
    private String message;
    private T data;
    private String tid;

    public R(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.tid = MDC.get("seq");
    }

    public R(Integer code, String message,T data) {
        this.code = code;
        this.message = message;
        this.data  = data;
        this.tid = MDC.get("seq");
    }

    public static R success() {
        return new R<>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage());
    }

    public static <T> R<T> success(T data) {
        return new R<T>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMessage(), data);
    }

    public static R failed() {
        return new R<>(ResponseEnum.FAILED.getCode(), ResponseEnum.FAILED.getMessage());
    }

    public static R failed(Integer code, String message) {
        return new R<>(code, message);
    }

    public static R failed(ResponseEnum responseEnum) {
        return new R<>(responseEnum.getCode(), responseEnum.getMessage());
    }

}
