package com.persagy.server.syn.models.tools

import com.persagy.base.extensions.toInt
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DefFuncId
import com.persagy.server.datacenter.datacenter.models.entities.objects.Tenant
import com.persagy.service.SObjectService
import org.slf4j.LoggerFactory

/**
 * 租户工具
 */
object VOTnUtil {

    /** 日志 */
    private val logger = LoggerFactory.getLogger(ProjectUtil::class.java)
    /** 信息点 */
    private val dictDefFuncIdService = SObjectService(SMybatisDao(DefFuncId::class.java))

    /**
     * 设备转换
     */
    fun equipSyns(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Tenant {
        /** 查建楼层型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("projectId", projectId), SFilter.eq("classCode", mapObj["classCode"].toString())).exec()
        val mapEx = HashMap<String, Any?>()
        val mapCode = HashMap<String, Any?>()
        /** 遍历项目类型字典 */
        for (explainProject in dictDefFuncIdList) {
            /** 输入类型 */
            mapEx[explainProject.code!!] = explainProject.dataType
            mapCode[explainProject.code!!] = explainProject.category
        }
        logger.debug("打印= ${mapObj.toJson()}")
        val tenant = Tenant()
        tenant.projectId = projectId
        for (key in mapObj.keys) {
            val containsKey = mapEx.containsKey(key)
            if (containsKey){
                if (key != "id"&& key!="name"&&key!="localId"&&key!="localName"&&key!="defaultQRCode"
                        &&key!="qRCodePic"&&key!="rfId"&&key!="cadId"&&key!="bimFamilyName"&&key!="bimFamilySymbol"
                        &&key!="bimTypeId"&&key!="bimId"&&key!="bimLocation"&&key!="equipSerial"){
                    val containsKey1 = mapCode.containsKey(key)
                    if (containsKey1){
                        val any = mapCode[key].toString()
                        if (any == "STATIC"){
                            when(mapEx[key].toString()) {
                                "INTEGER" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toInt()
                                }
                                "DOUBLE" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString().toDoubleOrNull()
                                }
                                "BOOLEAN" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString().toBoolean()
                                }
                                "STRING" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString()
                                }
                                "DATETIME" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ATTACHMENT" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString()
                                }
                                "OBJECT" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ENUM" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString()
                                }
                                "MENUM" -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString()
                                }
                                else -> {
                                    if (tenant.infos.isNullOrEmpty()){
                                        tenant.infos = HashMap<String,Any?>()
                                    }
                                    tenant.infos!![key] = mapObj[key]!!.toString()
                                }
                            }
                        }else{
                            if (tenant.infos.isNullOrEmpty()){
                                tenant.infos = HashMap<String,Any?>()
                            }
                            tenant.infos!![key] = mapObj[key]!!.toString()
                        }
                    }

                } else {
                    when(key) {
                        "id" -> tenant.id =  mapObj[key]!!.toString()
                        "name" -> tenant.name =  mapObj[key]!!.toString()
                        "localId" -> tenant.localId =  mapObj[key]!!.toString()
                        "localName" -> tenant.localName =  mapObj[key]!!.toString()
                    }
                }

            } else {
//                if (equipment.infos.isNullOrEmpty()){
//                    equipment.infos = HashMap<String,Any?>()
//                }
//                equipment.infos!![key] =  mapObj[key]!!.toString()
            }
        }

        return tenant
    }




    /**
     * 设备工具
     *
     * @param projectId     项目 id
     * @param equipment     设备对象
     * @return 返回结果
     */
    fun equipTools(projectId: String,tenant: Tenant): ArrayList<HashMap<String,Any?>> {
        var list = ArrayList<HashMap<String,Any?>>()
        val hashMap = HashMap<String, Any?>()
        hashMap["id"] = tenant.id
        hashMap["name"] = tenant.name
        hashMap["projectId"] = projectId
        hashMap["objType"] = "VOTn"
        hashMap["classCode"] = "VOTn"
        if (!tenant.localName.isNullOrEmpty()){
            hashMap["localName"] = tenant.localName
        }
        if (!tenant.localId.isNullOrEmpty()){
            hashMap["localId"] = tenant.localId
        }

        if (!tenant.infos.isNullOrEmpty()){
            for (key in tenant.infos!!.keys){
                hashMap[key] = tenant.infos!![key]
            }
        }

        list.add(hashMap)
        return list
    }



}