package com.hotent.entity; import com.hotent.enums.ResponseErrorEnums; import java.io.Serializable; public class CommonResult implements Serializable { private static final long serialVersionUID = 1L; Boolean state = true; String message; E value; private Integer code = 200; private String errorCode; private String logId; /** * 构造成功的返回结果 *

* 例如:CommonResult.ok(); 或者 CommonResult.<String>ok();
* 结果集中各属性默认值如下:
* state: true
* message: "操作成功"
* code: 200 *

* @param * @return */ public static CommonResult ok() { CommonResult cr = new CommonResult<>(ResponseErrorEnums.SUCCESS_OPTION); return cr; } /** * 构造成功的返回结果 *

* 例如:CommonResult.error(); 或者 CommonResult.<String>error();
* 结果集中各属性默认值如下:
* state: false
* message: "系统异常"
* code: 500 *

* @param * @return */ public static CommonResult error() { CommonResult cr = new CommonResult<>(ResponseErrorEnums.SYSTEM_ERROR); return cr; } /** * 构造指定的返回结果 *

* 例如:CommonResult.result(ResponseErrorEnums.SUCCESS_OPTION);
* 结果集中各属性对应传入的ResponseErrorEnums *

* @param * @return */ public static CommonResult result(ResponseErrorEnums res) { CommonResult cr = new CommonResult<>(res); return cr; } /** * 设置message并返回结果 * @param message * @return */ public CommonResult message(String message){ this.setMessage(message); return this; } /** * 设置value并返回结果 * @param val * @return */ public CommonResult value(E val){ this.setValue(val); return this; } /** * 设置logId并返回结果 * @param logId * @return */ public CommonResult log(String logId){ this.setLogId(logId); return this; } public CommonResult() { } /** * 返回成功及成功的提示信息 * @param message */ public CommonResult(String message) { this(true, message, null); } /** * 返回成功/失败,及对应的成功/失败提示信息 * @param state * @param message */ public CommonResult(boolean state, String message) { this(state, message, null); } /** * 返回成功/失败,及对应的成功/失败提示信息,还有返回对应的数据 * @param state * @param message * @param value */ public CommonResult(boolean state, String message, E value){ this.state = state; this.message = message; this.value = value; } /** * 返回错误,及错误编码,对应的错误信息 * @param error */ public CommonResult(ResponseErrorEnums error) { if(!error.getCode().equals("200")){ this.state = false; } this.errorCode = error.getCode(); this.message = error.getMessage(); this.code = error.getHttpCode(); } /** * 返回错误,及错误编码,对应的错误信息,还有返回对应的数据 * @param error * @param value */ public CommonResult(ResponseErrorEnums error, E value) { if(!error.getCode().equals("200")){ this.state = false; } this.errorCode = error.getCode(); this.message = error.getMessage(); this.code = error.getHttpCode(); this.value = value; } public Boolean getState() { return state; } public void setState(Boolean state) { this.state = state; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public E getValue() { return value; } public void setValue(E value) { this.value = value; } public String getLogId() { return logId; } public void setLogId(String logId) { this.logId = logId; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getErrorCode() { return errorCode; } public void setErrorCode(String errorCode) { this.errorCode = errorCode; } }