Przeglądaj źródła

refactor(weight): UserContentWeightService 接口化(遗留改动补提交)

Xiaogang Liao 1 tydzień temu
rodzic
commit
6d7e6298fe

+ 16 - 30
cfc-backend/src/main/java/com/etotem/cfc/service/UserContentWeightService.java

@@ -1,25 +1,15 @@
 package com.etotem.cfc.service;
 
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.etotem.cfc.entity.UserContentWeight;
-import com.etotem.cfc.mapper.UserContentWeightMapper;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Service;
 
-import javax.annotation.Resource;
-import java.util.Collections;
+import java.math.BigDecimal;
 import java.util.List;
 
 /**
  * 用户内容偏好权重服务。
  * 表为空时返回空列表,推荐排序将自动回退到 matchScore。
  */
-@Slf4j
-@Service
-public class UserContentWeightService {
-
-    @Resource
-    private UserContentWeightMapper userContentWeightMapper;
+public interface UserContentWeightService {
 
     /**
      * 查询内容偏好权重列表。
@@ -31,22 +21,18 @@ public class UserContentWeightService {
      * @param itemId      内容ID
      * @return 权重列表(表为空时返回空列表)
      */
-    public List<UserContentWeight> listWeights(Long userId, Long familyId, String contentType, Long itemId) {
-        if (familyId == null || contentType == null || itemId == null) {
-            return Collections.emptyList();
-        }
-        try {
-            return userContentWeightMapper.selectList(
-                    new LambdaQueryWrapper<UserContentWeight>()
-                            .eq(UserContentWeight::getFamilyId, familyId)
-                            .eq(UserContentWeight::getContentType, contentType)
-                            .eq(UserContentWeight::getItemId, itemId)
-                            .eq(userId != null, UserContentWeight::getUserId, userId)
-            );
-        } catch (Exception e) {
-            log.warn("查询用户内容偏好权重失败: familyId={}, contentType={}, itemId={}, error={}",
-                    familyId, contentType, itemId, e.getMessage());
-            return Collections.emptyList();
-        }
-    }
+    List<UserContentWeight> listWeights(Long userId, Long familyId, String contentType, Long itemId);
+
+    /** 查询单个用户的内容偏好权重,不存在返回 null */
+    UserContentWeight getWeight(Long userId, Long familyId, String contentType, Long itemId);
+
+    /** 保存/更新一条内容偏好权重(id 非空则更新,否则新增) */
+    void saveWeight(UserContentWeight weight);
+
+    /** 直接更新指定内容的权重值 */
+    void updateWeight(Long userId, Long familyId, String contentType, Long itemId, BigDecimal newWeight);
+
+    /** 记录一次内容行为,按增量累加权重(上限 10.0),无记录时新建 */
+    void recordBehavior(Long userId, Long familyId, String contentType, Long itemId,
+                        String dimensionCode, BigDecimal increment, String reason);
 }

+ 18 - 8
cfc-backend/src/main/java/com/etotem/cfc/service/UserContentWeightServiceImpl.java

@@ -10,7 +10,7 @@ import java.math.BigDecimal;
 import java.util.Date;
 import java.util.List;
 
-@Service
+@Service("userContentWeightService")
 public class UserContentWeightServiceImpl implements UserContentWeightService {
 
     private final UserContentWeightMapper mapper;
@@ -43,13 +43,23 @@ public class UserContentWeightServiceImpl implements UserContentWeightService {
 
     @Override
     public List<UserContentWeight> listWeights(Long userId, Long familyId, String contentType, Long itemId) {
-        LambdaQueryWrapper<UserContentWeight> qw = new LambdaQueryWrapper<UserContentWeight>()
-            .eq(UserContentWeight::getFamilyId, familyId)
-            .eq(UserContentWeight::getContentType, contentType)
-            .eq(UserContentWeight::getItemId, itemId)
-            .eq(userId != null, UserContentWeight::getUserId, userId)
-            .orderByDesc(UserContentWeight::getWeight);
-        return mapper.selectList(qw);
+        if (familyId == null || contentType == null || itemId == null) {
+            return java.util.Collections.emptyList();
+        }
+        try {
+            LambdaQueryWrapper<UserContentWeight> qw = new LambdaQueryWrapper<UserContentWeight>()
+                .eq(UserContentWeight::getFamilyId, familyId)
+                .eq(UserContentWeight::getContentType, contentType)
+                .eq(UserContentWeight::getItemId, itemId)
+                .eq(userId != null, UserContentWeight::getUserId, userId)
+                .orderByDesc(UserContentWeight::getWeight);
+            return mapper.selectList(qw);
+        } catch (Exception e) {
+            java.util.logging.Logger.getLogger("UserContentWeightService").warning(
+                String.format("查询用户内容偏好权重失败: familyId=%s, contentType=%s, itemId=%s, error=%s",
+                    familyId, contentType, itemId, e.getMessage()));
+            return java.util.Collections.emptyList();
+        }
     }
 
     @Override