shinnosuke
2026-08-20 bacaa6e8f87ba4167901095ddb82c4a2ac2e74ca
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
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.TicketPackage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
 
@Mapper
public interface TicketPackageMapper extends BaseMapper<TicketPackage> {
 
    @Select("""
            select p.id,
                   p.package_no as packageNo,
                   p.package_name as packageName,
                   p.total_quantity as totalQuantity,
                   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(pm1.merchant_id order by pm1.sort_no asc, pm1.id asc separator ',')
                       from ticket_package_merchant pm1
                       where pm1.package_id = p.id and pm1.deleted = 0
                   ) as merchantIdsText,
                   (
                       select group_concat(m1.merchant_name order by pm2.sort_no asc, pm2.id asc separator '、')
                       from ticket_package_merchant pm2
                       join merchant m1 on m1.id = pm2.merchant_id and m1.deleted = 0
                       where pm2.package_id = p.id and pm2.deleted = 0
                   ) as merchantNames,
                   (
                       select group_concat(concat(r.talent_level, '|', r.enabled, '|', r.claimable_count, '|', r.reset_cycle) order by r.id asc separator ';;')
                       from ticket_package_level_rule r
                       where r.package_id = p.id and r.deleted = 0
                   ) as levelRulesText,
                   p.cover_url as coverUrl,
                   p.talent_levels as talentLevels,
                   p.claim_price as claimPrice,
                   p.per_user_limit as perUserLimit,
                   p.valid_start as validStart,
                   p.valid_end as validEnd,
                   p.app_name as appName,
                   p.app_jump_url as appJumpUrl,
                   p.usage_text as usageText,
                   p.status,
                   p.sort_no as sortNo,
                   p.created_at as createdAt,
                   p.updated_at as updatedAt,
                   count(distinct c.id) as totalCodes,
                   count(distinct case when c.status = 'AVAILABLE' and c.deleted = 0 then c.id end) as availableCodes,
                   count(distinct case when c.status = 'LOCKED' and c.deleted = 0 then c.id end) as lockedCodes,
                   count(distinct case when c.status = 'ISSUED' and c.deleted = 0 then c.id end) as issuedCodes,
                   count(distinct case when c.status = 'USED' and c.deleted = 0 then c.id end) as usedCodes,
                   count(distinct case when c.status = 'VOID' and c.deleted = 0 then c.id end) as voidCodes,
                   count(distinct case when cl.status in ('ISSUED', 'USED') and cl.deleted = 0 then cl.id end) as claimedCount
            from ticket_package p
            left join merchant m on m.id = p.merchant_id and m.deleted = 0
            left join ticket_package_code c on c.package_id = p.id and c.deleted = 0
            left join ticket_package_claim cl on cl.package_id = p.id and cl.deleted = 0
            where p.deleted = 0
              and (#{merchantId} is null or 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 (#{status} is null or p.status = #{status})
              and (#{keyword} is null or #{keyword} = '' or p.package_name like concat('%', #{keyword}, '%') or p.package_no like concat('%', #{keyword}, '%'))
              and (#{merchantKeyword} is null or #{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}, '%')
              ) or exists (
                  select 1
                  from merchant mm
                  where mm.id = p.merchant_id and mm.deleted = 0 and mm.merchant_name like concat('%', #{merchantKeyword}, '%')
              ))
            group by p.id
            order by p.sort_no desc, p.id desc
            limit #{size} offset #{offset}
            """)
    List<Map<String, Object>> selectAdminPage(@Param("merchantId") Long merchantId,
                                              @Param("status") Integer status,
                                              @Param("keyword") String keyword,
                                              @Param("merchantKeyword") String merchantKeyword,
                                              @Param("size") int size,
                                              @Param("offset") int offset);
 
    @Select("""
            select count(*)
            from ticket_package p
            where p.deleted = 0
              and (#{merchantId} is null or 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 (#{status} is null or p.status = #{status})
              and (#{keyword} is null or #{keyword} = '' or p.package_name like concat('%', #{keyword}, '%') or p.package_no like concat('%', #{keyword}, '%'))
              and (#{merchantKeyword} is null or #{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}, '%')
              ) or exists (
                  select 1
                  from merchant mm
                  where mm.id = p.merchant_id and mm.deleted = 0 and mm.merchant_name like concat('%', #{merchantKeyword}, '%')
              ))
            """)
    Long countAdminPage(@Param("merchantId") Long merchantId,
                        @Param("status") Integer status,
                        @Param("keyword") String keyword,
                        @Param("merchantKeyword") String merchantKeyword);
 
