wenminjie
2026-08-05 652b229d0a675f5bfdf297d5f116fe9698ff2725
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
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.TicketPackageClaim;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
 
@Mapper
public interface TicketPackageClaimMapper extends BaseMapper<TicketPackageClaim> {
 
    @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
            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
            for update
            """)
    Map<String, Object> selectActiveCardSnapshotForUpdate(@Param("talentId") Long talentId, @Param("cardId") Long cardId);
 
    @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
            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 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
            from family_card_application f
            join talent_card c on c.talent_id = f.talent_id and c.card_type = 'PERSONAL'
            join talent_user u on u.id = c.talent_id and u.deleted = 0 and u.status = 1
            where f.deleted = 0
              and f.status = '已通过'
              and f.family_card_id = #{familyCardId}
              and 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())
            order by c.id desc
            limit 1
            for update
            """)
    Map<String, Object> selectPrimaryCardByFamilyCardForUpdate(@Param("familyCardId") Long familyCardId);
 
    @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
            from family_card_application f
            join talent_card c on c.talent_id = f.talent_id and c.card_type = 'PERSONAL'
            join talent_user u on u.id = c.talent_id and u.deleted = 0 and u.status = 1
            where f.deleted = 0
              and f.status = '已通过'
              and f.family_card_id = #{familyCardId}
              and 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())
            order by c.id desc
            limit 1
            """)
    Map<String, Object> selectPrimaryCardByFamilyCard(@Param("familyCardId") Long familyCardId);
    @Select("""
            select *
            from ticket_package_claim
            where idempotency_key = #{idempotencyKey} and deleted = 0
            limit 1
            """)
    TicketPackageClaim selectByIdempotencyKey(@Param("idempotencyKey") String idempotencyKey);
    @Select("""
            select *
            from ticket_package_claim
            where code_id = #{codeId}
              and deleted = 0
            limit 1
            """)
    TicketPackageClaim selectByCodeId(@Param("codeId") Long codeId);
 
    @Select("""
            select count(*)
            from ticket_package_claim
            where deleted = 0
              and package_id = #{packageId}
              and primary_card_id = #{primaryCardId}
              and status in ('ISSUED', 'USED')
              and created_at >= #{windowStart}
            """)
    Long countClaimedInWindow(@Param("packageId") Long packageId,
                              @Param("primaryCardId") Long primaryCardId,
                              @Param("windowStart") LocalDateTime windowStart);
 
    @Select("""
            select count(*)
            from ticket_package_claim
            where deleted = 0
              and primary_card_id = #{primaryCardId}
              and status in ('ISSUED', 'USED')
              and created_at >= #{windowStart}
            """)
    Long countTotalClaimedInWindow(@Param("primaryCardId") Long primaryCardId,
                                   @Param("windowStart") LocalDateTime windowStart);
 
    @Select("""
            select cl.id,
                   cl.claim_no as claimNo,
                   cl.package_id as packageId,
                   p.package_name as packageName,
                   cl.merchant_id as merchantId,
                   m.merchant_name as merchantName,
                   cl.talent_id as talentId,
                   cl.card_id as cardId,
                   cl.primary_card_id as primaryCardId,
                   cl.card_no as cardNo,
                   cl.card_level as cardLevel,
                   cl.talent_name as talentName,
                   cl.talent_mobile as talentMobile,
                   cl.exchange_code as exchangeCode,
                   cl.idempotency_key as idempotencyKey,
                   cl.status,
                   cl.valid_end as validEnd,
                   cl.used_at as usedAt,
                   cl.created_at as createdAt,
                   cl.updated_at as updatedAt
            from ticket_package_claim cl
            join ticket_package p on p.id = cl.package_id and p.deleted = 0
            left join merchant m on m.id = cl.merchant_id and m.deleted = 0
            where cl.deleted = 0
              and (#{packageId} is null or cl.package_id = #{packageId})
              and (#{talentId} is null or cl.talent_id = #{talentId})
              and (#{status} is null or #{status} = '' or cl.status = #{status})
              and (#{keyword} is null or #{keyword} = '' or cl.claim_no like concat('%', #{keyword}, '%') or cl.exchange_code like concat('%', #{keyword}, '%')
                   or cl.talent_name like concat('%', #{keyword}, '%') or cl.talent_mobile like concat('%', #{keyword}, '%'))
            order by cl.created_at desc, cl.id desc
            limit #{size} offset #{offset}
            """)
    List<Map<String, Object>> selectAdminPage(@Param("packageId") Long packageId,
                                              @Param("talentId") Long talentId,
                                              @Param("status") String status,
                                              @Param("keyword") String keyword,
                                              @Param("size") int size,
                                              @Param("offset") int offset);
 
    @Select("""
            select count(*)
            from ticket_package_claim cl
            join ticket_package p on p.id = cl.package_id and p.deleted = 0
            where cl.deleted = 0
              and (#{packageId} is null or cl.package_id = #{packageId})
              and (#{talentId} is null or cl.talent_id = #{talentId})
              and (#{status} is null or #{status} = '' or cl.status = #{status})
              and (#{keyword} is null or #{keyword} = '' or cl.claim_no like concat('%', #{keyword}, '%') or cl.exchange_code like concat('%', #{keyword}, '%')
                   or cl.talent_name like concat('%', #{keyword}, '%') or cl.talent_mobile like concat('%', #{keyword}, '%'))
            """)
    Long countAdminPage(@Param("packageId") Long packageId,
                        @Param("talentId") Long talentId,
                        @Param("status") String status,
                        @Param("keyword") String keyword);
}