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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package com.talentcard.server.mapper;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.talentcard.server.entity.TransportParkingPlateRegistration;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
 
@Mapper
public interface TransportParkingPlateRegistrationMapper extends BaseMapper<TransportParkingPlateRegistration> {
 
    @Select("""
            select c.id as cardId,
                   c.talent_id as talentId,
                   c.card_no as cardNo,
                   c.card_level as cardLevel,
                   c.card_type as cardType,
                   u.name as talentName,
                   u.mobile as talentMobile,
                   u.id_card_no as talentIdCardNo
            from talent_card c
            join talent_user u on u.id = c.talent_id and u.deleted = 0 and u.status = 1
            where c.deleted = 0
              and c.status = 1
              and (c.valid_start is null or c.valid_start <= current_date)
              and (c.valid_end is null or c.valid_end >= current_date)
              and (c.expire_time is null or c.expire_time > now())
              and ((#{cardId} is not null and c.id = #{cardId}) or (#{cardId} is null and c.talent_id = #{talentId}))
              and (#{talentId} is null or c.talent_id = #{talentId})
            order by case when c.card_type = 'PERSONAL' then 0 else 1 end, c.id desc
            limit 1
            """)
    Map<String, Object> selectActiveCardSnapshot(@Param("talentId") Long talentId, @Param("cardId") Long cardId);
 
    @Select("""
            select m.id as merchantId,
                   m.merchant_name as merchantName,
                   m.contact_mobile as contactMobile,
                   m.address,
                   m.cover_url as coverUrl,
                   m.status as merchantStatus,
                   b.id as benefitId,
                   b.action_mode as actionMode,
                   b.status as benefitStatus
            from merchant m
            left join transport_parking_benefit b on b.merchant_id = m.id and b.deleted = 0
            where m.id = #{merchantId}
              and m.deleted = 0
              and m.merchant_type_code = 'parking'
            limit 1
            """)
    Map<String, Object> selectParkingMerchant(@Param("merchantId") Long merchantId);
 
    @Select("""
            select count(*)
            from transport_parking_plate_registration r
            left join merchant m on m.id = r.merchant_id
            left join transport_parking_lot l on l.id = r.parking_lot_id
                and l.merchant_id = r.merchant_id and l.deleted = 0
            left join talent_user u on u.id = r.talent_id
            left join talent_card c on c.id = coalesce(r.card_id, (
                select c2.id
                from talent_card c2
                where c2.talent_id = r.talent_id
                  and c2.deleted = 0
                  and c2.status = 1
                order by case when c2.card_type = 'PERSONAL' then 0 else 1 end, c2.id desc
                limit 1
            ))
            where r.deleted = 0
              and (#{merchantId} is null or r.merchant_id = #{merchantId})
              and (#{parkingLotId} is null or r.parking_lot_id = #{parkingLotId})
              and (#{cardLevel} is null or #{cardLevel} = '' or coalesce(r.card_level, c.card_level) = #{cardLevel})
              and (#{status} is null or #{status} = '' or r.status = #{status})
              and (#{submissionType} is null or #{submissionType} = '' or r.submission_type = #{submissionType})
              and (#{submittedStart} is null or r.submitted_at >= #{submittedStart})
              and (#{submittedEnd} is null or r.submitted_at < #{submittedEnd})
              and (#{keyword} is null or #{keyword} = ''
                   or r.registration_no like concat('%', #{keyword}, '%')
                   or r.plate_number like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_name, u.name) like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_mobile, u.mobile) like concat('%', #{keyword}, '%')
                   or coalesce(r.card_no, c.card_no) like concat('%', #{keyword}, '%'))
              and (#{talentName} is null or #{talentName} = '' or coalesce(r.talent_name, u.name) like concat('%', #{talentName}, '%'))
              and (#{talentMobile} is null or #{talentMobile} = '' or coalesce(r.talent_mobile, u.mobile) like concat('%', #{talentMobile}, '%'))
              and (#{plateNumber} is null or #{plateNumber} = '' or r.plate_number like concat('%', #{plateNumber}, '%'))
              and (#{serviceKeyword} is null or #{serviceKeyword} = ''
                   or m.merchant_name like concat('%', #{serviceKeyword}, '%')
                   or coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) like concat('%', #{serviceKeyword}, '%'))
            """)
    Long countAdminPage(@Param("merchantId") Long merchantId,
                        @Param("parkingLotId") Long parkingLotId,
                        @Param("keyword") String keyword,
                        @Param("talentName") String talentName,
                        @Param("talentMobile") String talentMobile,
                        @Param("plateNumber") String plateNumber,
                        @Param("serviceKeyword") String serviceKeyword,
                        @Param("cardLevel") String cardLevel,
                        @Param("status") String status,
                        @Param("submissionType") String submissionType,
                        @Param("submittedStart") LocalDateTime submittedStart,
                        @Param("submittedEnd") LocalDateTime submittedEnd);
 
