package com.izouma.dingdong.repo.merchant;

import com.izouma.dingdong.domain.merchant.Goods;
import com.izouma.dingdong.enums.ApplyStatus;
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;

public interface GoodsRepo extends JpaRepository<Goods, Long>, JpaSpecificationExecutor<Goods> {

    @Query("update Goods t set t.enabled = false where t.id = ?1")
    @Modifying
    @Transactional
    void deleteById(Long id);

    List<Goods> findAllByMerchantId(Long merchantId);

    //按商户筛选，并用过审核
//    List<Goods> findAllByMerchantIdAndIsPassTrue(Long merchantId);

    //按商户筛选，并用过审核
    List<Goods> findAllByMerchantIdAndStatus(Long merchantId, ApplyStatus status);

//    List<Goods> findAllByMerchantIdAndTotalSalesIsLessThanEqual(Long merchant,Integer num);


    @Query("update Goods t set t.likes = t.likes + 1 where t.id = ?1")
    @Modifying
    @Transactional
    void updateLikesById(Long id);

    @Query("update Goods t set t.bad = t.bad + 1 where t.id = ?1")
    @Modifying
    @Transactional
    void updateBadById(Long id);

    @Query(nativeQuery = true, value = "SELECT ifnull(max(sort + 1),1) FROM menu")
    int nextSort();

    //按商户id删除所有商品
    @Query("update Goods t set t.enabled = false where t.merchantId = ?1")
    @Modifying
    @Transactional
    void deleteAllByMerchantId(Long merchantId);
}