package com.ssj.dao.sys.schoolmatecircle.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.ssj.bean.sys.schoolmatecircle.domain.SSJUserMessage;

import java.util.List;

@Repository
public interface SSJUserMessageDao extends JpaRepository<SSJUserMessage, String> {
	
	@Query(nativeQuery = true, value = "select count(*) from ssj_user_message where receive_user_id = ?1 and is_read = 0")
	public int countByReceiveUserId(String userId);
	
	@Modifying
	@Query(nativeQuery = true, value = "update ssj_user_message set is_read = 1 "
			+ "where send_user_id = ?2 and receive_user_id = ?1")
	public void updateMessageHaveRead(String sendUserId, String receiveUserId);
	
	@Query(nativeQuery = true, value = "select * from ssj_user_message m where (m.send_user_id = ?1 and m.receive_user_id = ?2 ) or (m.send_user_id = ?3 and m.receive_user_id = ?4) ORDER BY m.create_time desc,m.num desc limit 1")
	public SSJUserMessage findLastOnlineMessage(String sendUserId, String receiveUserId,String receiveUserId2,String sendUserId2);
	
	@Modifying
	@Query(nativeQuery = true, value = "delete from ssj_user_message where (send_user_id = ?1 and receive_user_id = ?2) or (send_user_id = ?2 and receive_user_id = ?1)")
	public void deleteUserMessage(String sendUserId, String receiveUserId);

	@Query(nativeQuery = true,value ="select * from ssj_user_message where (send_user_id = ?1 and receive_user_id = ?2) or (send_user_id = ?2 and receive_user_id = ?1)")
   public List<SSJUserMessage> findBySendUserIdAndRecieveUserId(String sendUserId, String receiveUserId);
}
