From 15854b40ac7e15769e91ac787808c807556945c9 Mon Sep 17 00:00:00 2001 From: aguanleishen Date: Tue, 28 Jul 2026 08:32:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0APP=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/launch/constant/AppConstant.java | 6 + .../org/springblade/core/tool/api/FR.java | 221 ++++++++ .../java/org/springblade/core/tool/api/R.java | 2 +- .../core/log/utils/AssertUtils.java | 471 ++++++++++++++++++ 4 files changed, 699 insertions(+), 1 deletion(-) create mode 100644 blade-core-tool/src/main/java/org/springblade/core/tool/api/FR.java create mode 100644 blade-starter-log/src/main/java/org/springblade/core/log/utils/AssertUtils.java diff --git a/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java b/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java index dabd835..427e182 100644 --- a/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java +++ b/blade-core-launch/src/main/java/org/springblade/core/launch/constant/AppConstant.java @@ -127,6 +127,12 @@ public interface AppConstant { */ String APPLICATION_DEMO_NAME = APPLICATION_NAME_PREFIX + "demo"; + /** + * 文件模块模块名称 + */ + String APPLICATION_FILE_NAME =APPLICATION_NAME_PREFIX +"file"; + + /** * 开发环境 */ diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/api/FR.java b/blade-core-tool/src/main/java/org/springblade/core/tool/api/FR.java new file mode 100644 index 0000000..3e6d125 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/api/FR.java @@ -0,0 +1,221 @@ +package org.springblade.core.tool.api; + +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.NullSerializer; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.servlet.http.HttpServletResponse; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; +import org.springblade.core.tool.constant.BladeConstant; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springframework.lang.Nullable; + +import java.io.Serial; +import java.util.Optional; + +/** + * feign 统一API响应结果封装 + * @author bfhuange + * @since 2024/11/28 + */ +@Getter +@Setter +@ToString +@Schema(description = "返回信息") +@NoArgsConstructor +public class FR extends R { + + @Serial + private static final long serialVersionUID = 1L; + + @JsonSerialize(nullsUsing = NullSerializer.class) + @Schema(description = "承载数据") + private T data; + + private FR(IResultCode resultCode) { + this(resultCode, null, resultCode.getMessage()); + } + + private FR(IResultCode resultCode, String msg) { + this(resultCode, null, msg); + } + + private FR(IResultCode resultCode, T data) { + this(resultCode, data, resultCode.getMessage()); + } + + private FR(IResultCode resultCode, T data, String msg) { + this(resultCode.getCode(), data, msg); + } + + private FR(int code, T data, String msg) { + super(code, data, msg); + this.data = data; + } + + public void setData(T data) { + this.data = data; + super.setData(data); + } + + /** + * 判断返回是否为成功 + * + * @param result Result + * @return 是否成功 + */ + public static boolean isSuccess(@Nullable FR result) { + return Optional.ofNullable(result) + .map(x -> ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.getCode(), x.getCode())) + .orElse(Boolean.FALSE); + } + + /** + * 判断返回是否为成功 + * + * @param result Result + * @return 是否成功 + */ + public static boolean isNotSuccess(@Nullable FR result) { + return !FR.isSuccess(result); + } + + /** + * 返回R + * + * @param data 数据 + * @param T 泛型标记 + * @return R + */ + public static FR data(T data) { + return data(data, BladeConstant.DEFAULT_SUCCESS_MESSAGE); + } + + /** + * 返回R + * + * @param data 数据 + * @param msg 消息 + * @param T 泛型标记 + * @return R + */ + public static FR data(T data, String msg) { + return data(HttpServletResponse.SC_OK, data, msg); + } + + /** + * 返回R + * + * @param code 状态码 + * @param data 数据 + * @param msg 消息 + * @param T 泛型标记 + * @return R + */ + public static FR data(int code, T data, String msg) { + return new FR<>(code, data, data == null ? BladeConstant.DEFAULT_NULL_MESSAGE : msg); + } + + /** + * 返回R + * + * @param msg 消息 + * @param T 泛型标记 + * @return R + */ + public static FR success(String msg) { + return new FR<>(ResultCode.SUCCESS, msg); + } + + /** + * 返回R + * + * @param resultCode 业务代码 + * @param T 泛型标记 + * @return R + */ + public static FR success(IResultCode resultCode) { + return new FR<>(resultCode); + } + + /** + * 返回R + * + * @param resultCode 业务代码 + * @param msg 消息 + * @param T 泛型标记 + * @return R + */ + public static FR success(IResultCode resultCode, String msg) { + return new FR<>(resultCode, msg); + } + + /** + * 返回R + * + * @param msg 消息 + * @param T 泛型标记 + * @return R + */ + public static FR fail(String msg) { + return new FR<>(ResultCode.FAILURE, msg); + } + + + /** + * 返回R + * + * @param code 状态码 + * @param msg 消息 + * @param T 泛型标记 + * @return R + */ + public static FR fail(int code, String msg) { + return new FR<>(code, null, msg); + } + + /** + * 返回R + * + * @param resultCode 业务代码 + * @param T 泛型标记 + * @return R + */ + public static FR fail(IResultCode resultCode) { + return new FR<>(resultCode); + } + + /** + * 返回R + * + * @param resultCode 业务代码 + * @param msg 消息 + * @param T 泛型标记 + * @return R + */ + public static FR fail(IResultCode resultCode, String msg) { + return new FR<>(resultCode, msg); + } + + /** + * 返回R + * + * @param flag 成功状态 + * @return R + */ + public static FR status(boolean flag) { + return flag ? success(BladeConstant.DEFAULT_SUCCESS_MESSAGE) : fail(BladeConstant.DEFAULT_FAILURE_MESSAGE); + } + + /** + * 从r复制 + * @param result + * @return + * @param + */ + public static FR copy(R result) { + return new FR<>(result.getCode(), result.getData(), result.getMsg()); + } +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java b/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java index 2317547..fa95ad1 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/api/R.java @@ -79,7 +79,7 @@ public class R implements Serializable { this(resultCode.getCode(), data, msg); } - private R(int code, T data, String msg) { + protected R(int code, T data, String msg) { this.code = code; this.data = data; this.msg = msg; diff --git a/blade-starter-log/src/main/java/org/springblade/core/log/utils/AssertUtils.java b/blade-starter-log/src/main/java/org/springblade/core/log/utils/AssertUtils.java new file mode 100644 index 0000000..8ff43bc --- /dev/null +++ b/blade-starter-log/src/main/java/org/springblade/core/log/utils/AssertUtils.java @@ -0,0 +1,471 @@ +package org.springblade.core.log.utils; + +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.tool.api.IResultCode; +import org.springblade.core.tool.utils.StringUtil; +import org.springframework.util.CollectionUtils; +import org.springframework.util.ObjectUtils; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * @author bfhuange + * @date 2024/8/27 + **/ +public class AssertUtils { + + /** + * 对象非空 + * @param object + * @param message + */ + public static void notNull(Object object, String message) throws ServiceException { + if (object == null) { + throw new ServiceException(message); + } + } + + /** + * 对象非空 + * @param object + * @param resultCode + */ + public static void notNull(Object object, IResultCode resultCode) throws ServiceException { + if (object == null) { + throw new ServiceException(resultCode); + } + } + + /** + * Map非空 + * @param map + * @param message + */ + public static void notEmpty(Map map, String message) throws ServiceException { + if (CollectionUtils.isEmpty(map)) { + throw new ServiceException(message); + } + } + + /** + * Map非空 + * @param map + * @param resultCode + */ + public static void notEmpty(Map map, IResultCode resultCode) throws ServiceException { + if (CollectionUtils.isEmpty(map)) { + throw new ServiceException(resultCode); + } + } + + /** + * 列表非空 + * @param collection + * @param message + */ + public static void notEmpty(Collection collection, String message) throws ServiceException { + if (CollectionUtils.isEmpty(collection)) { + throw new ServiceException(message); + } + } + + /** + * 列表非空 + * @param collection + * @param resultCode + */ + public static void notEmpty(Collection collection, IResultCode resultCode) throws ServiceException { + if (CollectionUtils.isEmpty(collection)) { + throw new ServiceException(resultCode); + } + } + + /** + * 数组非空 + * @param array + * @param message + */ + public static void notEmpty(Object[] array, String message) throws ServiceException { + if (ObjectUtils.isEmpty(array)) { + throw new ServiceException(message); + } + } + + /** + * 数组非空 + * @param array + * @param resultCode + */ + public static void notEmpty(Object[] array, IResultCode resultCode) throws ServiceException { + if (ObjectUtils.isEmpty(array)) { + throw new ServiceException(resultCode); + } + } + + /** + * 字符串非空 + * @param value + * @param message + */ + public static void notEmpty(String value, String message) throws ServiceException { + if (StringUtil.hasLength(value)) { + throw new ServiceException(message); + } + } + + /** + * 字符串非空 + * @param value + * @param resultCode + */ + public static void notEmpty(String value, IResultCode resultCode) throws ServiceException { + if (StringUtil.hasLength(value)) { + throw new ServiceException(resultCode); + } + } + + /** + * 字符串去除空格非空 + * @param value + * @param message + */ + public static void notBlank(String value, String message) throws ServiceException { + if (StringUtil.isBlank(value)) { + throw new ServiceException(message); + } + } + + /** + * 字符串去除空格非空 + * @param value + * @param resultCode + */ + public static void notBlank(String value, IResultCode resultCode) throws ServiceException { + if (StringUtil.isBlank(value)) { + throw new ServiceException(resultCode); + } + } + + /** + * 获取重复数据 + * @param dataList + * @return + * @throws ServiceException + */ + public static List getRepeatData(List dataList) throws ServiceException { + if(CollectionUtils.isEmpty(dataList)) { + return null; + } + //判断是否有重复数据 + List distinctDataList = dataList.stream().distinct().toList(); + if(dataList.size() == distinctDataList.size()) { + return null; + } + List repeatList = new ArrayList<>(); + Map dataMap = dataList.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting())); + //处理重复数据 + for(Map.Entry entry:dataMap.entrySet()) { + if(entry.getValue() > 1) { + repeatList.add(entry.getKey()); + } + } + return repeatList; + } + + /** + * 数据不允许重复 + * @param dataList + * @param message + * @throws ServiceException + */ + public static void notRepeat(List dataList,String message) throws ServiceException { + //获取重复数据 + List repeatList = getRepeatData(dataList); + //若有重复数据,则校验不通过处理 + if(!CollectionUtils.isEmpty(repeatList)) { + String repeatData = String.join(",",repeatList); + throw new ServiceException(String.format(message,repeatData)); + } + } + + /** + * 字符串相等校验 + * @param str1 + * @param str2 + * @param message + * @throws ServiceException + */ + public static void equals(String str1, String str2, String message) throws ServiceException { + if (!StringUtil.equals(str1,str2)) { + throw new ServiceException(message); + } + } + + /** + * 字符串相等校验 + * @param str1 + * @param str2 + * @param resultCode + * @throws ServiceException + */ + public static void equals(String str1, String str2, IResultCode resultCode) throws ServiceException { + if (!StringUtil.equals(str1,str2)) { + throw new ServiceException(resultCode); + } + } + + /** + * 数字相等校验 + * @param num1 + * @param num2 + * @param message + * @throws ServiceException + */ + public static void equals(BigDecimal num1, BigDecimal num2, String message) throws ServiceException { + if (num1.compareTo(num2) != 0) { + throw new ServiceException(message); + } + } + + /** + * 数字相等校验 + * @param num1 + * @param num2 + * @param resultCode + * @throws ServiceException + */ + public static void equals(BigDecimal num1, BigDecimal num2, IResultCode resultCode) throws ServiceException { + if (num1.compareTo(num2) != 0) { + throw new ServiceException(resultCode); + } + } + + /** + * 数字不大于校验 + * @param num1 + * @param num2 + * @param message + * @throws ServiceException + */ + public static void notGreaterThan(BigDecimal num1, BigDecimal num2, String message) throws ServiceException { + if(num1.compareTo(num2) > 0) { + throw new ServiceException(message); + } + } + + /** + * 数字不大于校验 + * @param num1 + * @param num2 + * @param resultCode + * @throws ServiceException + */ + public static void notGreaterThan(BigDecimal num1, BigDecimal num2, IResultCode resultCode) throws ServiceException { + if(num1.compareTo(num2) > 0) { + throw new ServiceException(resultCode); + } + } + + /** + * 数字不大于校验 + * @param num1 + * @param num2 + * @param message + * @throws ServiceException + */ + public static void notGreaterThan(Integer num1, Integer num2, String message) throws ServiceException { + if(num1 > num2) { + throw new ServiceException(message); + } + } + + /** + * 数字不大于校验 + * @param num1 + * @param num2 + * @param resultCode + * @throws ServiceException + */ + public static void notGreaterThan(Integer num1, Integer num2, IResultCode resultCode) throws ServiceException { + if(num1 > num2) { + throw new ServiceException(resultCode); + } + } + + /** + * 数字不小于校验 + * @param num1 + * @param num2 + * @param message + * @throws ServiceException + */ + public static void notLessThan(BigDecimal num1, BigDecimal num2, String message) throws ServiceException { + if(num1.compareTo(num2) < 0) { + throw new ServiceException(message); + } + } + + /** + * 数字不小于校验 + * @param num1 + * @param num2 + * @param resultCode + * @throws ServiceException + */ + public static void notLessThan(BigDecimal num1, BigDecimal num2, IResultCode resultCode) throws ServiceException { + if(num1.compareTo(num2) < 0) { + throw new ServiceException(resultCode); + } + } + + /** + * 表达式为真判断校验 + * @param expression + * @param message + * @throws ServiceException + */ + public static void isTrue(boolean expression, String message) throws ServiceException { + if(!expression) { + throw new ServiceException(message); + } + } + + /** + * 表达式为真判断校验 + * @param expression + * @param resultCode + * @throws ServiceException + */ + public static void isTrue(boolean expression, IResultCode resultCode) throws ServiceException { + if(!expression) { + throw new ServiceException(resultCode); + } + } + + /** + * 表达式判断为假校验 + * @param expression + * @param message + * @throws ServiceException + */ + public static void isFalse(boolean expression, String message) throws ServiceException { + if(expression) { + throw new ServiceException(message); + } + } + + /** + * 表达式判断为假校验 + * @param expression + * @param resultCode + * @throws ServiceException + */ + public static void isFalse(boolean expression, IResultCode resultCode) throws ServiceException { + if(expression) { + throw new ServiceException(resultCode); + } + } + + /** + * 字符串不相等校验 + * @param str1 + * @param str2 + * @param message + * @throws ServiceException + */ + public static void notEquals(String str1, String str2, String message) throws ServiceException { + if (StringUtil.equals(str1,str2)) { + throw new ServiceException(message); + } + } + + /** + * 字符串不相等校验 + * @param str1 + * @param str2 + * @param resultCode + * @throws ServiceException + */ + public static void notEquals(String str1, String str2, IResultCode resultCode) throws ServiceException { + if (StringUtil.equals(str1,str2)) { + throw new ServiceException(resultCode); + } + } + + /** + * 对象为空 + * @param object + * @param message + */ + public static void isNull(Object object, String message) throws ServiceException { + if (object != null) { + throw new ServiceException(message); + } + } + + /** + * 对象为空 + * @param object + * @param resultCode + */ + public static void isNull(Object object, IResultCode resultCode) throws ServiceException { + if (object != null) { + throw new ServiceException(resultCode); + } + } + + /** + * 对象为数值 + * @param value + * @param message + * @throws ServiceException + */ + public static void isInteger(String value,String message) throws ServiceException { + try { + Integer.valueOf(value); + } catch (NumberFormatException e) { + throw new ServiceException(message); + } + } + + /** + * 对象为数值 + * @param value + * @param resultCode + * @throws ServiceException + */ + public static void isInteger(String value,IResultCode resultCode) throws ServiceException { + try { + Integer.valueOf(value); + } catch (NumberFormatException e) { + throw new ServiceException(resultCode); + } + } + + /** + * 列表为空 + * @param collection + * @param message + */ + public static void isEmpty(Collection collection, String message) throws ServiceException { + if (!CollectionUtils.isEmpty(collection)) { + throw new ServiceException(message); + } + } + + /** + * 列表为空 + * @param collection + * @param resultCode + */ + public static void isEmpty(Collection collection, IResultCode resultCode) throws ServiceException { + if (!CollectionUtils.isEmpty(collection)) { + throw new ServiceException(resultCode); + } + } +}