123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package com.persagy.framework.common.pojo;
- import com.persagy.framework.common.exception.ErrorCode;
- import com.persagy.framework.common.exception.ServiceException;
- import com.persagy.framework.common.exception.enums.GlobalErrorCodeConstants;
- import com.fasterxml.jackson.annotation.JsonIgnore;
- import lombok.Data;
- import org.springframework.util.Assert;
- import java.io.Serializable;
- import java.util.Objects;
- @Data
- public class CommonResult<T> implements Serializable {
-
- private Integer code;
-
- private T data;
-
- private String msg;
-
- public static <T> CommonResult<T> error(CommonResult<?> result) {
- return error(result.getCode(), result.getMsg());
- }
- public static <T> CommonResult<T> error(Integer code, String message) {
- Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
- CommonResult<T> result = new CommonResult<>();
- result.code = code;
- result.msg = message;
- return result;
- }
- public static <T> CommonResult<T> error(ErrorCode errorCode) {
- return error(errorCode.getCode(), errorCode.getMsg());
- }
- public static <T> CommonResult<T> success(T data) {
- CommonResult<T> result = new CommonResult<>();
- result.code = GlobalErrorCodeConstants.SUCCESS.getCode();
- result.data = data;
- result.msg = "";
- return result;
- }
- public static boolean isSuccess(Integer code) {
- return Objects.equals(code, GlobalErrorCodeConstants.SUCCESS.getCode());
- }
- @JsonIgnore
- public boolean isSuccess() {
- return isSuccess(code);
- }
- @JsonIgnore
- public boolean isError() {
- return !isSuccess();
- }
-
-
- public void checkError() throws ServiceException {
- if (isSuccess()) {
- return;
- }
-
- throw new ServiceException(code, msg);
- }
- public static <T> CommonResult<T> error(ServiceException serviceException) {
- return error(serviceException.getCode(), serviceException.getMessage());
- }
- }
|