/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

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

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.Building
import com.persagy.server.syn.Opts
import com.persagy.service.SObjectService
import com.persagy.service.utils.SSpringContextUtil

/**
 * 建筑信息转换 - 中台转数据中心
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/19 9:43
 */
object BuildingUtil {
    /** 信息点 */
    private val dictDefFuncIdService = SObjectService(SMybatisDao(DefFuncId::class.java))
    /** 配置 */
    private val opts: Opts by lazy {
        SSpringContextUtil.getBean(Opts::class.java) as Opts
    }

//    /**
//     * 中台建筑转数据中心
//     */
//    fun buildingDataConversion(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Building {
//        /** 查建筑类型字典 */
//        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", "building")).exec()
//        val mapEx = HashMap<String, Any?>()
//        val mapCode = HashMap<String, Any?>()
//
//        for (explainProject in dictDefFuncIdList) {
//            mapEx[explainProject.code!!] = explainProject.firstName
//            if (!explainProject.secondName.isNullOrEmpty()) {
//                mapCode[explainProject.code!!] = explainProject.secondName
//            }
//        }
//
//        /** 建筑信息 */
//        val buildingInfo = HashMap<String, Any?>()
//        /** 工作历 */
//        val schedule = HashMap<String, Any?>()
//        /** 人员信息 */
//        val peopleInfo = HashMap<String, Any?>()
//        /** 能耗信息 */
//        val consumptionInfo = HashMap<String, Any?>()
//        /** 自定义 */
//        val customParam = HashMap<String, Any?>()
//        /** 建筑文档 */
//        val buildingDoc = HashMap<String, Any?>()
//
//        /** 建筑类型 */
//        var buildingType = HashMap<String, Any?>()
//        /** 设计指标 */
//        var designIndex = HashMap<String, Any?>()
//        /** 结构参数 */
//        var structureParam = HashMap<String, Any?>()
//        /** 面积信息 */
//        var areaInfo = HashMap<String, Any?>()
//        /** 人员信息 */
//        val peopleInfoTwo = HashMap<String, Any?>()
//
//        /** 工作作息 */
//        var workRest = HashMap<String, Any?>()
//        /** 季节 */
//        var season = HashMap<String, Any?>()
//        /** 工作历信息 */
//        var workCalendar = HashMap<String, Any?>()
//
//        /** 逐时 */
//        var hourly = HashMap<String, Any?>()
//        /** 累计 */
//        var cumulative = HashMap<String, Any?>()
//
//        /** 建筑对象 */
//        val building = Building()
//        building.projectId = projectId
//        /** id 是否存在 */
//        val containsKeyId = mapObj.containsKey("id")
//        if(containsKeyId){
//            building.id = mapObj["id"].toString()
//        }
//        /** name 是否存在 */
//        val containsKeyName = mapObj.containsKey("name")
//        if(containsKeyName){
//            building.name = mapObj["name"].toString()
//        }
//
//        for (key in mapObj!!.keys) {
//            val containsKey = mapEx.containsKey(key)
//            if (containsKey) {
//                val value = mapEx[key]
//                if (value == opts.BuildingInfo) {
//                    /** 建筑信息 */
//                    val contains = mapCode.containsKey(key)
//                    if (contains) {
//                        val any = mapCode[key].toString()
//                        if (any == "建筑类型") {
//
//                            /** 安装图纸 安装照片 安装质检报告 */
//                            if (key == "Pic" ){
//                                try {
//                                    val gson = Gson()
//                                    val type = object : TypeToken<ArrayList<AttPic>>() {
//                                    }.type
//                                    val attPicList = gson.fromJson<ArrayList<AttPic>>(mapObj[key].toString(),type)
//                                    if (attPicList.isNotEmpty()){
//                                        val listPic =ArrayList<TaskPic>()
//                                        for (attPic in attPicList){
//                                            val taskPic = TaskPic()
//                                            taskPic.key = attPic.key
//                                            taskPic.name = attPic.name
//                                            taskPic.systemId = attPic.systemId
//                                            taskPic.type = attPic.type
//                                            listPic.add(taskPic)
//                                        }
//                                        buildingType[key] = listPic
//                                    }
//                                } catch (e: Exception) {
//                                    e.printStackTrace()
//                                }
//                            }else{
//                                buildingType[key] = mapObj[key]
//                            }
//                        } else if (any == "设计指标") {
//                            designIndex[key] = mapObj[key]
//                        } else if (any == "结构参数") {
//                            structureParam[key] = mapObj[key]
//                        } else if (any == "面积信息") {
//                            areaInfo[key] = mapObj[key]
//                        } else if(any == "人员信息"){
//                            peopleInfo[key]= mapObj[key]
//                        }else {
//                            buildingInfo[key] = mapObj[key]
//                        }
//                    } else {
//                        buildingInfo[key] = mapObj[key]
//                    }
//                } else if (value == opts.Schedule) {
//                    /** 工作历 */
//                    val contains = mapCode.containsKey(key)
//                    if (contains) {
//                        val any = mapCode[key].toString()
//                        if (any == "工作作息") {
//                            workRest[key] = mapObj[key]
//                        } else if (any == "季节") {
//                            season[key] = mapObj[key]
//                        } else if (any == "工作历信息") {
//                            workCalendar[key] = mapObj[key]
//                        } else {
//                            schedule[key] = mapObj[key]
//                        }
//                    } else {
//                        schedule[key] = mapObj[key]
//                    }
////
//                } else if (value == opts.PeopleInfo) {
//                    /** 人员信息 */
//                    peopleInfo[key] = mapObj[key]
//                } else if (value == opts.ConsumptionInfo) {
//                    /** 能耗信息 */
//                    val contains = mapCode.containsKey(key)
//                    if (contains) {
//                        val any = mapCode[key].toString()
//                        if (any == "逐时") {
//                            hourly[key] = mapObj[key]
//                        } else if (any == "累计") {
//                            cumulative[key] = mapObj[key]
//                        } else {
//                            consumptionInfo[key] = mapObj[key]
//                        }
//                    } else {
//                        consumptionInfo[key] = mapObj[key]
//                    }
//                } else if (value == "建筑文档") {
//
//                    if (key == "drawing" ){
//                        try {
//                            val gson = Gson()
//                            val type = object : TypeToken<ArrayList<AttPic>>() {
//                            }.type
//                            val attPicList = gson.fromJson<ArrayList<AttPic>>(mapObj[key].toString(),type)
//                            if (attPicList.isNotEmpty()){
//                                val listPic =ArrayList<TaskPic>()
//                                for (attPic in attPicList){
//                                    val taskPic = TaskPic()
//                                    taskPic.key = attPic.key
//                                    taskPic.name = attPic.name
//                                    taskPic.systemId = attPic.systemId
//                                    taskPic.type = attPic.type
//                                    listPic.add(taskPic)
//                                }
//                                buildingType[key] = listPic
//                            }
//                        } catch (e: Exception) {
//                            e.printStackTrace()
//                        }
//                    }else if (key == "archive"){
//                        try {
//                            val gson = Gson()
//                            val type = object : TypeToken<ArrayList<AttPic>>() {
//                            }.type
//                            val attPicList = gson.fromJson<ArrayList<AttPic>>(mapObj[key].toString(),type)
//                            if (attPicList.isNotEmpty()){
//                                val listPic =ArrayList<TaskPic>()
//                                for (attPic in attPicList){
//                                    val taskPic = TaskPic()
//                                    taskPic.key = attPic.key
//                                    taskPic.name = attPic.name
//                                    taskPic.systemId = attPic.systemId
//                                    taskPic.type = attPic.type
//                                    listPic.add(taskPic)
//                                }
//                                buildingType[key] = listPic
//                            }
//                        } catch (e: Exception) {
//                            e.printStackTrace()
//                        }
//                    }else if (key == "simulationModel"){
//                        try {
//                            val gson = Gson()
//                            val type = object : TypeToken<ArrayList<AttPic>>() {
//                            }.type
//                            val attPicList = gson.fromJson<ArrayList<AttPic>>(mapObj[key].toString(),type)
//                            if (attPicList.isNotEmpty()){
//                                val listPic =ArrayList<TaskPic>()
//                                for (attPic in attPicList){
//                                    val taskPic = TaskPic()
//                                    taskPic.key = attPic.key
//                                    taskPic.name = attPic.name
//                                    taskPic.systemId = attPic.systemId
//                                    taskPic.type = attPic.type
//                                    listPic.add(taskPic)
//                                }
//                                buildingType[key] = listPic
//                            }
//                        } catch (e: Exception) {
//                            e.printStackTrace()
//                        }
//                    }else{
//                        buildingDoc[key] = mapObj[key]
//                    }
//
//                } else {
//                    when (key) {
//                        "localID" -> building.localId = mapObj[key].toString()
//                        "localName" -> building.localName = mapObj[key].toString()
//                        "defaultQRCode" -> building.defaultQRCode.toString()
//                        "buildQRCode" -> building.buildQRCode.toString()
//                    }
//                }
//            }
//        }
//
//        if (buildingType.size > 0) {
//            buildingInfo.put("BuildingType", buildingType)
//        }
//        if (designIndex.size > 0) {
//            buildingInfo.put("DesignIndex", designIndex)
//        }
//        if (structureParam.size > 0) {
//            buildingInfo.put("StructureParam", structureParam)
//        }
//        if (areaInfo.size > 0) {
//            buildingInfo.put("AreaInfo", areaInfo)
//        }
//        if (peopleInfoTwo.size>0){
//            buildingInfo.put("PeopleInfo", peopleInfoTwo)
//        }
//
//        if (workRest.size > 0) {
//            schedule.put("WorkRest", workRest)
//        }
//        if (season.size > 0) {
//            schedule.put("Season", season)
//        }
//
//        if (hourly.size > 0) {
//            consumptionInfo.put("Hourly", hourly)
//        }
//        if (cumulative.size > 0) {
//            consumptionInfo.put("Cumulative", cumulative)
//        }
//
//        if (workCalendar.size > 0) {
//            schedule.put("WorkCalendar", workCalendar)
//        }
//
//        if (buildingDoc.size > 0) {
//            building.buildingDoc = buildingDoc
//        }
//
//        if (buildingInfo.size > 0) {
//            building.buildingInfo = buildingInfo
//        }
//
//        if (schedule.size > 0) {
//            building.schedule = schedule
//        }
//
//        if (peopleInfo.size > 0) {
//            building.peopleInfo = peopleInfo
//        }
//        if (consumptionInfo.size > 0) {
//            building.consumptionInfo = consumptionInfo
//        }
//
//        if (customParam.size > 0) {
//            building.customParam = customParam
//        }
//
//        return building
//    }

