package com.starry.admin.modules.custom.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.starry.admin.modules.custom.module.entity.PlayCustomGiftInfoEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 顾客和礼物关系Mapper接口
 *
 * @author admin
 * @since 2024-06-05
 */
public interface PlayCustomGiftInfoMapper extends MPJBaseMapper<PlayCustomGiftInfoEntity> {

    /**
     * 原子递增顾客礼物数量
     *
     * @param customId 顾客ID
     * @param giftId   礼物ID
     * @param tenantId 租户ID
     * @param delta    增量
     * @return 受影响行数
     */
    @Update("UPDATE play_custom_gift_info "
            + "SET giff_number = giff_number + #{delta} "
            + "WHERE custom_id = #{customId} AND giff_id = #{giftId} "
            + "AND (tenant_id = #{tenantId} OR tenant_id IS NULL)")
    int incrementGiftCount(@Param("customId") String customId, @Param("giftId") String giftId,
                           @Param("tenantId") String tenantId, @Param("delta") long delta);

    @Update("UPDATE play_custom_gift_info SET giff_number = 0, deleted = 0 "
            + "WHERE tenant_id = #{tenantId} AND custom_id = #{customId} AND giff_id = #{giftId}")
    int resetGiftCount(@Param("tenantId") String tenantId, @Param("customId") String customId,
                       @Param("giftId") String giftId);

}
