package cn.ysk.cashier.repository.product;

import cn.ysk.cashier.pojo.product.TbProduct;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2023-12-11
**/
public interface TbProductRepository extends JpaRepository<TbProduct, Integer>, JpaSpecificationExecutor<TbProduct> {


    @Query("SELECT product from TbProduct product where product.id in :productIds order by product.sort")
    List<TbProduct> findByIds(List<Integer> productIds);

    @Query(value = "update tb_product set status = -1 where id in :productIds",nativeQuery = true)
    @Modifying
    void updateByStatus(List<Integer> productIds);
    @Query(value = "update tb_product set is_hot=:isHot where id = :id",nativeQuery = true)
    @Modifying
    void updateIsHot(@Param("id") Integer id,@Param("isHot")Integer isHot);

    @Query(value = "update tb_product set is_stock=:isStock where id = :proId and shop_id=:shopId",nativeQuery = true)
    @Modifying
    void updateIsStock(@Param("proId") Integer proId,@Param("shopId") String shopId,@Param("isStock")Integer isStock);


    @Query(value = "update tb_product set is_hot=0 where shop_id = :shopId and is_hot = 1",nativeQuery = true)
    @Modifying
    void updateNullHot(@Param("shopId")String shopId);

    @Modifying
    @Query("update FROM TbProduct pro set pro.stockNumber=:stockNumber WHERE pro.id =:productId")
    void updateProductStockNumber(@Param("productId") Integer productId,@Param("stockNumber") Integer stockNumber);

    @Modifying
    @Query("update FROM TbProduct pro set pro.stockNumber=pro.stockNumber+:number WHERE pro.id =:productId")
    void incrProductStockNumber(Integer productId, Integer number);

    @Transactional
    @Modifying
    @Query("update FROM TbProduct pro set pro.stockNumber=:stocktakinNum where pro.id=:id and pro.stockNumber=:stockNumber")
    Integer updateStock(@Param("id") Integer id,@Param("stockNumber") Integer stockNumber,
                     @Param("stocktakinNum") Integer stocktakinNum);

    @Modifying
    @Query("update TbProduct set stockNumber=stockNumber+:num where id=:id")
    void incrStock(@Param("id") Integer id, @Param("num") Integer num);

    @Query("select product from TbProduct product where product.id=:id and product.shopId=:shopId and product.isDel=0")
    TbProduct selectByShopIdAndId(Integer id, String shopId);

    @Query("select product from TbProduct product where product.shopId=:shopId")
    List<TbProduct> selectByShopId(String shopId);

    @Query(value = "select b.* from tb_product_sku as a left join tb_product as b on a.product_id=b.id  where a.id=:skuId", nativeQuery = true)
    TbProduct selectBySkuId(@Param("skuId") Integer skuId);

    @Query(value = "select * from tb_product as a where a.id=:skuId", nativeQuery = true)
    TbProduct selectById(@Param("skuId") Integer skuId);

}
