Fix Bug #550: AI修复
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import com.core.common.core.domain.R;
|
||||
import com.core.common.utils.poi.ExcelUtil;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.IOException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 共通工具类
|
||||
*
|
||||
* @author system
|
||||
*/
|
||||
public class CommonUtil {
|
||||
|
||||
/**
|
||||
* 尝试转化为int
|
||||
*
|
||||
* @param intStr int字符串
|
||||
* @return int值
|
||||
*/
|
||||
public static Integer tryParseInt(String intStr) {
|
||||
try {
|
||||
return Integer.parseInt(intStr);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取导入的Excel文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @param clazz 转换的Class
|
||||
* @return 转换后的实体列表
|
||||
*/
|
||||
public static <T> R<List<T>> readImportedExcelFile(MultipartFile file, Class<T> clazz) {
|
||||
ExcelUtil<T> util = new ExcelUtil<>(clazz);
|
||||
List<T> importDtoList;
|
||||
try {
|
||||
importDtoList = util.importExcel(file.getInputStream());
|
||||
} catch (IOException e) {
|
||||
return R.fail("导入失败!文件读取异常");
|
||||
}
|
||||
if (importDtoList.isEmpty()) {
|
||||
return R.fail("导入失败!文件不能为空");
|
||||
}
|
||||
return R.ok(importDtoList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建无视SSL验证的SSLSocketFactory
|
||||
*/
|
||||
public static SSLConnectionSocketFactory createIgnoreSslSocketFactory() {
|
||||
try {
|
||||
// 创建信任所有证书的TrustManager
|
||||
X509TrustManager trustAllCert = new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) {}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) {}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
};
|
||||
|
||||
// 初始化SSLContext
|
||||
SSLContext sslContext = SSLContext.getInstance("SSL");
|
||||
sslContext.init(null, new TrustManager[] {trustAllCert}, new SecureRandom());
|
||||
|
||||
// 创建不验证主机名的SocketFactory
|
||||
return new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE // 关键:禁用主机名验证
|
||||
);
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||
throw new RuntimeException("SSL配置失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class DateTimeUtils {
|
||||
|
||||
/**
|
||||
* 判断时间是否重叠
|
||||
*
|
||||
* @param start1 开始时间1
|
||||
* @param end1 开始时间1
|
||||
* @param start2 开始时间2
|
||||
* @param end2 开始时间2
|
||||
* @return 是否重叠
|
||||
*/
|
||||
public static boolean isOverlap(Date start1, Date end1, Date start2, Date end2) {
|
||||
// 判断是否重叠:start1 < end2 && end1 > start2
|
||||
return start1.before(end2) && end1.after(start2);
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import com.openhis.common.enums.HisEnumInterface;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class EnumUtils {
|
||||
|
||||
/**
|
||||
* 根据 value 获取枚举的 info
|
||||
*
|
||||
* @param enumClass 枚举类
|
||||
* @param value 枚举的 value
|
||||
* @param <E> 枚举类型
|
||||
* @return 对应的 info,如果未找到则返回 null
|
||||
*/
|
||||
public static <E extends Enum<E> & HisEnumInterface> String getInfoByValue(Class<E> enumClass, Integer value) {
|
||||
return Arrays.stream(enumClass.getEnumConstants()).filter(e -> e.getValue().equals(value)).findFirst()
|
||||
.map(HisEnumInterface::getInfo).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 code 获取枚举的 info
|
||||
*
|
||||
* @param enumClass 枚举类
|
||||
* @param code 枚举的 code
|
||||
* @param <E> 枚举类型
|
||||
* @return 对应的 info,如果未找到则返回 null
|
||||
*/
|
||||
public static <E extends Enum<E> & HisEnumInterface> String getInfoByCode(Class<E> enumClass, String code) {
|
||||
return Arrays.stream(enumClass.getEnumConstants())
|
||||
.filter(e -> e.getCode().equals(code))
|
||||
.findFirst()
|
||||
.map(HisEnumInterface:: getInfo)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class HisPageUtils {
|
||||
|
||||
/**
|
||||
* 执行分页查询并转换为目标类型
|
||||
*
|
||||
* @param mapper MyBatis Plus Mapper 接口
|
||||
* @param queryWrapper 查询条件
|
||||
* @param pageNo 当前页
|
||||
* @param pageSize 每页大小
|
||||
* @param targetClass 目标类(如 MedicationDto.class)
|
||||
* @param <T> 源对象类型(数据库实体类)
|
||||
* @param <R> 目标对象类型(DTO 类)
|
||||
* @return 分页结果(目标类型)
|
||||
*/
|
||||
public static <T, R> Page<R> selectPage(BaseMapper<T> mapper, QueryWrapper<T> queryWrapper, int pageNo,
|
||||
int pageSize, Class<R> targetClass) {
|
||||
// 构建分页对象
|
||||
Page<T> page = new Page<>(pageNo, pageSize);
|
||||
// 执行分页查询
|
||||
Page<T> sourcePage = mapper.selectPage(page, queryWrapper);
|
||||
// 转换为目标类型的分页对象
|
||||
Page<R> targetPage = new Page<>();
|
||||
// 复制分页信息
|
||||
BeanUtils.copyProperties(sourcePage, targetPage);
|
||||
// 转换记录列表
|
||||
targetPage.setRecords(convertToDtoList(sourcePage.getRecords(), targetClass));
|
||||
return targetPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将源对象列表转换为目标对象列表
|
||||
*
|
||||
* @param sourceList 源对象列表
|
||||
* @param targetClass 目标类
|
||||
* @param <T> 源对象类型
|
||||
* @param <R> 目标对象类型
|
||||
* @return 目标对象列表
|
||||
*/
|
||||
private static <T, R> List<R> convertToDtoList(List<T> sourceList, Class<R> targetClass) {
|
||||
return sourceList.stream().map(source -> convertToDto(source, targetClass)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将源对象转换为目标对象
|
||||
*
|
||||
* @param source 源对象
|
||||
* @param targetClass 目标类
|
||||
* @param <T> 源对象类型
|
||||
* @param <R> 目标对象类型
|
||||
* @return 目标对象
|
||||
*/
|
||||
private static <T, R> R convertToDto(T source, Class<R> targetClass) {
|
||||
try {
|
||||
// 创建目标对象实例
|
||||
R target = targetClass.getDeclaredConstructor().newInstance();
|
||||
BeanUtils.copyProperties(source, target); // 复制属性
|
||||
return target;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to convert object to DTO", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.core.common.utils.DateUtils;
|
||||
import com.core.common.utils.SecurityUtils;
|
||||
import com.core.framework.config.TenantContext;
|
||||
import com.openhis.common.constant.CommonConstants;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.lang.reflect.Field;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* His查询工具类
|
||||
*/
|
||||
public class HisQueryUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(HisQueryUtils.class);
|
||||
|
||||
/**
|
||||
* 条件查询构造器
|
||||
*
|
||||
* @param entity 传参实体
|
||||
* @param searchKey 模糊查询关键字
|
||||
* @param searchFields 支持模糊查询的字段集合 ; 不需要模糊查询传 null 即可
|
||||
* @param request 请求
|
||||
* @return 构造条件
|
||||
*/
|
||||
public static <T> QueryWrapper<T> buildQueryWrapper(Object entity, String searchKey, HashSet<String> searchFields,
|
||||
HttpServletRequest request) {
|
||||
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
||||
// 添加租户id查询条件
|
||||
queryWrapper.eq(CommonConstants.Common.TENANT_ID, getCurrentTenantId());
|
||||
// 处理模糊查询关键字
|
||||
if (searchKey != null && !searchKey.isEmpty() && searchFields != null && !searchFields.isEmpty()) {
|
||||
queryWrapper.and(wrapper -> {
|
||||
for (String field : searchFields) {
|
||||
wrapper.or().like(field, searchKey);
|
||||
}
|
||||
});
|
||||
}
|
||||
// 处理时间段查询
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD);
|
||||
if (request != null) {
|
||||
Map<String, String[]> parameterMap = request.getParameterMap();
|
||||
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
|
||||
String paramName = entry.getKey();
|
||||
// 检查参数名是否以 "STime" 或 "ETime" 结尾
|
||||
if (paramName.endsWith(CommonConstants.Common.S_TIME)) {
|
||||
// 提取字段名(去掉 "STime" 或 "ETime" 后缀)
|
||||
String fieldName = paramName.substring(0, paramName.length() - 5);
|
||||
// 驼峰转下划线
|
||||
String dbFieldName = camelToUnderline(fieldName);
|
||||
// 获取对应的 STime 和 ETime 值
|
||||
String startValue = getParameterValue(request, fieldName + CommonConstants.Common.S_TIME);
|
||||
String endValue = getParameterValue(request, fieldName + CommonConstants.Common.E_TIME);
|
||||
// 如果 Start 和 End 都有值,则添加时间段查询条件
|
||||
if (startValue != null && endValue != null) {
|
||||
try {
|
||||
SimpleDateFormat dateFormat;
|
||||
if (isValidFormat(formatter, startValue)) {
|
||||
dateFormat = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
|
||||
} else {
|
||||
dateFormat = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
|
||||
}
|
||||
Date startDate = dateFormat.parse(startValue);
|
||||
Date endDate = dateFormat.parse(endValue);
|
||||
queryWrapper.ge(dbFieldName, startDate); // 大于等于 STime
|
||||
queryWrapper.le(dbFieldName, endDate); // 小于等于 ETime
|
||||
} catch (ParseException e) {
|
||||
log.warn("日期解析失败: startValue={}, endValue={}", startValue, endValue, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (entity == null) {
|
||||
return queryWrapper;
|
||||
}
|
||||
// 反射获取实体类的所有字段(包括父类)
|
||||
Class<?> currentClass = entity.getClass();
|
||||
while (currentClass != null && currentClass != Object.class) {
|
||||
Field[] fields = currentClass.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
// 跳过静态字段,如 serialVersionUID
|
||||
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
Object value = field.get(entity);
|
||||
if (value != null && !value.toString().equals("")) {
|
||||
// 将驼峰命名的字段名转换为下划线命名的数据库字段名
|
||||
String fieldName = camelToUnderline(field.getName());
|
||||
// 处理等于条件
|
||||
queryWrapper.eq(fieldName, value);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
log.warn("反射获取字段值失败: field={}", field.getName(), e);
|
||||
}
|
||||
}
|
||||
currentClass = currentClass.getSuperclass();
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
// 新增SQL转义方法,防止注入风险
|
||||
private static String escapeSql(String value) {
|
||||
if (value == null) {
|
||||
return "";
|
||||
}
|
||||
// 转义单引号(PostgreSQL中用两个单引号表示一个单引号)
|
||||
return value.replace("'", "''");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查时间字符串是否符合指定格式
|
||||
*
|
||||
* @param formatter 时间格式
|
||||
* @param dateStr 时间字符串
|
||||
* @return 是否匹配
|
||||
*/
|
||||
private static boolean isValidFormat(DateTimeFormatter formatter, String dateStr) {
|
||||
try {
|
||||
formatter.parse(dateStr);
|
||||
return true;
|
||||
} catch (DateTimeParseException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 request 中获取参数值
|
||||
*/
|
||||
private static String getParameterValue(HttpServletRequest request, String paramName) {
|
||||
String[] values = request.getParameterValues(paramName);
|
||||
return (values != null && values.length > 0) ? values[0] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将驼峰命名的字段名转换为下划线命名的数据库字段名
|
||||
*/
|
||||
private static String camelToUnderline(String camel) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (char c : camel.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
result.append("_").append(Character.toLowerCase(c));
|
||||
} else {
|
||||
result.append(c);
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前租户 ID
|
||||
*/
|
||||
private static Integer getCurrentTenantId() {
|
||||
// 获取当前登录用户的租户 ID
|
||||
if (SecurityUtils.getAuthentication() != null) {
|
||||
return SecurityUtils.getLoginUser().getTenantId();
|
||||
} else if (TenantContext.getCurrentTenant() != null) {
|
||||
return TenantContext.getCurrentTenant();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Period;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
|
||||
/**
|
||||
* @ClassName IdCardAgeCalculator
|
||||
* @Description 根据身份证号 计算年纪
|
||||
* @Author raymond
|
||||
* @Date 2025/11/4 10:32
|
||||
* @Version 1.0
|
||||
**/
|
||||
public class IdCardAgeCalculator {
|
||||
/**
|
||||
* 根据身份证号计算年龄(支持15/18位)
|
||||
* @param idCard 身份证号
|
||||
* @return 年龄(若身份证号非法,返回-1)
|
||||
*/
|
||||
public static int calculateAge(String idCard) {
|
||||
// 1. 校验身份证号合法性(基础校验:长度、非空)
|
||||
if (!isValidIdCard(idCard)) {
|
||||
System.out.println("身份证号格式非法!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 2. 提取出生日期字符串(15位转18位格式)
|
||||
String birthDateStr = extractBirthDateStr(idCard);
|
||||
|
||||
// 3. 解析出生日期为LocalDate(处理格式异常)
|
||||
LocalDate birthDate;
|
||||
try {
|
||||
birthDate = LocalDate.parse(birthDateStr, DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
} catch (DateTimeParseException e) {
|
||||
System.out.println("出生日期解析失败,身份证号可能非法!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 4. 计算年龄(当前日期 - 出生日期)
|
||||
LocalDate currentDate = LocalDate.now(); // 若需指定日期,替换为 LocalDate.of(2024, 5, 20)
|
||||
return calculateAgeBetweenDates(birthDate, currentDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* 基础校验身份证号(非空、长度15或18位、数字/最后一位X)
|
||||
*/
|
||||
private static boolean isValidIdCard(String idCard) {
|
||||
if (idCard == null || idCard.trim().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
// 正则:15位纯数字,或18位数字(最后一位可大写X)
|
||||
String regex = "^(\\d{15}|\\d{17}([0-9]|X))$";
|
||||
return idCard.matches(regex);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从身份证号中提取出生日期字符串(统一转为yyyyMMdd格式)
|
||||
*/
|
||||
private static String extractBirthDateStr(String idCard) {
|
||||
String birthDateStr;
|
||||
if (idCard.length() == 18) {
|
||||
// 18位:第7-14位(索引6-13)
|
||||
birthDateStr = idCard.substring(6, 14);
|
||||
} else {
|
||||
// 15位:第7-12位(索引6-11),补前两位年份(19xx或20xx,这里简化为19xx,实际需根据规则判断)
|
||||
String year = "19" + idCard.substring(6, 8);
|
||||
String monthDay = idCard.substring(8, 12);
|
||||
birthDateStr = year + monthDay;
|
||||
}
|
||||
return birthDateStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据出生日期和当前日期计算年龄(处理未过生日的情况)
|
||||
*/
|
||||
private static int calculateAgeBetweenDates(LocalDate birthDate, LocalDate currentDate) {
|
||||
// 计算年份差
|
||||
Period period = Period.between(birthDate, currentDate);
|
||||
int age = period.getYears();
|
||||
|
||||
// 检查当前日期是否已超过当年的出生日期(未超过则年龄减1)
|
||||
LocalDate birthdayThisYear = birthDate.withYear(currentDate.getYear());
|
||||
if (currentDate.isBefore(birthdayThisYear)) {
|
||||
age--;
|
||||
}
|
||||
|
||||
// 防止年龄为负数(如出生日期在当前日期之后,理论上身份证号不会出现此情况)
|
||||
return Math.max(age, 0);
|
||||
}
|
||||
|
||||
// 测试示例
|
||||
public static void main(String[] args) {
|
||||
// 测试1:18位身份证(出生日期2000-01-01,当前日期2024-05-20 → 年龄24)
|
||||
String idCard18 = "110101200001011234";
|
||||
System.out.println("18位身份证年龄:" + calculateAge(idCard18));
|
||||
|
||||
// 测试2:15位身份证(出生日期2000-01-01 → 15位表示为000101,补全后20000101)
|
||||
String idCard15 = "110101000101123";
|
||||
System.out.println("15位身份证年龄:" + calculateAge(idCard15));
|
||||
|
||||
// 测试3:未过生日的情况(出生日期2000-06-01,当前日期2024-05-20 → 年龄23)
|
||||
String idCardNotBirthday = "110101200006011234";
|
||||
System.out.println("未过生日的年龄:" + calculateAge(idCardNotBirthday));
|
||||
|
||||
// 测试4:非法身份证号(长度错误)
|
||||
String idCardInvalid = "11010120000101123"; // 17位
|
||||
System.out.println("非法身份证年龄:" + calculateAge(idCardInvalid));
|
||||
}
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
/*
|
||||
* Copyright ©2023 CJB-CNIT Team. All rights reserved
|
||||
*/
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import com.openhis.common.constant.CommonConstants;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* 身份证号生成工具类
|
||||
*
|
||||
* @author zwh
|
||||
* @date 2025-07-24
|
||||
*/
|
||||
public class IdCardUtil {
|
||||
|
||||
// 权重因子(用于计算校验码)
|
||||
private static final int[] WEIGHT_FACTORS = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
||||
// 校验码对应表
|
||||
private static final char[] CHECK_CODES = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
|
||||
|
||||
/**
|
||||
* 根据年龄生成类似身份证的唯一编码 格式:6位地区码 + 8位出生日期 + 3位随机码 + 1位校验码
|
||||
*
|
||||
* @param age 年龄(整数)
|
||||
* @return 18位身份证号
|
||||
*/
|
||||
public static String generateIdByAge(int age) {
|
||||
// 1. 生成出生日期(确保当前年龄计算准确)
|
||||
LocalDate today = LocalDate.now();
|
||||
// 最大出生日期(今天减去年龄年)
|
||||
LocalDate maxBirthDate = today.minusYears(age);
|
||||
// 最小出生日期(最大日期减1年加1天,确保年龄计算准确)
|
||||
LocalDate minBirthDate = maxBirthDate.minusYears(1).plusDays(1);
|
||||
|
||||
// 生成介于最小和最大日期之间的随机日期
|
||||
long minDay = minBirthDate.toEpochDay();
|
||||
long maxDay = maxBirthDate.toEpochDay();
|
||||
long randomDay = ThreadLocalRandom.current().nextLong(minDay, maxDay + 1);
|
||||
|
||||
String birthDateStr = LocalDate.ofEpochDay(randomDay).format(DateTimeFormatter.BASIC_ISO_DATE); // yyyyMMdd
|
||||
|
||||
// 2. 生成3位随机码(001-999)
|
||||
String randomCode = String.format("%03d", ThreadLocalRandom.current().nextInt(1, 1000));
|
||||
|
||||
// 3. 组合前17位
|
||||
String partialId = CommonConstants.Common.AREA_CODE + birthDateStr + randomCode;
|
||||
|
||||
// 4. 计算校验码
|
||||
int sum = 0;
|
||||
for (int i = 0; i < partialId.length(); i++) {
|
||||
int digit = Character.getNumericValue(partialId.charAt(i));
|
||||
sum += digit * WEIGHT_FACTORS[i];
|
||||
}
|
||||
char checksum = CHECK_CODES[sum % 11];
|
||||
return partialId + checksum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从身份证号提取生日日期
|
||||
*
|
||||
* @param idCard 身份证号码(15位或18位)
|
||||
* @return 格式化后的生日日期字符串,格式为"yyyy-MM-dd HH:mm:ss.SSS Z"
|
||||
* @throws IllegalArgumentException 如果身份证号格式无效
|
||||
*/
|
||||
public static Date extractBirthdayFromIdCard(String idCard) {
|
||||
// 验证身份证号长度
|
||||
if (idCard == null || (idCard.length() != 15 && idCard.length() != 18)) {
|
||||
throw new IllegalArgumentException("身份证号码长度无效,必须是15位或18位");
|
||||
}
|
||||
String birthdayStr;
|
||||
// 处理15位身份证号(7-12位是生日,年份只有后两位)
|
||||
if (idCard.length() == 15) {
|
||||
// 15位身份证:年份前加"19",月份和日期直接取
|
||||
birthdayStr = "19" + idCard.substring(6, 8) + idCard.substring(8, 12);
|
||||
}
|
||||
// 处理18位身份证号(7-14位是完整生日)
|
||||
else {
|
||||
birthdayStr = idCard.substring(6, 14);
|
||||
}
|
||||
// 验证生日字符串是否全为数字
|
||||
if (!birthdayStr.matches("\\d{8}")) {
|
||||
throw new IllegalArgumentException("身份证号码中的生日部分无效");
|
||||
}
|
||||
// 解析生日日期
|
||||
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
|
||||
inputFormat.setLenient(false); // 严格模式,防止无效日期如2月30日
|
||||
try {
|
||||
// 解析为Date对象
|
||||
return inputFormat.parse(birthdayStr);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException("无法解析生日日期: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据身份证号计算年龄
|
||||
*
|
||||
* @param idCard 身份证号码(15位或18位)
|
||||
* @return 计算得到的年龄(整数)
|
||||
* @throws IllegalArgumentException 如果身份证号格式无效或包含非法字符
|
||||
*/
|
||||
public static Integer calculateAgeFromIdCard(String idCard) {
|
||||
// 验证身份证号基本格式
|
||||
if (idCard == null || (idCard.length() != 15 && idCard.length() != 18)) {
|
||||
throw new IllegalArgumentException("身份证号码长度无效,必须是15位或18位");
|
||||
}
|
||||
|
||||
// 验证身份证号字符合法性(仅包含数字和X)
|
||||
if (!idCard.matches("^[0-9Xx]+$")) {
|
||||
throw new IllegalArgumentException("身份证号码包含非法字符");
|
||||
}
|
||||
|
||||
try {
|
||||
// 提取出生日期
|
||||
String birthYearStr, birthMonthStr, birthDayStr;
|
||||
|
||||
if (idCard.length() == 15) {
|
||||
// 15位身份证:7-8位为年份(后两位),9-10位为月份,11-12位为日期
|
||||
birthYearStr = "19" + idCard.substring(6, 8);
|
||||
birthMonthStr = idCard.substring(8, 10);
|
||||
birthDayStr = idCard.substring(10, 12);
|
||||
} else {
|
||||
// 18位身份证:7-10位为年份,11-12位为月份,13-14位为日期
|
||||
birthYearStr = idCard.substring(6, 10);
|
||||
birthMonthStr = idCard.substring(10, 12);
|
||||
birthDayStr = idCard.substring(12, 14);
|
||||
}
|
||||
|
||||
// 转换为整数
|
||||
int birthYear = Integer.parseInt(birthYearStr);
|
||||
int birthMonth = Integer.parseInt(birthMonthStr);
|
||||
int birthDay = Integer.parseInt(birthDayStr);
|
||||
|
||||
// 验证日期有效性
|
||||
if (birthMonth < 1 || birthMonth > 12 || birthDay < 1 || birthDay > 31) {
|
||||
throw new IllegalArgumentException("身份证中的出生日期无效");
|
||||
}
|
||||
|
||||
// 获取当前日期
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
int currentYear = calendar.get(Calendar.YEAR);
|
||||
int currentMonth = calendar.get(Calendar.MONTH) + 1; // Calendar月份从0开始
|
||||
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
|
||||
// 计算年龄
|
||||
int age = currentYear - birthYear;
|
||||
|
||||
// 判断是否已过生日
|
||||
if (currentMonth < birthMonth || (currentMonth == birthMonth && currentDay < birthDay)) {
|
||||
age--; // 未过生日,年龄减1
|
||||
}
|
||||
|
||||
// 验证年龄合理性
|
||||
if (age < 0 || age > 150) {
|
||||
throw new IllegalArgumentException("计算得到的年龄超出合理范围");
|
||||
}
|
||||
|
||||
return age;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("身份证号码中的日期部分格式错误", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页工具类
|
||||
*/
|
||||
public class PageUtils {
|
||||
|
||||
/**
|
||||
* 手动分页
|
||||
*
|
||||
* @param allList 数据
|
||||
* @param pageNo 页码
|
||||
* @param pageSize 每页大小
|
||||
* @return 分页数据
|
||||
*/
|
||||
public static <T> Page<T> buildPage(List<T> allList, long pageNo, long pageSize) {
|
||||
Page<T> page = new Page<>();
|
||||
|
||||
int total = allList.size();
|
||||
if (total == 0) {
|
||||
page.setRecords(new ArrayList<>());
|
||||
page.setTotal(0);
|
||||
page.setCurrent(pageNo);
|
||||
page.setSize(pageSize);
|
||||
page.setPages(0L);
|
||||
return page;
|
||||
}
|
||||
|
||||
// 计算分页起始位置
|
||||
int startIndex = (int)((pageNo - 1) * pageSize);
|
||||
if (startIndex >= total) {
|
||||
// 如果起始位置超过总数,返回最后一页
|
||||
startIndex = (int)((total - 1) / pageSize * pageSize);
|
||||
pageNo = (total + pageSize - 1) / pageSize;
|
||||
}
|
||||
|
||||
int endIndex = (int)Math.min(startIndex + pageSize, total);
|
||||
|
||||
// 获取当前页数据
|
||||
List<T> currentPageList = allList.subList(startIndex, endIndex);
|
||||
|
||||
// 设置分页信息
|
||||
page.setRecords(new ArrayList<>(currentPageList));
|
||||
page.setCurrent(pageNo);
|
||||
page.setSize(pageSize);
|
||||
page.setTotal(total);
|
||||
page.setPages((total + pageSize - 1) / pageSize); // 计算总页数
|
||||
|
||||
return page;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.openhis.common.utils;
|
||||
|
||||
/**
|
||||
* Redis所有Keys
|
||||
*/
|
||||
public class RedisKeys {
|
||||
|
||||
|
||||
/**
|
||||
* 商品缓存
|
||||
* @param itemId
|
||||
* @return
|
||||
*/
|
||||
public static String getProductsKey(String itemId){
|
||||
return "products_change_price:item_" + itemId + "_key";
|
||||
}
|
||||
|
||||
/**
|
||||
* 手术信息缓存
|
||||
* @param surgeryId 手术ID
|
||||
* @return
|
||||
*/
|
||||
public static String getSurgeryKey(Long surgeryId){
|
||||
return "surgery:info:" + surgeryId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 手术列表缓存(按患者ID)
|
||||
* @param patientId 患者ID
|
||||
* @return
|
||||
*/
|
||||
public static String getSurgeryListByPatientKey(Long patientId){
|
||||
return "surgery:patient:" + patientId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 手术列表缓存(按就诊ID)
|
||||
* @param encounterId 就诊ID
|
||||
* @return
|
||||
*/
|
||||
public static String getSurgeryListByEncounterKey(Long encounterId){
|
||||
return "surgery:encounter:" + encounterId;
|
||||
}
|
||||
}
|
||||
@@ -1,616 +0,0 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* redis 工具类
|
||||
* @Author Scott
|
||||
*
|
||||
*/
|
||||
@Component
|
||||
public class RedisUtil {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<Object, Object> redisTemplate;
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 指定缓存失效时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean expire(String key, long time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
redisTemplate.expire(key, time, TimeUnit.SECONDS);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key 获取过期时间
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @return 时间(秒) 返回0代表为永久有效
|
||||
*/
|
||||
public long getExpire(String key) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断key是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean hasKey(String key) {
|
||||
try {
|
||||
return redisTemplate.hasKey(key);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
*
|
||||
* @param key 可以传一个值 或多个
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void del(String... key) {
|
||||
if (key != null && key.length > 0) {
|
||||
if (key.length == 1) {
|
||||
redisTemplate.delete(key[0]);
|
||||
} else {
|
||||
redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================String=============================
|
||||
/**
|
||||
* 普通缓存获取
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
*/
|
||||
public Object get(String key) {
|
||||
return key == null ? null : redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通缓存放入
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return true成功 false失败
|
||||
*/
|
||||
public boolean set(String key, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 普通缓存放入并设置时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
|
||||
* @return true成功 false 失败
|
||||
*/
|
||||
public boolean set(String key, Object value, long time) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
|
||||
} else {
|
||||
set(key, value);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递增
|
||||
*
|
||||
* @param key 键
|
||||
* @param by 要增加几(大于0)
|
||||
* @return
|
||||
*/
|
||||
public long incr(String key, long delta) {
|
||||
if (delta < 0) {
|
||||
throw new RuntimeException("递增因子必须大于0");
|
||||
}
|
||||
return redisTemplate.opsForValue().increment(key, delta);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递减
|
||||
*
|
||||
* @param key 键
|
||||
* @param by 要减少几(小于0)
|
||||
* @return
|
||||
*/
|
||||
public long decr(String key, long delta) {
|
||||
if (delta < 0) {
|
||||
throw new RuntimeException("递减因子必须大于0");
|
||||
}
|
||||
return redisTemplate.opsForValue().increment(key, -delta);
|
||||
}
|
||||
|
||||
// ================================Map=================================
|
||||
/**
|
||||
* HashGet
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 不能为null
|
||||
* @return 值
|
||||
*/
|
||||
public Object hget(String key, String item) {
|
||||
return redisTemplate.opsForHash().get(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取hashKey对应的所有键值
|
||||
*
|
||||
* @param key 键
|
||||
* @return 对应的多个键值
|
||||
*/
|
||||
public Map<Object, Object> hmget(String key) {
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* HashSet
|
||||
*
|
||||
* @param key 键
|
||||
* @param map 对应多个键值
|
||||
* @return true 成功 false 失败
|
||||
*/
|
||||
public boolean hmset(String key, Map<String, Object> map) {
|
||||
try {
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* HashSet 并设置时间
|
||||
*
|
||||
* @param key 键
|
||||
* @param map 对应多个键值
|
||||
* @param time 时间(秒)
|
||||
* @return true成功 false失败
|
||||
*/
|
||||
public boolean hmset(String key, Map<String, Object> map, long time) {
|
||||
try {
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向一张hash表中放入数据,如果不存在将创建
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param value 值
|
||||
* @return true 成功 false失败
|
||||
*/
|
||||
public boolean hset(String key, String item, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForHash().put(key, item, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 向一张hash表中放入数据,如果不存在将创建
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param value 值
|
||||
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
|
||||
* @return true 成功 false失败
|
||||
*/
|
||||
public boolean hset(String key, String item, Object value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForHash().put(key, item, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除hash表中的值
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 可以使多个 不能为null
|
||||
*/
|
||||
public void hdel(String key, Object... item) {
|
||||
redisTemplate.opsForHash().delete(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断hash表中是否有该项的值
|
||||
*
|
||||
* @param key 键 不能为null
|
||||
* @param item 项 不能为null
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean hHasKey(String key, String item) {
|
||||
return redisTemplate.opsForHash().hasKey(key, item);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param by 要增加几(大于0)
|
||||
* @return
|
||||
*/
|
||||
public double hincr(String key, String item, double by) {
|
||||
return redisTemplate.opsForHash().increment(key, item, by);
|
||||
}
|
||||
|
||||
/**
|
||||
* hash递减
|
||||
*
|
||||
* @param key 键
|
||||
* @param item 项
|
||||
* @param by 要减少记(小于0)
|
||||
* @return
|
||||
*/
|
||||
public double hdecr(String key, String item, double by) {
|
||||
return redisTemplate.opsForHash().increment(key, item, -by);
|
||||
}
|
||||
|
||||
// ============================set=============================
|
||||
/**
|
||||
* 根据key获取Set中的所有值
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public Set<Object> sGet(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据value从一个set中查询,是否存在
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return true 存在 false不存在
|
||||
*/
|
||||
public boolean sHasKey(String key, Object value) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().isMember(key, value);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据放入set缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param values 值 可以是多个
|
||||
* @return 成功个数
|
||||
*/
|
||||
public long sSet(String key, Object... values) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().add(key, values);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将set数据放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param time 时间(秒)
|
||||
* @param values 值 可以是多个
|
||||
* @return 成功个数
|
||||
*/
|
||||
public long sSetAndTime(String key, long time, Object... values) {
|
||||
try {
|
||||
Long count = redisTemplate.opsForSet().add(key, values);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return count;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取set缓存的长度
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public long sGetSetSize(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForSet().size(key);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除值为value的
|
||||
*
|
||||
* @param key 键
|
||||
* @param values 值 可以是多个
|
||||
* @return 移除的个数
|
||||
*/
|
||||
public long setRemove(String key, Object... values) {
|
||||
try {
|
||||
Long count = redisTemplate.opsForSet().remove(key, values);
|
||||
return count;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// ===============================list=================================
|
||||
|
||||
/**
|
||||
* 获取list缓存的内容
|
||||
*
|
||||
* @param key 键
|
||||
* @param start 开始
|
||||
* @param end 结束 0 到 -1代表所有值
|
||||
* @return
|
||||
*/
|
||||
public List<Object> lGet(String key, long start, long end) {
|
||||
try {
|
||||
return redisTemplate.opsForList().range(key, start, end);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取list缓存的长度
|
||||
*
|
||||
* @param key 键
|
||||
* @return
|
||||
*/
|
||||
public long lGetListSize(String key) {
|
||||
try {
|
||||
return redisTemplate.opsForList().size(key);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过索引 获取list中的值
|
||||
*
|
||||
* @param key 键
|
||||
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
|
||||
* @return
|
||||
*/
|
||||
public Object lGetIndex(String key, long index) {
|
||||
try {
|
||||
return redisTemplate.opsForList().index(key, index);
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, Object value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPush(key, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, List<Object> value) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPushAll(key, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将list放入缓存
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param time 时间(秒)
|
||||
* @return
|
||||
*/
|
||||
public boolean lSet(String key, List<Object> value, long time) {
|
||||
try {
|
||||
redisTemplate.opsForList().rightPushAll(key, value);
|
||||
if (time > 0) {
|
||||
expire(key, time);
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据索引修改list中的某条数据
|
||||
*
|
||||
* @param key 键
|
||||
* @param index 索引
|
||||
* @param value 值
|
||||
* @return
|
||||
*/
|
||||
public boolean lUpdateIndex(String key, long index, Object value) {
|
||||
try {
|
||||
redisTemplate.opsForList().set(key, index, value);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis expire操作失败, key: {}", key, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除N个值为value
|
||||
*
|
||||
* @param key 键
|
||||
* @param count 移除多少个
|
||||
* @param value 值
|
||||
* @return 移除的个数
|
||||
*/
|
||||
public long lRemove(String key, long count, Object value) {
|
||||
try {
|
||||
Long remove = redisTemplate.opsForList().remove(key, count, value);
|
||||
return remove;
|
||||
} catch (Exception e) {
|
||||
log.error("Redis操作失败, key: {}", key, e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定前缀的一系列key
|
||||
* 使用scan命令代替keys, Redis是单线程处理,keys命令在KEY数量较多时,
|
||||
* 操作效率极低【时间复杂度为O(N)】,该命令一旦执行会严重阻塞线上其它命令的正常请求
|
||||
* @param keyPrefix
|
||||
* @return
|
||||
*/
|
||||
private Set<String> keys(String keyPrefix) {
|
||||
String realKey = keyPrefix + "*";
|
||||
|
||||
try {
|
||||
return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
|
||||
Set<String> binaryKeys = new HashSet<>();
|
||||
Cursor<byte[]> cursor = connection.scan(new ScanOptions.ScanOptionsBuilder().match(realKey).count(Integer.MAX_VALUE).build());
|
||||
while (cursor.hasNext()) {
|
||||
binaryKeys.add(new String(cursor.next()));
|
||||
}
|
||||
|
||||
return binaryKeys;
|
||||
});
|
||||
} catch (Throwable e) {
|
||||
log.error("Redis scan操作失败, keyPrefix: {}", keyPrefix, e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定前缀的一系列key
|
||||
* @param keyPrefix
|
||||
*/
|
||||
public void removeAll(String keyPrefix) {
|
||||
try {
|
||||
Set<String> keys = keys(keyPrefix);
|
||||
redisTemplate.delete(keys);
|
||||
} catch (Throwable e) {
|
||||
log.error("Redis删除操作失败, keyPrefix: {}", keyPrefix, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user