shinnosuke
2026-08-21 bc2efbdb52190fe2e7be9920bbed6902d4ee28aa
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
package com.talentcard.server.mapper;
 
import java.util.List;
import java.util.Map;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.talentcard.server.entity.TicketPackageCode;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
 
@Mapper
public interface TicketPackageCodeMapper extends BaseMapper<TicketPackageCode> {
 
    @Select("""
            select *
            from ticket_package_code
            where id = #{id}
              and deleted = 0
            limit 1
            for update
            """)
    TicketPackageCode selectByIdForUpdate(@Param("id") Long id);
 
    @Select("""
            select *
            from ticket_package_code
            where exchange_code = #{exchangeCode}
              and deleted = 0
              and (#{packageId} is null or package_id = #{packageId})
            limit 1
            for update
            """)
    TicketPackageCode selectByExchangeCodeForUpdate(@Param("exchangeCode") String exchangeCode,
                                                    @Param("packageId") Long packageId);
 
    @Select("""
            select *
            from ticket_package_code
            where package_id = #{packageId}
              and status = 'AVAILABLE'
              and deleted = 0
            order by id asc
            limit 1
            for update
            """)
    TicketPackageCode selectAvailableCodeForUpdate(@Param("packageId") Long packageId);
 
    @Select("""
            select c.*
            from ticket_package_code c
            join ticket_package p on p.id = c.package_id and p.deleted = 0 and p.status = 1
            where c.deleted = 0
              and c.status = 'AVAILABLE'
              and (p.merchant_id = #{merchantId} or exists (
                  select 1 from ticket_package_merchant pm
                  where pm.package_id = p.id and pm.merchant_id = #{merchantId} and pm.deleted = 0
              ))
              and (p.valid_start is null or p.valid_start <= current_date)
              and (p.valid_end is null or p.valid_end >= current_date)
            order by c.id asc
            limit 1
            for update
            """)
    TicketPackageCode selectAvailableByMerchantForUpdate(@Param("merchantId") Long merchantId);
 
    @Update("""
            update ticket_package_code
            set status = 'LOCKED',
                locked_by_talent_id = #{talentId},
                locked_by_card_id = #{cardId},
                locked_at = now()
            where id = #{codeId}
              and status = 'AVAILABLE'
              and deleted = 0
            """)
    int lockForIssue(@Param("codeId") Long codeId,
                     @Param("talentId") Long talentId,
                     @Param("cardId") Long cardId);
 
    @Update("""
            update ticket_package_code
            set status = 'AVAILABLE',
                locked_by_talent_id = null,
                locked_by_card_id = null,
                locked_at = null
            where id = #{codeId}
              and status = 'LOCKED'
              and deleted = 0
            """)
    int releaseLock(@Param("codeId") Long codeId);
 
    @Select("""
            select c.id,
                   c.package_id as packageId,
                   p.package_no as packageNo,
                   p.package_name as packageName,
                   p.merchant_id as merchantId,
                   coalesce(m.merchant_name, (
                       select m0.merchant_name
                       from ticket_package_merchant pm0
                       join merchant m0 on m0.id = pm0.merchant_id and m0.deleted = 0
                       where pm0.package_id = p.id and pm0.deleted = 0
                       order by pm0.sort_no asc, pm0.id asc
                       limit 1
                   )) as merchantName,
                   (
                       select group_concat(m1.merchant_name order by pm1.sort_no asc, pm1.id asc separator '、')
                       from ticket_package_merchant pm1
                       join merchant m1 on m1.id = pm1.merchant_id and m1.deleted = 0
                       where pm1.package_id = p.id and pm1.deleted = 0
                   ) as merchantNames,
                   p.valid_start as validStart,
                   p.valid_end as validEnd,
                   p.status as packageStatus,
                   c.exchange_code as exchangeCode,
                   c.batch_no as batchNo,
                   c.status,
                   c.locked_by_talent_id as lockedByTalentId,
                   c.locked_by_card_id as lockedByCardId,
                   c.locked_at as lockedAt,
                   c.issued_claim_id as issuedClaimId,
                   c.issued_at as issuedAt,
                   c.used_at as usedAt,
                   c.remark,
                   c.created_at as createdAt,
                   c.updated_at as updatedAt,
                   cl.claim_no as claimNo,
                   cl.status as claimStatus,
                   cl.talent_id as claimTalentId,
                   cl.talent_name as claimTalentName,
                   cl.talent_mobile as claimTalentMobile,
                   cl.card_no as claimCardNo,
                   cl.card_level as claimCardLevel,
                   cl.idempotency_key as idempotencyKey,
                   cl.valid_end as claimValidEnd,
                   cl.used_at as claimUsedAt
            from ticket_package_code c
            join ticket_package p on p.id = c.package_id and p.deleted = 0
            left join merchant m on m.id = p.merchant_id and m.deleted = 0
            left join ticket_package_claim cl on cl.code_id = c.id and cl.deleted = 0
            where c.id = #{id}
              and c.deleted = 0
            limit 1
            """)
    Map<String, Object> selectAdminDetailById(@Param("id") Long id);
 
