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.Shaft
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 20:22
 */
object ShaftUtil {

    /** 日志 */
    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 shaftDataConversion(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Shaft {

        /** 查建楼层型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", "shaft")).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 settingParam = HashMap<String, Any?>()
        /** 基本设定参数 */
        val basicSetParam = HashMap<String, Any?>()

        val shaft = Shaft()
        shaft.projectId = projectId
        for (key in mapObj.keys) {
            val containsKey = mapEx.containsKey(key)
            if (containsKey) {
                val value = mapEx[key]
                if (value == "设定参数") {
                    /** 通讯参数 */
                    val contains = mapCode.containsKey(key)
                    if (contains) {
                        val any = mapCode[key].toString()
                        if (any == "基本设定参数"){
                            basicSetParam[key] = mapObj[key]
                        }
                    } else {
                        /** 结构信息 */
                        settingParam[key] = mapObj[key]
                    }
                } else {
                    when (key) {
                        "name" -> shaft.name = mapObj[key].toString()
                        "localID" -> shaft.localId = mapObj[key].toString()
                        "localName" -> shaft.localName = mapObj[key].toString()
                        "bimId" -> shaft.bimId = mapObj[key].toString()
                        "id" -> shaft.id = mapObj[key].toString()
                        "cadId" -> shaft.cadId = mapObj[key].toString()
                        "qRCodePic" -> shaft.qRCodePic = mapObj[key].toString()
                        "defaultQRCode" -> shaft.defaultQRCode = mapObj[key].toString()
                    }
                }

            }
//            else if (key.startsWith("ctm-")) {
//                /** 自定义 */
//                customParam[key] = infos[key]
//            }
        }

        if (!basicSetParam.isNullOrEmpty()){
            settingParam["basicSetParam"] = basicSetParam
        }
        if (!settingParam.isNullOrEmpty()){
            shaft.settingParam = settingParam
        }

        return shaft
    }

    /**
     * 竖井
     *
     * @param projectId     项目id
     * @param groupCode     集团编码
     * @param mapObj        对象信息
     * @return 竖井对象
     */
    fun shaftDataConversions(projectId: String,groupCode: String, mapObj: HashMap<String, Any?>): Shaft {
        /** 查建楼层型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode","shaft")).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 shaft = Shaft()
        shaft.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 (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toInt()
                                }
                                "DOUBLE" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString().toDouble()
                                }
                                "BOOLEAN" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString().toBoolean()
                                }
                                "STRING" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString()
                                }
                                "DATETIME" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ATTACHMENT" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString()
                                }
                                "OBJECT" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ENUM" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString()
                                }
                                "MENUM" -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString()
                                }
                                else -> {
                                    if (shaft.infos.isNullOrEmpty()){
                                        shaft.infos = HashMap<String,Any?>()
                                    }
                                    shaft.infos!![key] = mapObj[key]!!.toString()
                                }
                            }
                        }else{
                            if (shaft.infos.isNullOrEmpty()){
                                shaft.infos = HashMap<String,Any?>()
                            }
                            shaft.infos!![key] = mapObj[key]!!.toString()
                        }
                    }

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

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

        return shaft
    }

    /**
     * 竖井工具
     *
     * @param projectId     项目 id
     * @param shaft         竖井对象
     * @return 返回结果
     */
    fun shaftTools(projectId: String,shaft: Shaft): ArrayList<HashMap<String,Any?>> {
        var list = ArrayList<HashMap<String,Any?>>()
        val hashMap = HashMap<String, Any?>()
        hashMap["id"] = shaft.id
        hashMap["name"] = shaft.name
        hashMap["projectId"] = projectId
        hashMap["objType"] = "shaft"
        hashMap["classCode"] = "shaft"
        if (!shaft.localName.isNullOrEmpty()){
            hashMap["localName"] = shaft.localName
        }
        if (!shaft.localId.isNullOrEmpty()){
            hashMap["localId"] = shaft.localId
        }
        if (!shaft.defaultQRCode.isNullOrEmpty()){
            hashMap["defaultQRCode"] = shaft.defaultQRCode
        }
        if (!shaft.qRCodePic.isNullOrEmpty()){
            hashMap["qRCodePic"] = shaft.qRCodePic
        }
        if (!shaft.bimId.isNullOrEmpty()){
            hashMap["bimId"] = shaft.bimId
        }
        if (!shaft.bimLocation.isNullOrEmpty()){
            hashMap["bimLocation"] = shaft.bimLocation
        }
        if (!shaft.outline.isNullOrEmpty()){
            hashMap["outline"] = shaft.outline.toString()
        }
        if (shaft.shaftSequenceId!=null&&shaft.shaftSequenceId!=0){
            hashMap["shaftSequenceId"] = shaft.shaftSequenceId
        }
        if (!shaft.infos.isNullOrEmpty()){
            for (key in shaft.infos!!.keys){
                hashMap[key] = shaft.infos!![key]
            }
        }

        list.add(hashMap)
        return list
    }


}