package com.yccrc.common.vo;

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

import java.io.Serializable;

import com.yccrc.common.constant.HttpStatus;
import com.yccrc.common.enums.ResultCode;


/**
 * 响应数据(不用AjaxResult的原因是方便swagger Models中查看)
 *
 * @author chuanglx
 */
@ApiModel(value = "响应")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 编码：0表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：200表示成功，其他值表示失败")
    private int code = HttpStatus.SUCCESS;
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String msg = "success";
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public Result<T> ok(T data) {
        this.setData(data);
        return this;
    }

    public boolean success(){
        return code == HttpStatus.SUCCESS ? true : false;
    }

    public Result<T> error() {
        this.code =  ResultCode.ERROR.code();
        this.msg = ResultCode.ERROR.message();
        return this;
    }

    public Result<T> error(ResultCode error) {
        this.code = error.code();
        this.msg = error.message();
        return this;
    }
    
    public Result<T> error(ResultCode error,String msg) {
        this.code = error.code();
        this.msg = msg;
        return this;
    }

    public Result<T> error(int code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> error(String msg) {
        this.code =  ResultCode.ERROR.code();
        this.msg = msg;
        return this;
    }

    public int getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public T getData() {
        return data;
    }

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