zjk
2026-09-29 54dcf0a82e7490fa58b1c056d5c0b9cb739c2931
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?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.tcm.training.admin.live.mapper.LiveReserveMapper">
 
    <sql id="HasWatchedExpr">
        EXISTS (
            SELECT 1
            FROM tcm_live_watch_record wr
            WHERE wr.session_id = r.session_id
              AND wr.app_user_id = r.app_user_id
        )
    </sql>
 
    <sql id="GlobalReserveColumns">
        r.id,
        r.live_course_id AS courseId,
        r.session_id AS sessionId,
        r.app_user_id AS userId,
        u.nickname AS userNickname,
        u.phone AS userPhone,
        u.avatar AS userAvatar,
        c.title AS courseTitle,
        NULL AS categoryName,
        s.start_time AS startTime,
        s.live_status AS liveStatus,
        r.reserve_time AS reserveTime,
        r.is_cancel AS isCancel,
        CASE WHEN <include refid="HasWatchedExpr"/> THEN 1 ELSE 0 END AS hasWatched
    </sql>
 
    <sql id="GlobalReserveJoin">
        FROM tcm_live_reserve r
        INNER JOIN tcm_live_course c ON c.id = r.live_course_id AND c.deleted = 0
        INNER JOIN tcm_live_session s ON s.id = r.session_id AND s.deleted = 0
        LEFT JOIN tcm_app_user u ON u.id = r.app_user_id AND u.deleted = 0
    </sql>
 
    <sql id="GlobalReserveWhere">
        WHERE r.is_cancel = 0
        <if test="courseId != null">
            AND r.live_course_id = #{courseId}
        </if>
        <if test="sessionId != null">
            AND r.session_id = #{sessionId}
        </if>
        <if test="phone != null and phone.trim() != ''">
            AND u.phone LIKE CONCAT('%', #{phone}, '%')
        </if>
        <if test="courseName != null and courseName.trim() != ''">
            AND c.title LIKE CONCAT('%', #{courseName}, '%')
        </if>
        <if test="liveStatus != null">
            AND s.live_status = #{liveStatus}
        </if>
        <if test="hasWatched != null and hasWatched == 1">
            AND <include refid="HasWatchedExpr"/>
        </if>
        <if test="hasWatched != null and hasWatched == 0">
            AND NOT <include refid="HasWatchedExpr"/>
        </if>
        <if test="reserveFrom != null">
            AND r.reserve_time <![CDATA[>=]]> #{reserveFrom}
        </if>
        <if test="reserveTo != null">
            AND r.reserve_time <![CDATA[<=]]> #{reserveTo}
        </if>
    </sql>
 
    <!-- 预约记录分页列表(场次维度) -->
    <select id="selectReservePage"
            resultType="com.tcm.training.admin.live.vo.LiveReserveRow">
        SELECT
            r.id,
            r.app_user_id AS userId,
            s.nickname AS userNickname,
            s.phone AS userPhone,
            s.avatar AS userAvatar,
            r.reserve_time AS reserveTime,
            r.is_cancel AS isCancel
        FROM tcm_live_reserve r
        LEFT JOIN tcm_app_user s ON s.id = r.app_user_id AND s.deleted = 0
        WHERE r.live_course_id = #{courseId} AND r.session_id = #{sessionId}
        <if test="keyword != null and keyword.trim() != ''">
            AND (s.nickname LIKE CONCAT('%', #{keyword}, '%')
              OR s.phone LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        <if test="isCancel != null">
            AND r.is_cancel = #{isCancel}
        </if>
        ORDER BY r.id DESC
    </select>
 
    <select id="selectGlobalReservePage"
            resultType="com.tcm.training.admin.live.vo.LiveReserveRow">
        SELECT
            <include refid="GlobalReserveColumns"/>
        <include refid="GlobalReserveJoin"/>
        <include refid="GlobalReserveWhere"/>
        ORDER BY r.reserve_time DESC, r.id DESC
    </select>
 
    <select id="selectGlobalReserveList"
            resultType="com.tcm.training.admin.live.vo.LiveReserveRow">
        SELECT
            <include refid="GlobalReserveColumns"/>
        <include refid="GlobalReserveJoin"/>
        <include refid="GlobalReserveWhere"/>
        ORDER BY r.reserve_time DESC, r.id DESC
    </select>
 
    <select id="selectGlobalReserveSummary"
            resultType="com.tcm.training.admin.live.vo.LiveReserveSummaryRow">
        SELECT
            COUNT(1) AS reserveTotal,
            COALESCE(SUM(CASE WHEN <include refid="HasWatchedExpr"/> THEN 1 ELSE 0 END), 0) AS watchedTotal
        <include refid="GlobalReserveJoin"/>
        <include refid="GlobalReserveWhere"/>
    </select>
 
</mapper>