package com.gxwebsoft.shop.mapper;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.shop.entity.ShopSeckill;
import com.gxwebsoft.shop.param.ShopSeckillParam;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 秒杀活动Mapper
 *
 * @author 科技小王子
 * @since 2026-06-25
 */
public interface ShopSeckillMapper extends BaseMapper<ShopSeckill> {

    /**
     * 分页关联查询（含商品信息）
     */
    List<ShopSeckill> selectPageRel(@Param("page") IPage<ShopSeckill> page,
                                     @Param("param") ShopSeckillParam param);

    /**
     * 关联查询全部
     */
    List<ShopSeckill> selectListRel(@Param("param") ShopSeckillParam param);

    /**
     * 原子扣减秒杀库存（乐观锁）
     * 忽略租户隔离，确保扣减成功
     */
    @InterceptorIgnore(tenantLine = "true")
    @Update("UPDATE shop_seckill SET seckill_stock = seckill_stock - #{quantity}, sold_count = IFNULL(sold_count, 0) + #{quantity} WHERE seckill_id = #{seckillId} AND seckill_stock >= #{quantity}")
    int deductSeckillStock(@Param("seckillId") Integer seckillId, @Param("quantity") Integer quantity);

}
