From cdd62c29320f88bf87941d6f3c8badb8064796e9 Mon Sep 17 00:00:00 2001 From: junjun Date: Fri, 8 Mar 2024 15:06:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../engine/trans/CustomWhere2Str.java | 37 ++++++----- .../dataease/engine/trans/ExtWhere2Str.java | 11 +--- .../dataease/engine/trans/WhereTree2Str.java | 36 +++++----- .../java/io/dataease/engine/utils/Utils.java | 65 +++++++++++++++++++ 4 files changed, 108 insertions(+), 41 deletions(-) diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java index 7b4bbb1929..599dc2c81f 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/CustomWhere2Str.java @@ -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 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 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()); } diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java index 6f8cb4b996..7382489ad2 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/ExtWhere2Str.java @@ -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) { diff --git a/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java b/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java index 5ae06c268c..3f29d4b060 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java +++ b/core/core-backend/src/main/java/io/dataease/engine/trans/WhereTree2Str.java @@ -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 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 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) diff --git a/core/core-backend/src/main/java/io/dataease/engine/utils/Utils.java b/core/core-backend/src/main/java/io/dataease/engine/utils/Utils.java index a41b3589a0..5bad3f1e9d 100644 --- a/core/core-backend/src/main/java/io/dataease/engine/utils/Utils.java +++ b/core/core-backend/src/main/java/io/dataease/engine/utils/Utils.java @@ -273,4 +273,69 @@ public class Utils { } return time; } + + public static Map parseDateTimeValue(String value) { + Map 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; + } }