    @Select("""
            select r.id,
                   r.registration_no as registrationNo,
                   r.merchant_id as merchantId,
                   m.merchant_name as merchantName,
                   r.benefit_id as benefitId,
                   r.parking_lot_id as parkingLotId,
                   coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) as parkingLotName,
                   r.talent_id as talentId,
                   r.card_id as cardId,
                   coalesce(r.talent_name, u.name) as talentName,
                   coalesce(r.talent_mobile, u.mobile) as talentMobile,
                   coalesce(r.card_no, c.card_no) as cardNo,
                   coalesce(r.card_level, c.card_level) as cardLevel,
                   r.plate_number as plateNumber,
                   r.submission_type as submissionType,
                   r.submitted_at as submittedAt,
                   r.status,
                   r.handled_by as handledBy,
                   coalesce(op.display_name, mop.display_name) as handledByName,
                   r.handled_at as handledAt,
                   r.handle_remark as handleRemark,
                   r.created_at as createdAt,
                   r.updated_at as updatedAt
            from transport_parking_plate_registration r
            left join merchant m on m.id = r.merchant_id
            left join transport_parking_lot l on l.id = r.parking_lot_id
                and l.merchant_id = r.merchant_id and l.deleted = 0
            left join talent_user u on u.id = r.talent_id
            left join talent_card c on c.id = coalesce(r.card_id, (
                select c2.id
                from talent_card c2
                where c2.talent_id = r.talent_id
                  and c2.deleted = 0
                  and c2.status = 1
                order by case when c2.card_type = 'PERSONAL' then 0 else 1 end, c2.id desc
                limit 1
            ))
            left join admin_operator op on op.id = r.handled_by and op.deleted = 0
            left join merchant_operator mop on mop.id = r.handled_by and mop.deleted = 0
            where r.deleted = 0
              and (#{merchantId} is null or r.merchant_id = #{merchantId})
              and (#{parkingLotId} is null or r.parking_lot_id = #{parkingLotId})
              and (#{cardLevel} is null or #{cardLevel} = '' or coalesce(r.card_level, c.card_level) = #{cardLevel})
              and (#{status} is null or #{status} = '' or r.status = #{status})
              and (#{submissionType} is null or #{submissionType} = '' or r.submission_type = #{submissionType})
              and (#{submittedStart} is null or r.submitted_at >= #{submittedStart})
              and (#{submittedEnd} is null or r.submitted_at < #{submittedEnd})
              and (#{keyword} is null or #{keyword} = ''
                   or r.registration_no like concat('%', #{keyword}, '%')
                   or r.plate_number like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_name, u.name) like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_mobile, u.mobile) like concat('%', #{keyword}, '%')
                   or coalesce(r.card_no, c.card_no) like concat('%', #{keyword}, '%'))
              and (#{talentName} is null or #{talentName} = '' or coalesce(r.talent_name, u.name) like concat('%', #{talentName}, '%'))
              and (#{talentMobile} is null or #{talentMobile} = '' or coalesce(r.talent_mobile, u.mobile) like concat('%', #{talentMobile}, '%'))
              and (#{plateNumber} is null or #{plateNumber} = '' or r.plate_number like concat('%', #{plateNumber}, '%'))
              and (#{serviceKeyword} is null or #{serviceKeyword} = ''
                   or m.merchant_name like concat('%', #{serviceKeyword}, '%')
                   or coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) like concat('%', #{serviceKeyword}, '%'))
            order by r.submitted_at desc, r.id desc
            limit #{size} offset #{offset}
            """)
    List<Map<String, Object>> selectAdminPage(@Param("merchantId") Long merchantId,
                                              @Param("parkingLotId") Long parkingLotId,
                                              @Param("keyword") String keyword,
                                              @Param("talentName") String talentName,
                                              @Param("talentMobile") String talentMobile,
                                              @Param("plateNumber") String plateNumber,
                                              @Param("serviceKeyword") String serviceKeyword,
                                              @Param("cardLevel") String cardLevel,
                                              @Param("status") String status,
                                              @Param("submissionType") String submissionType,
                                              @Param("submittedStart") LocalDateTime submittedStart,
                                              @Param("submittedEnd") LocalDateTime submittedEnd,
                                              @Param("size") int size,
                                              @Param("offset") int offset);
 
