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

import androidx.room.*
import com.ys.bdtp.adm.mvvm.model.db.entity.task.ObjectEntity

/**
 * 表 object Dao
 */
@Dao
interface ObjectDao {
    @Query("select * from object")
    suspend fun getObjects(): List<ObjectEntity>

    @Query("select * from object where id = :id and valid = 1")
    suspend fun getObjectById(id: String): ObjectEntity?

    @Query("select * from object where bim_id = :bimId")
    suspend fun getObjectByBimId(bimId: String): ObjectEntity?

    @Query("select * from object where obj_type = :objType and valid = 1")
    suspend fun getObjectByObjType(objType: String): List<ObjectEntity>

    @Query("select * from object where building_id = :buildingId and obj_type=:type and valid = 1")
    suspend fun getObjectByBuildingId(buildingId: String, type: String): List<ObjectEntity>

    @Query("select * from object where building_id = :buildingId and valid = 1")
    suspend fun getObjectByBuildingId(buildingId: String): List<ObjectEntity>

    @Query("select * from object where floor_id = :floorId and obj_type=:type and valid = 1")
    suspend fun getEquipByFloorId(
        floorId: String,
        type: String,
    ): List<ObjectEntity>

    @Query("select * from object where class_code = :code and building_id =:buildingId and valid = 1")
    suspend fun getObjectByCode(code: String, buildingId: String): List<ObjectEntity>

    @Query("select * from object where container_id = :objId and valid = 1")
    suspend fun getContainerObjs(objId: String): List<ObjectEntity>

    @Query("select * from object where local_id = UPPEr(:localId) and valid = 1")
    suspend fun getObjectByLocalId(localId: String): ObjectEntity?

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insObject(vararg obj: ObjectEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insObjects(objects: List<ObjectEntity>)

    @Update
    suspend fun updateObjects(objects: List<ObjectEntity>)

    @Delete
    suspend fun delObject(obj: ObjectEntity)

    /**
     * 返回楼层类型的 ObjectEntity
     */
    @Query("select * from object where obj_type = 'floor'")
    suspend fun getFloors(): List<ObjectEntity>

    /**
     * 返回楼层类型的 ObjectEntity
     */
    @Query("select * from object where obj_type = 'floor' and building_id =:buildingId")
    suspend fun getFloorsByBuildingId(buildingId: String): List<ObjectEntity>

    /**
     * 返回楼层类型的 ObjectEntity
     */
    @Query("select * from object where obj_type = 'floor' and id =:floorId")
    suspend fun getFloorById(floorId: String): ObjectEntity

    /**
     * 返回所有建筑 ObjectEntity
     */
    @Query("select * from object where obj_type = 'building'")
    suspend fun getBuildings(): List<ObjectEntity>

    /**
     * 返回项目下的建筑
     * */
    @Query("select * from object where obj_type = 'building' and project_id = :projectId")
    suspend fun getBuildingsByProjectId(projectId: String): List<ObjectEntity>

    /**
     * 返回所有建筑 ObjectEntity
     */
    @Query("select * from object where obj_type = 'space' and class_code ='GeneralZone' and floor_id =:floorId")
    suspend fun getSpaceByFloorId(floorId: String): List<ObjectEntity>

    /**
     * 更新 ObjectEntity
     */
    @Update
    suspend fun updateObject(obj: ObjectEntity)

    // @Transaction
    // @Query("select * from object")
    // fun getObjectAndInfosWithOutlines() : List<ObjectAndInfosWithOutlines>

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