    @Select("""
            select c.id,
                   c.package_id as packageId,
                   p.package_no as packageNo,
                   p.package_name as packageName,
                   p.merchant_id as merchantId,
                   coalesce(m.merchant_name, (
                       select m0.merchant_name
                       from ticket_package_merchant pm0
                       join merchant m0 on m0.id = pm0.merchant_id and m0.deleted = 0
                       where pm0.package_id = p.id and pm0.deleted = 0
                       order by pm0.sort_no asc, pm0.id asc
                       limit 1
                   )) as merchantName,
                   (
                       select group_concat(m1.merchant_name order by pm1.sort_no asc, pm1.id asc separator '、')
                       from ticket_package_merchant pm1
                       join merchant m1 on m1.id = pm1.merchant_id and m1.deleted = 0
                       where pm1.package_id = p.id and pm1.deleted = 0
                   ) as merchantNames,
                   p.valid_start as validStart,
                   p.valid_end as validEnd,
                   p.status as packageStatus,
                   c.exchange_code as exchangeCode,
                   c.batch_no as batchNo,
                   c.status,
                   c.locked_by_talent_id as lockedByTalentId,
                   c.locked_by_card_id as lockedByCardId,
                   c.locked_at as lockedAt,
                   c.issued_claim_id as issuedClaimId,
                   c.issued_at as issuedAt,
                   c.used_at as usedAt,
                   c.remark,
                   c.created_at as createdAt,
                   c.updated_at as updatedAt,
                   cl.claim_no as claimNo,
                   cl.status as claimStatus,
                   cl.talent_id as claimTalentId,
                   cl.talent_name as claimTalentName,
                   cl.talent_mobile as claimTalentMobile,
                   cl.card_no as claimCardNo,
                   cl.card_level as claimCardLevel,
                   cl.idempotency_key as idempotencyKey,
                   cl.valid_end as claimValidEnd,
                   cl.used_at as claimUsedAt
            from ticket_package_code c
            join ticket_package p on p.id = c.package_id and p.deleted = 0
            left join merchant m on m.id = p.merchant_id and m.deleted = 0
            left join ticket_package_claim cl on cl.code_id = c.id and cl.deleted = 0
            where c.exchange_code = #{exchangeCode}
              and c.deleted = 0
              and (#{packageId} is null or c.package_id = #{packageId})
            limit 1
            """)
    Map<String, Object> selectAdminDetailByExchangeCode(@Param("exchangeCode") String exchangeCode,
                                                        @Param("packageId") Long packageId);
 