    @Select("""
            select r.id,
                   r.registration_no as registrationNo,
                   r.merchant_id as merchantId,
                   m.merchant_name as merchantName,
                   m.contact_mobile as merchantContactMobile,
                   m.address as merchantAddress,
                   r.benefit_id as benefitId,
                   r.parking_lot_id as parkingLotId,
                   coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) as parkingLotName,
                   coalesce(r.parking_lot_address_snapshot, l.address) as parkingLotAddress,
                   r.talent_id as talentId,
                   r.card_id as cardId,
                   coalesce(r.talent_name, u.name) as talentName,
                   coalesce(r.talent_mobile, u.mobile) as talentMobile,
                   coalesce(r.card_no, c.card_no) as cardNo,
                   coalesce(r.card_level, c.card_level) as cardLevel,
                   r.plate_number as plateNumber,
                   r.submission_type as submissionType,
                   r.submitted_at as submittedAt,
                   r.status,
                   r.handled_by as handledBy,
                   coalesce(op.display_name, mop.display_name) as handledByName,
                   r.handled_at as handledAt,
                   r.handle_remark as handleRemark,
                   r.created_at as createdAt,
                   r.updated_at as updatedAt
            from transport_parking_plate_registration r
            left join merchant m on m.id = r.merchant_id
            left join transport_parking_lot l on l.id = r.parking_lot_id
                and l.merchant_id = r.merchant_id and l.deleted = 0
            left join talent_user u on u.id = r.talent_id
            left join talent_card c on c.id = coalesce(r.card_id, (
                select c2.id
                from talent_card c2
                where c2.talent_id = r.talent_id
                  and c2.deleted = 0
                  and c2.status = 1
                order by case when c2.card_type = 'PERSONAL' then 0 else 1 end, c2.id desc
                limit 1
            ))
            left join admin_operator op on op.id = r.handled_by and op.deleted = 0
            left join merchant_operator mop on mop.id = r.handled_by and mop.deleted = 0
            where r.id = #{id} and r.deleted = 0
            limit 1
            """)
    Map<String, Object> selectAdminDetail(@Param("id") Long id);
 
    @Select("""
            select *
            from transport_parking_plate_registration
            where id = #{id} and deleted = 0
            limit 1
            for update
            """)
    TransportParkingPlateRegistration selectByIdForUpdate(@Param("id") Long id);
 
