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

import com.persagy.base.extensions.toDouble
import com.persagy.base.extensions.toInt
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DefFuncId
import com.persagy.server.datacenter.models.entities.objects.GeneralSystem
import com.persagy.server.syn.Opts
import com.persagy.server.syn.controllers.obj.ObjectFromToControllers
import com.persagy.service.SObjectService
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory

/**
 * 系统工具
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/21 19:34
 */
object SystemUtil {

    /** 日志 */
    private val logger = LoggerFactory.getLogger(ObjectFromToControllers::class.java)
    /** 信息点 */
    private val dictDefFuncIdService = SObjectService(SMybatisDao(DefFuncId::class.java))
    /** 配置 */
    private val opts: Opts by lazy {
        SSpringContextUtil.getBean(Opts::class.java) as Opts
    }

    /**
     * 系统转换工具
     *
     * @param projectId     项目 id
     * @param groupCode     集团编码
     * @param mapObj        中台数据对象
     * @return 返回结果
     */
    fun systemDataConversion(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>):GeneralSystem {
        /** 查建楼层型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", mapObj["classCode"].toString())).exec()
        val map = java.util.HashMap<String, Any?>()
        val mapCode = java.util.HashMap<String, Any?>()
        for (explainProject in dictDefFuncIdList) {
            map[explainProject.code!!] = explainProject.firstName
            if (!explainProject.secondName.isNullOrEmpty()) {
                mapCode[explainProject.code!!] = explainProject.secondName
            }
        }
        /** 技术参数 */
        val techParamMap = HashMap<String, Any?>()
        /** 运行参数 */
        val runParamMap = HashMap<String, Any?>()
        /** 设定参数 */
        val settingParamMap = HashMap<String, Any?>()
        /** 自定义参数 */
        val customParamMap = HashMap<String, Any?>()
        /** 运行状态 */
        val runningState = HashMap<String, Any?>()
        /** 反馈参数 */
        val feedbackParam = HashMap<String, Any?>()

        /** 技术参数 ******************************************************************************/
        /** 供水 */
        val waterSupply = HashMap<String, Any?>()
        /** 末端 */
        val end = HashMap<String, Any?>()
        /** 水处理 */
        val waterTreatment = HashMap<String, Any?>()
        /** 整体 */
        val whole = HashMap<String, Any?>()
        /** 太阳能 */
        val solarEnergy = HashMap<String, Any?>()
        /** 水质要求 */
        val waterQuality = HashMap<String, Any?>()
        /** 喷泉 */
        val fountain = HashMap<String, Any?>()
        /** 园林 */
        val gardens = HashMap<String, Any?>()
        /** 排水 */
        val drainage = HashMap<String, Any?>()
        /** 排放标准 */
        val emissStand = HashMap<String, Any?>()
        /** 软件厂家 */
        val softwareManu = HashMap<String, Any?>()
        /** 供应购买 */
        val supplyPurchase = HashMap<String, Any?>()
        /** 运行维保 */
        val operationMainte = HashMap<String, Any?>()
        /** 客流探测 */
        val passeFlowDetec = HashMap<String, Any?>()
        /** 软件系统 */
        val softwareSystem = HashMap<String, Any?>()
        /** 终端设备 */
        val terminalEquip = HashMap<String, Any?>()
        /** 移动信号放大 */
        val mobileSignal = HashMap<String, Any?>()
        /** 网络 */
        val network = HashMap<String, Any?>()
        /** 核心层 */
        val coreLayer = HashMap<String, Any?>()
        /** 汇聚层 */
        val distriLayer = HashMap<String, Any?>()
        /** 接入层 */
        val accessLayer = HashMap<String, Any?>()
        /** 无线 */
        val wireless = HashMap<String, Any?>()
        /** VLAN */
        val vlan = HashMap<String, Any?>()
        /** 车库出入口 */
        val garageEntraExit = HashMap<String, Any?>()
        /** 车位 */
        val parkingLot = HashMap<String, Any?>()
        /** 寻车 */
        val carSearching = HashMap<String, Any?>()
        /** 功能 */
        val function = HashMap<String, Any?>()
        /** 对讲系统 */
        val intercomSystem = HashMap<String, Any?>()
        /** 电视信号 */
        val tvSignal = HashMap<String, Any?>()
        /** 租户管理 */
        val tenantManag = HashMap<String, Any?>()
        /** 前端 */
        val frontEnd = HashMap<String, Any?>()
        /** 传输 */
        val transmission = HashMap<String, Any?>()
        /** 显示  */
        val display = HashMap<String, Any?>()
        /** 灯具 */
        val lampsLanterns = HashMap<String, Any?>()
        /** 配电柜 */
        val powerCabinet = HashMap<String, Any?>()
        /** 电梯 */
        val elevator = HashMap<String, Any?>()
        /** 柴发机组 */
        val chaiFa = HashMap<String, Any?>()
        /** 排风 */
        val exhaustAir = HashMap<String, Any?>()
        /** 备用电源 */
        val emergencySupply = HashMap<String, Any?>()
        /** 高压 */
        val highPressure = HashMap<String, Any?>()
        /** 低压 */
        val lowPress = HashMap<String, Any?>()
        /** 电源 */
        val powerSupply = HashMap<String, Any?>()
        /** 应急照明 */
        val emerLighting = HashMap<String, Any?>()
        /** 疏散指示 */
        val evacuaInstr = HashMap<String, Any?>()

        /** 运行参数 ******************************************************/
        /** 瞬时状态 */
        val instanState = HashMap<String, Any?>()
        /** 累计指标 */
        val cumulaIndex = HashMap<String, Any?>()
        /** 瞬时指标 */
        val instanIndex = HashMap<String, Any?>()
        /** 设定参数************************************************************************ **/
        val basicSetParam = HashMap<String, Any?>()

        val generalSystem = GeneralSystem()
        generalSystem.projectId = projectId
        generalSystem.classCode = mapObj["classCode"].toString()

        for (key in mapObj.keys) {
            /** 判断key是否存在 */
            val containsKey = map.containsKey(key)
            if (containsKey) {
                val value = map[key]
                if (value == opts.TechParam) {
                    /** 技术参数 */
                    val contains = mapCode.containsKey(key)
                    if (contains) {
                        val any = mapCode[key].toString()
                        if (any == "供水") {
                            waterSupply[key] = mapObj[key]
                        } else if (any == "末端") {
                            end[key] = mapObj[key]
                        } else if (any == "水处理") {
                            waterTreatment[key] = mapObj[key]
                        } else if (any == "整体") {
                            whole[key] = mapObj[key]
                        } else if (any == "太阳能") {
                            solarEnergy[key] = mapObj[key]
                        } else if (any == "水质要求") {
                            waterQuality[key] = mapObj[key]
                        } else if (any == "喷泉") {
                            fountain[key] = mapObj[key]
                        } else if (any == "园林") {
                            gardens[key] = mapObj[key]
                        } else if (any == "排水") {
                            drainage[key] = mapObj[key]
                        } else if (any == "排放标准") {
                            emissStand[key] = mapObj[key]
                        } else if (any == "软件厂家") {
                            softwareManu[key] = mapObj[key]
                        } else if (any == "供应购买") {
                            supplyPurchase[key] = mapObj[key]
                        } else if (any == "运行维保") {
                            operationMainte[key] = mapObj[key]
                        } else if (any == "客流探测") {
                            passeFlowDetec[key] = mapObj[key]
                        } else if (any == "软件系统") {
                            softwareSystem[key] = mapObj[key]
                        } else if (any == "终端设备") {
                            terminalEquip[key] = mapObj[key]
                        } else if (any == "网络") {
                            network[key] = mapObj[key]
                        } else if (any == "移动信号放大") {
                            mobileSignal[key] = mapObj[key]
                        } else if (any == "核心层") {
                            coreLayer[key] = mapObj[key]
                        } else if (any == "汇聚层") {
                            distriLayer[key] = mapObj[key]
                        } else if (any == "接入层") {
                            accessLayer[key] = mapObj[key]
                        } else if (any == "无线") {
                            wireless[key] = mapObj[key]
                        } else if (any == "VLAN") {
                            vlan[key] = mapObj[key]
                        } else if (any == "车库出入口") {
                            garageEntraExit[key] = mapObj[key]
                        } else if (any == "车位") {
                            parkingLot[key] = mapObj[key]
                        } else if (any == "寻车") {
                            carSearching[key] = mapObj[key]
                        } else if (any == "功能") {
                            function[key] = mapObj[key]
                        } else if (any == "对讲系统") {
                            intercomSystem[key] = mapObj[key]
                        } else if (any == "电视信号") {
                            tvSignal[key] = mapObj[key]
                        } else if (any == "租户管理") {
                            tenantManag[key] = mapObj[key]
                        } else if (any == "前端") {
                            frontEnd[key] = mapObj[key]
                        } else if (any == "传输") {
                            transmission[key] = mapObj[key]
                        } else if (any == "显示") {
                            display[key] = mapObj[key]
                        } else if (any == "灯具") {
                            lampsLanterns[key] = mapObj[key]
                        } else if (any == "配电柜") {
                            powerCabinet[key] = mapObj[key]
                        } else if (any == "电梯") {
                            elevator[key] = mapObj[key]
                        } else if (any == "柴发机组") {
                            chaiFa[key] = mapObj[key]
                        } else if (any == "排风") {
                            exhaustAir[key] = mapObj[key]
                        } else if (any == "备用电源") {
                            emergencySupply[key] = mapObj[key]
                        } else if (any == "高压") {
                            highPressure[key] = mapObj[key]
                        } else if (any == "低压") {
                            lowPress[key] = mapObj[key]
                        } else if (any == "电源") {
                            powerSupply[key] = mapObj[key]
                        } else if (any == "应急照明") {
                            emerLighting[key] = mapObj[key]
                        } else if (any == "疏散指示") {
                            evacuaInstr[key] = mapObj[key]
                        } else {
                            techParamMap[key] = mapObj[key]
                        }
                    } else {
                        techParamMap[key] = mapObj[key]
                    }
                } else if (value == opts.RunParam) {
                    /** 运行参数 */
                    val contains = mapCode.containsKey(key)
                    if (contains) {
                        val any = mapCode[key].toString()
                        if (any == "瞬时状态") {
                            instanState[key] = mapObj[key]
                        } else if (any == "累计指标") {
                            cumulaIndex[key] = mapObj[key]
                        } else if (any == "瞬时指标") {
                            instanIndex[key] = mapObj[key]
                        } else {
                            runParamMap[key] = mapObj[key]
                        }
                    } else {
                        runParamMap[key] = mapObj[key]
                    }
                } else if (value == opts.SettingParam) {
                    /** 设定参数 */
                    val contains = mapCode.containsKey(key)
                    if (contains) {
                        val any = mapCode[key].toString()
                        if (any == "基本设定参数") {
                            basicSetParam[key] = mapObj[key]
                        } else {
                            settingParamMap[key] = mapObj[key]
                        }
                    } else {
                        settingParamMap[key] = mapObj[key]
                    }
                } else if (value == "运行状态") {
                    runningState[key] = mapObj[key]
                } else if (value == "反馈参数") {
                    feedbackParam[key] = mapObj[key]
                } else {
                    if (key == "name") {
                        generalSystem.name = mapObj[key].toString()
                    } else if (key == "localId") {
                        generalSystem.localId = mapObj[key].toString()
                    } else if (key == "localName") {
                        generalSystem.localName = mapObj[key].toString()
                    } else if (key == "id") {
                        generalSystem.id = mapObj[key].toString()
                    } else if (key == "cadId") {
                        generalSystem.cadId = mapObj[key].toString()
                    } else if (key == "bimId") {
                        generalSystem.bimId = mapObj[key].toString()
                    }else if (key == "qRCodePic") {
                        generalSystem.qRCodePic = mapObj[key].toString()
                    }else if (key == "defaultQRCode") {
                        generalSystem.defaultQRCode = mapObj[key].toString()
                    }
                }
            } else if (key.startsWith("ctm-")) {
                customParamMap[key] = mapObj[key].toString()
            }
        }

        /** 运行参数 */
        if (instanState.size > 0) {
            runParamMap["instanState"] = instanState
        }
        if (cumulaIndex.size > 0) {
            runParamMap["cumulaIndex"] = cumulaIndex
        }
        if (instanIndex.size > 0) {
            runParamMap["instanIndex"] = instanIndex
        }

        /** 技术参数 */
        if (waterSupply.size > 0) {
            techParamMap["waterSupply"] = waterSupply
        }
        if (end.size > 0) {
            techParamMap["end"] = end
        }
        if (whole.size > 0) {
            techParamMap["whole"] = whole
        }
        if (waterTreatment.size > 0) {
            techParamMap["waterTreatment"] = waterTreatment
        }
        if (solarEnergy.size > 0) {
            techParamMap["solarEnergy"] = solarEnergy
        }
        if (waterQuality.size > 0) {
            techParamMap["waterQuality"] = waterQuality
        }
        if (fountain.size > 0) {
            techParamMap["fountain"] = fountain
        }
        if (gardens.size > 0) {
            techParamMap["gardens"] = gardens
        }
        if (drainage.size > 0) {
            techParamMap["drainage"] = drainage
        }

        if (emissStand.size > 0) {
            techParamMap["emissStand"] = emissStand
        }
        if (softwareManu.size > 0) {
            techParamMap["softwareManu"] = softwareManu
        }
        if (supplyPurchase.size > 0) {
            techParamMap["supplyPurchase"] = supplyPurchase
        }
        if (operationMainte.size > 0) {
            techParamMap["operationMainte"] = operationMainte
        }
        if (passeFlowDetec.size > 0) {
            techParamMap["passeFlowDetec"] = passeFlowDetec
        }
        if (softwareSystem.size > 0) {
            techParamMap["softwareSystem"] = softwareSystem
        }
        if (terminalEquip.size > 0) {
            techParamMap["terminalEquip"] = terminalEquip
        }
        if (mobileSignal.size > 0) {
            techParamMap["mobileSignal"] = mobileSignal
        }
        if (network.size > 0) {
            techParamMap["network"] = network
        }
        if (coreLayer.size > 0) {
            techParamMap["coreLayer"] = coreLayer
        }
        if (distriLayer.size > 0) {
            techParamMap["distriLayer"] = distriLayer
        }
        if (accessLayer.size > 0) {
            techParamMap["accessLayer"] = accessLayer
        }
        if (wireless.size > 0) {
            techParamMap["wireless"] = wireless
        }
        if (vlan.size > 0) {
            techParamMap["vlan"] = vlan
        }
        if (garageEntraExit.size > 0) {
            techParamMap["garageEntraExit"] = garageEntraExit
        }
        if (parkingLot.size > 0) {
            techParamMap["parkingLot"] = parkingLot
        }
        if (carSearching.size > 0) {
            techParamMap["carSearching"] = carSearching
        }
        if (function.size > 0) {
            techParamMap["function"] = function
        }
        if (intercomSystem.size > 0) {
            techParamMap["intercomSystem"] = intercomSystem
        }
        if (tvSignal.size > 0) {
            techParamMap["tvSignal"] = tvSignal
        }
        if (tenantManag.size > 0) {
            techParamMap["tenantManag"] = tenantManag
        }
        if (frontEnd.size > 0) {
            techParamMap["frontEnd"] = frontEnd
        }
        if (transmission.size > 0) {
            techParamMap["transmission"] = transmission
        }
        if (display.size > 0) {
            techParamMap["display"] = display
        }
        if (lampsLanterns.size > 0) {
            techParamMap["lampsLanterns"] = lampsLanterns
        }
        if (powerCabinet.size > 0) {
            techParamMap["powerCabinet"] = powerCabinet
        }
        if (elevator.size > 0) {
            techParamMap["elevator"] = elevator
        }
        if (chaiFa.size > 0) {
            techParamMap["chaiFa"] = chaiFa
        }
        if (exhaustAir.size > 0) {
            techParamMap["exhaustAir"] = exhaustAir
        }
        if (emergencySupply.size > 0) {
            techParamMap["emergencySupply"] = emergencySupply
        }
        if (highPressure.size > 0) {
            techParamMap["highPressure"] = highPressure
        }
        if (lowPress.size > 0) {
            techParamMap["lowPress"] = lowPress
        }
        if (powerSupply.size > 0) {
            techParamMap["powerSupply"] = powerSupply
        }
        if (emerLighting.size > 0) {
            techParamMap["emerLighting"] = emerLighting
        }
        if (evacuaInstr.size > 0) {
            techParamMap["evacuaInstr"] = evacuaInstr
        }
        /** 设定参数 */
        if (basicSetParam.size > 0) {
            settingParamMap["basicSetParam"] = basicSetParam
        }

        if (!techParamMap.isNullOrEmpty()){
            generalSystem.techParam = techParamMap
        }
        if (!runParamMap.isNullOrEmpty()){
            generalSystem.runParam = runParamMap
        }
        if (!settingParamMap.isNullOrEmpty()){
            generalSystem.settingParam = settingParamMap
        }
        if (!customParamMap.isNullOrEmpty()){
            generalSystem.customParam = customParamMap
        }
        if (!runningState.isNullOrEmpty()){
            generalSystem.runningState = runningState
        }
        if (!feedbackParam.isNullOrEmpty()){
            generalSystem.feedbackParam = feedbackParam
        }

        return  generalSystem
    }

