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

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

@Mapper
public interface CartMapper extends BaseMapper<Cart> {

    /**
     * 数量累加（已存在记录时调用）
     */
    @Update("UPDATE cart SET quantity = quantity + #{delta}, selected = 1 " +
            "WHERE user_id = #{userId} AND sku_id = #{skuId}")
    int incrQuantity(@Param("userId") Long userId,
                     @Param("skuId") Long skuId,
                     @Param("delta") Integer delta);

    /**
     * 设置数量（用户手动修改购物车数量）
     */
    @Update("UPDATE cart SET quantity = #{quantity} " +
            "WHERE user_id = #{userId} AND sku_id = #{skuId}")
    int updateQuantity(@Param("userId") Long userId,
                       @Param("skuId") Long skuId,
                       @Param("quantity") Integer quantity);
}
