package com.gxwebsoft.shop.mapper;

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

import java.util.List;

/**
 * 商品Mapper
 *
 * @author 科技小王子
 * @since 2025-04-24 20:52:13
 */
public interface ShopGoodsMapper extends BaseMapper<ShopGoods> {

    /**
     * 分页查询
     *
     * @param page  分页对象
     * @param param 查询参数
     * @return List<ShopGoods>
     */
    List<ShopGoods> selectPageRel(@Param("page") IPage<ShopGoods> page,
                             @Param("param") ShopGoodsParam param);

    /**
     * 查询全部
     *
     * @param param 查询参数
     * @return List<User>
     */
    List<ShopGoods> selectListRel(@Param("param") ShopGoodsParam param);

    /**
     * 累加商品销售数量
     * 使用@InterceptorIgnore忽略租户隔离，确保能更新成功
     *
     * @param goodsId 商品ID
     * @param saleCount 累加的销售数量
     * @return 影响的行数
     */
    @InterceptorIgnore(tenantLine = "true")
    @Update("UPDATE shop_goods SET sales = IFNULL(sales, 0) + #{saleCount} WHERE goods_id = #{goodsId}")
    int addSaleCount(@Param("goodsId") Integer goodsId, @Param("saleCount") Integer saleCount);

}
