package com.ys.bdtp.adm.mvvm.model.db.dao

import androidx.room.*
import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
import com.ys.bdtp.adm.mvvm.model.db.relation.UserWithProjects
import kotlinx.coroutines.flow.Flow

/**
 * user 表访问接口
 *
 * @author bai
 */
@Dao
interface UserDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insUser(vararg user: UserEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insUsers(users: List<UserEntity>): List<Long>

    /**
     * 获取所有本地登陆过的用户
     */
    @Query("select * from user")
    suspend fun getUsers(): List<UserEntity>

    /**
     * 获取当前登录用户
     */
    @Query("select * from user where state = 1")
    suspend fun currentUser(): UserEntity?

    @Query("select * from user where state = 1")
    fun getUser(): Flow<UserEntity?>

    @Query("select * from user where id = :id")
    fun getUserById(id: String): UserEntity

    @Query("select * from user where name =:phone and password = :password")
    suspend fun getUser(phone: String, password: String): UserEntity?

    @Update
    fun updUser(user: UserEntity)

    @Delete
    fun delUser(user: UserEntity)

    @Transaction
    @Query("select * from user where id = :id")
    suspend fun getUserWithProjectsById(id: String): UserWithProjects

    /**
     * 获取 User, Project 一对多
     */
    @Transaction
    @Query("select * from user where state = 1")
    fun getUserWithProjects(): Flow<UserWithProjects>

}