blob: 54f9e1d3b18bbf69dd9d1b360f1f53ca84800403 [file] [log] [blame]
21301050f5f827d2025-06-09 15:09:33 +08001package com.pt5.pthouduan.mapper;
2
3import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4import com.pt5.pthouduan.entity.UserBehavior;
5import org.apache.ibatis.annotations.*;
6import org.springframework.stereotype.Repository;
7
8import java.util.List;
9import java.util.Map;
10
11
12@Repository
13public interface RecommendMapper extends BaseMapper<UserBehavior> {
14
15
16 @Insert("INSERT INTO user_behavior(user_id, torrent_id, behavior_type, weight, timestamp) " +
17 "VALUES(#{userId}, #{torrentId}, #{behaviorType}, #{weight}, #{timestamp})")
18 int insertUserBehavior(UserBehavior userBehavior);
19
20 @Select("SELECT COALESCE(SUM(weight), 0) FROM user_behavior WHERE user_id = #{userId}")
21 double sumWeightsByUserId(@Param("userId") Long userId);
22 /**
23 * 查找用户最喜欢的分类
24 */
25 @Select("SELECT torrent.categoryid as categoryId, SUM(ub.weight) as totalWeight " +
26 "FROM user_behavior ub " +
27 "JOIN torrent ON ub.torrent_id = torrent.torrentid " +
28 "WHERE ub.user_id = #{userId} " +
29 "GROUP BY torrent.categoryid " +
30 "ORDER BY totalWeight DESC ")
31 List<Map<String, Object>> findFavoriteCategories(@Param("userId") Long userId);
32
33 /**
34 * 统计用户行为总数
35 */
36 @Select("SELECT COUNT(*) FROM user_behavior WHERE user_id = #{userId}")
37 long countByUserId(@Param("userId") Long userId);
38
39 /**
40 * 获取用户最近的行为记录
41 */
42 @Select("SELECT * FROM user_behavior WHERE user_id = #{userId} ORDER BY timestamp DESC LIMIT #{limit}")
43 List<UserBehavior> findRecentBehaviors(@Param("userId") Long userId, @Param("limit") int limit);
44
45 /**
46 * 获取用户对特定资源的行为记录
47 */
48 @Select("SELECT * FROM user_behavior WHERE user_id = #{userId} AND torrent_id = #{torrentId}")
49 List<UserBehavior> findByUserAndTorrent(@Param("userId") Long userId, @Param("torrentId") Long torrentId);
50
51 /**
52 * 获取用户的高权重行为
53 */
54 @Select("SELECT * FROM user_behavior WHERE user_id = #{userId} AND weight >= #{minWeight}")
55 List<UserBehavior> findHighWeightBehaviors(@Param("userId") Long userId, @Param("minWeight") Integer minWeight);
56
57 /**
58 * 获取用户最近交互的资源ID
59 */
60 @Select("SELECT DISTINCT torrent_id FROM user_behavior " +
61 "WHERE user_id = #{userId} AND timestamp >= DATE_SUB(NOW(), INTERVAL #{days} DAY)")
62 List<Long> findRecentTorrentIds(@Param("userId") Long userId, @Param("days") int days);
63
64
65
66 /**
67 * 删除旧的行为记录
68 */
69 @Delete("DELETE FROM user_behavior WHERE timestamp < DATE_SUB(NOW(), INTERVAL #{days} DAY)")
70 int deleteOldBehaviorsForAllUsers(@Param("days") int days);
71}