package garbagetown.domain.repository.reserve;

import garbagetown.domain.model.Customer;
import garbagetown.domain.model.Reserve;
import garbagetown.domain.model.Tourinfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.persistence.LockModeType;
import java.util.List;

/**
 * Created by garbagetown on 10/27/15.
 */
public interface ReserveRepository extends JpaRepository<Reserve, String> {

    @Query("SELECT r FROM Reserve AS r LEFT JOIN FETCH r.tourinfo AS t LEFT JOIN FETCH t.departure LEFT JOIN FETCH t.arrival WHERE r.customer = :customer ORDER BY t.depDay, r.reserveNo")
    List<Reserve> findAllByCustomer(@Param("customer") Customer customer);

    @Query("SELECT SUM(r.adultCount + r.childCount) FROM Reserve r WHERE r.tourinfo = :tourinfo")
    Long countReservedPersonSumByTourinfo(@Param("tourinfo") Tourinfo tourinfo);

    @Query("SELECT r FROM Reserve r WHERE reserveNo = :reserveNo")
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    Reserve findOneForUpdate(@Param("reserveNo") String reserveNo);
}

@@ -592,32 +592,49 @@
 Query(%22SELECT r 
+%22 +%0A            %22
 FROM Reserve AS 
@@ -635,16 +635,33 @@
 ve AS r 
+%22 +%0A            %22
 LEFT JOI
@@ -684,16 +684,33 @@
 fo AS t 
+%22 +%0A            %22
 LEFT JOI
@@ -729,16 +729,33 @@
 parture 
+%22 +%0A            %22
 LEFT JOI
@@ -772,16 +772,33 @@
 arrival 
+%22 +%0A            %22
 WHERE r.
@@ -818,16 +818,33 @@
 ustomer 
+%22 +%0A            %22
 ORDER BY

wrap the query for readablity