    @Select("""
            select p.id,
                   p.package_no as packageNo,
                   p.package_name as packageName,
                   p.total_quantity as totalQuantity,
                   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(pm1.merchant_id order by pm1.sort_no asc, pm1.id asc separator ',')
                       from ticket_package_merchant pm1
                       where pm1.package_id = p.id and pm1.deleted = 0
                   ) as merchantIdsText,
                   (
                       select group_concat(m1.merchant_name order by pm2.sort_no asc, pm2.id asc separator '、')
                       from ticket_package_merchant pm2
                       join merchant m1 on m1.id = pm2.merchant_id and m1.deleted = 0
                       where pm2.package_id = p.id and pm2.deleted = 0
                   ) as merchantNames,
                   (
                       select group_concat(concat(r.talent_level, '|', r.enabled, '|', r.claimable_count, '|', r.reset_cycle) order by r.id asc separator ';;')
                       from ticket_package_level_rule r
                       where r.package_id = p.id and r.deleted = 0
                   ) as levelRulesText,
                   p.cover_url as coverUrl,
                   p.talent_levels as talentLevels,
                   p.claim_price as claimPrice,
                   p.per_user_limit as perUserLimit,
                   p.valid_start as validStart,
                   p.valid_end as validEnd,
                   p.app_name as appName,
                   p.app_jump_url as appJumpUrl,
                   p.usage_text as usageText,
                   p.status,
                   p.sort_no as sortNo,
                   p.created_at as createdAt,
                   p.updated_at as updatedAt,
                   count(distinct c.id) as totalCodes,
                   count(distinct case when c.status = 'AVAILABLE' and c.deleted = 0 then c.id end) as availableCodes,
                   count(distinct case when c.status = 'LOCKED' and c.deleted = 0 then c.id end) as lockedCodes,
                   count(distinct case when c.status = 'ISSUED' and c.deleted = 0 then c.id end) as issuedCodes,
                   count(distinct case when c.status = 'USED' and c.deleted = 0 then c.id end) as usedCodes,
                   count(distinct case when c.status = 'VOID' and c.deleted = 0 then c.id end) as voidCodes,
                   count(distinct case when cl.status in ('ISSUED', 'USED') and cl.deleted = 0 then cl.id end) as claimedCount
            from ticket_package p
            left join merchant m on m.id = p.merchant_id and m.deleted = 0
            left join ticket_package_code c on c.package_id = p.id and c.deleted = 0
            left join ticket_package_claim cl on cl.package_id = p.id and cl.deleted = 0
            where p.id = #{id} and p.deleted = 0
            group by p.id
            limit 1
            """)
    Map<String, Object> selectAdminDetail(@Param("id") Long id);
 
    @Select("""
            select p.id,
                   p.package_no as packageNo,
                   p.package_name as packageName,
                   p.total_quantity as totalQuantity,
                   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.cover_url as coverUrl,
                   p.talent_levels as talentLevels,
                   p.claim_price as claimPrice,
                   p.per_user_limit as perUserLimit,
                   p.valid_start as validStart,
                   p.valid_end as validEnd,
                   p.app_name as appName,
                   p.app_jump_url as appJumpUrl,
                   p.usage_text as usageText,
                   p.status,
                   p.sort_no as sortNo,
                   count(distinct c.id) as totalCodes,
                   count(distinct case when c.status = 'AVAILABLE' and c.deleted = 0 then c.id end) as availableCodes,
                   count(distinct case when c.status = 'LOCKED' and c.deleted = 0 then c.id end) as lockedCodes,
                   count(distinct case when c.status = 'ISSUED' and c.deleted = 0 then c.id end) as issuedCodes,
                   count(distinct case when c.status = 'USED' and c.deleted = 0 then c.id end) as usedCodes,
                   count(distinct case when c.status = 'VOID' and c.deleted = 0 then c.id end) as voidCodes,
                   count(distinct case when cl.status in ('ISSUED', 'USED') and cl.deleted = 0 then cl.id end) as claimedCount
            from ticket_package p
            left join merchant m on m.id = p.merchant_id and m.deleted = 0
            left join ticket_package_code c on c.package_id = p.id and c.deleted = 0
            left join ticket_package_claim cl on cl.package_id = p.id and cl.deleted = 0
            where p.deleted = 0
              and p.status = 1
              and (p.valid_start is null or p.valid_start <= current_date)
              and (p.valid_end is null or p.valid_end >= current_date)
              and (#{merchantId} is null or 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
              ))
            group by p.id
            order by p.sort_no desc, p.id desc
            """)
    List<Map<String, Object>> selectPortalPackages(@Param("merchantId") Long merchantId);
 
    @Select("""
            select p.*
            from ticket_package p
            where p.id = #{id} and p.deleted = 0
            limit 1
            for update
            """)
    TicketPackage selectByIdForUpdate(@Param("id") Long id);
 
    @Select("""
            select count(*)
            from ticket_package p
            where p.id = #{packageId}
              and p.deleted = 0
              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
              ))
            """)
    Long countPackageMerchant(@Param("packageId") Long packageId, @Param("merchantId") Long merchantId);
}