AjaxResultTv.java 2.77 KB
package com.huaheng.framework.web.domain;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 操作消息提醒
 *
 * @author huaheng
 */
@Data
public class AjaxResultTv<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    @JSONField(name = "Code")
    public int Code;
    @JSONField(name = "Message")
    private String Message;
    @JSONField(name = "Result")
    private T Result;

    public Boolean hasErr(){
        if(Code == RetCode.SUCCESS.getValue()){
            return false;
        }else {
            return true;
        }
    }

    public AjaxResultTv<T> setCode(RetCode retCode) {
        this.Code = retCode.getValue();
        return this;
    }




    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    /**
     * 响应返回结果
     *
     * @param succeed 是否成功
     * @return 操作结果
     */
    public static AjaxResultTv toAjax(boolean succeed)
    {
        return succeed ? success("成功") : error("失败");
    }

    /**
     * 返回成功消息
     */
    public static AjaxResultTv success(String message)
    {
        AjaxResultTv ajaxResultTv= new AjaxResultTv<>();
        ajaxResultTv.setCode(RetCode.SUCCESS).setMessage(message);
        return  ajaxResultTv;
    }

    /**
     * 返回成功消息
     */
    public static AjaxResultTv success()
    {
        AjaxResultTv ajaxResultTv= new AjaxResultTv<>();
        ajaxResultTv.setCode(RetCode.SUCCESS).setMessage("成功");
        return  ajaxResultTv;
    }

    /**
     * 返回成功消息
     */
    public static <T> AjaxResultTv<T> success(T data)
    {
        AjaxResultTv ajaxResultTv= new AjaxResultTv<T>();
        ajaxResultTv.setCode(RetCode.SUCCESS).setMessage("成功");
        ajaxResultTv.setResult(data);
        return  ajaxResultTv;
    }

    /**
     * 返回成功消息
     */
    public static <T> AjaxResultTv<T> success(String message, T data)
    {
        AjaxResultTv ajaxResultTv= new AjaxResultTv<T>();
        ajaxResultTv.setCode(RetCode.SUCCESS).setMessage(message);
        ajaxResultTv.setResult(data);
        return  ajaxResultTv;
    }

    /**
     * 返回失败消息
     */
    public static AjaxResultTv error(String message)
    {
        AjaxResultTv ajaxResultTv= new AjaxResultTv<>();
        ajaxResultTv.setCode(RetCode.SUCCESS).setMessage(message);
        return  ajaxResultTv;
    }



    /**
     * 返回失败消息
     */
    public static <T> AjaxResultTv<T> error()
    {
        AjaxResultTv ajaxResultTv= new AjaxResultTv<T>();
        ajaxResultTv.setCode(RetCode.FAIL).setMessage("失败");
        return  ajaxResultTv;
    }


}