refactor: 日期范围查询时间比较代码优化
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
|
||||
package com.youlai.system.common.util;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
@@ -9,7 +8,6 @@ import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 日期工具类
|
||||
@@ -19,15 +17,16 @@ import java.util.List;
|
||||
*/
|
||||
public class DateUtils {
|
||||
|
||||
|
||||
/**
|
||||
* 将日期格式化为数据库日期格式 (yyyy-MM-dd HH:mm:ss) 并更新对象中指定的起始时间字段和结束时间字段。
|
||||
* 区间日期格式化为数据库日期格式
|
||||
* <p>
|
||||
* eg:2021-01-01 → 2021-01-01 00:00:00
|
||||
*
|
||||
* @param obj 要处理的对象
|
||||
* @param obj 要处理的对象
|
||||
* @param startTimeFieldName 起始时间字段名
|
||||
* @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 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) {
|
||||
Object fieldValue = ReflectUtil.getFieldValue(obj, fieldName);
|
||||
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);
|
||||
// 转换为目标数据库日期格式
|
||||
ReflectUtil.setFieldValue(obj, fieldName, dateTime.toString(targetPattern));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,8 +74,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
int pageSize = queryParams.getPageSize();
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user