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

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.ys.bdtp.adm.mvvm.model.db.entity.task.RelationEntity

/**
 * 表 relation Dao
 */
@Dao
interface RelationDao {
    @Query("select * from relation")
    suspend fun getRelations(): List<RelationEntity>

    @Query("select * from relation where id = :id")
    suspend fun getRelationById(id: String): RelationEntity

    @Query("select * from relation where graph_code = :graphCode and rel_code = :relCode and valid = 1")
    suspend fun getRelationByCode(graphCode: String, relCode: String): List<RelationEntity>

    @Query("select * from relation where graph_code = :graphCode and rel_code = :relCode and obj_from =:objFrom and obj_to =:objto and valid = 1")
    suspend fun getRelByGraphCodeAndRelCodeAndObjFromAndObjTo(graphCode: String, relCode: String, objFrom: String, objto: String): RelationEntity?

    @Query("select * from relation where graph_code = :graphCode and rel_code = :relCode and obj_from=:equipId and valid = 1")
    suspend fun getRelationByCodeFrom(
        graphCode: String,
        relCode: String,
        equipId: String
    ): List<RelationEntity>

    @Query("select * from relation where graph_code = :graphCode and rel_code = :relCode and obj_to=:equipId and valid = 1")
    suspend fun getRelationByCodeTo(
        graphCode: String,
        relCode: String,
        equipId: String
    ): List<RelationEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insRelation(vararg relation: RelationEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insRelations(relations: List<RelationEntity>)

    @Update
    suspend fun updateRelations(relations: List<RelationEntity>)

    @Delete
    suspend fun delRelation(relation: RelationEntity)

    /*@Query("select count(*) from relation where project_id = :projectId and (state = 0 or state = 1)")
    suspend fun countUnDone(projectId: String): Int*/

}