upload功能

Change-Id: Iad725ce3e2edd913531bf11705bf51000dde010d
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 34a3a7e..749faf7 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1,14 +1,37 @@
 spring.application.name=PT-houduan
 spring.datasource.url=jdbc:mysql://localhost:3306/pt?useSSL=false&serverTimezone=Asia/Shanghai
+#spring.datasource.url=jdbc:mysql://host.docker.internal:3306/pt?useSSL=false&serverTimezone=Asia/Shanghai
 spring.datasource.username=root
-spring.datasource.password=123456
+spring.datasource.password=12345
 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
+# application.properties
+#spring.datasource.url=jdbc:mysql://202.205.102.121:3306/1group5?useSSL=false&serverTimezone=Asia/Shanghai
+#spring.datasource.username=team5
+#spring.datasource.password=Team5001#
+#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
+hikari.maximum-pool-size = 20
+hikari.minimum-idle = 5
+hikari.idle-timeout = 30000
 # MyBatis-Plus ??
 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
 mybatis-plus.global-config.db-config.id-type=auto
 mybatis-plus.global-config.db-config.logic-delete-value=1
 mybatis-plus.global-config.db-config.logic-not-delete-value=0
-mybatis-plus.mapper-locations=classpath:/mapper/*.xml
+#logging.level.root=DEBUG
+#logging.level.org.springframework.web=DEBUG
+#logging.level.com.pt5=TRACE
+#logging.level.root=DEBUG
+mybatis-plus.mapper-locations=classpath:mapper/xml/*.xml
 mybatis-plus.type-aliases-package=com.pt5.pthouduan.entity
 
+uploadDirectory= ./uploads/files    # ????????
+torrent-dir= ./uploads/torrents # ????????
+# ??????
+pt.storage.torrent-path=/var/pt/torrents
+pt.storage.temp-path=/var/pt/temp
+# application.properties ??
+tracker.url=http://localhost:6969/announce
+
+
+
+
diff --git a/src/main/resources/mapper/xml/TorrentMapper.xml b/src/main/resources/mapper/xml/TorrentMapper.xml
new file mode 100644
index 0000000..609ec46
--- /dev/null
+++ b/src/main/resources/mapper/xml/TorrentMapper.xml
@@ -0,0 +1,228 @@
+<?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>
\ No newline at end of file
diff --git a/src/main/resources/mapper/xml/UserMapper.xml b/src/main/resources/mapper/xml/UserMapper.xml
new file mode 100644
index 0000000..229f3c0
--- /dev/null
+++ b/src/main/resources/mapper/xml/UserMapper.xml
@@ -0,0 +1,91 @@
+<?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.UserMapper">
+
+    <!-- 通用查询映射结果 -->
+    <resultMap id="BaseResultMap" type="com.pt5.pthouduan.entity.User">
+        <id column="userid" property="userid" />
+        <result column="username" property="username" />
+        <result column="password" property="password" />
+        <result column="user_upload" property="userUpload" />
+        <result column="user_download" property="userDownload" />
+        <result column="credit" property="credit" />
+        <result column="image" property="image" />
+        <result column="sex" property="sex" />
+        <result column="grade_id" property="gradeId" />
+        <result column="passkey" property="passkey" />
+        <result column="user_created_time" property="userCreatedTime" />
+        <result column="ratio" property="ratio" />
+        <result column="age" property="age" />
+        <result column="privacy" property="privacy" />
+    </resultMap>
+
+    <!-- 通用查询结果列 -->
+    <sql id="Base_Column_List">
+        username, password, user_upload, user_download, credit, image, sex, userid, grade_id, passkey, user_created_time, ratio, age, privacy
+    </sql>
+    <!-- 根据 passkey 查询用户名 -->
+    <select id="getUsernameByPasskey" parameterType="java.lang.String" resultType="java.lang.String">
+        SELECT username
+        FROM user
+        WHERE passkey = #{passkey}
+    </select>
+
+    <select id="findSeedersByInfoHash" resultType="com.pt5.pthouduan.entity.PeerInfo">
+        SELECT
+            ps.ip,
+            ps.port,
+            ps.peer_id,
+            ps.uploaded,
+            ps.upload_speed,
+            ps.downloaded,
+            ps.download_speed,
+            ps.last_event,
+            ps.last_updated,
+            ps.created_at,
+            ps.client,
+            ps.username,
+            ps.created_at,
+            ps.completed_time,
+            ps.port
+        FROM peer_stats ps
+        WHERE ps.info_hash = #{infoHash}
+    </select>
+
+
+
+    <!-- 根据 passkey 检查用户是否存在 -->
+    <select id="existsByPasskey" resultType="boolean">
+        SELECT COUNT(*) > 0
+        FROM user
+        WHERE passkey = #{passkey}
+    </select>
+
+<!--    <update id="incrementUserTraffic">-->
+<!--        UPDATE user-->
+<!--        SET user_upload = user_upload + #{user_upload},-->
+<!--            user_download = user_download + #{user_download}-->
+<!--        WHERE passkey = #{passkey}-->
+<!--    </update>-->
+    <update id="incrementUserTraffic">
+        UPDATE user u
+            JOIN torrent t ON t.info_hash = #{info_hash} AND u.passkey = #{passkey}
+            SET
+                u.user_upload = u.user_upload + (
+                CASE
+                WHEN t.promotionid = 1 THEN #{user_upload} * 2      -- 上传加倍
+                ELSE #{user_upload}
+                END
+                ),
+                u.user_download = u.user_download + (
+                CASE
+                WHEN t.promotionid = 2 THEN #{user_download} / 2    -- 下载减半
+                WHEN t.promotionid = 3 THEN 0                       -- 免费下载
+                ELSE #{user_download}
+                END
+                )
+    </update>
+
+
+
+</mapper>