package com.izouma.dingdong.repo.user;

import com.izouma.dingdong.domain.user.ShoppingCart;

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 ShoppingCartRepo extends JpaRepository<ShoppingCart, Long>, JpaSpecificationExecutor<ShoppingCart> {

    //按用户和商户查找
    ShoppingCart findByUserIdAndMerchantId(Long userId, Long merchantId);

    List<ShoppingCart> findAllByUserId(Long userId);

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

    @Query("update ShoppingCart t set t.enabled = false where t.userId = ?1")
    @Modifying
    @Transactional
    void deleteAllByUserId(Long userId);

}