|
@@ -0,0 +1,99 @@
|
|
|
+package com.phm.manage.common.domain;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
|
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
|
|
|
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
|
|
+import com.phm.manage.common.enums.GlobalErrorCodeConstants;
|
|
|
+import com.phm.manage.common.util.ErrorCode;
|
|
|
+import com.phm.manage.common.util.ServiceException;
|
|
|
+import lombok.Data;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * XML格式,通用返回结果
|
|
|
+ *
|
|
|
+ * @author wgk
|
|
|
+ * @date 2023-09-06
|
|
|
+ */
|
|
|
+@JacksonXmlRootElement(localName = "RESULT")
|
|
|
+@Data
|
|
|
+public class CommonResultXML<T> implements Serializable {
|
|
|
+
|
|
|
+ @JacksonXmlProperty(localName = "CODE")
|
|
|
+ private Integer code;
|
|
|
+
|
|
|
+ @JacksonXmlProperty(localName = "DATA")
|
|
|
+ private T data;
|
|
|
+
|
|
|
+ @JacksonXmlProperty(localName = "MSG")
|
|
|
+ private String msg;
|
|
|
+
|
|
|
+ public static <T> CommonResultXML<T> error(CommonResultXML<?> result) {
|
|
|
+ return error(result.getCode(), result.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResultXML<T> error(Integer code, String message) {
|
|
|
+ Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code), "code 必须是错误的!");
|
|
|
+ CommonResultXML<T> result = new CommonResultXML<>();
|
|
|
+ result.code = code;
|
|
|
+ result.msg = message;
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResultXML<T> error(ErrorCode errorCode) {
|
|
|
+ return error(errorCode.getCode(), errorCode.getMsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResultXML<T> success(T data) {
|
|
|
+ CommonResultXML<T> result = new CommonResultXML<>();
|
|
|
+ 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 // 避免 jackson 序列化
|
|
|
+ public boolean isSuccess() {
|
|
|
+ return isSuccess(code);
|
|
|
+ }
|
|
|
+
|
|
|
+ @JsonIgnore // 避免 jackson 序列化
|
|
|
+ public boolean isError() {
|
|
|
+ return !isSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ // ========= 和 Exception 异常体系集成 =========
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
|
|
|
+ */
|
|
|
+ public void checkError() throws ServiceException {
|
|
|
+ if (isSuccess()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 业务异常
|
|
|
+ throw new ServiceException(code, msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否有异常。如果有,则抛出 {@link ServiceException} 异常
|
|
|
+ * 如果没有,则返回 {@link #data} 数据
|
|
|
+ */
|
|
|
+ @JsonIgnore // 避免 jackson 序列化
|
|
|
+ public T getCheckedData() {
|
|
|
+ checkError();
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static <T> CommonResultXML<T> error(ServiceException serviceException) {
|
|
|
+ return error(serviceException.getCode(), serviceException.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+}
|