种子,促销

Change-Id: I0ce919ce4228dcefec26ef636bacd3298c0dc77a
diff --git a/src/main/java/com/example/myproject/mapper/FavoriteMapper.java b/src/main/java/com/example/myproject/mapper/FavoriteMapper.java
new file mode 100644
index 0000000..08b8151
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/FavoriteMapper.java
@@ -0,0 +1,15 @@
+package com.example.myproject.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.entity.FavoriteEntity;
+import org.apache.ibatis.annotations.*;
+
+@Mapper
+public interface FavoriteMapper extends BaseMapper<FavoriteEntity> {
+
+   @Select("SELECT * FROM favorite WHERE user_id = #{userId} AND seed_id = #{seedId} LIMIT 1")
+    FavoriteEntity selectByUserIdAndSeedId(@Param("userId") Long userId, @Param("seedId") Long seedId);
+
+    @Delete("DELETE FROM favorite WHERE user_id = #{userId} AND seed_id = #{seedId}")
+    void deleteByUserIdAndSeedId(@Param("userId") Long userId, @Param("seedId") Long seedId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/PromotionMapper.java b/src/main/java/com/example/myproject/mapper/PromotionMapper.java
new file mode 100644
index 0000000..a478835
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/PromotionMapper.java
@@ -0,0 +1,52 @@
+package com.example.myproject.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.entity.Promotion;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+import java.time.LocalDateTime;
+import java.util.List;
+
+@Mapper
+public interface PromotionMapper extends BaseMapper<Promotion> {
+
+    @Select("SELECT * FROM promotion WHERE is_deleted = false " +
+            "AND start_time <= #{now} AND end_time >= #{now}")
+    List<Promotion> findActivePromotions(@Param("now") LocalDateTime now);
+
+    /**
+     * 查找某个torrentId是否在促销活动的applicable_torrent_ids字符串中
+     * 用MySQL的FIND_IN_SET判断
+     */
+    @Select("SELECT p.* FROM promotion p " +
+            "WHERE p.is_deleted = false " +
+            "AND p.start_time <= #{now} AND p.end_time >= #{now} " +
+            "AND FIND_IN_SET(#{torrentId}, p.applicable_torrent_ids) > 0")
+    List<Promotion> findActivePromotionsForTorrent(
+            @Param("torrentId") Long torrentId,
+            @Param("now") LocalDateTime now);
+
+    /**
+     * 校验种子id是否存在,返回计数,0代表不存在,>0代表存在
+     */
+    @Select("SELECT COUNT(*) FROM torrent WHERE id = #{torrentId}")
+    int checkTorrentExists(@Param("torrentId") Long torrentId);
+
+    /**
+     * 插入促销活动
+     */
+//    int insert(Promotion promotion);
+
+    /**
+     * 根据ID更新促销活动(例如软删除)
+     */
+    int updateById(Promotion promotion);
+
+    /**
+     * 根据ID查询促销活动
+     */
+    Promotion selectById(@Param("id") Long id);
+}
diff --git a/src/main/java/com/example/myproject/mapper/TorrentMapper.java b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
new file mode 100644
index 0000000..f9cdcdc
--- /dev/null
+++ b/src/main/java/com/example/myproject/mapper/TorrentMapper.java
@@ -0,0 +1,32 @@
+package com.example.myproject.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.myproject.dto.param.TorrentParam;
+import com.example.myproject.entity.TorrentEntity;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+import java.util.List;
+
+@Mapper
+public interface TorrentMapper extends BaseMapper<TorrentEntity> {
+
+    @Select("SELECT * FROM torrents WHERE info_hash = #{infoHash}")
+    TorrentEntity selectByInfoHash(String infoHash);
+    
+    @Select("SELECT * FROM torrents WHERE seed_id = #{seedId}")
+    TorrentEntity selectBySeedId(Long seedId);
+    
+    List<TorrentEntity> search(@Param("param") TorrentParam param);
+    
+    @Update("UPDATE torrent SET downloads = downloads + 1 WHERE id = #{torrentId}")
+    void increaseDownloads(@Param("torrentId") Long torrentId);
+    
+    boolean checkFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
+    
+    void addFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
+    
+    void removeFavorite(@Param("seedId") Long seedId, @Param("userId") Long userId);
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/mapper/UserMapper.java b/src/main/java/com/example/myproject/mapper/UserMapper.java
index a070bb5..3a29b64 100644
--- a/src/main/java/com/example/myproject/mapper/UserMapper.java
+++ b/src/main/java/com/example/myproject/mapper/UserMapper.java
@@ -4,6 +4,7 @@
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
 import org.springframework.data.repository.query.Param;
 
 import java.util.List;
@@ -22,4 +23,9 @@
     // 根据用户名包含查找用户
     List<User> selectByUsernameContaining(@Param("name") String name);
 
+    @Update("UPDATE user SET downloaded = downloaded + #{size} WHERE id = #{userId}")
+    void increaseDownloaded(@Param("userId") Long userId, @Param("size") double size);
+    
+    boolean hasRole(@Param("userId") Long userId, @Param("role") String role);
+
 }
\ No newline at end of file