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.PipeEntity
import com.ys.bdtp.adm.mvvm.model.db.entity.config.PipeConfigEntity

/**
 * 管道表 Dao
 */
@Dao
interface PipeDao {
    @Query("select * from pipe")
    suspend fun getPipes(): List<PipeEntity>

    @Query("select * from pipe where id = :id")
    suspend fun getPipeById(id: String): PipeEntity

    @Query("select * from pipe where building_id = :id")
    suspend fun getPipeByBuildingId(id: String): List<PipeEntity>

    @Query("select * from pipe where obj_from = :id and pipe_type = :pipeType and pipe_sub_type=:pipeSubType and valid = 1")
    suspend fun getPipeByIdFrom(id: String, pipeType: String, pipeSubType: String): List<PipeEntity>

    @Query("select * from pipe where obj_to = :id and pipe_type = :pipeType and pipe_sub_type=:pipeSubType and valid = 1")
    suspend fun getPipeByIdTo(id: String, pipeType: String, pipeSubType: String): List<PipeEntity>

    @Query("select * from pipe where floor_id = :floorId and valid = 1")
    suspend fun getPipeByFloorId(floorId: String): List<PipeEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insPipe(vararg pipe: PipeEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insPipes(pipes: List<PipeEntity>)

    @Update
    suspend fun updatePipe(pipe: PipeEntity)

    @Update
    suspend fun updatePipes(pipes: List<PipeEntity>)

    @Delete
    suspend fun delConfig(config: PipeConfigEntity)

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