package cn.ysk.cashier.repository.product;

import cn.ysk.cashier.pojo.product.TbProductSku;
import org.apache.ibatis.annotations.Param;
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.math.BigDecimal;
import java.util.List;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2024-01-03
**/
public interface TbProductSkuRepository extends JpaRepository<TbProductSku, Integer>, JpaSpecificationExecutor<TbProductSku> {


    @Query("SELECT sku FROM TbProductSku sku WHERE sku.productId = :productId and sku.isDel=0")
    List<TbProductSku> searchSku(@Param("productId")String productId);


    @Transactional
    @Modifying
    @Query("update FROM TbProductSku sku set sku.isDel=1 WHERE sku.id = :skuId")
    Integer deleteBySkuId(@Param("skuId") Integer skuId);


    @Query(value = "SELECT min(sale_price) from tb_product_sku where product_id = ?1 and id != ?2", nativeQuery = true)
    BigDecimal searchMinSalePrice(@Param("productId") Integer productId, @Param("skuId") Integer skuId);


    @Modifying
    @Query("update FROM TbProductSku sku set sku.costPrice=:cost WHERE sku.productId=:productId")
    Integer updateCostByProductId(String productId, BigDecimal cost);



    @Transactional
    @Modifying
    @Query("update TbProductSku set isGrounding=:isGrounding where productId=:productId")
    void updateGroundingByProId(@Param("productId") String productId, @Param("isGrounding") Integer isGrounding);

    @Modifying
    @Query("update TbProductSku set salePrice = :salePrice where productId= :productId")
    void upSalePrice(@Param("productId") String productId, @Param("lowPrice") BigDecimal salePrice);
}
