package com.yihu.wlyy.repository.doctor;

import com.yihu.wlyy.entity.doctor.reply.DoctorQuickReply;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

/**
 * Created by lyr-pc on 2016/12/28.
 */
public interface DoctorQuickReplyDao extends PagingAndSortingRepository<DoctorQuickReply, Long> {

    @Query("select max(q.sort) from DoctorQuickReply q where q.doctor = ?1 and q.status = 1")
    Integer findMaxSort(String doctor);

    @Query("update DoctorQuickReply q set q.sort = q.sort - 1 where q.doctor = ?1 and q.sort > ?2 and q.status = 1")
    @Modifying
    int updateSort(String doctor,Integer sort);

    @Query("update DoctorQuickReply q set q.sort = q.sort - 1 where q.doctor = ?1 and q.sort > ?2 and q.sort <= ?3 and q.status = 1")
    @Modifying
    int updateSortLt(String doctor,Integer sort1,Integer sort2);

    @Query("update DoctorQuickReply q set q.sort = q.sort + 1 where q.doctor = ?1 and q.sort < ?2 and q.sort >= ?3 and q.status = 1")
    @Modifying
    int updateSortGt(String doctor,Integer sort1,Integer sort2);

    @Query("select q from DoctorQuickReply q where q.doctor = ?1 and q.status = 1")
    List<DoctorQuickReply> findDoctorReplies(String doctor, Sort sort);
}
