package com.snack.server.module.seckill.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.snack.server.module.seckill.entity.SeckillProduct;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface SeckillProductMapper extends BaseMapper<SeckillProduct> {

    /**
     * 扣减 MySQL 兜底库存（异步消费 MQ 时调用）
     */
    @Update("UPDATE seckill_product SET remain_stock = remain_stock - #{quantity}, sales = sales + #{quantity} " +
            "WHERE id = #{id} AND remain_stock >= #{quantity}")
    int decrRemainStock(@Param("id") Long id, @Param("quantity") Integer quantity);

    /**
     * 释放 MySQL 兜底库存（订单取消 / 退款时调用）
     */
    @Update("UPDATE seckill_product SET remain_stock = remain_stock + #{quantity}, sales = sales - #{quantity} " +
            "WHERE id = #{id}")
    int incrRemainStock(@Param("id") Long id, @Param("quantity") Integer quantity);
}
