From 253d2ae99c369e9c57d5498a29934f84f2fd80a2 Mon Sep 17 00:00:00 2001 From: smallchill Date: Sat, 9 Mar 2024 16:30:14 +0800 Subject: [PATCH] =?UTF-8?q?:tada:=20=E6=96=B0=E5=A2=9E=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springblade/core/mp/base/BaseEntity.java | 17 ++++++ .../springblade/core/mp/base/BaseService.java | 11 ++++ .../core/mp/base/BaseServiceImpl.java | 13 +++++ .../core/tool/utils/DesensitizationUtil.java | 16 +++--- .../core/tool/utils/TemplateUtil.java | 56 +++++++++++++++++++ .../core/tool/utils/ValidationRule.java | 44 +++++++++++++++ .../core/tool/utils/ValidationUtil.java | 55 ++++++++++++++++++ 7 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 blade-core-tool/src/main/java/org/springblade/core/tool/utils/TemplateUtil.java create mode 100644 blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationRule.java create mode 100644 blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationUtil.java diff --git a/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseEntity.java b/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseEntity.java index bf31156..25f9c48 100644 --- a/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseEntity.java +++ b/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseEntity.java @@ -16,6 +16,8 @@ package org.springblade.core.mp.base; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableLogic; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonSerialize; @@ -36,6 +38,14 @@ import java.util.Date; @Data public class BaseEntity implements Serializable { + /** + * 主键 + */ + @ApiModelProperty(value = "主键") + @TableId(value = "id", type = IdType.ASSIGN_ID) + @JsonSerialize(using = ToStringSerializer.class) + private Long id; + /** * 创建人 */ @@ -43,6 +53,13 @@ public class BaseEntity implements Serializable { @ApiModelProperty(value = "创建人") private Long createUser; + /** + * 创建部门 + */ + @JsonSerialize(using = ToStringSerializer.class) + @ApiModelProperty(value = "创建部门") + private Long createDept; + /** * 创建时间 */ diff --git a/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseService.java b/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseService.java index c1f356f..8b4b5bd 100644 --- a/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseService.java +++ b/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseService.java @@ -15,6 +15,7 @@ */ package org.springblade.core.mp.base; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.service.IService; import javax.validation.constraints.NotEmpty; @@ -36,4 +37,14 @@ public interface BaseService extends IService { */ boolean deleteLogic(@NotEmpty List ids); + /** + * 判断字段是否重复 + * + * @param field 字段 + * @param value 字段值 + * @param excludedId 排除的id + * @return boolean + */ + boolean isFieldDuplicate(SFunction field, Object value, Long excludedId); + } diff --git a/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseServiceImpl.java b/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseServiceImpl.java index 493c0d1..e4e9f76 100644 --- a/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseServiceImpl.java +++ b/blade-core-mybatis/src/main/java/org/springblade/core/mp/base/BaseServiceImpl.java @@ -15,7 +15,10 @@ */ package org.springblade.core.mp.base; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.SneakyThrows; import org.springblade.core.secure.BladeUser; @@ -68,6 +71,16 @@ public class BaseServiceImpl, T extends BaseEntity> exte return super.removeByIds(ids); } + @Override + public boolean isFieldDuplicate(SFunction field, Object value, Long excludedId) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.eq(field, value); + if (excludedId != null) { + queryWrapper.ne(T::getId, excludedId); + } + return baseMapper.selectCount(queryWrapper) > 0; + } + @SneakyThrows private void resolveSave(T entity) { BladeUser user = SecureUtil.getUser(); diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/DesensitizationUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/DesensitizationUtil.java index 4c2ec68..946532e 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/DesensitizationUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/DesensitizationUtil.java @@ -31,7 +31,7 @@ import java.util.Arrays; public class DesensitizationUtil { /** - * [中文姓名] 只显示第一个汉字,其他隐藏为2个星号<例子:李**> + * [中文姓名] 只显示第一个汉字,其他隐藏为2个星号(例子:李**) * * @param fullName 全名 * @return 脱敏后的字符串 @@ -42,7 +42,7 @@ public class DesensitizationUtil { } /** - * [身份证号] 显示最后四位,其他隐藏。共计18位或者15位。<例子:*************5762> + * [身份证号] 显示最后四位,其他隐藏。共计18位或者15位。(例子:*************5762) * * @param id 身份证号 * @return 脱敏后的字符串 @@ -53,7 +53,7 @@ public class DesensitizationUtil { } /** - * [固定电话] 后四位,其他隐藏<例子:****1234> + * [固定电话] 后四位,其他隐藏(例子:****1234) * * @param num 固定电话号 * @return 脱敏后的字符串 @@ -64,7 +64,7 @@ public class DesensitizationUtil { } /** - * [手机号码] 前三位,后四位,其他隐藏<例子:138****1234> + * [手机号码] 前三位,后四位,其他隐藏(例子:138****1234) * * @param num 手机号 * @return 脱敏后的字符串 @@ -75,7 +75,7 @@ public class DesensitizationUtil { } /** - * [地址] 只显示到地区,不显示详细地址;我们要对个人信息增强保护<例子:北京市海淀区****> + * [地址] 只显示到地区,不显示详细地址;我们要对个人信息增强保护(例子:北京市海淀区****) * * @param address 地区 * @param sensitiveSize 敏感信息长度 @@ -87,7 +87,7 @@ public class DesensitizationUtil { } /** - * [电子邮箱] 邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示<例子:g**@163.com> + * [电子邮箱] 邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示(例子:g**@163.com) * * @param email 邮箱 * @return 脱敏后的字符串 @@ -109,7 +109,7 @@ public class DesensitizationUtil { } /** - * [银行卡号] 前六位,后四位,其他用星号隐藏每位1个星号<例子:622260***********1234> + * [银行卡号] 前六位,后四位,其他用星号隐藏每位1个星号(例子:622260***********1234) * * @param cardNum 银行卡号 * @return 脱敏后的字符串 @@ -120,7 +120,7 @@ public class DesensitizationUtil { } /** - * [公司开户银行联号] 公司开户银行联行号,显示前两位,其他用星号隐藏,每位1个星号<例子:12********> + * [公司开户银行联号] 公司开户银行联行号,显示前两位,其他用星号隐藏,每位1个星号(例子:12********) * * @param code 银行联行号 * @return 脱敏后的字符串 diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/TemplateUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/TemplateUtil.java new file mode 100644 index 0000000..df252ac --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/TemplateUtil.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springblade.core.tool.utils; + + +import org.springblade.core.tool.support.Kv; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 模版解析工具类 + */ +public class TemplateUtil { + + /** + * 支持 ${} 与 #{} 两种模版占位符 + */ + private static final Pattern pattern = Pattern.compile("\\$\\{([^{}]+)}|\\#\\{([^{}]+)}"); + + /** + * 解析模版 + * + * @param template 模版 + * @param params 参数 + * @return 解析后的字符串 + */ + public static String process(String template, Kv params) { + Matcher matcher = pattern.matcher(template); + StringBuffer sb = new StringBuffer(); + while (matcher.find()) { + String key = matcher.group(1) != null ? matcher.group(1) : matcher.group(2); + String replacement = params.getStr(key); + if (replacement == null) { + throw new IllegalArgumentException("参数中缺少必要的键: " + key); + } + matcher.appendReplacement(sb, replacement); + } + matcher.appendTail(sb); + return sb.toString(); + } + +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationRule.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationRule.java new file mode 100644 index 0000000..057c9b9 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationRule.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springblade.core.tool.utils; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.io.Serializable; + +/** + * 校验规则类 + * + * @author Chill + */ +@Data +@AllArgsConstructor +public class ValidationRule implements Serializable { + private static final long serialVersionUID = 1L; + /** + * 校验值 + */ + private String value; + /** + * 校验正则 + */ + private String regex; + /** + * 校验信息提示 + */ + private String message; +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationUtil.java new file mode 100644 index 0000000..196fb44 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ValidationUtil.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springblade.core.tool.utils; + +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + * 校验通用工具类 + * + * @author Chill + */ +public class ValidationUtil { + + /** + * 自定义字段校验 + * + * @param value 字段值 + * @param regex 正则表达式 + * @param message 验证消息 + * @return String + */ + public static String validateField(String value, String regex, String message) { + if (!RegexUtil.match(regex, value)) { + return message; + } + return StringPool.EMPTY; + } + + /** + * 如果字段值为空,则设置一个默认值 + * + * @param getter 字段的getter方法 + * @param setter 字段的setter方法 + * @param valueSupplier 默认值提供方法 + */ + public static void setValueIfBlank(Supplier getter, Consumer setter, Supplier valueSupplier) { + if (StringUtil.isBlank(getter.get())) { + setter.accept(valueSupplier.get()); + } + } +}