blob: 8e5419bd515665967b4feb514b865a4d87ac19f6 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.myproject.mapper.TorrentMapper">
<resultMap id="BaseResultMap" type="com.example.myproject.entity.TorrentEntity">
<id column="info_hash" property="infoHash"/>
<result column="file_name" property="fileName"/>
<result column="uploader" property="uploader"/>
<result column="upload_time" property="uploadTime"/>
<result column="size" property="size"/>
<result column="title" property="title"/>
<result column="description" property="description"/>
<result column="category" property="category"/>
<result column="image_url" property="imageUrl"/>
</resultMap>
<select id="selectByInfoHash" resultMap="BaseResultMap">
SELECT * FROM torrent WHERE info_hash = #{infoHash}
</select>
<select id="selectBySeedId" resultMap="BaseResultMap">
SELECT * FROM torrent WHERE seed_id = #{seedId}
</select>
<update id="update" parameterType="com.example.myproject.entity.TorrentEntity">
UPDATE torrent
SET file_name = #{fileName},
uploader = #{uploader},
upload_time = #{uploadTime},
size = #{size},
title = #{title},
description = #{description},
category = #{category},
image_url = #{imageUrl}
WHERE info_hash = #{infoHash}
</update>
<select id="search" resultType="com.example.myproject.entity.TorrentEntity">
SELECT * FROM torrent
<where>
<if test="param.category != null">
AND category = #{param.category}
</if>
<!-- <if test="param.free != null and param.free != ''">-->
<!-- AND free = #{param.free}-->
<!-- </if>-->
<if test="param.free != null">
<choose>
<!-- 筛选“正在促销中”的种子 -->
<when test="param.free == true">
AND EXISTS (
SELECT 1 FROM promotion p
WHERE
JSON_CONTAINS(p.applicable_torrent_ids, JSON_ARRAY(t.id))
AND NOW() BETWEEN p.start_time AND p.end_time
AND p.is_deleted = 0
)
</when>
<!-- 筛选“未在促销中”的种子 -->
<otherwise>
AND NOT EXISTS (
SELECT 1 FROM promotion p
WHERE
JSON_CONTAINS(p.applicable_torrent_ids, JSON_ARRAY(t.id))
AND NOW() BETWEEN p.start_time AND p.end_time
AND p.is_deleted = 0
)
</otherwise>
</choose>
</if>
<if test="param.likeExpressions != null and param.likeExpressions.size > 0">
AND (
<foreach collection="param.likeExpressions" item="item" open="(" separator=" AND " close=")">
( title LIKE CONCAT('%', #{item}, '%') ) or ( description LIKE CONCAT('%', #{item}, '%') ) or ( tags LIKE CONCAT('%', #{item}, '%') )
</foreach>
)
</if>
</where>
<if test="param.prop != null and param.sort != null">
ORDER BY ${param.prop} ${param.sort}
</if>
</select>
<select id="checkFavorite" resultType="boolean">
SELECT COUNT(*) > 0
FROM favorite
WHERE seed_id = #{seedId} AND user_id = #{userId}
</select>
<insert id="addFavorite">
INSERT INTO favorite (seed_id, user_id)
VALUES (#{seedId}, #{userId})
</insert>
<delete id="removeFavorite">
DELETE FROM favorite
WHERE seed_id = #{seedId} AND user_id = #{userId}
</delete>
<select id="selectMyFavorite" resultType="com.example.myproject.entity.TorrentEntity">
SELECT t.*
FROM torrent t
JOIN favorite f ON t.id = f.seed_id
WHERE f.user_id = #{userId}
</select>
</mapper>