package com.persagy.revit.backstage.sync

import com.persagy.revit.backstage.model.DCFloor
import com.persagy.revit.models.modelFile.FloorModel
import com.persagy.revit.services.BaseDataService
import com.persagy.revit.services.datacenter.DataCenterService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Scope
import org.springframework.stereotype.Component

/**
 * 同步楼层
 *
 * @author jxing
 */
@Component
@Scope("prototype")
class SyncFloor {

    var floor: FloorModel? = null

    @Autowired
    lateinit var dataCenterService: DataCenterService

    fun init(obj: FloorModel): SyncFloor {
        this.floor = obj
        return this
    }

    // 数据中心设备
    var dcFloor : ArrayList<DCFloor> = ArrayList()
    // 交集(BIMID)
    val intersection = ArrayList<String>()

    val dataService = BaseDataService

    fun prepareDataCenterData(): SyncFloor {
//        if(floor == null || floor!!.floorIdList.isNullOrEmpty() || floor!!.projectId == null)
        if(floor == null || floor!!.projectId == null)
            return this
        val result = dataCenterService.getFloor(floor!!.id!!, floor!!.projectId!!)
        if(result != null && result.isNotEmpty()) {
            dcFloor = result
        }
        return this
    }

    fun syncUpdate(dcFloor: DCFloor): SyncFloor {
        if(!dataCenterService.floorUpdate(dcFloor, floor!!.projectId!!)) {
            throw Exception("同步楼层失败")
        }
        return this
    }
}