package com.cherish.commonutils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
//统一返回结果的类
@Data
public class ResultData {
@ApiModelProperty(value = "是否成功")
private Boolean success;
@ApiModelProperty(value = "返回码")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String message;
@ApiModelProperty(value = "返回数据")
private Map<String, Object> data=new HashMap<String, Object>();
//构造方法私有化,别人就不能new,直接调用暴露的方法
private ResultData(){}
//成功的静态方法
public static ResultData ok(){
ResultData result=new ResultData();
result.setSuccess(true);
result.setCode(ResultCode.SUCCESS);
result.setMessage("成功");
return result;
}
//失败的静态方法
public static ResultData error(){
ResultData result=new ResultData();
result.setCode(ResultCode.ERROR);
result.setMessage("失败");
result.setSuccess(false);
return result;
}
public ResultData succes(Boolean success){
this.setSuccess(success);
return this;
}
public ResultData code(Integer code){
this.setCode(code);
return this;
}
public ResultData message(String message){
this.setMessage(message);
return this;
}
public ResultData data(String key ,Object value){
this.data.put(key, value);
return this;
}
//直接传递一个map进去
public ResultData data(Map<String ,Object> map){
this.setData(map);
return this;
}
}