package com.renrenliao.im.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.renrenliao.im.bean.UserInfo
import kotlinx.coroutines.flow.Flow

/**
 **@ClassName TestDao
 **@Description
 **@Author gan
 **@Date 2022/5/25 14:41
 **/

@Dao
interface FriendDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFriend(friendEntity: UserInfo)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertFriends(friends: List<UserInfo>)

    @Query("SELECT * FROM friend WHERE account_uid = :account_uid AND userUid = :uid")
    fun getFriend(account_uid:String,uid: String):UserInfo

    @Query("SELECT * FROM friend WHERE account_uid = :account_uid")
    fun getFriends(account_uid:String):List<UserInfo>

    @Query("SELECT * FROM friend WHERE account_uid = :account_uid AND nickname like '%' ||:key|| '%'")
    fun getFriendsByKey(account_uid:String,key:String):List<UserInfo>

    @Query("SELECT * FROM friend WHERE account_uid = :account_uid")
    fun getFriendsLiveData(account_uid:String): Flow<List<UserInfo>>

    @Query("UPDATE friend SET userAvatarFileName = :avatarUrl where account_uid = :account_uid and userUid = :userUid")
    fun updateFriendAvatar(account_uid:String,userUid:Int,avatarUrl:String)

    @Query("SELECT count(*) FROM friend WHERE account_uid = :account_uid AND userUid = :uid")
    fun countByUid(account_uid:String,uid:String):Int

    @Query("DELETE FROM friend WHERE account_uid = :account_uid AND userUid = :uid")
    fun deleteByUid(account_uid:String,uid:String)

    //更新备注
    @Query("UPDATE friend SET buddyRemark = :buddyRemark where account_uid = :account_uid and userUid = :userUid")
    fun updateFriendRemark(account_uid:String,userUid:String,buddyRemark:String)

    //设置所有好友离线
    @Query("UPDATE friend SET liveStatus = 0 where account_uid = :account_uid")
    fun  setAllFriendsOffline(account_uid: String)

    //设置好友在线离线状态
    @Query("UPDATE friend SET liveStatus = :liveStatus where userUid = :uid AND account_uid = :account_uid")
    fun  setFriendLiveStatus(liveStatus:Int,uid: String,account_uid: String)


    //更新头像版本
    @Query("UPDATE friend SET avatarVersion = :newVersion WHERE account_uid = :account_uid AND userUid = :uid")
    fun updateAvatarVersion(account_uid: String,uid: String,newVersion:Long)

}