longz
2026-04-27 25df88d0ab9d7444ed1d162b9efa1259ac2f6f66
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?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.sinata.appointment.mapper.AppointmentOrderMapper">
 
    <sql id="AdminOrderWhere">
        o.del_flag = 0
        <if test="visitedUserName != null and visitedUserName != ''">
            AND o.visited_user_name LIKE CONCAT('%', #{visitedUserName}, '%')
        </if>
        <if test="visitedDeptId != null">
            AND o.visited_dept_id = #{visitedDeptId}
        </if>
        <if test="accessArea != null and accessArea != ''">
            AND o.pass_access_area_names LIKE CONCAT('%', #{accessArea}, '%')
        </if>
        <if test="localUnit != null and localUnit != ''">
            AND o.pass_local_unit_names LIKE CONCAT('%', #{localUnit}, '%')
        </if>
        <if test="visitReasonCode != null and visitReasonCode != ''">
            AND o.visit_reason_code = #{visitReasonCode}
        </if>
        <if test="visitDateEnd != null">
            AND o.visit_start_date &lt;= #{visitDateEnd}
        </if>
        <if test="visitDateStart != null">
            AND o.visit_end_date &gt;= #{visitDateStart}
        </if>
        <if test="visitorKeyword != null and visitorKeyword != ''">
            AND EXISTS (
                SELECT 1
                FROM appointment_visitor av
                WHERE av.del_flag = 0
                  AND av.order_id = o.id
                  AND av.name LIKE CONCAT('%', #{visitorKeyword}, '%')
            )
        </if>
        <if test="displayStatus != null and displayStatus != ''">
            <choose>
                <when test="displayStatus == 'PENDING_VISIT'">
                    AND (
                        o.status = 'PENDING_APPROVAL'
                        OR (o.status = 'APPROVED' AND CURDATE() &lt; o.visit_start_date)
                    )
                </when>
                <when test="displayStatus == 'EFFECTIVE'">
                    AND o.status = 'APPROVED'
                    AND o.visit_start_date IS NOT NULL
                    AND o.visit_end_date IS NOT NULL
                    AND CURDATE() &gt;= o.visit_start_date
                    AND CURDATE() &lt;= o.visit_end_date
                </when>
                <otherwise>
                    AND NOT (
                        o.status = 'PENDING_APPROVAL'
                        OR (o.status = 'APPROVED' AND CURDATE() &lt; o.visit_start_date)
                        OR (
                            o.status = 'APPROVED'
                            AND o.visit_start_date IS NOT NULL
                            AND o.visit_end_date IS NOT NULL
                            AND CURDATE() &gt;= o.visit_start_date
                            AND CURDATE() &lt;= o.visit_end_date
                        )
                    )
                </otherwise>
            </choose>
        </if>
        <if test="approvalStatus != null and approvalStatus != ''">
            <choose>
                <when test="approvalStatus == 'APPROVED'">
                    AND o.status IN ('APPROVED', 'VISITED', 'EXPIRED')
                </when>
                <when test="approvalStatus == 'REJECTED'">
                    AND o.status = 'REJECTED'
                </when>
                <when test="approvalStatus == 'CANCELLED'">
                    AND o.status = 'CANCELLED'
                </when>
                <when test="approvalStatus == 'PENDING_APPROVAL'">
                    AND o.status = 'PENDING_APPROVAL'
                    AND (ai.current_node_code IS NULL OR ai.current_node_code = '')
                </when>
                <otherwise>
                    AND o.status = 'PENDING_APPROVAL'
                    AND ai.current_node_code = #{approvalStatus}
                </otherwise>
            </choose>
        </if>
    </sql>
 
    <select id="pageAdminOrders" resultType="com.sinata.appointment.entity.AppointmentOrder">
        SELECT o.*
        FROM appointment_order o
        LEFT JOIN (
            SELECT ai1.order_id, ai1.current_node_code
            FROM approval_instance ai1
            INNER JOIN (
                SELECT order_id, MAX(id) AS max_id
                FROM approval_instance
                WHERE del_flag = 0
                GROUP BY order_id
            ) latest ON latest.max_id = ai1.id
            WHERE ai1.del_flag = 0
        ) ai ON ai.order_id = o.id
        WHERE
        <include refid="AdminOrderWhere"/>
        ORDER BY o.create_time DESC, o.id DESC
    </select>
 
    <select id="pageMiniVisitRecords" resultType="com.sinata.appointment.entity.AppointmentOrder">
        SELECT o.*
        FROM appointment_order o
        WHERE o.del_flag = 0
          AND o.status = 'APPROVED'
          AND o.visited_user_id = #{visitedUserId}
        <if test="futureOnly">
            AND (
                CASE
                    WHEN o.visit_time_mode = 'SINGLE' AND o.visit_time_slot = 'PM'
                        THEN TIMESTAMP(o.visit_start_date, '12:00:00')
                    ELSE TIMESTAMP(o.visit_end_date, '23:59:59')
                END
            ) &gt; NOW()
        </if>
        <if test="visitDate != null">
            AND o.visit_start_date IS NOT NULL
            AND o.visit_end_date IS NOT NULL
            AND #{visitDate} &gt;= o.visit_start_date
            AND #{visitDate} &lt;= o.visit_end_date
        </if>
        <if test="visitorNameKeyword != null and visitorNameKeyword != ''">
            AND EXISTS (
                SELECT 1
                FROM appointment_visitor av
                WHERE av.del_flag = 0
                  AND av.order_id = o.id
                  AND av.name LIKE CONCAT('%', #{visitorNameKeyword}, '%')
            )
        </if>
        ORDER BY o.visit_end_date DESC,
                 CASE
                     WHEN o.visit_time_mode = 'SINGLE' AND o.visit_time_slot = 'ALL_DAY' THEN 3
                     WHEN o.visit_time_mode = 'SINGLE' AND o.visit_time_slot = 'PM' THEN 2
                     WHEN o.visit_time_mode = 'SINGLE' AND o.visit_time_slot = 'AM' THEN 1
                     ELSE 0
                 END DESC,
                 o.create_time DESC,
                 o.id DESC
    </select>
 
    <select id="pageMiniMyOrders" resultType="com.sinata.appointment.entity.AppointmentOrder">
        SELECT o.*
        FROM appointment_order o
        LEFT JOIN biz_visit_reason bvr
               ON bvr.del_flag = 0
              AND bvr.reason_code = o.visit_reason_code
        WHERE o.del_flag = 0
          AND o.submitter_id = #{submitterId}
          AND o.submitter_visible_flag = 1
        <if test="status != null and status != ''">
            <choose>
                <when test="status == 'PENDING_VISIT'">
                    AND (
                        o.status = 'PENDING_APPROVAL'
                        OR (o.status = 'APPROVED' AND CURDATE() &lt; o.visit_start_date)
                    )
                </when>
                <when test="status == 'EFFECTIVE'">
                    AND o.status = 'APPROVED'
                    AND o.visit_start_date IS NOT NULL
                    AND o.visit_end_date IS NOT NULL
                    AND CURDATE() &gt;= o.visit_start_date
                    AND CURDATE() &lt;= o.visit_end_date
                </when>
                <otherwise>
                    AND NOT (
                        o.status = 'PENDING_APPROVAL'
                        OR (o.status = 'APPROVED' AND CURDATE() &lt; o.visit_start_date)
                        OR (
                            o.status = 'APPROVED'
                            AND o.visit_start_date IS NOT NULL
                            AND o.visit_end_date IS NOT NULL
                            AND CURDATE() &gt;= o.visit_start_date
                            AND CURDATE() &lt;= o.visit_end_date
                        )
                    )
                </otherwise>
            </choose>
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
                o.visited_user_name LIKE CONCAT('%', #{keyword}, '%')
                OR o.visit_reason_code LIKE CONCAT('%', #{keyword}, '%')
                OR o.visit_reason_other LIKE CONCAT('%', #{keyword}, '%')
                OR bvr.reason_name LIKE CONCAT('%', #{keyword}, '%')
            )
        </if>
        ORDER BY o.create_time DESC, o.id DESC
    </select>
 
    <select id="selectMiniVisibleOrderById" resultType="com.sinata.appointment.entity.AppointmentOrder">
        SELECT o.*
        FROM appointment_order o
        WHERE o.del_flag = 0
          AND o.id = #{id}
          AND o.submitter_id = #{submitterId}
          AND o.submitter_visible_flag = 1
        LIMIT 1
    </select>
 
    <select id="listDashboardTrend" resultType="com.sinata.appointment.dto.DashboardTrendPointVo">
        SELECT DATE(o.create_time) AS statDate,
               COUNT(*) AS appointmentCount,
               0 AS inProgressCount,
               SUM(CASE WHEN o.status = 'REJECTED' THEN 1 ELSE 0 END) AS rejectedCount,
               SUM(CASE WHEN o.status = 'CANCELLED' THEN 1 ELSE 0 END) AS cancelledCount
        FROM appointment_order o
        WHERE o.del_flag = 0
          AND o.create_time <![CDATA[>=]]> CONCAT(#{startDate}, ' 00:00:00')
          AND o.create_time <![CDATA[<]]> DATE_ADD(CONCAT(#{endDate}, ' 00:00:00'), INTERVAL 1 DAY)
        <if test="deptIds != null and deptIds.size() > 0">
            AND o.visited_dept_id IN
            <foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
                #{deptId}
            </foreach>
        </if>
        GROUP BY DATE(o.create_time)
        ORDER BY DATE(o.create_time)
    </select>
 
</mapper>