    @Select("""
            select id, registration_no as registrationNo, status
            from transport_parking_plate_registration
            where idempotency_key = #{idempotencyKey} and deleted = 0
            limit 1
            """)
    Map<String, Object> selectByIdempotencyKey(@Param("idempotencyKey") String idempotencyKey);
    @Select("""
            select count(*)
            from transport_parking_plate_registration r
            left join transport_parking_lot l on l.id = r.parking_lot_id
                and l.merchant_id = r.merchant_id and l.deleted = 0
            left join talent_user u on u.id = r.talent_id
            left join talent_card c on c.id = coalesce(r.card_id, (
                select c2.id
                from talent_card c2
                where c2.talent_id = r.talent_id
                  and c2.deleted = 0
                  and c2.status = 1
                order by case when c2.card_type = 'PERSONAL' then 0 else 1 end, c2.id desc
                limit 1
            ))
            where r.deleted = 0
              and r.merchant_id = #{merchantId}
              and (#{parkingLotId} is null or r.parking_lot_id = #{parkingLotId})
              and (#{cardLevel} is null or #{cardLevel} = '' or coalesce(r.card_level, c.card_level) = #{cardLevel})
              and (#{status} is null or #{status} = '' or r.status = #{status})
              and (#{submissionType} is null or #{submissionType} = '' or r.submission_type = #{submissionType})
              and (#{submittedStart} is null or r.submitted_at >= #{submittedStart})
              and (#{submittedEnd} is null or r.submitted_at < #{submittedEnd})
              and (#{keyword} is null or #{keyword} = ''
                   or r.registration_no like concat('%', #{keyword}, '%')
                   or r.plate_number like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_name, u.name) like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_mobile, u.mobile) like concat('%', #{keyword}, '%')
                   or coalesce(r.card_no, c.card_no) like concat('%', #{keyword}, '%')
                   or coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) like concat('%', #{keyword}, '%'))
            """)
    Long countMerchantPage(@Param("merchantId") Long merchantId,
                           @Param("parkingLotId") Long parkingLotId,
                           @Param("keyword") String keyword,
                           @Param("cardLevel") String cardLevel,
                           @Param("status") String status,
                           @Param("submissionType") String submissionType,
                           @Param("submittedStart") LocalDateTime submittedStart,
                           @Param("submittedEnd") LocalDateTime submittedEnd);
 
