shinnosuke
2026-09-27 35c0810b268661c79f776fe922f7e3834cba4186
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package com.talentcard.server.service;
 
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.qrcode.QRCodeWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.talentcard.server.business.BusinessIdGenerator;
import com.talentcard.server.request.BenefitVerificationCodeRequest;
import com.talentcard.server.common.BusinessException;
import com.talentcard.server.common.ErrorCode;
import com.talentcard.server.entity.BenefitCinemaCoupon;
import com.talentcard.server.entity.BenefitCinemaCouponClaim;
import com.talentcard.server.entity.BenefitFreeStayVerificationCode;
import com.talentcard.server.entity.BenefitFreeStayVoucher;
import com.talentcard.server.entity.BenefitMerchant;
import com.talentcard.server.entity.TicketPackageCode;
import com.talentcard.server.entity.TicketPackageClaim;
import com.talentcard.server.mapper.BenefitCinemaCouponClaimMapper;
import com.talentcard.server.mapper.BenefitCinemaCouponMapper;
import com.talentcard.server.mapper.BenefitFreeStayVerificationCodeMapper;
import com.talentcard.server.mapper.BenefitFreeStayVoucherMapper;
import com.talentcard.server.mapper.BenefitMerchantMapper;
import com.talentcard.server.mapper.TicketPackageCodeMapper;
import com.talentcard.server.mapper.TicketPackageClaimMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
public class BenefitVerificationCodeService {
    public static final int MOVIE_CODE_SECONDS = 0;
    public static final int HOTEL_CODE_SECONDS = 60;
    public static final String MOVIE_TOKEN_PREFIX = "WD-";
    public static final String HOTEL_TOKEN_PREFIX = "HV2";
 
    private final BenefitQuotaService quotaService;
    private final BenefitMerchantMapper merchantMapper;
    private final BenefitCinemaCouponMapper couponMapper;
    private final BenefitCinemaCouponClaimMapper claimMapper;
    private final BenefitFreeStayVoucherMapper voucherMapper;
    private final BenefitFreeStayVerificationCodeMapper freeStayCodeMapper;
    private final TicketPackageCodeMapper codeMapper;
    private final TicketPackageClaimMapper ticketPackageClaimMapper;
    private final TicketPackagePortalService ticketPackageService;
 
    public BenefitVerificationCodeService(
            BenefitQuotaService quotaService,
            BenefitMerchantMapper merchantMapper,
            TicketPackageCodeMapper codeMapper,
            TicketPackageClaimMapper ticketPackageClaimMapper,
            TicketPackagePortalService ticketPackageService,
            BenefitCinemaCouponMapper couponMapper,
            BenefitCinemaCouponClaimMapper claimMapper,
            BenefitFreeStayVoucherMapper voucherMapper,
            BenefitFreeStayVerificationCodeMapper freeStayCodeMapper
    ) {
        this.quotaService = quotaService;
        this.merchantMapper = merchantMapper;
        this.codeMapper = codeMapper;
        this.ticketPackageClaimMapper = ticketPackageClaimMapper;
        this.ticketPackageService = ticketPackageService;
        this.couponMapper = couponMapper;
        this.claimMapper = claimMapper;
        this.voucherMapper = voucherMapper;
        this.freeStayCodeMapper = freeStayCodeMapper;
    }
 
    @Transactional(readOnly = true)
    public byte[] movieCodeImage(Long claimId) {
        BenefitCinemaCouponClaim claim = movieClaimById(claimId);
        if (!activeMovieCode(claim)) {
            throw new BusinessException(ErrorCode.CONFLICT, "电影票兑换码不可用");
        }
        return qrPng(claim.getExchangeCode(), "电影票二维码生成失败");
    }
 
