package com.playlet.common.core.domain;

import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "统一返回参数")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1084094914898698049L;
    private static final String DEFAULT_ERROR_CODE = "999999";
    private static final String DEFAULT_SUCCESS_CODE = "100000";
    @ApiModelProperty("请求编码，code为100000代表的是成功，其他都是失败！")
    private String code;
    @ApiModelProperty("返回的提示信息")
    private String msg;
    @ApiModelProperty("true 代表的是返回成功，false代表的是返回失败！")
    private boolean success;
    private T data;
    @ApiModelProperty("请求的时间戳")
    private String timestamp;

    private Result() {
    }

    private Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.timestamp = DateUtil.now();
    }

    public static <T> Result<T> error() {
        return error("999999", "未知异常，请联系管理员");
    }

    public static <T> Result<T> error(String msg) {
        return error("999999", msg);
    }

    public static <T> Result<T> error(String code, String msg) {
        Result result = new Result(code, msg, (Object)null);
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> error(String code, T data, String msg) {
        Result result = new Result(code, msg, data);
        result.setSuccess(false);
        return result;
    }

    public static <T> Result<T> success() {
        return success(null, "操作成功！");
    }

    public static <T> Result<T> success(T data) {
        return success(data, "操作成功！");
    }

    public static <T> Result<T> success(String msg) {
        return success(null, msg);
    }

    public static <T> Result<T> success(T data, String msg) {
        Result result = new Result("100000", msg, data);
        result.setSuccess(true);
        return result;
    }

}
