refactor: 优化过滤逻辑

This commit is contained in:
junjun 2024-03-08 15:06:17 +08:00
parent d709e194b0
commit cdd62c2932
4 changed files with 108 additions and 41 deletions

View File

@ -6,13 +6,13 @@ import io.dataease.api.dataset.union.model.SQLMeta;
import io.dataease.api.dataset.union.model.SQLObj;
import io.dataease.dto.dataset.DatasetTableFieldDTO;
import io.dataease.engine.constant.SQLConstants;
import io.dataease.engine.utils.DateUtils;
import io.dataease.engine.utils.Utils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Author Junjun
@ -47,18 +47,15 @@ public class CustomWhere2Str {
if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) {
// 此处获取标准格式的日期
whereName = String.format(SQLConstants.DE_STR_TO_DATE, originName, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT);
whereName = String.format(SQLConstants.DATE_FORMAT, whereName, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT);
}
if (field.getDeExtractType() == 2 || field.getDeExtractType() == 3 || field.getDeExtractType() == 4) {
String cast = String.format(SQLConstants.CAST, originName, SQLConstants.DEFAULT_INT_FORMAT);
// 此处获取标准格式的日期
whereName = String.format(SQLConstants.FROM_UNIXTIME, cast, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT);
whereName = String.format(SQLConstants.DATE_FORMAT, whereName, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT);
whereName = String.format(SQLConstants.FROM_UNIXTIME, cast, SQLConstants.DEFAULT_DATE_FORMAT);
}
if (field.getDeExtractType() == 1) {
// 此处获取标准格式的日期
String f = DateUtils.get_date_format(originName);
whereName = String.format(SQLConstants.DATE_FORMAT, originName, f);
whereName = originName;
}
} else if (field.getDeType() == 2 || field.getDeType() == 3) {
if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) {
@ -88,14 +85,10 @@ public class CustomWhere2Str {
String whereTerm = Utils.transFilterTerm(filterItemDTO.getTerm());
String whereValue = "";
String whereNameReal;
// String whereNameReal;
if (field.getDeType() == 1) {
// 规定几种日期格式一一匹配匹配到就是该格式
String f = DateUtils.get_date_format(filterItemDTO.getValue());
String n = String.format(SQLConstants.DE_STR_TO_DATE, whereName, f);
whereNameReal = String.format(SQLConstants.UNIX_TIMESTAMP, n);
} else {
whereNameReal = whereName;
whereName = String.format(SQLConstants.UNIX_TIMESTAMP, whereName);
}
if (StringUtils.equalsIgnoreCase(filterItemDTO.getTerm(), "null")) {
@ -111,13 +104,27 @@ public class CustomWhere2Str {
} else if (StringUtils.containsIgnoreCase(filterItemDTO.getTerm(), "like")) {
whereValue = "'%" + value + "%'";
} else {
// 如果是时间字段过滤当条件是等于和不等于的时候转换成between和not between
if (field.getDeType() == 1) {
value = Utils.allDateFormat2Long(value) + "";
if (StringUtils.containsIgnoreCase(whereTerm, "=")) {
whereTerm = " BETWEEN ";
// 把value类似过滤组件处理获得start time和end time
Map<String, Long> stringLongMap = Utils.parseDateTimeValue(value);
whereValue = String.format(SQLConstants.WHERE_VALUE_BETWEEN, stringLongMap.get("startTime"), stringLongMap.get("endTime"));
} else if (StringUtils.containsIgnoreCase(whereTerm, "<>")) {
whereTerm = " NOT BETWEEN ";
Map<String, Long> stringLongMap = Utils.parseDateTimeValue(value);
whereValue = String.format(SQLConstants.WHERE_VALUE_BETWEEN, stringLongMap.get("startTime"), stringLongMap.get("endTime"));
} else {
value = Utils.allDateFormat2Long(value) + "";
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
list.add(SQLObj.builder()
.whereField(whereNameReal)
.whereField(whereName)
.whereTermAndValue(whereTerm + whereValue)
.build());
}

View File

@ -5,7 +5,6 @@ import io.dataease.api.dataset.union.model.SQLMeta;
import io.dataease.api.dataset.union.model.SQLObj;
import io.dataease.dto.dataset.DatasetTableFieldDTO;
import io.dataease.engine.constant.SQLConstants;
import io.dataease.engine.utils.DateUtils;
import io.dataease.engine.utils.Utils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@ -53,12 +52,6 @@ public class ExtWhere2Str {
}
if (field.getDeType() == 1) {
String date_format;
if (StringUtils.containsIgnoreCase(request.getOperator(), "between")) {
date_format = "yyyy-MM-dd HH:mm:ss";
} else {
date_format = DateUtils.get_date_format(value.get(0));
}
if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) {
// 此处获取标准格式的日期
whereName = String.format(SQLConstants.DE_STR_TO_DATE, originName, StringUtils.isEmpty(field.getDateFormat()) ? SQLConstants.DEFAULT_DATE_FORMAT : field.getDateFormat());
@ -66,12 +59,12 @@ public class ExtWhere2Str {
if (field.getDeExtractType() == 2 || field.getDeExtractType() == 3 || field.getDeExtractType() == 4) {
String cast = String.format(SQLConstants.CAST, originName, SQLConstants.DEFAULT_INT_FORMAT);
// 此处获取标准格式的日期
whereName = String.format(SQLConstants.FROM_UNIXTIME, cast, date_format);
whereName = String.format(SQLConstants.FROM_UNIXTIME, cast, SQLConstants.DEFAULT_DATE_FORMAT);
whereName = String.format(SQLConstants.UNIX_TIMESTAMP, whereName);
}
if (field.getDeExtractType() == 1) {
// 此处获取标准格式的日期
whereName = String.format(SQLConstants.DE_STR_TO_DATE, originName, StringUtils.isEmpty(field.getDateFormat()) ? SQLConstants.DEFAULT_DATE_FORMAT : field.getDateFormat());
whereName = originName;
}
} else if (field.getDeType() == 2 || field.getDeType() == 3) {
if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) {

View File

@ -8,7 +8,6 @@ import io.dataease.api.permissions.dataset.dto.DatasetRowPermissionsTreeObj;
import io.dataease.dto.dataset.DatasetTableFieldDTO;
import io.dataease.engine.constant.ExtFieldConstant;
import io.dataease.engine.constant.SQLConstants;
import io.dataease.engine.utils.DateUtils;
import io.dataease.engine.utils.Utils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
@ -16,6 +15,7 @@ import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
@ -92,16 +92,13 @@ public class WhereTree2Str {
if (field.getDeType() == 1) {
if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) {
whereName = String.format(SQLConstants.DE_STR_TO_DATE, originName, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT);
whereName = String.format(SQLConstants.DATE_FORMAT, whereName, StringUtils.isNotEmpty(field.getDateFormat()) ? field.getDateFormat() : SQLConstants.DEFAULT_DATE_FORMAT);
}
if (field.getDeExtractType() == 2 || field.getDeExtractType() == 3 || field.getDeExtractType() == 4) {
String cast = String.format(SQLConstants.CAST, originName, SQLConstants.DEFAULT_INT_FORMAT);
whereName = String.format(SQLConstants.FROM_UNIXTIME, cast, SQLConstants.DEFAULT_DATE_FORMAT);
whereName = String.format(SQLConstants.DATE_FORMAT, whereName, SQLConstants.DEFAULT_DATE_FORMAT);
}
if (field.getDeExtractType() == 1) {
String f = DateUtils.get_date_format(originName);
whereName = String.format(SQLConstants.DATE_FORMAT, originName, f);
whereName = originName;
}
} else if (field.getDeType() == 2 || field.getDeType() == 3) {
if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) {
@ -130,16 +127,7 @@ public class WhereTree2Str {
String whereValue = "";
if (field.getDeType() == 1) {
// 规定几种日期格式一一匹配匹配到就是该格式
if (field.getDeExtractType() == 0 || field.getDeExtractType() == 5) {
String f = DateUtils.get_date_format(item.getValue());
whereName = String.format(SQLConstants.DE_STR_TO_DATE, whereName, f);
whereName = String.format(SQLConstants.UNIX_TIMESTAMP, whereName);
} else {
String f = DateUtils.get_date_format(item.getValue());
whereName = String.format(SQLConstants.DE_STR_TO_DATE, whereName, f);
whereName = String.format(SQLConstants.UNIX_TIMESTAMP, whereName);
}
whereName = String.format(SQLConstants.UNIX_TIMESTAMP, whereName);
}
if (StringUtils.equalsIgnoreCase(item.getTerm(), "null")) {
@ -155,10 +143,24 @@ public class WhereTree2Str {
} else if (StringUtils.containsIgnoreCase(item.getTerm(), "like")) {
whereValue = "'%" + value + "%'";
} else {
// 如果是时间字段过滤当条件是等于和不等于的时候转换成between和not between
if (field.getDeType() == 1) {
value = Utils.allDateFormat2Long(value) + "";
if (StringUtils.containsIgnoreCase(whereTerm, "=")) {
whereTerm = " BETWEEN ";
// 把value类似过滤组件处理获得start time和end time
Map<String, Long> stringLongMap = Utils.parseDateTimeValue(value);
whereValue = String.format(SQLConstants.WHERE_VALUE_BETWEEN, stringLongMap.get("startTime"), stringLongMap.get("endTime"));
} else if (StringUtils.containsIgnoreCase(whereTerm, "<>")) {
whereTerm = " NOT BETWEEN ";
Map<String, Long> stringLongMap = Utils.parseDateTimeValue(value);
whereValue = String.format(SQLConstants.WHERE_VALUE_BETWEEN, stringLongMap.get("startTime"), stringLongMap.get("endTime"));
} else {
value = Utils.allDateFormat2Long(value) + "";
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
} else {
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
whereValue = String.format(SQLConstants.WHERE_VALUE_VALUE, value);
}
SQLObj build = SQLObj.builder()
.whereField(whereName)

View File

@ -273,4 +273,69 @@ public class Utils {
}
return time;
}
public static Map<String, Long> parseDateTimeValue(String value) {
Map<String, Long> map = new LinkedHashMap<>();
long startTime = 0;
long endTime = 0;
String split = "-";
if (value != null && value.contains("/")) {
split = "/";
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy" + split + "MM" + split + "dd HH:mm:ss");
startTime = simpleDateFormat.parse(value).getTime();
endTime = startTime + 999;
} catch (Exception e) {
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy" + split + "MM" + split + "dd HH:mm");
startTime = simpleDateFormat.parse(value).getTime();
endTime = startTime + (60 * 1000 - 1);
} catch (Exception e) {
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy" + split + "MM" + split + "dd HH");
startTime = simpleDateFormat.parse(value).getTime();
endTime = startTime + (60 * 60 * 1000 - 1);
} catch (Exception e) {
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
startTime = simpleDateFormat.parse(value).getTime();
endTime = startTime + 999;
} catch (Exception e) {
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy" + split + "MM" + split + "dd");
startTime = simpleDateFormat.parse(value).getTime();
endTime = startTime + (24 * 60 * 60 * 1000 - 1);
} catch (Exception e) {
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy" + split + "MM");
Date parse = simpleDateFormat.parse(value);
startTime = parse.getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
calendar.add(Calendar.MONTH, 1);
endTime = calendar.getTime().getTime() - 1;
} catch (Exception e) {
}
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
Date parse = simpleDateFormat.parse(value);
startTime = parse.getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTime(parse);
calendar.add(Calendar.YEAR, 1);
endTime = calendar.getTime().getTime() - 1;
} catch (Exception e) {
}
map.put("startTime", startTime);
map.put("endTime", endTime);
return map;
}
}