diff --git a/openhis-server/openhis-common/src/main/java/com/openhis/common/utils/HisPageUtils.java b/openhis-server/openhis-common/src/main/java/com/openhis/common/utils/HisPageUtils.java new file mode 100644 index 00000000..4a70f7e5 --- /dev/null +++ b/openhis-server/openhis-common/src/main/java/com/openhis/common/utils/HisPageUtils.java @@ -0,0 +1,74 @@ +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 源对象类型(数据库实体类) + * @param 目标对象类型(DTO 类) + * @return 分页结果(目标类型) + */ + public static Page selectPage(BaseMapper mapper, QueryWrapper queryWrapper, + int pageNo, int pageSize, Class targetClass) { + // 构建分页对象 + Page page = new Page<>(pageNo, pageSize); + // 执行分页查询 + Page sourcePage = mapper.selectPage(page, queryWrapper); + // 转换为目标类型的分页对象 + Page targetPage = new Page<>(); + // 复制分页信息 + BeanUtils.copyProperties(sourcePage, targetPage); + // 转换记录列表 + targetPage.setRecords(convertToDtoList(sourcePage.getRecords(), targetClass)); + return targetPage; + } + + /** + * 将源对象列表转换为目标对象列表 + * + * @param sourceList 源对象列表 + * @param targetClass 目标类 + * @param 源对象类型 + * @param 目标对象类型 + * @return 目标对象列表 + */ + private static List convertToDtoList(List sourceList, Class targetClass) { + return sourceList.stream() + .map(source -> convertToDto(source, targetClass)) + .collect(Collectors.toList()); + } + + /** + * 将源对象转换为目标对象 + * + * @param source 源对象 + * @param targetClass 目标类 + * @param 源对象类型 + * @param 目标对象类型 + * @return 目标对象 + */ + private static R convertToDto(T source, Class 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); + } + } +} diff --git a/openhis-server/openhis-common/src/main/java/com/openhis/common/utils/HisQueryUtils.java b/openhis-server/openhis-common/src/main/java/com/openhis/common/utils/HisQueryUtils.java index a1f5ce94..c2765cae 100644 --- a/openhis-server/openhis-common/src/main/java/com/openhis/common/utils/HisQueryUtils.java +++ b/openhis-server/openhis-common/src/main/java/com/openhis/common/utils/HisQueryUtils.java @@ -24,14 +24,14 @@ public class HisQueryUtils { /** * 条件查询构造器 * - * @param entity 传参实体 - * @param searchKey 模糊查询关键字 + * @param entity 传参实体 + * @param searchKey 模糊查询关键字 * @param searchFields 支持模糊查询的字段集合 ; 不需要模糊查询传 null 即可 - * @param request 请求 + * @param request 请求 * @return 构造条件 */ - public static QueryWrapper buildQueryWrapper(T entity, String searchKey, HashSet searchFields, - HttpServletRequest request) { + public static QueryWrapper buildQueryWrapper(Object entity, String searchKey, HashSet searchFields, + HttpServletRequest request) { QueryWrapper queryWrapper = new QueryWrapper<>(); // 添加租户id查询条件 queryWrapper.eq(CommonConstants.Common.TENANT_ID, getCurrentTenantId()); @@ -45,7 +45,6 @@ public class HisQueryUtils { try { Object value = field.get(entity); if (value != null && !value.toString().equals("")) { - // String fieldName = field.getName(); // 将驼峰命名的字段名转换为下划线命名的数据库字段名 String fieldName = camelToUnderline(field.getName()); // 处理等于条件 @@ -65,13 +64,12 @@ public class HisQueryUtils { } // 处理时间段查询 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateUtils.YYYY_MM_DD); - // DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); Map parameterMap = request.getParameterMap(); for (Map.Entry entry : parameterMap.entrySet()) { String paramName = entry.getKey(); // 检查参数名是否以 "STime" 或 "ETime" 结尾 if (paramName.endsWith(CommonConstants.Common.S_TIME) - || paramName.endsWith(CommonConstants.Common.E_TIME)) { + || paramName.endsWith(CommonConstants.Common.E_TIME)) { // 提取字段名(去掉 "STime" 或 "ETime" 后缀) String fieldName = paramName.substring(0, paramName.length() - 5); // 驼峰转下划线