import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.Page;
import lombok.Data;
import java.io.Serializable;
/**
-
统一返回内容格式
-
@author Li ShiJie
-
@since 1.0.0
*/
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseBean implements Serializable {private boolean success;
private Integer errorCode = 0;
private String message;private T data;
public ResponseBean() {
}public ResponseBean(T data) {
this.data = data;
this.success = true;
}public static ResponseBean success(T data) {
return new ResponseBean<>(data);
}public static ResponseBean success(Page<?> page, T data) {
ResponseBean bean = new ResponseBean<>();
PageBase base = new PageBase<>();
base.setTotalPage(page.getPages());
base.setPageNum(page.getPageNum());
base.setPageSize(page.getPageSize());
base.setTotalCount(page.getTotal());
base.setStartIndex(base.getPageNum() <= 1 ? 0 : 1);
base.setIsMore(base.getPageNum() >= base.getTotalPage() ? 0 : 1);
base.setItems(data);
bean.setData(base);
bean.setSuccess(true);
return bean;
}public static ResponseBean<?> success() {
ResponseBean<?> responseBean = new ResponseBean<>();
responseBean.setSuccess(true);
return responseBean;
}public static ResponseBean<?> failure() {
ResponseBean<?> responseBean = new ResponseBean<>();
responseBean.setSuccess(false);
return responseBean;
}public static ResponseBean<?> failure(String message) {
ResponseBean<?> responseBean = new ResponseBean<>();
responseBean.setSuccess(false);
responseBean.setMessage(message);
return responseBean;
}public static ResponseBean failure(Integer errCode, String message) {
ResponseBean res = new ResponseBean<>();
res.setSuccess(false);
res.setMessage(message);
res.setErrorCode(errCode);
return res;
}
}