package com.softwarebr.nunu.dao.user;

import com.brframework.commondb.core.CommonRepository;
import com.softwarebr.nunu.entity.user.Member;
import com.softwarebr.nunu.entity.user.Message;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @date 2020/03/17 15:40:01
 * @author lilin
 */

@Repository
public interface MessageDao extends CommonRepository<Long, Message> {

    /**
     * 根据用户id查询记录
     * @param memberId
     * @param id
     * @return
     */
    @Query(value= "select m.* from u_message as m where m.member_id = :memberId and m.id < :id  order by create_date DESC LIMIT 12", nativeQuery = true)
    List<Message> findByMemberId(@Param("memberId") Long memberId, @Param("id") Long id);

    /**
     * 根据用户id查询记录
     * @param memberId
     * @return
     */
    @Query(value= "select m.* from u_message as m where m.member_id = :memberId order by create_date DESC LIMIT 12", nativeQuery = true)
    List<Message> findByMemberId(@Param("memberId") Long memberId);

    /**
     * 根据用户id查询其最后一条记录
     * @param memberId
     * @return
     */
    Message findTopByMemberIdOrderByCreateDateDesc(Long memberId);

    /**
     * 查询多个id的消息进行分页
     * @param memberId
     * @param pageable
     * @return
     */
    Page<Message> findByMemberId(Long memberId, Pageable pageable);

    /**
     * 查询多个id的消息
     *
     * @param createDate
     * @param sendUserIds
     * @return
     */
    List<Message> findByCreateDateGreaterThanAndMemberIdIn(LocalDateTime createDate, List<Long> sendUserIds);



}