    @Select("""
            select r.id,
                   r.registration_no as registrationNo,
                   r.merchant_id as merchantId,
                   m.merchant_name as merchantName,
                   r.benefit_id as benefitId,
                   r.parking_lot_id as parkingLotId,
                   coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) as parkingLotName,
                   r.talent_id as talentId,
                   r.card_id as cardId,
                   coalesce(r.talent_name, u.name) as talentName,
                   coalesce(r.talent_mobile, u.mobile) as talentMobile,
                   coalesce(r.card_no, c.card_no) as cardNo,
                   coalesce(r.card_level, c.card_level) as cardLevel,
                   r.plate_number as plateNumber,
                   r.submission_type as submissionType,
                   r.submitted_at as submittedAt,
                   r.status,
                   r.handled_by as handledBy,
                   mo.display_name as handledByName,
                   r.handled_at as handledAt,
                   r.handle_remark as handleRemark,
                   r.created_at as createdAt,
                   r.updated_at as updatedAt
            from transport_parking_plate_registration r
            left join merchant m on m.id = r.merchant_id
            left join transport_parking_lot l on l.id = r.parking_lot_id
                and l.merchant_id = r.merchant_id and l.deleted = 0
            left join talent_user u on u.id = r.talent_id
            left join talent_card c on c.id = coalesce(r.card_id, (
                select c2.id
                from talent_card c2
                where c2.talent_id = r.talent_id
                  and c2.deleted = 0
                  and c2.status = 1
                order by case when c2.card_type = 'PERSONAL' then 0 else 1 end, c2.id desc
                limit 1
            ))
            left join merchant_operator mo on mo.id = r.handled_by and mo.deleted = 0
            where r.deleted = 0
              and r.merchant_id = #{merchantId}
              and (#{parkingLotId} is null or r.parking_lot_id = #{parkingLotId})
              and (#{cardLevel} is null or #{cardLevel} = '' or coalesce(r.card_level, c.card_level) = #{cardLevel})
              and (#{status} is null or #{status} = '' or r.status = #{status})
              and (#{submissionType} is null or #{submissionType} = '' or r.submission_type = #{submissionType})
              and (#{submittedStart} is null or r.submitted_at >= #{submittedStart})
              and (#{submittedEnd} is null or r.submitted_at < #{submittedEnd})
              and (#{keyword} is null or #{keyword} = ''
                   or r.registration_no like concat('%', #{keyword}, '%')
                   or r.plate_number like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_name, u.name) like concat('%', #{keyword}, '%')
                   or coalesce(r.talent_mobile, u.mobile) like concat('%', #{keyword}, '%')
                   or coalesce(r.card_no, c.card_no) like concat('%', #{keyword}, '%')
                   or coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) like concat('%', #{keyword}, '%'))
            order by r.submitted_at desc, r.id desc
            limit #{size} offset #{offset}
            """)
    List<Map<String, Object>> selectMerchantPage(@Param("merchantId") Long merchantId,
                                                 @Param("parkingLotId") Long parkingLotId,
                                                 @Param("keyword") String keyword,
                                                 @Param("cardLevel") String cardLevel,
                                                 @Param("status") String status,
                                                 @Param("submissionType") String submissionType,
                                                 @Param("submittedStart") LocalDateTime submittedStart,
                                                 @Param("submittedEnd") LocalDateTime submittedEnd,
                                                 @Param("size") int size,
                                                 @Param("offset") int offset);
 
    @Select("""
            select r.id,
                   r.registration_no as registrationNo,
                   r.merchant_id as merchantId,
                   m.merchant_name as merchantName,
                   m.contact_mobile as merchantContactMobile,
                   m.address as merchantAddress,
                   r.benefit_id as benefitId,
                   r.parking_lot_id as parkingLotId,
                   coalesce(r.parking_lot_name_snapshot, l.parking_lot_name) as parkingLotName,
                   coalesce(r.parking_lot_address_snapshot, l.address) as parkingLotAddress,
                   r.talent_id as talentId,
                   r.card_id as cardId,
                   coalesce(r.talent_name, u.name) as talentName,
                   coalesce(r.talent_mobile, u.mobile) as talentMobile,
                   coalesce(r.card_no, c.card_no) as cardNo,
                   coalesce(r.card_level, c.card_level) as cardLevel,
                   r.plate_number as plateNumber,
                   r.submission_type as submissionType,
                   r.submitted_at as submittedAt,
                   r.status,
                   r.handled_by as handledBy,
                   mo.display_name as handledByName,
                   r.handled_at as handledAt,
                   r.handle_remark as handleRemark,
                   r.created_at as createdAt,
                   r.updated_at as updatedAt
            from transport_parking_plate_registration r
            left join merchant m on m.id = r.merchant_id
            left join transport_parking_lot l on l.id = r.parking_lot_id
                and l.merchant_id = r.merchant_id and l.deleted = 0
            left join talent_user u on u.id = r.talent_id
            left join talent_card c on c.id = coalesce(r.card_id, (
                select c2.id
                from talent_card c2
                where c2.talent_id = r.talent_id
                  and c2.deleted = 0
                  and c2.status = 1
                order by case when c2.card_type = 'PERSONAL' then 0 else 1 end, c2.id desc
                limit 1
            ))
            left join merchant_operator mo on mo.id = r.handled_by and mo.deleted = 0
            where r.id = #{id} and r.merchant_id = #{merchantId} and r.deleted = 0
            limit 1
            """)
    Map<String, Object> selectMerchantDetail(@Param("id") Long id, @Param("merchantId") Long merchantId);
}