|
|
@@ -0,0 +1,81 @@
|
|
|
+package com.tzld.longarticle.recommend.server.service.recommend.score.strategy;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.tzld.longarticle.recommend.server.common.enums.StatusEnum;
|
|
|
+import com.tzld.longarticle.recommend.server.model.dto.Content;
|
|
|
+import com.tzld.longarticle.recommend.server.model.entity.longArticle.AccountUserCategory;
|
|
|
+import com.tzld.longarticle.recommend.server.repository.longArticle.AccountUserCategoryRepository;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.score.Score;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreParam;
|
|
|
+import com.tzld.longarticle.recommend.server.service.recommend.score.ScoreStrategy;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户分类评分策略
|
|
|
+ * 基于 account_user_category 表中的用户分类偏好数据进行评分
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class AccountUserCategoryStrategy implements ScoreStrategy {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AccountUserCategoryRepository accountUserCategoryRepository;
|
|
|
+
|
|
|
+ @Value("${account.user.category.active.version:1}")
|
|
|
+ private Integer activeVersion;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Score> score(ScoreParam param) {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ List<Score> scores = new ArrayList<>();
|
|
|
+ if (CollectionUtils.isEmpty(param.getContents())) {
|
|
|
+ return scores;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取账号的用户分类配置
|
|
|
+ List<AccountUserCategory> accountUserCategoryList = accountUserCategoryRepository.getByGhIdAndStatusAndVersion(
|
|
|
+ param.getGhId(), StatusEnum.ONE.getCode(), activeVersion);
|
|
|
+ if (CollectionUtils.isEmpty(accountUserCategoryList)) {
|
|
|
+ return scores;
|
|
|
+ }
|
|
|
+ AccountUserCategory accountUserCategory = null;
|
|
|
+ if (CollectionUtils.isNotEmpty(accountUserCategoryList)) {
|
|
|
+ accountUserCategory = accountUserCategoryList.stream()
|
|
|
+ .sorted(Comparator.comparing(AccountUserCategory::getDt, Comparator.reverseOrder()))
|
|
|
+ .findFirst().orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Content content : param.getContents()) {
|
|
|
+ if (CollectionUtils.isEmpty(content.getCategory())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Score score = new Score();
|
|
|
+ score.setStrategy(this);
|
|
|
+ score.setContentId(content.getId());
|
|
|
+ double scoreValue = 0.0;
|
|
|
+
|
|
|
+ for (String category : content.getCategory()) {
|
|
|
+ if (Objects.nonNull(accountUserCategory) && StringUtils.hasText(accountUserCategory.getCategoryMap())) {
|
|
|
+ JSONObject categoryWeightMap = JSONObject.parseObject(accountUserCategory.getCategoryMap());
|
|
|
+ double categoryWeight = categoryWeightMap.getDoubleValue(category);
|
|
|
+ scoreValue += categoryWeight;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ score.setScore(scoreValue);
|
|
|
+ scores.add(score);
|
|
|
+ }
|
|
|
+ log.info("AccountUserCategoryStrategy cost:{}", System.currentTimeMillis() - start);
|
|
|
+ return scores;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|