mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-11-14 22:49:33 +08:00
🎉 新增工具类
This commit is contained in:
parent
8cfbadaa60
commit
253d2ae99c
@ -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;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
@ -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<T> extends IService<T> {
|
||||
*/
|
||||
boolean deleteLogic(@NotEmpty List<Long> ids);
|
||||
|
||||
/**
|
||||
* 判断字段是否重复
|
||||
*
|
||||
* @param field 字段
|
||||
* @param value 字段值
|
||||
* @param excludedId 排除的id
|
||||
* @return boolean
|
||||
*/
|
||||
boolean isFieldDuplicate(SFunction<T, ?> field, Object value, Long excludedId);
|
||||
|
||||
}
|
||||
|
@ -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<M extends BaseMapper<T>, T extends BaseEntity> exte
|
||||
return super.removeByIds(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFieldDuplicate(SFunction<T, ?> field, Object value, Long excludedId) {
|
||||
LambdaQueryWrapper<T> 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();
|
||||
|
@ -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 脱敏后的字符串
|
||||
|
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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<String> getter, Consumer<String> setter, Supplier<String> valueSupplier) {
|
||||
if (StringUtil.isBlank(getter.get())) {
|
||||
setter.accept(valueSupplier.get());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user