AjaxResultWCS.java 3.49 KB
package com.huaheng.framework.web.domain;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

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

    public int code;

    private String message;

    private String errorCode;

    private T data;

    private String msg;



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

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

    public int getCode() {
        return code;
    }

    public AjaxResultWCS<T> setCode(int code) {
        this.code = code;
        return this;
    }
    public String getMsg() {
        return msg;
    }

    public AjaxResultWCS<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public AjaxResultWCS<T> setMessage(String msg) {
        this.message = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public AjaxResultWCS<T> setData(T data) {
        this.data = data;
        return this;
    }

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

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

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

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

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

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

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

    /**
     * 返回失败消息
     */
    public static <T> AjaxResultWCS<T> error(String message, T data)
    {
        return new AjaxResultWCS<T>().setCode(RetCode.FAIL).setMessage(message).setData(data);
    }

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

    /**
     * 返回消息
     */
    public static <T> AjaxResultWCS<T> setResult(RetCode retCode, String message, T data)
    {
        return new AjaxResultWCS<T>().setCode(retCode).setMessage(message).setData(data);
    }

    /**
     * 返回响应码和消息
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public  static <T> AjaxResultWCS<T> setResultCS(int code, String message)
    {
        return new AjaxResultWCS<T>().setCode(code).setMessage(message);
    }

}