刘嘉昕 | 88d3f7d | 2025-06-04 11:54:09 +0800 | [diff] [blame] | 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
| 3 | "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| 4 | |
| 5 | <mapper namespace="com.pt5.pthouduan.mapper.TorrentMapper"> |
| 6 | |
| 7 | <!-- 按上传者查询 --> |
| 8 | <select id="selectByUploaderId" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 9 | SELECT * FROM torrent |
| 10 | WHERE userid = #{uploaderId} |
| 11 | </select> |
| 12 | |
| 13 | <!-- 按名称模糊搜索 --> |
| 14 | <select id="searchByName" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 15 | SELECT * FROM torrent |
| 16 | WHERE torrent_title LIKE CONCAT('%', #{keyword}, '%') |
| 17 | </select> |
| 18 | |
| 19 | <select id="searchByKeyword" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 20 | SELECT * FROM torrent |
| 21 | WHERE torrent_title LIKE CONCAT('%', #{keyword}, '%') |
| 22 | </select> |
| 23 | |
| 24 | <!-- 获取热门种子 --> |
| 25 | <select id="selectTopPopular" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 26 | SELECT * FROM torrent |
| 27 | ORDER BY download_count DESC |
| 28 | LIMIT 10 |
| 29 | </select> |
| 30 | |
| 31 | <!-- 检查infoHash是否存在 --> |
| 32 | <select id="existsByInfoHash" resultType="int" parameterType="string"> |
| 33 | SELECT COUNT(*) FROM torrent WHERE info_hash = #{infoHash} |
| 34 | </select> |
| 35 | |
| 36 | |
| 37 | <!-- 下载计数+1 --> |
| 38 | <update id="incrementDownloadCount"> |
| 39 | UPDATE torrent |
| 40 | SET download_count = download_count + 1 |
| 41 | WHERE torrentid = #{id} |
| 42 | </update> |
| 43 | |
| 44 | <!-- 复杂查询示例(带动态SQL) --> |
| 45 | <select id="selectByCondition" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 46 | SELECT * FROM torrent |
| 47 | <where> |
| 48 | <if test="condition.categoryId != null"> |
| 49 | AND categoryid = #{condition.categoryId} |
| 50 | </if> |
| 51 | <if test="condition.minSize != null"> |
| 52 | AND torrent_size >= #{condition.minSize} |
| 53 | </if> |
| 54 | </where> |
| 55 | ORDER BY ${condition.orderBy} DESC |
| 56 | </select> |
| 57 | |
| 58 | <!-- <select id="selectById" resultType="com.pt5.pthouduan.entity.Torrent">--> |
| 59 | <!-- SELECT--> |
| 60 | <!-- t.torrentid,--> |
| 61 | <!-- t.torrent_title,--> |
| 62 | <!-- t.description,--> |
| 63 | <!-- t.uploader_id,--> |
| 64 | <!-- t.upload_time,--> |
| 65 | <!-- t.download_count,--> |
| 66 | <!-- t.torrent_size,--> |
| 67 | <!-- t.filename,--> |
| 68 | <!-- t.path,--> |
| 69 | <!-- t.categoryid,--> |
| 70 | <!-- t.caption,--> |
| 71 | <!-- t.dpi--> |
| 72 | <!-- FROM torrent t--> |
| 73 | <!-- WHERE t.torrentid = #{torrentid}--> |
| 74 | <!-- </select>--> |
| 75 | <select id="selectById" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 76 | SELECT |
| 77 | t.torrentid, |
| 78 | t.torrent_title, |
| 79 | t.description, |
| 80 | t.uploader_id, |
| 81 | t.upload_time, |
| 82 | t.download_count, |
| 83 | t.torrent_size, |
| 84 | t.filename, |
| 85 | t.path, |
| 86 | t.categoryid, |
| 87 | t.caption, |
| 88 | t.dpi, |
| 89 | t.last_seed, |
| 90 | t.info_hash |
| 91 | FROM torrent t |
| 92 | WHERE t.torrentid = #{torrentid} |
| 93 | </select> |
| 94 | |
| 95 | <select id="getAllTorrents" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 96 | SELECT |
| 97 | torrentid, |
| 98 | torrent_title, |
| 99 | filename, |
| 100 | description, |
| 101 | path, |
| 102 | uploader_id, |
| 103 | upload_time, |
| 104 | download_count, |
| 105 | categoryid, |
| 106 | promotionid, |
| 107 | dpi, |
| 108 | caption |
| 109 | FROM |
| 110 | torrent |
| 111 | </select> |
| 112 | |
| 113 | <insert id="save" parameterType="com.pt5.pthouduan.entity.Torrent" |
| 114 | useGeneratedKeys="true" keyProperty="torrentid" keyColumn="torrentid"> |
| 115 | INSERT INTO torrent ( |
| 116 | uploader_id, |
| 117 | promotionid, |
| 118 | categoryid, |
| 119 | info_hash, |
| 120 | torrent_title, |
| 121 | dpi, |
| 122 | caption, |
| 123 | torrent_size, |
| 124 | upload_time, |
| 125 | download_count, |
| 126 | description, |
| 127 | path, |
| 128 | filename |
| 129 | ) VALUES ( |
| 130 | #{uploaderid}, |
| 131 | #{promotionid}, |
| 132 | #{categoryid}, |
| 133 | #{infohash}, |
| 134 | #{torrenttitle}, |
| 135 | #{dpi}, |
| 136 | #{caption}, |
| 137 | #{torrentsize}, |
| 138 | #{uploadtime}, |
| 139 | #{downloadCount}, |
| 140 | #{description}, |
| 141 | #{path}, |
| 142 | #{filename} |
| 143 | ) |
| 144 | </insert> |
| 145 | <!-- 按分类查询种子 --> |
| 146 | <select id="selectTorrentsByCategory" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 147 | SELECT * FROM torrent WHERE categoryid = #{category} |
| 148 | </select> |
| 149 | |
| 150 | <!-- 免费下载 --> |
| 151 | <update id="setFreePromotion"> |
| 152 | <![CDATA[ |
| 153 | UPDATE torrent t |
| 154 | LEFT JOIN ( |
| 155 | SELECT info_hash, MAX(last_updated) AS last_time |
| 156 | FROM peer_stats |
| 157 | GROUP BY info_hash |
| 158 | ) ps ON t.info_hash = ps.info_hash |
| 159 | SET t.promotionid = 3 |
| 160 | WHERE ps.last_time IS NULL OR ps.last_time < NOW() - INTERVAL 7 DAY |
| 161 | ]]> |
| 162 | </update> |
| 163 | |
| 164 | |
| 165 | <!-- 上传加倍 --> |
| 166 | <update id="setDoubleUpload"> |
| 167 | <![CDATA[ |
| 168 | UPDATE torrent t |
| 169 | LEFT JOIN ( |
| 170 | SELECT info_hash, MAX(last_updated) AS last_time, |
| 171 | SUM(delta_upload) AS total_upload |
| 172 | FROM peer_stats |
| 173 | GROUP BY info_hash |
| 174 | ) ps ON t.info_hash = ps.info_hash |
| 175 | SET t.promotionid = 1 |
| 176 | WHERE ps.last_time < NOW() - INTERVAL 5 DAY |
| 177 | AND (ps.total_upload IS NULL OR ps.total_upload = 0) |
| 178 | ]]> |
| 179 | </update> |
| 180 | |
| 181 | |
| 182 | <!-- 下载减半 --> |
| 183 | <update id="setHalfDownload"> |
| 184 | <![CDATA[ |
| 185 | UPDATE torrent t |
| 186 | LEFT JOIN ( |
| 187 | SELECT info_hash, MAX(last_updated) AS last_time, |
| 188 | SUM(delta_download) AS total_download |
| 189 | FROM peer_stats |
| 190 | GROUP BY info_hash |
| 191 | ) ps ON t.info_hash = ps.info_hash |
| 192 | SET t.promotionid = 2 |
| 193 | WHERE ps.last_time < NOW() - INTERVAL 5 DAY |
| 194 | AND (ps.total_download IS NULL OR ps.total_download = 0) |
| 195 | ]]> |
| 196 | </update> |
| 197 | |
| 198 | |
| 199 | <!-- 取消促销 --> |
| 200 | <update id="clearPromotion"> |
| 201 | <![CDATA[ |
| 202 | UPDATE torrent t |
| 203 | LEFT JOIN ( |
| 204 | SELECT info_hash, MAX(last_updated) AS last_time |
| 205 | FROM peer_stats |
| 206 | GROUP BY info_hash |
| 207 | ) ps ON t.info_hash = ps.info_hash |
| 208 | SET t.promotionid = NULL |
| 209 | WHERE ps.last_time >= NOW() - INTERVAL 3 DAY |
| 210 | ]]> |
| 211 | </update> |
| 212 | |
| 213 | <select id="listByCategoryWithFilters" resultType="com.pt5.pthouduan.entity.Torrent"> |
| 214 | SELECT * FROM torrent t |
| 215 | WHERE t.categoryid = #{categoryid} |
| 216 | <if test="filters != null and !filters.isEmpty()"> |
| 217 | AND t.torrentid IN ( |
| 218 | SELECT e.torrentid FROM |
| 219 | ${extendTable} e |
| 220 | WHERE |
| 221 | <foreach collection="filters" index="key" item="value" separator=" AND "> |
| 222 | (e.${key} = #{value}) |
| 223 | </foreach> |
| 224 | ) |
| 225 | </if> |
| 226 | </select> |
| 227 | |
| 228 | </mapper> |