| <?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.pt5.pthouduan.mapper.TorrentMapper"> |
| |
| <!-- 按上传者查询 --> |
| <select id="selectByUploaderId" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT * FROM torrent |
| WHERE userid = #{uploaderId} |
| </select> |
| |
| <!-- 按名称模糊搜索 --> |
| <select id="searchByName" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT * FROM torrent |
| WHERE torrent_title LIKE CONCAT('%', #{keyword}, '%') |
| </select> |
| |
| <select id="searchByKeyword" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT * FROM torrent |
| WHERE torrent_title LIKE CONCAT('%', #{keyword}, '%') |
| </select> |
| |
| <!-- 获取热门种子 --> |
| <select id="selectTopPopular" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT * FROM torrent |
| ORDER BY download_count DESC |
| LIMIT 10 |
| </select> |
| |
| <!-- 检查infoHash是否存在 --> |
| <select id="existsByInfoHash" resultType="int" parameterType="string"> |
| SELECT COUNT(*) FROM torrent WHERE info_hash = #{infoHash} |
| </select> |
| |
| |
| <!-- 下载计数+1 --> |
| <update id="incrementDownloadCount"> |
| UPDATE torrent |
| SET download_count = download_count + 1 |
| WHERE torrentid = #{id} |
| </update> |
| |
| <!-- 复杂查询示例(带动态SQL) --> |
| <select id="selectByCondition" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT * FROM torrent |
| <where> |
| <if test="condition.categoryId != null"> |
| AND categoryid = #{condition.categoryId} |
| </if> |
| <if test="condition.minSize != null"> |
| AND torrent_size >= #{condition.minSize} |
| </if> |
| </where> |
| ORDER BY ${condition.orderBy} DESC |
| </select> |
| |
| <!-- <select id="selectById" resultType="com.pt5.pthouduan.entity.Torrent">--> |
| <!-- SELECT--> |
| <!-- t.torrentid,--> |
| <!-- t.torrent_title,--> |
| <!-- t.description,--> |
| <!-- t.uploader_id,--> |
| <!-- t.upload_time,--> |
| <!-- t.download_count,--> |
| <!-- t.torrent_size,--> |
| <!-- t.filename,--> |
| <!-- t.path,--> |
| <!-- t.categoryid,--> |
| <!-- t.caption,--> |
| <!-- t.dpi--> |
| <!-- FROM torrent t--> |
| <!-- WHERE t.torrentid = #{torrentid}--> |
| <!-- </select>--> |
| <select id="selectById" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT |
| t.torrentid, |
| t.torrent_title, |
| t.description, |
| t.uploader_id, |
| t.upload_time, |
| t.download_count, |
| t.torrent_size, |
| t.filename, |
| t.path, |
| t.categoryid, |
| t.caption, |
| t.dpi, |
| t.last_seed, |
| t.info_hash |
| FROM torrent t |
| WHERE t.torrentid = #{torrentid} |
| </select> |
| |
| <select id="getAllTorrents" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT |
| torrentid, |
| torrent_title, |
| filename, |
| description, |
| path, |
| uploader_id, |
| upload_time, |
| download_count, |
| categoryid, |
| promotionid, |
| dpi, |
| caption |
| FROM |
| torrent |
| </select> |
| |
| <insert id="save" parameterType="com.pt5.pthouduan.entity.Torrent" |
| useGeneratedKeys="true" keyProperty="torrentid" keyColumn="torrentid"> |
| INSERT INTO torrent ( |
| uploader_id, |
| promotionid, |
| categoryid, |
| info_hash, |
| torrent_title, |
| dpi, |
| caption, |
| torrent_size, |
| upload_time, |
| download_count, |
| description, |
| path, |
| filename |
| ) VALUES ( |
| #{uploaderid}, |
| #{promotionid}, |
| #{categoryid}, |
| #{infohash}, |
| #{torrenttitle}, |
| #{dpi}, |
| #{caption}, |
| #{torrentsize}, |
| #{uploadtime}, |
| #{downloadCount}, |
| #{description}, |
| #{path}, |
| #{filename} |
| ) |
| </insert> |
| <!-- 按分类查询种子 --> |
| <select id="selectTorrentsByCategory" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT * FROM torrent WHERE categoryid = #{category} |
| </select> |
| |
| <!-- 免费下载 --> |
| <update id="setFreePromotion"> |
| <![CDATA[ |
| UPDATE torrent t |
| LEFT JOIN ( |
| SELECT info_hash, MAX(last_updated) AS last_time |
| FROM peer_stats |
| GROUP BY info_hash |
| ) ps ON t.info_hash = ps.info_hash |
| SET t.promotionid = 3 |
| WHERE ps.last_time IS NULL OR ps.last_time < NOW() - INTERVAL 7 DAY |
| ]]> |
| </update> |
| |
| |
| <!-- 上传加倍 --> |
| <update id="setDoubleUpload"> |
| <![CDATA[ |
| UPDATE torrent t |
| LEFT JOIN ( |
| SELECT info_hash, MAX(last_updated) AS last_time, |
| SUM(delta_upload) AS total_upload |
| FROM peer_stats |
| GROUP BY info_hash |
| ) ps ON t.info_hash = ps.info_hash |
| SET t.promotionid = 1 |
| WHERE ps.last_time < NOW() - INTERVAL 5 DAY |
| AND (ps.total_upload IS NULL OR ps.total_upload = 0) |
| ]]> |
| </update> |
| |
| |
| <!-- 下载减半 --> |
| <update id="setHalfDownload"> |
| <![CDATA[ |
| UPDATE torrent t |
| LEFT JOIN ( |
| SELECT info_hash, MAX(last_updated) AS last_time, |
| SUM(delta_download) AS total_download |
| FROM peer_stats |
| GROUP BY info_hash |
| ) ps ON t.info_hash = ps.info_hash |
| SET t.promotionid = 2 |
| WHERE ps.last_time < NOW() - INTERVAL 5 DAY |
| AND (ps.total_download IS NULL OR ps.total_download = 0) |
| ]]> |
| </update> |
| |
| |
| <!-- 取消促销 --> |
| <update id="clearPromotion"> |
| <![CDATA[ |
| UPDATE torrent t |
| LEFT JOIN ( |
| SELECT info_hash, MAX(last_updated) AS last_time |
| FROM peer_stats |
| GROUP BY info_hash |
| ) ps ON t.info_hash = ps.info_hash |
| SET t.promotionid = NULL |
| WHERE ps.last_time >= NOW() - INTERVAL 3 DAY |
| ]]> |
| </update> |
| |
| <select id="listByCategoryWithFilters" resultType="com.pt5.pthouduan.entity.Torrent"> |
| SELECT * FROM torrent t |
| WHERE t.categoryid = #{categoryid} |
| <if test="filters != null and !filters.isEmpty()"> |
| AND t.torrentid IN ( |
| SELECT e.torrentid FROM |
| ${extendTable} e |
| WHERE |
| <foreach collection="filters" index="key" item="value" separator=" AND "> |
| (e.${key} = #{value}) |
| </foreach> |
| ) |
| </if> |
| </select> |
| |
| </mapper> |