package com.uas.platform.b2b.manage.controller.support;

import java.io.*;

/**
 * 定义controller返回结果标准
 *
 * @author hejq
 * @date 2018-09-07 11:37
 */
public class ResultBean<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 定义返回成功标识
     */
    private static final String SUCCESS = "success";

    /**
     * 成功状态码
     */
    private static final int SUCCESS_CODE = 0;

    /**
     * 失败状态码
     */
    public static final int FAIL_CODE = 1;

    /**
     * 消息
     */
    private String msg = SUCCESS;

    /**
     * 状态码
     */
    private int code = SUCCESS_CODE;

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

    public static String getSUCCESS() {
        return SUCCESS;
    }

    public static int getSuccessCode() {
        return SUCCESS_CODE;
    }

    public static int getFailCode() {
        return FAIL_CODE;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResultBean(int status, T data) {
        if (SUCCESS_CODE == status) {
            this.code = SUCCESS_CODE;
        } else {
            this.code = FAIL_CODE;
        }
        this.code = status;
        this.data = data;
    }

    public ResultBean(int status, T data, String msg) {
        if (SUCCESS_CODE == status) {
            this.code = SUCCESS_CODE;
        } else {
            this.code = FAIL_CODE;
        }
        this.code = status;
        this.data = data;
        this.msg = msg;
    }

    public ResultBean() {
        super();
    }

    public ResultBean(T data) {
        super();
        this.data = data;
    }

    public ResultBean(Throwable e) {
        super();
        this.msg = e.toString();
        this.code = FAIL_CODE;
    }

    /**
     * 操作成功返回结果
     *
     * @param data 数据
     * @return ResultBean
     * @see ResultBean
     */
    public static ResultBean success(Object data) {
        return new ResultBean(SUCCESS_CODE, data);
    }

    /**
     * 操作成功返回结果
     *
     * @return ResultBean
     * @see ResultBean
     */
    public static ResultBean success() {
        return new ResultBean(SUCCESS_CODE, null);
    }

    /**
     * 操作成功返回结果
     *
     * @param data 数据
     * @param msg 信息
     * @return ResultBean
     */
    public static ResultBean success(Object data, String msg) {
        return new ResultBean(SUCCESS_CODE, data, msg);
    }
}
