package com.izouma.nineth.repo;

import com.izouma.nineth.domain.GiftOrder;
import com.izouma.nineth.enums.OrderStatus;
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.time.LocalDateTime;
import java.util.List;

public interface GiftOrderRepo extends JpaRepository<GiftOrder, Long>, JpaSpecificationExecutor<GiftOrder> {
    /**
     * 查询待取消的转赠订单
     * @param status
     * @param time
     * @return
     */
    @Query("select gif.id from GiftOrder gif where gif.status = ?1 and gif.createdAt < ?2 and gif.del = false")
    List<Long> findWaitingCancelList(OrderStatus status, LocalDateTime time);

    @Query("update GiftOrder t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    GiftOrder findByTransactionId(String txId);

    List<GiftOrder> findByAssetIdAndStatusIn(Long assetId, Iterable<OrderStatus> statuses);


    /**
     * 更新实际支付的订单编号
     * @param orderId
     * @param orderNo
     */
    @Modifying
    @Query("update GiftOrder gif set gif.orderNo = ?2 , gif.transactionId = ?3 where gif.id = ?1")
    void updateOrderNo(Long orderId, String orderNo, String transactionId);

}