    @Select("""
            select c.id,
                   c.package_id as packageId,
                   p.package_no as packageNo,
                   p.package_name as packageName,
                   coalesce((
                       select group_concat(m1.merchant_name order by pm1.sort_no asc, pm1.id asc separator '、')
                       from ticket_package_merchant pm1
                       join merchant m1 on m1.id = pm1.merchant_id and m1.deleted = 0
                       where pm1.package_id = p.id and pm1.deleted = 0
                   ), m.merchant_name) as merchantNames,
                   c.exchange_code as exchangeCode,
                   c.batch_no as batchNo,
                   c.status,
                   c.locked_by_talent_id as lockedByTalentId,
                   c.locked_by_card_id as lockedByCardId,
                   coalesce(cl.talent_name, u.name) as lockedTalentName,
                   coalesce(cl.talent_mobile, u.mobile) as lockedTalentMobile,
                   c.locked_at as lockedAt,
                   c.issued_claim_id as issuedClaimId,
                   c.issued_at as issuedAt,
                   c.used_at as usedAt,
                   c.remark,
                   c.created_at as createdAt,
                   c.updated_at as updatedAt
            from ticket_package_code c
            join ticket_package p on p.id = c.package_id and p.deleted = 0
            left join merchant m on m.id = p.merchant_id and m.deleted = 0
            left join ticket_package_claim cl on cl.code_id = c.id and cl.deleted = 0
            left join talent_user u on u.id = c.locked_by_talent_id and u.deleted = 0
            where c.deleted = 0
              and (#{packageId} is null or c.package_id = #{packageId})
              and (#{status} is null or #{status} = '' or c.status = #{status})
              and (#{batchNo} is null or #{batchNo} = '' or c.batch_no = #{batchNo})
              and (#{keyword} is null or #{keyword} = '' or cast(c.id as char) like concat('%', #{keyword}, '%') or c.exchange_code like concat('%', #{keyword}, '%'))
              and (#{packageKeyword} is null or #{packageKeyword} = '' or p.package_name like concat('%', #{packageKeyword}, '%') or p.package_no like concat('%', #{packageKeyword}, '%'))
              and (#{merchantKeyword} is null or #{merchantKeyword} = '' or m.merchant_name like concat('%', #{merchantKeyword}, '%') or exists (
                  select 1
                  from ticket_package_merchant pm
                  join merchant mm on mm.id = pm.merchant_id and mm.deleted = 0
                  where pm.package_id = p.id and pm.deleted = 0 and mm.merchant_name like concat('%', #{merchantKeyword}, '%')
              ))
            order by c.id desc
            limit #{size} offset #{offset}
            """)
    List<Map<String, Object>> selectAdminPage(@Param("packageId") Long packageId,
                                              @Param("status") String status,
                                              @Param("batchNo") String batchNo,
                                              @Param("keyword") String keyword,
                                              @Param("packageKeyword") String packageKeyword,
                                              @Param("merchantKeyword") String merchantKeyword,
                                              @Param("size") int size,
                                              @Param("offset") int offset);
 
    @Select("""
            select count(*)
            from ticket_package_code c
            join ticket_package p on p.id = c.package_id and p.deleted = 0
            left join merchant m on m.id = p.merchant_id and m.deleted = 0
            where c.deleted = 0
              and (#{packageId} is null or c.package_id = #{packageId})
              and (#{status} is null or #{status} = '' or c.status = #{status})
              and (#{batchNo} is null or #{batchNo} = '' or c.batch_no = #{batchNo})
              and (#{keyword} is null or #{keyword} = '' or cast(c.id as char) like concat('%', #{keyword}, '%') or c.exchange_code like concat('%', #{keyword}, '%'))
              and (#{packageKeyword} is null or #{packageKeyword} = '' or p.package_name like concat('%', #{packageKeyword}, '%') or p.package_no like concat('%', #{packageKeyword}, '%'))
              and (#{merchantKeyword} is null or #{merchantKeyword} = '' or m.merchant_name like concat('%', #{merchantKeyword}, '%') or exists (
                  select 1
                  from ticket_package_merchant pm
                  join merchant mm on mm.id = pm.merchant_id and mm.deleted = 0
                  where pm.package_id = p.id and pm.deleted = 0 and mm.merchant_name like concat('%', #{merchantKeyword}, '%')
              ))
            """)
    Long countAdminPage(@Param("packageId") Long packageId,
                        @Param("status") String status,
                        @Param("batchNo") String batchNo,
                        @Param("keyword") String keyword,
                        @Param("packageKeyword") String packageKeyword,
                        @Param("merchantKeyword") String merchantKeyword);
}