package com.huixinpn.dionysus.repository.user;

import com.huixinpn.dionysus.domain.appointment.AppointmentStatus;
import com.huixinpn.dionysus.domain.user.Consultant;
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.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ConsultantRepository extends JpaRepository<Consultant, Long> {
  @Override
  @PreAuthorize("#user.username == principal or hasRole('ADMIN')")
  <T extends Consultant> T save(T user);

  @Override
  @PreAuthorize("#user.username == principal or hasRole('ADMIN')")
  void delete(Consultant user);

  @Override
  @PreAuthorize("#id == authentication.details or hasRole('ADMIN')")
  void delete(Long id);

  @Query(value = "select c from Consultant c where size(c.appointments) > 0 order by size(c.appointments) desc")
  Page<Consultant> findConsultantsHasAppointments(Pageable pageable);

  @Query(value = "select c from Consultant c where c.username like %:name%")
  List<Consultant> nameContains(@Param("name") String name);

  @Override
  @Query(value = "select c from Consultant c order by size(c.appointments) desc")
  Page<Consultant> findAll(Pageable pageable);

  @Query(value = "select c from Consultant c join c.appointments a where a.state = ?1 and size(c.appointments) > 0 order by size(c.appointments) desc")
  Page<Consultant> findConsultantsOrderByAppointmentState(AppointmentStatus state, Pageable pageable);

}

@@ -1174,32 +1174,222 @@
 ble pageable);%0A%0A
+  @Query(value = %22select c from Consultant c where c.expertise like %25:expertise%25%22)%0A  Page%3CConsultant%3E findConsultantsByExpertise(@Param(%22expertise%22) String expertise, Pageable pageable);%0A  %0A
   @Query(value =

add findconsultantbyexpertise