package com.changhu.common.pojo.model;

import com.changhu.common.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * author: luozhun
 * desc: JsonResult
 * createTime: 2023/8/16 17:29
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class JsonResult<T> implements java.io.Serializable {

    /**
     * 响应编码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    public JsonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonResult(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    /**
     * 成功信息
     */
    public static <O> JsonResult<O> success() {
        return new JsonResult<>(ResultCode.SUCCESS, null);
    }

    public static <O> JsonResult<O> successMsg(String message) {
        return new JsonResult<>(ResultCode.SUCCESS.getCode(), message);
    }

    public static <O> JsonResult<O> success(O data) {
        return new JsonResult<>(ResultCode.SUCCESS, data);
    }

    /**
     * 操作失败
     */
    public static <O> JsonResult<O> error() {
        return new JsonResult<>(ResultCode.ERROR, null);
    }

    public static <O> JsonResult<O> errorMsg(String message) {
        return new JsonResult<>(ResultCode.ERROR.getCode(), message);
    }

    public static <O> JsonResult<O> error(O data) {
        return new JsonResult<>(ResultCode.ERROR, data);
    }

    /**
     * 警告信息
     */
    public static <O> JsonResult<O> warn() {
        return new JsonResult<>(ResultCode.WARN, null);
    }

    public static <O> JsonResult<O> warnMsg(String message) {
        return new JsonResult<>(ResultCode.WARN.getCode(), message);
    }

    public static <O> JsonResult<O> warn(O data) {
        return new JsonResult<>(ResultCode.WARN, data);
    }

    /**
     * 自定义信息
     */
    public static <O> JsonResult<O> custom(Integer code, String message, O data) {
        return new JsonResult<>(code, message, data);
    }

    public static <O> JsonResult<O> custom(Integer code, String message) {
        return new JsonResult<>(code, message, null);
    }

    public static <O> JsonResult<O> custom(ResultCode resultCode) {
        return new JsonResult<>(resultCode, null);
    }

    public static <O> JsonResult<O> custom(ResultCode resultCode, O data) {
        return new JsonResult<>(resultCode, data);
    }

    public static <O> JsonResult<O> custom(Boolean result) {
        return new JsonResult<>(result ? ResultCode.SUCCESS : ResultCode.ERROR, null);
    }

    public static <O> JsonResult<O> custom(Integer count) {
        return new JsonResult<>(count > 0 ? ResultCode.SUCCESS : ResultCode.ERROR, null);
    }
}
