|
@@ -0,0 +1,98 @@
|
|
|
|
|
+package com.tzld.piaoquan.recommend.server.service.rerank.config;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.function.Function;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public abstract class RerankConfigBasicManager implements RerankConfigManager {
|
|
|
|
|
+
|
|
|
|
|
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH");
|
|
|
|
|
+
|
|
|
|
|
+ protected Function<LocalDateTime, Boolean> parseTimeToFunction(String timeRange) {
|
|
|
|
|
+ if (StringUtils.isBlank(timeRange)) {
|
|
|
|
|
+ return localDateTime -> true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String[] split = timeRange.split("-");
|
|
|
|
|
+ if (split.length != 2) {
|
|
|
|
|
+ return localDateTime -> true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String start = split[0].trim();
|
|
|
|
|
+ String end = split[1].trim();
|
|
|
|
|
+
|
|
|
|
|
+ Pair<LocalDateTime, LocalDateTime> dateTimePair = this.parseDateTimeRange(start, end);
|
|
|
|
|
+ if (Objects.nonNull(dateTimePair)) {
|
|
|
|
|
+ return new Function<LocalDateTime, Boolean>() {
|
|
|
|
|
+ private final Pair<LocalDateTime, LocalDateTime> localDateTimePair = dateTimePair;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Boolean apply(LocalDateTime now) {
|
|
|
|
|
+ LocalDateTime startDt = localDateTimePair.getLeft();
|
|
|
|
|
+ LocalDateTime endDt = localDateTimePair.getRight();
|
|
|
|
|
+
|
|
|
|
|
+ // 等价于 startDt <= now <= endDt
|
|
|
|
|
+ return !now.isBefore(startDt) && !now.isAfter(endDt);
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Pair<Integer, Integer> hourRangePair = this.parseHourRange(start, end);
|
|
|
|
|
+ if (Objects.nonNull(hourRangePair)) {
|
|
|
|
|
+ return new Function<LocalDateTime, Boolean>() {
|
|
|
|
|
+ private final Pair<Integer, Integer> hourPair = hourRangePair;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Boolean apply(LocalDateTime now) {
|
|
|
|
|
+ int nowHour = now.getHour();
|
|
|
|
|
+ return hourPair.getLeft() <= nowHour && nowHour <= hourPair.getRight();
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return localDateTime -> true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 判断是否符合 yyyyMMddHH 格式
|
|
|
|
|
+ protected Pair<LocalDateTime, LocalDateTime> parseDateTimeRange(String start, String end) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (start.length() != 8 && end.length() != 8) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Pair.of(LocalDateTime.parse(start, formatter), LocalDateTime.parse(end, formatter));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("datetime parse error. {}-{} \n", start, end, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected Pair<Integer, Integer> parseHourRange(String start, String end) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int startHour = Integer.parseInt(start);
|
|
|
|
|
+ int endHour = Integer.parseInt(end);
|
|
|
|
|
+ if (startHour < 0 || startHour > 23 || endHour < 0 || endHour > 23) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return Pair.of(startHour, endHour);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("parse hour range error. {}-{} \n", start, end, e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|