package com.izouma.nineth.repo; import com.izouma.nineth.domain.Order; import com.izouma.nineth.enums.CollectionSource; import com.izouma.nineth.enums.OrderStatus; import com.izouma.nineth.enums.PayMethod; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; 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.time.LocalDateTime; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; public interface OrderRepo extends JpaRepository, JpaSpecificationExecutor { @Query("update Order t set t.del = true where t.id = ?1") @Modifying @Transactional void softDelete(Long id); Optional findByIdAndDelFalse(Long id); List findByStatusAndCreatedAtBeforeAndDelFalse(OrderStatus status, LocalDateTime time); List findByCollectionId(Long collectionId); int countByUserIdAndCollectionIdAndStatusIn(Long userId, Long collectionId, Iterable orderStatuses); int countByUserIdAndCountIdAndStatusIn(Long userId, String countId, Iterable orderStatuses); List findByStatus(OrderStatus orderStatus); @Query("select count(o) from Order o join Collection c on o.collectionId = c.id " + "where c.minterId = ?1 and c.source = 'OFFICIAL' and o.status <> 'NOT_PAID' and o.status <> 'CANCELLED'") long countSales(Long userId); Order findByTransactionId(String txId); long countAllByPayTimeAfter(LocalDateTime payTime); @Query("select id from Order where userId = ?1 and opened = false") List findAllByUserIdAndOpenedFalse(Long userId); int countByUserIdAndCollectionIdAndVipTrueAndStatusIn(Long userId, Long collectionId, Collection status); List findAllByStatusAndCompanyId(OrderStatus status, Long companyId); List findAllByStatusAndMinterId(OrderStatus status, Long minterId); List findAllByCreatedAtIsAfterAndStatusInAndCompanyId(LocalDateTime payTime, List status, Long companyId); List findAllByCreatedAtBetweenAndStatusInAndCompanyId(LocalDateTime start, LocalDateTime end, List status, Long companyId); List findAllByCreatedAtIsAfterAndMinterIdAndStatusIn(LocalDateTime payTime, Long minterId, List status); List findAllByCreatedAtBetweenAndMinterIdAndStatusIn(LocalDateTime start, LocalDateTime end, Long minterId, List status); @Query(nativeQuery = true, value = "select user_id, sum(price) total from raex.order_info " + "where (status = 'FINISH' or status = 'PROCESSING') " + "group by user_id order by sum(price) desc limit 50") List sumPrice(); List findByCreatedAtBetweenAndSourceAndStatusIn(LocalDateTime start, LocalDateTime end, CollectionSource source, Collection statuses); @Query(nativeQuery = true, value = "select o.user_id from order_info o " + " WHERE o.created_at < ?2 and o.created_at > ?1 and o.status = 'CANCELLED' " + "GROUP BY o.user_id " + "HAVING count(*) > 2") List checkBlackList(LocalDateTime start, LocalDateTime end); @Query("update Order o set o.status = com.izouma.nineth.enums.OrderStatus.FINISH where o.id = ?1") @Transactional @Modifying int finishOrder(Long id); @Query("update Order o set o.status = com.izouma.nineth.enums.OrderStatus.PROCESSING, " + "o.payTime = ?2, o.payMethod = ?3, o.transactionId = ?4 where o.id = ?1") @Transactional @Modifying int processingOrder(Long id, LocalDateTime payTime, PayMethod payMethod, String transactionId); @Query(value = "select sum(price) from order_info where user_id = ?1 and status = 'FINISH'", nativeQuery = true) BigDecimal sumUserPrice(Long userId); @Query("select o from Order o " + " join Asset a on a.id = o.assetId " + " join a.tags t on t.id = ?1 " + "where o.userId not in ?2 " + " and o.source = com.izouma.nineth.enums.CollectionSource.TRANSFER " + " and o.status = com.izouma.nineth.enums.OrderStatus.FINISH") Page byTag(Long tagId, List excludeUserId, Pageable pageable); @Query("select o from Order o " + " join Asset a on a.id = o.assetId " + " join a.tags t on t.id = ?1 " + "where o.userId = ?2" + " and o.source = com.izouma.nineth.enums.CollectionSource.TRANSFER " + " and o.status = com.izouma.nineth.enums.OrderStatus.FINISH") Page byTag(Long tagId, Long userId, Pageable pageable); @Query(value = "select user_id from asset where id = ?1", nativeQuery = true) Long selectUserId(Long assetId); int countAllByUserIdAndCollectionIdAndStatusIn(Long userId, Long collectionId, Collection status); Order findFirstByCollectionIdOrderByCreatedAtDesc(Long collectionId); @Query(value = "SELECT a.id id, a.name name ,a.prefixName prefixName, a.source source, SUM(a.num) num FROM " + "(SELECT a.id id, o.name name,a.prefix_name prefixName, o.source source,COUNT(*) num FROM order_info o LEFT JOIN asset a ON o.asset_id = a.id WHERE o.`status` = 'FINISH' AND o.source = 'TRANSFER' AND o.pay_time > ?1 GROUP BY a.prefix_name " + "UNION ALL" + " SELECT a.id id, o.name name,a.prefix_name prefixName, o.source source,COUNT(*) num FROM order_info o LEFT JOIN collection_info a ON o.collection_id = a.id WHERE o.`status` = 'FINISH' and o.source = 'OFFICIAL' AND o.pay_time > ?1 GROUP BY a.prefix_name) a " + "GROUP BY a.prefixName ORDER BY (SUM(a.num)) DESC LIMIT 10", nativeQuery = true) List> transactionTopTen(LocalDateTime payTime); }