版本更新
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
/**
|
||||
* 共通工具类
|
||||
*
|
||||
* @author system
|
||||
*/
|
||||
public class CommonUtil {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.openhis.common.enums.HisEnumInterface;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.openhis.common.utils;
|
||||
|
||||
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;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.core.common.utils.DateUtils;
|
||||
import com.core.common.utils.SecurityUtils;
|
||||
import com.openhis.common.constant.CommonConstants;
|
||||
|
||||
/**
|
||||
* His查询工具类
|
||||
*/
|
||||
public class HisQueryUtils {
|
||||
|
||||
/**
|
||||
* 条件查询构造器
|
||||
*
|
||||
* @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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (entity == null) {
|
||||
return queryWrapper;
|
||||
}
|
||||
// 反射获取实体类的字段
|
||||
Field[] fields = entity.getClass().getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查时间字符串是否符合指定格式
|
||||
*
|
||||
* @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();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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.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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user