package com.ym.dao;

import com.ym.pojo.RoomMember;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import java.util.List;

/**
 * Created by weiqinxiao on 2019/2/25.
 */
@Repository
public interface RoomMemberDao extends JpaRepository<RoomMember, Long> {
    List<RoomMember> findByRid(String rid);

    @Lock(value = LockModeType.PESSIMISTIC_WRITE)
    RoomMember findByRidAndUid(String rid, String uid);

    List<RoomMember> findByRidAndRole(String rid, int role);

    List<RoomMember> findByUid(String uid);

    @Modifying
    int deleteByRid(String roomId);

    @Query(value = "select count(*) from rongyun_room_member where rid=?1", nativeQuery = true)
    int countByRid(String roomId);

    @Query(value = "select count(*) from rongyun_room_member where rid=?1 and role!=?2", nativeQuery = true)
    int countByRidAndExcludeRole(String roomId, int excludeRole);

    @Modifying
    @Query(value = "delete from rongyun_room_member where rid=?1 and uid=?2", nativeQuery = true)
    int deleteUserByRidAndUid(String rid, String uid);

    @Modifying
    @Query(value = "update rongyun_room_member set role=?3 where rid=?1 and uid=?2", nativeQuery = true)
    int updateRoleByRidAndUid(String rid, String uid, int role);

    @Modifying
    @Query(value = "update rongyun_room_member set camera=?3 where rid=?1 and uid=?2", nativeQuery = true)
    int updateCameraByRidAndUid(String rid, String uid, boolean camera);

    @Modifying
    @Query(value = "update rongyun_room_member set mic=?3 where rid=?1 and uid=?2", nativeQuery = true)
    int updateMicByRidAndUid(String rid, String uid, boolean mic);

    @Modifying
    @Query(value = "update rongyun_room_member set hand=?3 where rid=?1 and uid=?2", nativeQuery = true)
    int updateHandByRidAndUid(String rid, String uid, boolean hand);

    @Modifying
    @Query(value = "update rongyun_room_member set music_mode=?3 where rid=?1 and uid=?2", nativeQuery = true)
    int updateMusicByRidAndUid(String rid, String uid, boolean musicMode);

    boolean existsByRidAndUid(String rid, String uid);
}