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.dictnew.Group
import com.persagy.server.datacenter.models.entities.objects.Project
import com.persagy.server.syn.Opts
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory

/**
 * 中台数据转换为数据中心，项目
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/18 19:03
 */
object ProjectUtil {
    /** 日志 */
    private val logger = LoggerFactory.getLogger(ProjectUtil::class.java)

    /** 集团服务 */
    private val groupService = SBaseService(SMybatisDao(Group::class.java))
    /** 信息点 */
    private val dictDefFuncIdService = SObjectService(SMybatisDao(DefFuncId::class.java))
    /** 配置 */
    private val opts: Opts by lazy {
        SSpringContextUtil.getBean(Opts::class.java) as Opts
    }

    /**
     * 项目
     *
     * @param groupCode     集团编码
     * @param mapObj        中台信息点
     * @return 返回项目对象
     */
    fun projectDataConversion(groupCode: String, mapObj: HashMap<String, Any?>): Project {

        /** 查询项目类型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", "project")).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 project = Project()
        /** id 是否存在 */
        val containsKeyId = mapObj.containsKey("id")
        if(containsKeyId){
            project.id = mapObj["id"].toString()
        }
        /** name 是否存在 */
        val containsKeyName = mapObj.containsKey("name")
        if(containsKeyName){
            project.name = mapObj["name"].toString()
        }

        /** 地理位置 */
        val geoInfoParamMap = HashMap<String, Any?>()
        /** 项目信息 */
        val projectInfoMap = HashMap<String, Any?>()
        /** 商业信息 */
        val commercialInfoMap = HashMap<String, Any?>()
        /** 能源价格 */
        val energyCostMap = HashMap<String, Any?>()
        /** 室外环境 */
        val outdoorEnvMap = HashMap<String, Any?>()
        /** 天气预报 */
        val weatherMap = HashMap<String, Any?>()
        /** 能耗信息 */
        val consumptionInfoMap = HashMap<String, Any?>()
        /** 自定义参数 */
        val customParamMap = HashMap<String, Any?>()

        /** 电 */
        val electricMap = HashMap<String, Any?>()
        /** 燃气 */
        val gasMap = HashMap<String, Any?>()
        /** 燃油 */
        val fuelMap = HashMap<String, Any?>()
        /** 自来水 */
        val tapWaterMap = HashMap<String, Any?>()
        /** 供热 */
        val heatSupplyMap = HashMap<String, Any?>()
        /** 供冷 */
        val coolingMap = HashMap<String, Any?>()
        /** 生活热水 */
        val domesticHotWaterMap = HashMap<String, Any?>()
        /** 蒸汽 */
        val steamMap = HashMap<String, Any?>()

        /** 未来第1天 */
        val dayOneMap = HashMap<String, Any?>()
        /** 未来第2天 */
        val dayTwoMap = HashMap<String, Any?>()
        /** 未来第3天 */
        val dayThreeMap = HashMap<String, Any?>()

        /** 逐时 */
        val hourlyMap = HashMap<String, Any?>()
        /** 累计 */
        val cumulativeMap = HashMap<String, Any?>()

        for (key in mapObj.keys) {

            val containsKey = mapEx.containsKey(key)
            if (containsKey) {
                val value = mapEx[key]
                logger.debug("value = ${value}")
                if (value == opts.GeoInfo) {
                    /** 地理位置 */
                    geoInfoParamMap[key] = mapObj[key]
                } else if (value == opts.ProjectInfo) {
                    /** 项目信息 */
                    projectInfoMap[key] = mapObj[key]
                } else if (value == opts.CommercialInfo) {
                    /** 商业信息 */
                    commercialInfoMap[key] = mapObj[key]
                } else if (value == opts.EnergyCost) {
                    /** 能源价格 */
                    val contains = mapCode.containsKey(key)
                    if (contains) {
                        val any = mapCode[key].toString()
                        if (any == "电") {
                            electricMap[key] = mapObj[key]
                        } else if (any == "燃气") {
                            gasMap[key] = mapObj[key]
                        } else if (any == "燃油") {
                            fuelMap[key] = mapObj[key]
                        } else if (any == "自来水") {
                            tapWaterMap[key] = mapObj[key]
                        } else if (any == "供热") {
                            heatSupplyMap[key] = mapObj[key]
                        } else if (any == "供冷") {
                            coolingMap[key] = mapObj[key]
                        } else if (any == "生活热水") {
                            domesticHotWaterMap[key] = mapObj[key]
                        } else if (any == "蒸汽") {
                            steamMap[key] = mapObj[key]
                        } else {
                            energyCostMap[key] = mapObj[key]
                        }
                    } else {
                        energyCostMap[key] = mapObj[key]
                    }
                } else if (value == opts.OutdoorEnv) {
                    /** 室外环境 */
                    outdoorEnvMap[key] = mapObj[key]
                } else if (value == opts.Weather) {
                    /** 天气预报 */

                    val contains = mapCode.containsKey(key)
                    if (contains) {
                        val any = mapCode[key].toString()
                        if (any == "未来第1天") {
                            dayOneMap[key] = mapObj[key]
                        } else if (any == "未来第2天") {
                            dayTwoMap[key] = mapObj[key]
                        } else if (any == "未来第3天") {
                            dayThreeMap[key] = mapObj[key]
                        } else {
                            weatherMap[key] = mapObj[key]
                        }
                    } else {
                        weatherMap[key] = mapObj[key]
                    }
                } else if (value == opts.ConsumptionInfo) {
                    /** 能耗信息 */
                    val contains = mapCode.containsKey(key)
                    if (contains) {
                        val any = mapCode[key].toString()
                        if (any == "逐时") {
                            hourlyMap[key] = mapObj[key]
                        } else if (any == "累计") {
                            cumulativeMap[key] = mapObj[key]
                        } else {
                            consumptionInfoMap[key] = mapObj[key]
                        }
                    } else {
                        consumptionInfoMap[key] = mapObj[key]
                    }
                } else {
                    when (key) {
//                        "ProjName" -> project.name = projectInfo[key].toString()
//                        "ProjLocalID" -> project.localId = projectInfo[key].toString()
//                        "ProjLocalName" -> project.localName = projectInfo[key].toString()
//                        "BIMID" -> project.bimId = projectInfo[key].toString()
                          "introduction" -> project.introduction = mapObj[key].toString()
                          "businessFloorNum" -> project.businessFloorNum = mapObj[key].toString()
                          "officeFloorNum" -> project.officeFloorNum = mapObj[key].toString()
                          "localId" -> project.localId = mapObj[key].toString()
                          "localName" -> project.localName = mapObj[key].toString()
                    }
                }

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


        if (electricMap.size > 0) {
            energyCostMap.put("electric", electricMap)
        }
        if (gasMap.size > 0) {
            energyCostMap.put("gas", gasMap)
        }
        if (fuelMap.size > 0) {
            energyCostMap.put("fuel", fuelMap)
        }
        if (tapWaterMap.size > 0) {
            energyCostMap.put("tapWater", tapWaterMap)
        }
        if (heatSupplyMap.size > 0) {
            energyCostMap.put("heatSupply", heatSupplyMap)
        }
        if (domesticHotWaterMap.size > 0) {
            energyCostMap.put("domesticHotWater", domesticHotWaterMap)
        }
        if (coolingMap.size > 0) {
            energyCostMap.put("cooling", coolingMap)
        }
        if (steamMap.size > 0) {
            energyCostMap.put("steam", steamMap)
        }
        if (dayOneMap.size > 0) {
            weatherMap.put("dayOne", dayOneMap)
        }
        if (dayTwoMap.size > 0) {
            weatherMap.put("dayTwo", dayTwoMap)
        }
        if (dayThreeMap.size > 0) {
            weatherMap.put("dayThreeMap", dayThreeMap)
        }
        if (hourlyMap.size > 0) {
            consumptionInfoMap.put("hourly", hourlyMap)
        }
        if (cumulativeMap.size > 0) {
            consumptionInfoMap.put("cumulative", cumulativeMap)
        }
        if (commercialInfoMap.size>0){
            project.commercialInfo = commercialInfoMap
        }
        if (consumptionInfoMap.size>0){
            project.consumptionInfo = consumptionInfoMap
        }
        if (energyCostMap.size>0){
            project.energyCost = energyCostMap
        }
        if (geoInfoParamMap.size>0){
            project.geoInfo = geoInfoParamMap
        }
        if (outdoorEnvMap.size>0){
            project.outdoorEnv = outdoorEnvMap
        }
        if (projectInfoMap.size>0){
            project.projectInfo = projectInfoMap
        }
        if (weatherMap.size>0){
            project.weather = weatherMap
        }
        return project
    }


    /**
     * 项目转换工具
     *
     * @param groupCode     集团编码
     * @param mapObj        中台信息点
     * @return 返回项目对象
     */
    fun projectDataConversions(groupCode: String, mapObj: HashMap<String, Any?>): Project{

        /** 查询项目类型字典 */
        val dictDefFuncIdList = dictDefFuncIdService.select(SFilter.eq("groupCode", groupCode), SFilter.eq("classCode", "project")).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 project = Project()
        for (key in mapObj.keys) {
            val containsKey = mapEx.containsKey(key)
            if (containsKey){
                if (key != "id"&& key!="name"&&key!="localId"&&key!="localName"&&key!="introduction"
                    &&key!="businessFloorNum"&&key!="officeFloorNum"){
                    val containsKey1 = mapCode.containsKey(key)
                    if (containsKey1){
                        val any = mapCode[key].toString()
                        if (any == "STATIC"){
                            when(mapEx[key].toString()) {
                                "INTEGER" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toInt()
                                }
                                "DOUBLE" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString().toDoubleOrNull()
                                }
                                "BOOLEAN" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString().toBoolean()
                                }
                                "STRING" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString()
                                }
                                "DATETIME" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ATTACHMENT" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString()
                                }
                                "OBJECT" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString()
                                }
                                "ENUM" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString()
                                }
                                "MENUM" -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString()
                                }
                                else -> {
                                    if (project.infos.isNullOrEmpty()){
                                        project.infos = HashMap<String,Any?>()
                                    }
                                    project.infos!![key] = mapObj[key]!!.toString()
                                }
                            }
                        }else{
                            if (project.infos.isNullOrEmpty()){
                                project.infos = HashMap<String,Any?>()
                            }
                            project.infos!![key] = mapObj[key]!!.toString()
                        }
                    }
                } else {
                  when(key) {
                      "id" -> project.id =  mapObj[key]!!.toString()
                      "name" -> project.name =  mapObj[key]!!.toString()
                      "localId" -> project.localId =  mapObj[key]!!.toString()
                      "localName" -> project.localName =  mapObj[key]!!.toString()
                      "introduction" -> project.introduction =  mapObj[key]!!.toString()
                      "businessFloorNum" -> project.businessFloorNum =  mapObj[key]!!.toString()
                      "officeFloorNum" -> project.officeFloorNum =  mapObj[key]!!.toString()
                  }
                }

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

        return project
    }




}