refactor: 日期范围查询时间比较代码优化

This commit is contained in:
haoxr
2023-10-20 22:56:28 +08:00
parent ee438b5370
commit 693fad43bd
2 changed files with 19 additions and 10 deletions

View File

@@ -1,7 +1,6 @@
package com.youlai.system.common.util; package com.youlai.system.common.util;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
@@ -9,7 +8,6 @@ import cn.hutool.core.util.StrUtil;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List;
/** /**
* 日期工具类 * 日期工具类
@@ -19,15 +17,16 @@ import java.util.List;
*/ */
public class DateUtils { public class DateUtils {
/** /**
* 日期格式化为数据库日期格式 (yyyy-MM-dd HH:mm:ss) 并更新对象中指定的起始时间字段和结束时间字段。 * 区间日期格式化为数据库日期格式
* <p>
* eg2021-01-01 → 2021-01-01 00:00:00
* *
* @param obj 要处理的对象 * @param obj 要处理的对象
* @param startTimeFieldName 起始时间字段名 * @param startTimeFieldName 起始时间字段名
* @param endTimeFieldName 结束时间字段名 * @param endTimeFieldName 结束时间字段名
*/ */
public static void formatDateTimeForDatabase(Object obj, String startTimeFieldName, String endTimeFieldName) { public static void toDatabaseFormat(Object obj, String startTimeFieldName, String endTimeFieldName) {
Field startTimeField = ReflectUtil.getField(obj.getClass(), startTimeFieldName); Field startTimeField = ReflectUtil.getField(obj.getClass(), startTimeFieldName);
Field endTimeField = ReflectUtil.getField(obj.getClass(), endTimeFieldName); Field endTimeField = ReflectUtil.getField(obj.getClass(), endTimeFieldName);
@@ -40,12 +39,22 @@ public class DateUtils {
} }
} }
/**
* 处理日期字段
*
* @param obj 要处理的对象
* @param field 字段
* @param fieldName 字段名
* @param targetPattern 目标数据库日期格式
*/
private static void processDateTimeField(Object obj, Field field, String fieldName, String targetPattern) { private static void processDateTimeField(Object obj, Field field, String fieldName, String targetPattern) {
Object fieldValue = ReflectUtil.getFieldValue(obj, fieldName); Object fieldValue = ReflectUtil.getFieldValue(obj, fieldName);
if (fieldValue != null) { if (fieldValue != null) {
String pattern = field.isAnnotationPresent(DateTimeFormat.class) ? // 得到原始的日期格式
field.getAnnotation(DateTimeFormat.class).pattern() : "yyyy-MM-dd"; String pattern = field.isAnnotationPresent(DateTimeFormat.class) ? field.getAnnotation(DateTimeFormat.class).pattern() : "yyyy-MM-dd";
// 转换为日期对象
DateTime dateTime = DateUtil.parse(StrUtil.toString(fieldValue), pattern); DateTime dateTime = DateUtil.parse(StrUtil.toString(fieldValue), pattern);
// 转换为目标数据库日期格式
ReflectUtil.setFieldValue(obj, fieldName, dateTime.toString(targetPattern)); ReflectUtil.setFieldValue(obj, fieldName, dateTime.toString(targetPattern));
} }
} }

View File

@@ -74,8 +74,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
int pageSize = queryParams.getPageSize(); int pageSize = queryParams.getPageSize();
Page<UserBO> page = new Page<>(pageNum, pageSize); Page<UserBO> page = new Page<>(pageNum, pageSize);
// 日期格式化为数据库日期格式,避免日期比较使用函数导致索引失效 // 格式化为数据库日期格式,避免日期比较使用格式化函数导致索引失效
DateUtils.formatDateTimeForDatabase(queryParams, "startTime", "endTime"); DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
// 查询数据 // 查询数据
Page<UserBO> userPage = this.baseMapper.getUserPage(page, queryParams); Page<UserBO> userPage = this.baseMapper.getUserPage(page, queryParams);