|
|
@@ -0,0 +1,93 @@
|
|
|
+package com.tzld.piaoquan.recommend.server.service.rerank.strategy;
|
|
|
+
|
|
|
+import com.tzld.piaoquan.recommend.server.service.rerank.RerankStrategy;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.commons.lang3.tuple.Pair;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public abstract class BasicRerankStrategy implements RerankStrategy {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ @Qualifier("redisTemplate")
|
|
|
+ public RedisTemplate<String, String> redisTemplate;
|
|
|
+
|
|
|
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH");
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否在时间范围条件内
|
|
|
+ */
|
|
|
+ protected boolean isInTimeRangeCondition(String timeRange) {
|
|
|
+ if (StringUtils.isBlank(timeRange)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ String[] split = timeRange.split("-");
|
|
|
+ if (split.length != 2) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ String start = split[0].trim();
|
|
|
+ String end = split[1].trim();
|
|
|
+
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+
|
|
|
+ // 具体的时间判断
|
|
|
+ Pair<LocalDateTime, LocalDateTime> localDateTimePair = this.parseDateTimeRange(start, end);
|
|
|
+ if (Objects.nonNull(localDateTimePair)) {
|
|
|
+ LocalDateTime startDt = localDateTimePair.getLeft();
|
|
|
+ LocalDateTime endDt = localDateTimePair.getRight();
|
|
|
+
|
|
|
+ // 等价于 startDt <= now <= endDt
|
|
|
+ return !now.isBefore(startDt) && !now.isAfter(endDt);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 小时判断
|
|
|
+ Pair<Integer, Integer> hourPair = this.parseHourRange(start, end);
|
|
|
+ if (Objects.nonNull(hourPair)) {
|
|
|
+ int nowHour = now.getHour();
|
|
|
+ return hourPair.getLeft() <= nowHour && nowHour <= hourPair.getRight();
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断是否符合 yyyyMMddHH 格式
|
|
|
+ protected Pair<LocalDateTime, LocalDateTime> parseDateTimeRange(String start, String end) {
|
|
|
+ try {
|
|
|
+ if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|