package com.blueland.common.core.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author bigtian
 * @Description 返回对象封装类
 * @Date 2021/7/23 2:03 下午
 */
@Data
@ApiModel(value = "统一返回对象")
public class AjaxResult<T> {

    @ApiModelProperty("返回信息")
    private String resultMsg;
    @ApiModelProperty("返回码")
    private int resultCode;
    @ApiModelProperty("返回数据")
    private T result;


    public static AjaxResult success(String msg) {
        AjaxResult result = new AjaxResult();
        result.setResultCode(200);
        result.setResultMsg(msg);
        return result;
    }

    public static AjaxResult success(Object data, String msg) {
        AjaxResult result = new AjaxResult();
        result.setResultCode(400);
        result.setResultMsg(msg);
        result.setResult(data);
        return result;
    }

    public static AjaxResult success() {
        AjaxResult result = new AjaxResult();
        result.setResultCode(200);
        result.setResultMsg("操作成功");
        return result;
    }


    public static AjaxResult success(Object data) {
        AjaxResult result = new AjaxResult();
        result.setResultCode(200);
        result.setResultMsg("操作成功");
        result.setResult(data);
        return result;
    }

    public static AjaxResult failed(Object data) {
        AjaxResult result = new AjaxResult();
        result.setResultCode(400);
        result.setResultMsg("操作失败");
        result.setResult(data);
        return result;
    }

    public static AjaxResult failed() {
        AjaxResult result = new AjaxResult();
        result.setResultCode(400);
        result.setResultMsg("操作失败");
        return result;
    }

    public static AjaxResult failed(String msg) {
        AjaxResult result = new AjaxResult();
        result.setResultCode(400);
        result.setResultMsg(msg);
        return result;
    }

    public static AjaxResult failed(int code, String msg) {
        AjaxResult result = new AjaxResult();
        result.setResultCode(code);
        result.setResultMsg(msg);
        return result;
    }

    public static AjaxResult success(int code, String msg) {
        AjaxResult result = new AjaxResult();
        result.setResultCode(code);
        result.setResultMsg(msg);
        return result;
    }


    public static AjaxResult operates(Boolean flag) {
        if (flag) {
            return success();
        }
        return failed();
    }

    public static AjaxResult operates(Boolean flag, String msg, Object data) {
        return flag ? success(data, msg) : failed(msg);
    }

    public static AjaxResult operates(int rows) {
        if (rows > 0) {
            return success();
        }
        return failed();
    }
}

