package com.renrenliao.im.db

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

/**
 **@ClassName AlarmDao
 **@Description
 **@Author gan
 **@Date 2022/6/17 15:39
 **/
@Dao
interface AlarmDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAlarm(alarmEntity: AlarmEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAlarms(alarms:List<AlarmEntity>)

    @Query("SELECT * FROM alarm WHERE account_uid = :account_uid")
    fun getAlarmsLiveData(account_uid:String): LiveData<List<AlarmEntity>>

    @Query("SELECT * FROM alarm WHERE account_uid = :account_uid")
    fun getAlarms(account_uid:String): List<AlarmEntity>

    @Query("SELECT * FROM alarm WHERE account_uid = :account_uid AND gId = :gId AND msgType = :msgType")
    fun getAlarm(account_uid:String,gId:String,msgType:Int): AlarmEntity

    @Query("SELECT * FROM alarm WHERE account_uid = :account_uid AND msgType = :msgType")
    fun getAlarmListByTypeLiveData(account_uid:String,msgType:Int): LiveData<List<AlarmEntity>>

    @Query("delete FROM alarm")
    fun deleteAllAlarm()

    @Query("delete FROM alarm WHERE account_uid != :account_uid")
    fun deleteAlarmsNotCurrentUid(account_uid:String)

    @Query("delete FROM alarm WHERE account_uid = :account_uid AND gId = :gId AND msgType = :msgType")
    fun deleteAlarm(account_uid:String,gId: String,msgType:Int)

    @Query("delete FROM alarm WHERE account_uid = :account_uid AND gId = :gId")
    fun deleteAlarm(account_uid:String,gId: String)

    //更新置顶状态
    @Query("UPDATE alarm SET isTop = :top WHERE account_uid = :account_uid AND tid = :uid AND msgType = :msgType")
    fun updateIsTop(account_uid: String,uid: String,msgType:Int,top:Boolean)

    //更新已读未读状态
    @Query("UPDATE alarm SET num = :num WHERE account_uid = :account_uid AND tid = :uid AND msgType = :msgType")
    fun updateReadStatus(account_uid: String,uid: String,msgType:Int,num:Int)

    //统计未读消息
    @Query("SELECT SUM(num) FROM alarm WHERE account_uid = :account_uid ")
    fun countUnRead(account_uid:String):LiveData<Int>

    //获取通知列表
    @Query("SELECT * FROM alarm WHERE account_uid = :account_uid AND tid = :tid")
    fun getAlarms(account_uid:String,tid:String): List<AlarmEntity>

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

}