package cn.sagacloud.server.algorithm.services.json

import cn.sagacloud.server.algorithm.models.entities.Space
import cn.sagacloud.server.algorithm.models.jsonAnalyzer.AllComponents
import cn.sagacloud.server.algorithm.models.jsonAnalyzer.BaseObj
import cn.sagacloud.server.algorithm.services.CommonService
import com.google.common.base.Strings
import com.sybotan.database.SFilter
import com.sybotan.service.SObjectService
import com.sybotan.service.models.enums.SResponseType
import com.sybotan.service.models.requests.SCreateRequest
import java.util.ArrayList

/**
 *
 *
 * @author jxing
 */
object Space2Analyzer {

    /**
     * @Description: 房间和空间分析
     */
    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
                    }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!!), SFilter.eq("elementType","Room"))
        val create = SCreateRequest<Space>()
        create.content = spaces
        val result = spaceService.createList(create)
        return SResponseType.success == result.result
    }
}