package com.ys.bdtp.adm.mvvm.vm.project

import com.ys.bdtp.adm.mvvm.model.Data
import com.ys.bdtp.adm.mvvm.model.db.AdmDatabase
import com.ys.bdtp.adm.mvvm.model.db.relation.UserWithProjects
import com.ys.bdtp.adm.mvvm.model.nt.Api
import com.ys.bdtp.adm.mvvm.model.toStateFlow
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.withContext

/**
 * ProjectRepository
 *
 * @author bai
 *
 * @param
 */
class ProjectRepository(private val api: Api, private val db: AdmDatabase) {
    /**
     *  获取用户以及用户项目
     */
    fun getUserWithProjects(): Flow<Data<UserWithProjects>> {
        return db.userDao().getUserWithProjects().toStateFlow()
    }

    suspend fun logout() {
        withContext(Dispatchers.IO) {
            db.userDao().getUser().collect {
                it?.let {
                    it.state = 0
                    db.userDao().updUser(it)
                }
            }
        }
    }
}