package com.ema.common.domain.vo;

import cn.hutool.core.util.StrUtil;
import com.ema.common.exceptions.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应结果对象
 */
@ApiModel(description = "ResultVo")
@Data
@NoArgsConstructor
public class ResultVo<T> implements Serializable {

    @ApiModelProperty(value = "响应码")
    private Integer code;

    @ApiModelProperty(value = "响应信息")
    private String message;

    @ApiModelProperty(value = "响应数据")
    private T data;

    public ResultVo(Integer code, String message, T data) {
        super();
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ResultVo<T> success() {
        return success(null, null);
    }

    public static <T> ResultVo<T> success(T data) {
        return success(null, data);
    }

    public static <T> ResultVo<T> success(String message, T data) {
        return new ResultVo<>(ResultEnum.SUCCESS.getCode(),
                message != null ? message : ResultEnum.SUCCESS.getMessage(), data);
    }

    public static <T> ResultVo<T> failure() {
        return failure(ResultEnum.FAILURE);
    }

    public static <T> ResultVo<T> failure(String message) {
        return failure(ResultEnum.FAILURE, message);
    }
    public static <T> ResultVo<T> failure(String message, T data) {
        return failure(ResultEnum.FAILURE, message, data);
    }

    public static <T> ResultVo<T> failure(ResultEnum resultEnum) {
        return failure(resultEnum, resultEnum.getMessage(), null);
    }

    public static <T> ResultVo<T> failure(ResultEnum resultEnum, String message) {
        return failure(resultEnum, message, null);
    }

    public static <T> ResultVo<T> failure(ResultEnum resultEnum, String message, T data) {
        return new ResultVo<>(resultEnum.getCode(), StrUtil.isNotBlank(message) ? message : resultEnum.getMessage(), data);
    }

    public static <T> ResultVo<T> failure(Integer code, String message) {
        return new ResultVo<>(code, message, null);
    }

    public static <T> ResultVo<T> isSuccess(boolean bool, String failureMsg) {
        return isSuccess(bool, null, failureMsg);
    }

    public static <T> ResultVo<T> isSuccess(boolean bool) {
        return isSuccess(bool, null, null);
    }

    public static <T> ResultVo<T> isSuccess(boolean bool, String successMsg, String failureMsg) {
        return bool ? success(successMsg,null) : failure(failureMsg);
    }

}