    /**
     * 建筑
     *
     * @param projectId     项目 id
     * @param groupCode     集团编码
     * @param mapObj        对象信息
     * @return  建筑对象
     */
    fun buildingDataConversions(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Building {
        /** 查建筑类型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", "building")).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 building = Building()
        building.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!="buildQRCode"){
                    val containsKey1 = mapCode.containsKey(key)
                    if (containsKey1){
                        val any = mapCode[key].toString()
                        if (any == "STATIC"){
                            when(mapEx[key].toString()) {
                                "INTEGER" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toInt()
                                }
                                "DOUBLE" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString().toDoubleOrNull()
                                }
                                "BOOLEAN" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString().toBoolean()
                                }
                                "STRING" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString()
                                }
                                "DATETIME" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ATTACHMENT" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString()
                                }
                                "OBJECT" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ENUM" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString()
                                }
                                "MENUM" -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString()
                                }
                                else -> {
                                    if (building.infos.isNullOrEmpty()){
                                        building.infos = HashMap<String,Any?>()
                                    }
                                    building.infos!![key] = mapObj[key]!!.toString()
                                }
                            }
                        }else{
                            if (building.infos.isNullOrEmpty()){
                                building.infos = HashMap<String,Any?>()
                            }
                            building.infos!![key] = mapObj[key]!!.toString()
                        }
                    }

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

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

        return building
    }

    /**
     * 建筑工具
     *
     * @param projectId     项目 id
     * @param building      建筑对象
     * @return 返回对象
     */
    fun buildingTools(projectId: String,building: Building): ArrayList<HashMap<String,Any?>> {
        var list = ArrayList<HashMap<String,Any?>>()
        val hashMap = HashMap<String, Any?>()
        hashMap["id"] = building.id
        hashMap["name"] = building.name
        hashMap["projectId"] = projectId
//        hashMap["objType"] = "building"
//        hashMap["classCode"] = "building"
        if (!building.localName.isNullOrEmpty()){
            hashMap["localName"] = building.localName
        }
        if (!building.localId.isNullOrEmpty()){
            hashMap["localId"] = building.localId
        }
        if (!building.defaultQRCode.isNullOrEmpty()){
            hashMap["defaultQRCode"] = building.defaultQRCode
        }
        if (!building.buildQRCode.isNullOrEmpty()){
            hashMap["buildQRCode"] = building.buildQRCode
        }
       if (!building.infos.isNullOrEmpty()){
           for (key in building.infos!!.keys){
               hashMap[key] = building.infos!![key]
           }
       }

        list.add(hashMap)
        return list
    }


}