shinnosuke
2026-08-11 7aa1d7b9f9ad0b95b24d0c18fe04d326630d7dc2
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
package com.talentcard.server.mapper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.talentcard.server.entity.MerchantCouponManagement;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
 
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
 
@Mapper
public interface MerchantCouponManagementMapper extends BaseMapper<MerchantCouponManagement> {
    @Select("""
            <script>
            select c.*, m.merchant_name as merchantName,
                   (select count(*) from merchant_coupon_claim cc where cc.coupon_id = c.id and cc.deleted = 0) as claimRecordCount,
                   (select count(*) from merchant_coupon_claim cc where cc.coupon_id = c.id and cc.deleted = 0 and cc.claim_status = 1 and cc.use_status = 0) as unusedCount,
                   (select count(*) from merchant_coupon_claim cc where cc.coupon_id = c.id and cc.deleted = 0 and cc.use_status = 1) as usedCount,
                   (select count(*) from merchant_coupon_claim cc where cc.coupon_id = c.id and cc.deleted = 0 and cc.use_status = 2) as expiredCount,
                   (select count(*) from merchant_coupon_claim cc where cc.coupon_id = c.id and cc.deleted = 0 and cc.use_status = 3) as invalidCount
            from merchant_coupon c
            join merchant m on m.id = c.merchant_id and m.deleted = 0
            where c.deleted = 0
              <if test="merchantId != null">and c.merchant_id = #{merchantId}</if>
              <if test="couponType != null and couponType != ''">and c.coupon_type = #{couponType}</if>
              <if test="batchNo != null and batchNo != ''">and c.batch_no = #{batchNo}</if>
              <if test="status != null">and c.status = #{status}</if>
              <if test="keyword != null and keyword != ''">and (c.coupon_name like concat('%', #{keyword}, '%') or m.merchant_name like concat('%', #{keyword}, '%'))</if>
              <if test="validOn != null">and (c.valid_start is null or c.valid_start &lt;= #{validOn}) and (c.valid_end is null or c.valid_end &gt;= #{validOn})</if>
            order by c.created_at desc, c.id desc
            limit #{size} offset #{offset}
            </script>
            """)
    List<Map<String, Object>> selectManagementPage(@Param("merchantId") Long merchantId,
                                                    @Param("couponType") String couponType,
                                                    @Param("batchNo") String batchNo,
                                                    @Param("status") Integer status,
                                                    @Param("keyword") String keyword,
                                                    @Param("validOn") LocalDate validOn,
                                                    @Param("size") int size,
                                                    @Param("offset") int offset);
 
    @Select("""
            <script>
            select count(*) from merchant_coupon c
            join merchant m on m.id = c.merchant_id and m.deleted = 0
            where c.deleted = 0
              <if test="merchantId != null">and c.merchant_id = #{merchantId}</if>
              <if test="couponType != null and couponType != ''">and c.coupon_type = #{couponType}</if>
              <if test="batchNo != null and batchNo != ''">and c.batch_no = #{batchNo}</if>
              <if test="status != null">and c.status = #{status}</if>
              <if test="keyword != null and keyword != ''">and (c.coupon_name like concat('%', #{keyword}, '%') or m.merchant_name like concat('%', #{keyword}, '%'))</if>
              <if test="validOn != null">and (c.valid_start is null or c.valid_start &lt;= #{validOn}) and (c.valid_end is null or c.valid_end &gt;= #{validOn})</if>
            </script>
            """)
    Long countManagementPage(@Param("merchantId") Long merchantId,
                             @Param("couponType") String couponType,
                             @Param("batchNo") String batchNo,
                             @Param("status") Integer status,
                             @Param("keyword") String keyword,
                             @Param("validOn") LocalDate validOn);
 
    @Select("select count(*) from merchant where id = #{merchantId} and deleted = 0")
    Long countMerchant(@Param("merchantId") Long merchantId);
 
    @Update("""
            update merchant_coupon
            set claimed_quantity = claimed_quantity + 1
            where id = #{couponId} and deleted = 0 and status = 1
              and (total_quantity = -1 or claimed_quantity < total_quantity)
            """)
    int reserveOne(@Param("couponId") Long couponId);
 
    @Update("update merchant_coupon set claimed_quantity = greatest(claimed_quantity - 1, 0) where id = #{couponId} and deleted = 0")
    int releaseOne(@Param("couponId") Long couponId);
 
    @Select("select count(*) from talent_user where id = #{talentId} and deleted = 0")
    Long countTalent(@Param("talentId") Long talentId);
 
    @Select("select count(*) from talent_card where id = #{cardId} and talent_id = #{talentId} and deleted = 0")
    Long countTalentCard(@Param("cardId") Long cardId, @Param("talentId") Long talentId);}