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

import cn.sagacloud.server.algorithm.models.entities.Room
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.*

/**
 *
 *
 * @author Cainga
 */
object RoomAnalyzer {
    fun analyze(roomService: SObjectService<Room>, baseObj: BaseObj<AllComponents>, rooms: ArrayList<Room>?) : Boolean{
        if(rooms == null || rooms.isEmpty())
            return true
        for(obj in rooms){
            obj.id = CommonService.generateUUID(null, true)
            obj.modelId = baseObj.modelId
            if(obj.parameters != null){
                var code = ""
                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 = java.lang.String.join("", Strings.nullToEmpty(obj.localName), Strings.nullToEmpty(code))
                }
            }
        }
        roomService.delete(SFilter.eq("modelId", baseObj.modelId!!))
        val create = SCreateRequest<Room>()
        create.content = rooms
        val result = roomService.createList(create)
        return SResponseType.success == result.result
    }
}