    /**
     * 系统转换工具
     *
     * @param projectId     项目 id
     * @param groupCode     集团编码
     * @param mapObj        中台数据对象
     * @return 返回结果
     */
    fun systemDataConversions(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): GeneralSystem {
        /** 查建楼层型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), 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
        }

        val generalSystem = GeneralSystem()
        generalSystem.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!="cadId"&&key!="bimId"){
                    val containsKey1 = mapCode.containsKey(key)
                    if (containsKey1){
                        val any = mapCode[key].toString()
                        if (any == "STATIC"){
                            when(mapEx[key].toString()) {
                                "INTEGER" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toInt()
                                }
                                "DOUBLE" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString().toDouble()
                                }
                                "BOOLEAN" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString().toBoolean()
                                }
                                "STRING" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString()
                                }
                                "DATETIME" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ATTACHMENT" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString()
                                }
                                "OBJECT" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ENUM" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString()
                                }
                                "MENUM" -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString()
                                }
                                else -> {
                                    if (generalSystem.infos.isNullOrEmpty()){
                                        generalSystem.infos = HashMap<String,Any?>()
                                    }
                                    generalSystem.infos!![key] = mapObj[key]!!.toString()
                                }
                            }
                        }else{
                            if (generalSystem.infos.isNullOrEmpty()){
                                generalSystem.infos = HashMap<String,Any?>()
                            }
                            generalSystem.infos!![key] = mapObj[key]!!.toString()
                        }
                    }

                } else {
                    when(key) {
                        "id" -> generalSystem.id =  mapObj[key]!!.toString()
                        "name" -> generalSystem.name =  mapObj[key]!!.toString()
                        "localId" -> generalSystem.localId =  mapObj[key]!!.toString()
                        "localName" -> generalSystem.localName =  mapObj[key]!!.toString()
                        "defaultQRCode" -> generalSystem.defaultQRCode =  mapObj[key]!!.toString()
                        "qRCodePic" -> generalSystem.qRCodePic =  mapObj[key]!!.toString()
                        "cadId" -> generalSystem.cadId =  mapObj[key]!!.toString()
                        "bimId" -> generalSystem.bimId =  mapObj[key]!!.toString()
                    }
                }

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

        return generalSystem
    }

    /**
     * 系统工具
     *
     * @param projectId         项目 id
     * @param generalSystem     系统对象
     * @return 返回结果
     */
    fun systemTools(projectId: String,generalSystem: GeneralSystem): ArrayList<HashMap<String,Any?>> {
        var list = ArrayList<HashMap<String,Any?>>()
        val hashMap = HashMap<String, Any?>()
        hashMap["id"] = generalSystem.id
        hashMap["name"] = generalSystem.name
        hashMap["projectId"] = projectId
        hashMap["objType"] = "generalSystem"
        hashMap["classCode"] = generalSystem.classCode
        if (!generalSystem.localName.isNullOrEmpty()){
            hashMap["localName"] = generalSystem.localName
        }
        if (!generalSystem.localId.isNullOrEmpty()){
            hashMap["localId"] = generalSystem.localId
        }
        if (!generalSystem.defaultQRCode.isNullOrEmpty()){
            hashMap["defaultQRCode"] = generalSystem.defaultQRCode
        }
        if (!generalSystem.qRCodePic.isNullOrEmpty()){
            hashMap["qRCodePic"] = generalSystem.qRCodePic
        }
        if (!generalSystem.cadId.isNullOrEmpty()){
            hashMap["cadId"] = generalSystem.cadId
        }
        if (!generalSystem.bimId.isNullOrEmpty()){
            hashMap["bimId"] = generalSystem.bimId
        }
        if (!generalSystem.infos.isNullOrEmpty()){
            for (key in generalSystem.infos!!.keys){
                hashMap[key] = generalSystem.infos!![key]
            }
        }

        list.add(hashMap)
        return list
    }



}