package com.uas.search.jms;

import com.uas.search.annotation.NotEmpty;
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 org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sunyj
 * @since 2016年7月7日 下午5:52:52
 */
@Repository
public interface LuceneMessageDao
        extends JpaSpecificationExecutor<LuceneMessage>, JpaRepository<LuceneMessage, Long> {

    /**
     * 获取指定数目的数据
     *
     * @param start 开始的记录
     * @param size 指定数目
     * @return 数据
     */
    @Query(value = "select * from lucene$message where me_retry_count < 5 order by me_priority desc, me_create_time limit ?1, ?2", nativeQuery = true)
    List<LuceneMessage> findList(@NotEmpty("start") Integer start, @NotEmpty("size") Integer size);

    /**
     * 统计行数
     * @return 行数
     */
    @Query(value = "select count(1) from lucene$message where me_retry_count < 5", nativeQuery = true)
    long count();

    /**
     * 出队消息
     * @param id 消息 id
     */
    @Transactional
    @Procedure(procedureName = "dequeue_lucene_message")
    void dequeueLuceneMessage(@NotEmpty("id") Long id);
}
