package com.izouma.dingdong.repo;

import com.izouma.dingdong.domain.OrderInfo;
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;

public interface OrderInfoRepo extends JpaRepository<OrderInfo, Long>, JpaSpecificationExecutor<OrderInfo> {

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

    //按商家查找
    List<OrderInfo> findAllByMerchantId(Long merchantId);

    //按用户查找
    List<OrderInfo> findAllByUserId(Long userId);

    //按骑手查找
    List<OrderInfo> findAllByRiderId(Long riderId);

    //按商家和用户查找订单，判断是不是第一次购买
    List<OrderInfo> findAllByUserIdAndMerchantId(Long userId,Long merchantId);

    @Query("select sum (t.realAmount) from OrderInfo t where t.userId = ?1")
    BigDecimal sumRealAmountByUserId(Long userId);

    @Query("update OrderInfo t set t.enabled = false where t.merchantId = ?1")
    @Modifying
    @Transactional
    void deleteAllByMerchantId(Long merchantId);
}