/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.google.gson.Gson
import com.google.gson.reflect.TypeToken
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.assistant.PointPosition
import com.persagy.server.datacenter.models.entities.dictnew.DefFuncId
import com.persagy.server.datacenter.wanda.obj.Floor
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 15:01
 */
object FloorUtil {
    /** 信息点 */
    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 floorSyns(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Floor {
        /** 查建楼层型字典 */
        var dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode),
            SFilter.eq("classCode", "floor"),SFilter.eq("projectId", projectId)).exec()
        if (dictDefFuncIdList.isNullOrEmpty()) {
            dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("type", "commo"),
                SFilter.eq("classCode","floor")).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 floor = Floor()
        floor.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!="bimIdPre"&&key!="bimId"&&key!="modelId"&&key!="bimLocation"
                    &&key!="outline"&&key!="floorSequenceId"&&key!="permanentPeopleNum"&&key!="floorIdentity"&&key!="floorSequenceID"
                ){
                    val containsKey1 = mapCode.containsKey(key)
                    if (containsKey1){
                        val any = mapCode[key].toString()
                        if (any == "STATIC"){
                            when(mapEx[key].toString()) {
                                "INTEGER" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toInt()
                                }
                                "DOUBLE" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString().toDoubleOrNull()
                                }
                                "BOOLEAN" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                                "STRING" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]
                                }
                                "DATETIME" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]
                                }
                                "ATTACHMENT" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]
                                }
                                "OBJECT" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]
                                }
                                "ENUM" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]
                                }
                                "MENUM" -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]
                                }
                                else -> {
                                    if (floor.infos.isNullOrEmpty()){
                                        floor.infos = HashMap<String,Any?>()
                                    }
                                    floor.infos!![key] = mapObj[key]!!.toString()
                                }
                            }
                        }else{
                            if (floor.infos.isNullOrEmpty()){
                                floor.infos = HashMap<String,Any?>()
                            }
                            floor.infos!![key] = mapObj[key]!!.toString()
                        }
                    }

                } else {
                    when(key) {
                        "id" -> floor.id =  mapObj[key]!!.toString()
                        "name" -> floor.name =  mapObj[key]!!.toString()
                        "localId" -> floor.localId =  mapObj[key]!!.toString()
                        "localName" -> floor.localName =  mapObj[key]!!.toString()
                        "defaultQRCode" -> floor.defaultQRCode =  mapObj[key]!!.toString()
                        "qRCodePic" -> floor.qRCodePic =  mapObj[key]!!.toString()
                        "bimIpPre" -> floor.bimIdPre =  mapObj[key]!!.toString()
                        "bimId" -> floor.bimId =  mapObj[key]!!.toString()
                        "modelId" -> floor.modelId =  mapObj[key]!!.toString()
                        "bimLocation" -> floor.bimLocation =  mapObj[key]!!.toString()
                        "floorSequenceId" -> floor.floorSequenceId =  mapObj[key]!!.toString().toInt()
                        "floorSequenceID" -> floor.floorSequenceId =  mapObj[key]!!.toString().toInt()
                        "permanentPeopleNum" -> floor.permanentPeopleNum =  mapObj[key]!!.toString().toInt()
                        "floorIdentity" -> floor.floorIdentity =  mapObj[key]!!.toString()
                        "outline" -> {
                            val replace1 = mapObj[key].toString().replace("X", "x")
                            val replace2 = replace1.replace("Y", "y")
                            val replace3 = replace2.replace("Z", "z")
                            val type = object : TypeToken<ArrayList<PointPosition>>() {
                            }.getType()
                            var outline=  Gson().fromJson<ArrayList<PointPosition>>(replace3,type)
                            floor.outline = outline
                        }
                    }
                }

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

        return floor
    }

    /**
     * 楼层工具
     *
     * @param projectId     项目 id
     * @param floor         楼层对象
     * @return 返回结果
     */
    fun floorTools(projectId: String,floor: Floor): ArrayList<HashMap<String,Any?>> {
        var list = ArrayList<HashMap<String,Any?>>()
        val hashMap = HashMap<String, Any?>()
        hashMap["id"] = floor.id
        hashMap["name"] = floor.name
        hashMap["projectId"] = projectId
        hashMap["objType"] = "floor"
        hashMap["classCode"] = "floor"
        hashMap["floorSequenceId"] = floor.floorSequenceId
        hashMap["floorSequenceID"] = floor.floorSequenceId
        if (!floor.localName.isNullOrEmpty()){
            hashMap["localName"] = floor.localName
        }
        if (!floor.localId.isNullOrEmpty()){
            hashMap["localId"] = floor.localId
        }
        if (!floor.defaultQRCode.isNullOrEmpty()){
            hashMap["defaultQRCode"] = floor.defaultQRCode
        }
        if (!floor.qRCodePic.isNullOrEmpty()){
            hashMap["qRCodePic"] = floor.qRCodePic
        }
        if (!floor.bimIdPre.isNullOrEmpty()){
            hashMap["bimIdPre"] = floor.bimIdPre
        }
        if (!floor.modelId.isNullOrEmpty()){
            hashMap["modelId"] = floor.modelId
        }
        if (!floor.bimId.isNullOrEmpty()){
            hashMap["bimId"] = floor.bimId
        }
        if (!floor.bimLocation.isNullOrEmpty()){
            hashMap["bimLocation"] = floor.bimLocation
        }
        if (!floor.outline.isNullOrEmpty()){
            hashMap["outline"] = floor.outline!!.toJson()
        }
        if (floor.permanentPeopleNum!=null&&floor.permanentPeopleNum!=0){
            hashMap["permanentPeopleNum"] = floor.permanentPeopleNum
        }
        if (!floor.floorIdentity.isNullOrEmpty()){
            hashMap["floorIdentity"] = floor.floorIdentity
        }
        if (!floor.infos.isNullOrEmpty()){
            for (key in floor.infos!!.keys){
                hashMap[key] = floor.infos!![key]
            }
        }

        list.add(hashMap)
        return list
    }


}