package com.persagy.revit.services.json

import com.google.common.base.Strings
import com.persagy.database.SFilter
import com.persagy.revit.models.entities.Space
import com.persagy.revit.models.jsonAnalyzer.AllComponents
import com.persagy.revit.models.jsonAnalyzer.BaseObj
import com.persagy.revit.services.CommonService
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SCreateRequest
import java.util.*

/**
 *
 *
 * @author jxing
 */
object SpaceAnalyzer {
    fun analyze(spaceService: SObjectService<Space>, baseObj: BaseObj<AllComponents>, spaces: ArrayList<Space>?) : Boolean{
        if(spaces == null || spaces.isEmpty())
            return true
        for(obj in spaces){
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            if(obj.parameters != null){
                var code : String = ""
                for (param in obj.parameters!!){
                    if(param.name.equals("名称")){
                        if(param.value.isNullOrBlank()){
                            obj.localName = obj.name
                        }else
                            obj.localName = param.value
                    }else if(param.name.equals("编号") && param.value != null){
                        code = param.value
                    }else if(param.name.equals("2.空间标准类型编码")){
                        obj.wDRoomFuncID = param.value
                    }else if(param.name.equals("3.空间标准类型")){
                        obj.wDRoomFuncName = param.value
                    }
                }
                if(obj.localName == null || obj.localName!!.isEmpty() || obj.localName.equals("空间")){
                    obj.localName = java.lang.String.join("", Strings.nullToEmpty(obj.localName), Strings.nullToEmpty(code))
                }
            }
        }
        spaceService.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<Space>()
        create.content = spaces
        val result = spaceService.createList(create)
        return SResponseType.success == result.result
    }
}