    /**
     * 观影权益(票包兑换码)的二维码:内容是后台批量生成、绑定影城的那串兑换码。
     *
     * <p>与电影券的二维码分成两个入口,因为两者的领取记录不在同一张表——同一个 id 可能两边都存在,
     * 合并成一个接口会张冠李戴。
     */
    @Transactional(readOnly = true)
    public byte[] ticketPackageCodeImage(Long claimId) {
        TicketPackageClaim claim = ticketPackageClaimMapper.selectById(claimId);
        if (claim == null || (claim.getDeleted() != null && claim.getDeleted() != 0)) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "观影权益领取记录不存在");
        }
        if (!"ISSUED".equals(upper(claim.getStatus()))) {
            throw new BusinessException(ErrorCode.CONFLICT, "观影权益已核销或已失效");
        }
        if (expiredDate(claim.getValidEnd())) {
            throw new BusinessException(ErrorCode.CONFLICT, "观影权益已过期");
        }
        return qrPng(claim.getExchangeCode(), "观影权益二维码生成失败");
    }
 
    private byte[] qrPng(String content, String failureMessage) {
        try {
            var matrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, 480, 480);
            BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            ImageIO.write(image, "PNG", output);
            return output.toByteArray();
        } catch (Exception ex) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, failureMessage);
        }
    }
 
    @Transactional
    public Map<String, Object> quota(BenefitVerificationCodeRequest request) {
        Long merchantId = requirePositive(request.merchantId(), "商户ID不能为空");
        String benefitType = quotaService.normalizeBenefitType(request.benefitType());
        if (BenefitQuotaService.MOVIE_TICKET.equals(benefitType)) {
            ensureCinemaMerchant(merchantId);
            if (ticketPackageService.hasPortalPackages(merchantId)) {
                return ticketPackageService.merchantQuota(request.talentId(), request.cardId(), merchantId);
            }
        } else {
            ensureHotelMerchant(merchantId);
        }
        BenefitQuotaService.QuotaSnapshot quota = quotaService.current(
                benefitType, request.cardId(), request.talentId(), merchantId, null);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("benefitType", benefitType);
        result.put("merchantId", merchantId);
        result.put("available", quota.remaining() > 0);
        result.put("quotaTotal", quota.total());
        result.put("quotaUsed", quota.used());
        result.put("quotaRemaining", quota.remaining());
        result.put("quotaResetCycle", quota.resetCycle());
        result.put("quotaDetail", quota.detail());
        result.put("quotaWindowStart", quota.windowStart());
        result.put("sourceCardId", quota.cardId());
        result.put("sourceTalentId", quota.talentId());
        result.put("sourceCardType", quota.cardType());
        result.put("familyCard", quota.familyCard());
        result.put("primaryCardId", quota.primaryCardId());
        result.put("primaryTalentId", quota.primaryTalentId());
        result.put("primaryCardNo", quota.primaryCardNo());
        result.put("cardLevel", quota.cardLevel());
        return result;
    }
 
    @Transactional
    public Map<String, Object> generate(BenefitVerificationCodeRequest request, String idempotencyKey) {
        String benefitType = quotaService.normalizeBenefitType(request.benefitType());
        if (BenefitQuotaService.MOVIE_TICKET.equals(benefitType)) {
            return generateMovieCode(request, idempotencyKey);
        }
        return generateHotelFreeStayCode(request);
    }
 
    private Map<String, Object> generateMovieCode(BenefitVerificationCodeRequest request, String idempotencyKey) {
        Long merchantId = requirePositive(request.merchantId(), "商户ID不能为空");
        ensureCinemaMerchant(merchantId);
        if (ticketPackageService.hasPortalPackages(merchantId)) {
            return generateMovieTicketPackageCode(request, idempotencyKey, merchantId);
        }
 
        BenefitQuotaService.QuotaSnapshot quota = quotaService.requireAvailable(
                BenefitQuotaService.MOVIE_TICKET, request.cardId(), request.talentId(), merchantId, null, 1);
        MovieCouponChoice choice = movieCouponForUpdate(merchantId, quota.cardLevel(), quota.sharedCardIds());
        BenefitCinemaCoupon coupon = choice.coupon();
        Long couponId = coupon.getId();
        int dailyLimit = choice.dailyLimit();
        int dailyUsed = choice.dailyUsed();
 
        String claimKey = claimIdempotencyKey(idempotencyKey, "MOVIE");
        BenefitCinemaCouponClaim idempotent = movieClaimByIdempotencyKey(claimKey);
        if (idempotent != null) {
            ensureSameMovieClaim(idempotent, couponId, merchantId, quota.cardId());
            if (activeMovieCode(idempotent)) {
                return movieCodeResponse(idempotent, coupon, quota, dailyLimit, dailyUsed, false);
            }
            throw new BusinessException(ErrorCode.CONFLICT, "幂等键对应的电影票核销码已过期或已使用");
        }
 
        BenefitCinemaCouponClaim existing = movieClaimForUpdate(couponId, quota.talentId());
        if (existing != null) {
            String status = upper(existing.getCodeStatus());
            if ("USED".equals(status)) {
                throw new BusinessException(ErrorCode.CONFLICT, "电影票已核销");
            }
            if (activeMovieCode(existing)) {
                return movieCodeResponse(existing, coupon, quota, dailyLimit, dailyUsed, false);
            }
        }
 
        TicketPackageCode poolCode = reserveMoviePoolCode(merchantId, quota.talentId(), quota.cardId());
        Long claimId;
        if (existing != null) {
            decrementMovieStock(couponId, merchantId);
            BenefitCinemaCouponClaim update = new BenefitCinemaCouponClaim();
            update.setId(existing.getId());
            update.setExchangeCode(poolCode.getExchangeCode());
            update.setCodeStatus("ACTIVE");
            update.setStatus("RECEIVED");
            update.setUsedAt(null);
            update.setValidEnd(coupon.getValidEnd());
            update.setIdempotencyKey(claimKey);
            update.setDeleted(0);
            claimMapper.updateById(update);
            claimId = existing.getId();
        } else {
            decrementMovieStock(couponId, merchantId);
            BenefitCinemaCouponClaim claim = new BenefitCinemaCouponClaim();
            claim.setClaimNo(BusinessIdGenerator.next("MC"));
            claim.setCouponId(couponId);
            claim.setMerchantId(merchantId);
            claim.setTalentId(quota.talentId());
            claim.setCardId(quota.cardId());
            claim.setCardNo(quota.cardNo());
            claim.setValidEnd(coupon.getValidEnd());
            claim.setStatus("RECEIVED");
            claim.setExchangeCode(poolCode.getExchangeCode());
            claim.setCodeStatus("ACTIVE");
            claim.setIdempotencyKey(claimKey);
            claim.setDeleted(0);
            claimMapper.insert(claim);
            claimId = claim.getId();
        }
        issueMoviePoolCode(poolCode, claimId);
        BenefitCinemaCouponClaim claim = movieClaimById(claimId);
        return movieCodeResponse(claim, couponMapper.selectById(couponId), quota, dailyLimit, dailyUsed + 1, true);
    }
 
    private Map<String, Object> generateMovieTicketPackageCode(BenefitVerificationCodeRequest request,
                                                               String idempotencyKey,
                                                               Long merchantId) {
        String claimKey = claimIdempotencyKey(idempotencyKey, "MOVIE");
        Map<String, Object> claim = ticketPackageService.claimByMerchant(
                merchantId, request.talentId(), request.cardId(), claimKey);
        Long packageId = longValue(claim.get("packageId"));
        Map<String, Object> quota = ticketPackageService.packageQuota(
                request.talentId(), request.cardId(), merchantId, packageId);
        LocalDateTime expiresAt = ticketPackageExpiresAt(claim.get("validEnd"));
        int quotaTotal = intValue(quota.get("quotaTotal"));
        int quotaUsed = intValue(quota.get("quotaUsed"));
        int quotaRemaining = intValue(quota.get("quotaRemaining"));
        boolean newlyIssued = booleanValue(claim.get("newlyIssued"));
 
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("benefitType", BenefitQuotaService.MOVIE_TICKET);
        result.put("verifyCode", claim.get("exchangeCode"));
        result.put("displayCode", claim.get("exchangeCode"));
        result.put("exchangeCode", claim.get("exchangeCode"));
        result.put("expiresAt", expiresAt);
        result.put("expiresAtEpochMillis", expiresAt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        result.put("expiresInSeconds", Math.max(0, java.time.Duration.between(LocalDateTime.now(), expiresAt).toSeconds()));
        result.put("refreshAfterSeconds", MOVIE_CODE_SECONDS);
        result.put("sharedQuotaMode", "AUTO_DEDUCT");
        result.put("sourceType", "TICKET_PACKAGE_CLAIM");
        result.put("sourceId", claim.get("claimId"));
        result.put("claimId", claim.get("claimId"));
        result.put("merchantId", merchantId);
        result.put("merchantName", quota.get("merchantName"));
        result.put("packageId", claim.get("packageId"));
        result.put("couponId", claim.get("packageId"));
        result.put("couponName", first(claim.get("packageName"), quota.get("packageName")));
        result.put("packageName", first(claim.get("packageName"), quota.get("packageName")));
        result.put("quotaTotal", quotaTotal);
        result.put("quotaUsed", quotaUsed);
        result.put("quotaRemainingBefore", newlyIssued ? quotaRemaining + 1 : quotaRemaining);
        result.put("quotaRemaining", quotaRemaining);
        result.put("quotaResetCycle", quota.get("quotaResetCycle"));
        result.put("quotaWindowStart", quota.get("quotaWindowStart"));
        result.put("sourceCardId", quota.get("sourceCardId"));
        result.put("sourceTalentId", quota.get("sourceTalentId"));
        result.put("sourceCardType", quota.get("sourceCardType"));
        result.put("familyCard", quota.get("familyCard"));
        result.put("primaryCardId", quota.get("primaryCardId"));
        result.put("primaryTalentId", quota.get("primaryTalentId"));
        result.put("primaryCardNo", quota.get("primaryCardNo"));
        result.put("cardLevel", quota.get("cardLevel"));
        result.put("dailyLimit", quotaTotal);
        result.put("dailyUsed", quotaUsed);
        result.put("dailyRemaining", quotaRemaining);
        result.put("quotaEffect", newlyIssued ? "RESERVED_ON_CODE_GENERATION" : "EXISTING_ACTIVE_CODE");
        return result;
    }
 
    private Map<String, Object> generateHotelFreeStayCode(BenefitVerificationCodeRequest request) {
        Long merchantId = requirePositive(request.merchantId(), "商户ID不能为空");
        ensureHotelMerchant(merchantId);
        BenefitQuotaService.QuotaSnapshot quota = quotaService.requireAvailable(
                BenefitQuotaService.HOTEL, request.cardId(), request.talentId(), merchantId, null, 1);
        BenefitFreeStayVoucher voucher = voucherForUpdate(quota, merchantId);
        if (intValue(voucher.getStatus()) != 1 || expiredDate(voucher.getValidEnd())) {
            throw new BusinessException(ErrorCode.CONFLICT, "酒店权益券当前不可用");
        }
        if (intValue(voucher.getUsedCount()) >= Math.min(intValue(voucher.getTotalCount()), quota.total())) {
            throw new BusinessException(ErrorCode.CONFLICT, "酒店权益次数已用完");
        }
        freeStayCodeMapper.update(null, new UpdateWrapper<BenefitFreeStayVerificationCode>()
                .set("status", 2)
                .eq("voucher_id", voucher.getId())
                .eq("status", 0));
 
        BenefitFreeStayVerificationCode code = new BenefitFreeStayVerificationCode();
        code.setCodeToken(hotelFreeStayToken(quota.cardId()));
        code.setVoucherId(voucher.getId());
        code.setExpiresAt(LocalDateTime.now().plusSeconds(HOTEL_CODE_SECONDS));
        code.setStatus(0);
        freeStayCodeMapper.insert(code);
        return hotelCodeResponse(code, voucher, quota);
    }
 
    private MovieCouponChoice movieCouponForUpdate(Long merchantId, String cardLevel, List<Long> sharedCardIds) {
        ensureCinemaMerchant(merchantId);
        List<BenefitCinemaCoupon> coupons = couponMapper.selectList(new QueryWrapper<BenefitCinemaCoupon>()
                .eq("merchant_id", merchantId)
                .eq("deleted", 0)
                .eq("status", 1)
                .gt("stock", 0)
                .and(wrapper -> wrapper.isNull("valid_end").or().ge("valid_end", LocalDate.now()))
                .orderByAsc("sort_no", "id")
                .last("for update"));
        if (coupons.isEmpty()) {
            throw new BusinessException(ErrorCode.CONFLICT, "未找到可用电影票权益券");
        }
        List<String> aliases = quotaService.cardLevelAliases(cardLevel);
        boolean cardLevelSupported = false;
        for (BenefitCinemaCoupon coupon : coupons) {
            if (!supportsCard(coupon.getTalentLevels(), aliases)) {
                continue;
            }
            cardLevelSupported = true;
            int dailyLimit = Math.max(1, intValue(coupon.getPerUserLimit()));
            int dailyUsed = dailyMovieClaims(sharedCardIds, coupon.getId());
            if (dailyUsed < dailyLimit) {
                return new MovieCouponChoice(coupon, dailyLimit, dailyUsed);
            }
        }
        if (!cardLevelSupported) {
            throw new BusinessException(ErrorCode.CONFLICT, "当前卡类型不适用该电影票权益券");
        }
        throw new BusinessException(ErrorCode.CONFLICT, "当天电影票领取次数已达上限");
    }
 
    private TicketPackageCode reserveMoviePoolCode(Long merchantId, Long talentId, Long cardId) {
        TicketPackageCode code = codeMapper.selectAvailableByMerchantForUpdate(merchantId);
        if (code == null) {
            throw new BusinessException(ErrorCode.CONFLICT, "电影票兑换码码池库存不足,请联系后台导入兑换码");
        }
        int locked = codeMapper.lockForIssue(code.getId(), talentId, cardId);
        if (locked != 1) {
            throw new BusinessException(ErrorCode.CONFLICT, "电影票兑换码已被领取,请重试");
        }
        return code;
    }
 
    private void issueMoviePoolCode(TicketPackageCode code, Long claimId) {
        codeMapper.update(null, new UpdateWrapper<TicketPackageCode>()
                .set("status", "ISSUED")
                .set("issued_claim_id", claimId)
                .set("issued_at", LocalDateTime.now())
                .eq("id", code.getId())
                .eq("status", "LOCKED")
                .eq("deleted", 0));
    }
    private void decrementMovieStock(Long couponId, Long merchantId) {
        int updated = couponMapper.update(null, new UpdateWrapper<BenefitCinemaCoupon>()
                .setSql("stock = stock - 1")
                .eq("id", couponId)
                .eq("merchant_id", merchantId)
                .eq("deleted", 0)
                .eq("status", 1)
                .gt("stock", 0));
        if (updated != 1) {
            throw new BusinessException(ErrorCode.CONFLICT, "电影票权益券库存不足");
        }
    }
 
    private int dailyMovieClaims(List<Long> sharedCardIds, Long couponId) {
        Long count = claimMapper.selectCount(new QueryWrapper<BenefitCinemaCouponClaim>()
                .eq("deleted", 0)
                .in("card_id", sharedCardIds)
                .eq("coupon_id", couponId)
                .apply("((upper(coalesce(code_status, 'ACTIVE')) = 'USED' and used_at >= current_date) or "
                        + "(upper(coalesce(code_status, 'ACTIVE')) = 'ACTIVE' and updated_at >= current_date))"));
        return intValue(count);
    }
    private BenefitCinemaCouponClaim movieClaimByIdempotencyKey(String key) {
        return claimMapper.selectOne(new QueryWrapper<BenefitCinemaCouponClaim>()
                .eq("deleted", 0)
                .eq("idempotency_key", key)
                .last("limit 1 for update"));
    }
 
    private BenefitCinemaCouponClaim movieClaimForUpdate(Long couponId, Long talentId) {
        return claimMapper.selectOne(new QueryWrapper<BenefitCinemaCouponClaim>().eq("coupon_id", couponId)
                .eq("talent_id", talentId)
                .last("limit 1 for update"));
    }
 
    private BenefitCinemaCouponClaim movieClaimById(Long claimId) {
        BenefitCinemaCouponClaim claim = claimMapper.selectOne(new QueryWrapper<BenefitCinemaCouponClaim>()
                .eq("deleted", 0)
                .eq("id", claimId)
                .last("limit 1"));
        if (claim == null) {
            throw new BusinessException(ErrorCode.INTERNAL_ERROR, "电影票领取记录保存失败");
        }
        return claim;
    }
 
    private void ensureSameMovieClaim(BenefitCinemaCouponClaim claim, Long couponId, Long merchantId, Long cardId) {
        if (!couponId.equals(claim.getCouponId()) || !merchantId.equals(claim.getMerchantId()) || !cardId.equals(claim.getCardId())) {
            throw new BusinessException(ErrorCode.CONFLICT, "幂等键已被其他电影票核销码请求使用");
        }
    }
 
    private void ensureCinemaMerchant(Long merchantId) {
        BenefitMerchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null || intValue(merchant.getDeleted()) != 0 || intValue(merchant.getStatus()) != 1
                || !isCinemaMerchantType(merchant.getMerchantType())) {
            throw new BusinessException(ErrorCode.CONFLICT, "未找到有效影院商户");
        }
    }
 
    private boolean isCinemaMerchantType(String merchantType) {
        String type = merchantType == null ? "" : merchantType.trim();
        return "cinema".equalsIgnoreCase(type) || "theatre".equalsIgnoreCase(type)
                || "观影服务".equals(type) || "电影院".equals(type) || "影院".equals(type) || "影城".equals(type) || "剧院".equals(type);
    }
 
    private void ensureHotelMerchant(Long merchantId) {
        BenefitMerchant merchant = merchantMapper.selectById(merchantId);
        if (merchant == null || intValue(merchant.getDeleted()) != 0 || intValue(merchant.getStatus()) != 1
                || !isHotelMerchantType(merchant.getMerchantType())) {
            throw new BusinessException(ErrorCode.CONFLICT, "未找到有效酒店商户");
        }
    }
 
    private boolean isHotelMerchantType(String merchantType) {
        String type = merchantType == null ? "" : merchantType.trim();
        return "hotel".equalsIgnoreCase(type) || "酒店服务".equals(type);
    }
 
    private BenefitFreeStayVoucher voucherForUpdate(BenefitQuotaService.QuotaSnapshot quota, Long merchantId) {
        BenefitFreeStayVoucher voucher = voucherMapper.selectOne(new QueryWrapper<BenefitFreeStayVoucher>()
                .eq("talent_id", quota.primaryTalentId())
                .eq("merchant_id", merchantId)
                .last("limit 1 for update"));
        LocalDate validEnd = LocalDate.now().plusYears(1);
        if (voucher == null) {
            voucher = new BenefitFreeStayVoucher();
            voucher.setVoucherNo(BusinessIdGenerator.next("FSV"));
            voucher.setTalentId(quota.primaryTalentId());
            voucher.setMerchantId(merchantId);
            voucher.setCardId(quota.primaryCardId());
            voucher.setTotalCount(quota.total());
            voucher.setUsedCount(quota.used());
            voucher.setValidEnd(validEnd);
            voucher.setStatus(1);
            voucherMapper.insert(voucher);
            return voucher;
        }
        voucher.setCardId(quota.primaryCardId());
        voucher.setTotalCount(quota.total());
        voucher.setUsedCount(quota.used());
        voucher.setValidEnd(validEnd);
        voucher.setStatus(1);
        voucherMapper.updateById(voucher);
        return voucher;
    }
    private Map<String, Object> movieCodeResponse(BenefitCinemaCouponClaim claim, BenefitCinemaCoupon coupon,
                                                  BenefitQuotaService.QuotaSnapshot quota,
                                                  int dailyLimit, int dailyUsed, boolean newlyReserved) {
        LocalDateTime expiresAt = coupon != null && coupon.getValidEnd() != null
                ? coupon.getValidEnd().plusDays(1).atStartOfDay()
                : LocalDateTime.now().plusYears(1);
        BenefitMerchant merchant = merchantMapper.selectById(claim.getMerchantId());
        Map<String, Object> result = baseResponse(BenefitQuotaService.MOVIE_TICKET, claim.getExchangeCode(), expiresAt, quota);
        result.put("displayCode", claim.getExchangeCode());
        result.put("sourceType", "CINEMA_COUPON_CLAIM");
        result.put("sourceId", claim.getId());
        result.put("merchantId", claim.getMerchantId());
        result.put("merchantName", merchant == null ? "" : merchant.getMerchantName());
        result.put("couponId", claim.getCouponId());
        result.put("couponName", coupon == null ? "" : coupon.getCouponName());
        result.put("stockRemaining", coupon == null ? null : coupon.getStock());
        result.put("dailyLimit", dailyLimit);
        result.put("dailyUsed", dailyUsed);
        result.put("dailyRemaining", Math.max(dailyLimit - dailyUsed, 0));
        result.put("quotaEffect", newlyReserved ? "RESERVED_ON_CODE_GENERATION" : "EXISTING_ACTIVE_CODE");
        return result;
    }
 
    private Map<String, Object> hotelCodeResponse(BenefitFreeStayVerificationCode code, BenefitFreeStayVoucher voucher,
                                                  BenefitQuotaService.QuotaSnapshot quota) {
        Map<String, Object> result = baseResponse(BenefitQuotaService.HOTEL, code.getCodeToken(), code.getExpiresAt(), quota);
        result.put("displayCode", shortDisplayCode(code.getCodeToken()));
        result.put("sourceType", "FREE_STAY_VERIFICATION_CODE");
        result.put("sourceId", code.getId());
        result.put("merchantId", voucher.getMerchantId());
        result.put("voucherId", voucher.getId());
        result.put("voucherNo", voucher.getVoucherNo());
        result.put("voucherTotal", voucher.getTotalCount());
        result.put("voucherUsed", voucher.getUsedCount());
        result.put("quotaEffect", "DEDUCT_ON_VERIFICATION");
        return result;
    }
 
    private Map<String, Object> baseResponse(String benefitType, Object codeToken, LocalDateTime expiresAt,
                                             BenefitQuotaService.QuotaSnapshot quota) {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("benefitType", benefitType);
        result.put("verifyCode", codeToken);
        result.put("expiresAt", expiresAt);
        result.put("expiresAtEpochMillis", expiresAt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
        result.put("expiresInSeconds", Math.max(0, java.time.Duration.between(LocalDateTime.now(), expiresAt).toSeconds()));
        result.put("refreshAfterSeconds", BenefitQuotaService.HOTEL.equals(benefitType)
                ? HOTEL_CODE_SECONDS : 0);
        result.put("sharedQuotaMode", "AUTO_DEDUCT");
        result.put("sourceCardId", quota.cardId());
        result.put("sourceTalentId", quota.talentId());
        result.put("sourceCardType", quota.cardType());
        result.put("familyCard", quota.familyCard());
        result.put("primaryCardId", quota.primaryCardId());
        result.put("primaryTalentId", quota.primaryTalentId());
        result.put("primaryCardNo", quota.primaryCardNo());
        result.put("cardLevel", quota.cardLevel());
        result.put("quotaTotal", quota.total());
        result.put("quotaUsed", quota.used());
        result.put("quotaRemainingBefore", quota.remaining());
        result.put("quotaResetCycle", quota.resetCycle());
        result.put("quotaDetail", quota.detail());
        result.put("quotaWindowStart", quota.windowStart());
        return result;
    }
 
    private String hotelFreeStayToken(Long sourceCardId) {
        long card = sourceCardId == null ? 0 : sourceCardId;
        return HOTEL_TOKEN_PREFIX + "C" + card + "P0_" + UUID.randomUUID().toString().replace("-", "").toUpperCase(Locale.ROOT);
    }
 
    private boolean activeMovieCode(BenefitCinemaCouponClaim claim) {
        return "ACTIVE".equals(upper(claim.getCodeStatus())) && !expiredDate(claim.getValidEnd());
    }
 
    private boolean startsWithMoviePrefix(String code) {
        return code != null && code.startsWith(MOVIE_TOKEN_PREFIX);
    }
 
    private LocalDateTime ticketPackageExpiresAt(Object validEnd) {
        if (validEnd instanceof LocalDate date) {
            return date.plusDays(1).atStartOfDay();
        }
        if (validEnd instanceof java.sql.Date date) {
            return date.toLocalDate().plusDays(1).atStartOfDay();
        }
        String raw = validEnd == null ? "" : String.valueOf(validEnd).trim();
        if (raw.length() >= 10) {
            try {
                return LocalDate.parse(raw.substring(0, 10)).plusDays(1).atStartOfDay();
            } catch (RuntimeException ignored) {
                return LocalDateTime.now().plusYears(1);
            }
        }
        return LocalDateTime.now().plusYears(1);
    }
 
    private String claimIdempotencyKey(String raw, String token) {
        if (raw == null || raw.isBlank()) {
            return "VCODE-" + token;
        }
        String key = raw.trim();
        if (key.length() > 100) {
            throw new BusinessException(ErrorCode.BAD_REQUEST, "幂等键长度不能超过限制");
        }
        return "VCODE-" + key;
    }
 
    private boolean supportsCard(String levels, List<String> aliases) {
        String configured = levels == null ? "" : levels.trim();
        if (configured.isBlank()) {
            return true;
        }
        for (String alias : aliases) {
            if (alias != null && !alias.isBlank() && configured.contains(alias)) {
                return true;
            }
        }
        return false;
    }
 
    private String shortDisplayCode(String token) {
        if (token == null || token.length() <= 8) {
            return token;
        }
        return token.substring(token.length() - 8);
    }
 
    private Long requirePositive(Long value, String message) {
        if (value == null || value <= 0) {
            throw new BusinessException(ErrorCode.BAD_REQUEST, message);
        }
        return value;
    }
 
    private boolean expiredDate(LocalDate value) {
        return value != null && value.isBefore(LocalDate.now());
    }
 
    private String upper(String value) {
        return value == null ? "" : value.trim().toUpperCase(Locale.ROOT);
    }
 
    private int intValue(Object value) {
        return value instanceof Number number ? number.intValue() : 0;
    }
 
    private Long longValue(Object value) {
        return value instanceof Number number ? number.longValue() : null;
    }
 
    private Object first(Object... values) {
        for (Object value : values) {
            if (value != null && !String.valueOf(value).isBlank()) {
                return value;
            }
        }
        return "";
    }
 
    private boolean booleanValue(Object value) {
        if (value instanceof Boolean bool) {
            return bool;
        }
        if (value instanceof Number number) {
            return number.intValue() != 0;
        }
        String raw = value == null ? "" : String.valueOf(value).trim();
        return "true".equalsIgnoreCase(raw) || "1".equals(raw);
    }
 
    private record MovieCouponChoice(BenefitCinemaCoupon coupon, int dailyLimit, int dailyUsed) {
    }
}