package com.izouma.dingdong.repo;

import com.izouma.dingdong.domain.OrderGoodsSpec;
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 OrderGoodsSpecRepo extends JpaRepository<OrderGoodsSpec, Long>, JpaSpecificationExecutor<OrderGoodsSpec> {

    List<OrderGoodsSpec> findAllByOrderInfoId(Long orderId);

    List<OrderGoodsSpec> findAllByGoodsId(Long goodsId);

    List<OrderGoodsSpec> findAllByShoppingCartId(Long shoppingCardId);

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

    @Query("update OrderGoodsSpec t set t.enabled = false where t.shoppingCartId = ?1 and t.orderInfoId is null")
    @Modifying
    @Transactional
    void deleteAllByShoppingCartIdAndOrderInfoIdIsNull(Long shoppingCartId);

}