package com.zcxk.core.common.pojo;

import com.zcxk.core.common.enums.RespCodeEnum;
import com.zcxk.core.common.support.RespCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一信息返回处理
 * @author Andy
 * @date 2021-07-22 11:03
 */
@Data
@ApiModel
public class AjaxMessage<T> implements Serializable {

    @ApiModelProperty(value = "返回状态", position = 0)
    private int status;

    @ApiModelProperty(value = "返回信息", position = 1)
    private String message;

    @ApiModelProperty(value = "返回内容", position = 2)
    private T data;

    private AjaxMessage(RespCode respCode) {
        this.status = respCode.getStatus();
        this.message = respCode.getMessage();
    }

    private AjaxMessage(int status, String message) {
        this.status = status;
        this.message = message;
        this.data = null;
    }

    public AjaxMessage(int status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public AjaxMessage(RespCode resultStatus, T data) {
        this.status = resultStatus.getStatus();
        this.message = resultStatus.getMessage();
        this.data = data;
    }

    public void setMsg(int status, String msg, T data) {
        this.status = status;
        this.message = msg;
        this.data = data;
    }

    public void setMsg(int status, String msg) {
        this.status = status;
        this.message = msg;
        this.data = null;
    }

    public void setMsg(RespCode resultStatus, T data) {
        this.status = resultStatus.getStatus();
        this.message = resultStatus.getMessage();
        this.data = data;
    }

    public void setMsg(RespCode resultStatus) {
        this.status = resultStatus.getStatus();
        this.message = resultStatus.getMessage();
        this.data = null;
    }

    public static AjaxMessage<Void> fail(int code, String message) {
        return new AjaxMessage<>(code, message);
    }

    public static AjaxMessage<Void> fail(RespCode respCode) {
        return fail(respCode.getStatus(), respCode.getMessage());
    }


    public static <T> AjaxMessage<T> success(T data) {
        AjaxMessage<T> responseBean = new AjaxMessage<>(RespCodeEnum.OK);
        responseBean.setData(data);
        return responseBean;
    }

    public static <T> AjaxMessage<T> success(RespCode respCode, T data) {
        AjaxMessage<T> responseBean = new AjaxMessage<>(respCode);
        responseBean.setData(data);
        return responseBean;
    }

    public static AjaxMessage<Void> success() {
        return new AjaxMessage<>(RespCodeEnum.OK);
    }


}
