package cn.ysk.cashier.mybatis.mapper;

import cn.ysk.cashier.pojo.product.TbProduct;
import cn.ysk.cashier.pojo.product.TbProductSku;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.jpa.repository.Query;

public interface TbProductMapper extends BaseMapper<TbProduct> {

    @Select("select * from tb_product_sku as sku where sku.is_del=0 and sku.is_grounding=1 and sku.is_pause_sale=0 and sku.shop_id=#{shopId} and sku.id=#{skuId}")
    TbProductSku selectSkuByIdAndShopId(@Param("shopId") Integer shopId, @Param("skuId") Integer skuId);

    @Select("select * from tb_product product where product.id=#{id} and product.shop_id=#{shopId} and product.is_del=0")
    TbProduct selectByIdAndShopId(@Param("shopId") Integer shopId, @Param("id") Integer id);

    @Update("update tb_product set stock_number=stock_number+#{addNum} WHERE id=#{id}")
    int incrStock(@Param("id") Integer id, @Param("addNum") Integer addNum);

    @Update("update tb_product set stock_number=stock_number-#{decrNum} WHERE id=#{productId} and stock_number-#{decrNum} >= 0")
    int decrStock(@Param("productId") Integer productId, @Param("decrNum") int decrNum);

    @Update("update tb_product set stock_number=stock_number-#{num} WHERE id=#{id}")
    int decrStockUnCheck(Integer id, int num);
}
