From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java | 225 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 225 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java new file mode 100644 index 0000000..b30ca40 --- /dev/null +++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java @@ -0,0 +1,225 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 搴勯獮 (smallchill@163.com) + */ +package org.springblade.core.tool.api; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.*; +import org.springblade.core.tool.constant.BladeConstant; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springframework.lang.Nullable; + +import javax.servlet.http.HttpServletResponse; +import java.io.Serializable; +import java.util.Optional; + +/** + * 缁熶竴API鍝嶅簲缁撴灉灏佽 + * + * @author Chill + */ +@Getter +@Setter +@ToString +@ApiModel(description = "杩斿洖淇℃伅") +@NoArgsConstructor +public class R<T> implements Serializable { + + private static final long serialVersionUID = 1L; + + @ApiModelProperty(value = "鐘舵�佺爜", required = true) + private int code; + @ApiModelProperty(value = "鏄惁鎴愬姛", required = true) + private boolean success; + @ApiModelProperty(value = "鎵胯浇鏁版嵁") + private T data; + @ApiModelProperty(value = "杩斿洖娑堟伅", required = true) + private String msg; + + private R(IResultCode resultCode) { + this(resultCode, null, resultCode.getMessage()); + } + + private R(IResultCode resultCode, String msg) { + this(resultCode, null, msg); + } + + private R(IResultCode resultCode, T data) { + this(resultCode, data, resultCode.getMessage()); + } + + private R(IResultCode resultCode, T data, String msg) { + this(resultCode.getCode(), data, msg); + } + + private R(int code, T data, String msg) { + this.code = code; + this.data = data; + this.msg = msg; + this.success = ResultCode.SUCCESS.code == code; + } + + /** + * 鍒ゆ柇杩斿洖鏄惁涓烘垚鍔� + * + * @param result Result + * @return 鏄惁鎴愬姛 + */ + public static boolean isSuccess(@Nullable R<?> result) { + return Optional.ofNullable(result) + .map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.code)) + .orElse(Boolean.FALSE); + } + + /** + * 鍒ゆ柇杩斿洖鏄惁涓烘垚鍔� + * + * @param result Result + * @return 鏄惁鎴愬姛 + */ + public static boolean isNotSuccess(@Nullable R<?> result) { + return !R.isSuccess(result); + } + + /** + * 杩斿洖R + * + * @param data 鏁版嵁 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> data(T data) { + return data(data, BladeConstant.DEFAULT_SUCCESS_MESSAGE); + } + + /** + * 杩斿洖R + * + * @param data 鏁版嵁 + * @param msg 娑堟伅 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> data(T data, String msg) { + return data(HttpServletResponse.SC_OK, data, msg); + } + + /** + * 杩斿洖R + * + * @param code 鐘舵�佺爜 + * @param data 鏁版嵁 + * @param msg 娑堟伅 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> data(int code, T data, String msg) { + return new R<>(code, data, data == null ? BladeConstant.DEFAULT_NULL_MESSAGE : msg); + } + + /** + * 杩斿洖R + * + * @param msg 娑堟伅 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> success(String msg) { + return new R<>(ResultCode.SUCCESS, msg); + } + + /** + * 杩斿洖R + * + * @param resultCode 涓氬姟浠g爜 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> success(IResultCode resultCode) { + return new R<>(resultCode); + } + + /** + * 杩斿洖R + * + * @param resultCode 涓氬姟浠g爜 + * @param msg 娑堟伅 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> success(IResultCode resultCode, String msg) { + return new R<>(resultCode, msg); + } + + /** + * 杩斿洖R + * + * @param msg 娑堟伅 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> fail(String msg) { + return new R<>(ResultCode.FAILURE, msg); + } + + + /** + * 杩斿洖R + * + * @param code 鐘舵�佺爜 + * @param msg 娑堟伅 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> fail(int code, String msg) { + return new R<>(code, null, msg); + } + + /** + * 杩斿洖R + * + * @param resultCode 涓氬姟浠g爜 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> fail(IResultCode resultCode) { + return new R<>(resultCode); + } + + /** + * 杩斿洖R + * + * @param resultCode 涓氬姟浠g爜 + * @param msg 娑堟伅 + * @param <T> T 娉涘瀷鏍囪 + * @return R + */ + public static <T> R<T> fail(IResultCode resultCode, String msg) { + return new R<>(resultCode, msg); + } + + /** + * 杩斿洖R + * + * @param flag 鎴愬姛鐘舵�� + * @return R + */ + public static <T> R<T> status(boolean flag) { + return flag ? success(BladeConstant.DEFAULT_SUCCESS_MESSAGE) : fail(BladeConstant.DEFAULT_FAILURE_MESSAGE); + } + +} -- Gitblit v1.9.3