package com.persagy.revit.services.endpoint

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.revit.models.baseGraph.*
import com.persagy.revit.models.jsonAnalyzer.BaseGraph
import com.persagy.revit.models.jsonAnalyzer.BaseObj
import com.persagy.revit.models.request.BaseGraphRequest
import com.persagy.revit.services.BaseDataService
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
object BaseGraphService {

    private var doorService = SObjectService(SMybatisDao(BGDoor::class.java))
    private var windowService = SObjectService(SMybatisDao(BGWindow::class.java))
    private var columnService = SObjectService(SMybatisDao(BGColumn::class.java))
    private var wallService = SObjectService(SMybatisDao(BGWall::class.java))
    private var virtualWallService = SObjectService(SMybatisDao(BGVirtualWall::class.java))
    private var spaceService = SObjectService(SMybatisDao(BGSpace::class.java))
//    private var modelService = SObjectService(SMybatisDao(ModelFile::class.java))

    val dataService = BaseDataService

    /**
     * 构建底图后台服务
     * @param request 接口请求对象
     * @return 接口返回
     */
    fun composeBaseGraph(request: BaseGraphRequest): SBaseResponse { //SCreateResponse<BaseObj<BaseGraph>> {
        try {
            val response = SCreateResponse<BaseObj<BaseGraph>>()
            val baseObj = BaseObj<BaseGraph>()
            val modelList = dataService.modelFileService.select(SFilter.Companion.eq("id", request.modelId!!)).exec()
            if(modelList.isEmpty())
                return SBaseResponse(SResponseType.failure, "不存在该模型: ${request.modelId}")
            val model = modelList[0]
            baseObj.mbiName = model.mbiName
            baseObj.modelId = model.id
            baseObj.planName = model.planName
            baseObj.elements = fullfillBaseGraph(request.modelId!!)
            response.entityList = arrayListOf(baseObj)
            response.result = SResponseType.success
            return response
        }catch (e : Exception){
            return SBaseResponse(SResponseType.failure, e.message ?: "异常!")
        }
    }

    /**
     * 填充底图
     * @param modelId  模型id
     * @return  底图对象
     */
    private fun fullfillBaseGraph(modelId : String): BaseGraph? {
        val baseGraph = BaseGraph()
        baseGraph.spaces = spaceService.select(SFilter.Companion.eq("modelId", modelId)).exec()
        baseGraph.walls = wallService.select(SFilter.Companion.eq("modelId", modelId)).exec()
        baseGraph.virtualWalls = virtualWallService.select(SFilter.Companion.eq("modelId", modelId)).exec()
        baseGraph.columns = columnService.select(SFilter.Companion.eq("modelId", modelId)).exec()
        baseGraph.doors = doorService.select(SFilter.Companion.eq("modelId", modelId)).exec()
        baseGraph.windows = windowService.select(SFilter.Companion.eq("modelId", modelId)).exec()
        return baseGraph
    }


}