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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.starry.admin.modules.shop.module.entity.PlayClerkGiftInfoEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * 店员和礼物关系Mapper接口
 *
 * @author admin
 * @since 2024-05-25
 */
public interface PlayClerkGiftInfoMapper extends BaseMapper<PlayClerkGiftInfoEntity> {

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

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

}
