/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.controllers.obj

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.server.datacenter.models.entities.assistant.PointPosition
import com.persagy.server.datacenter.models.entities.objects.SpaceOutline
import com.persagy.server.datacenter.models.entities.rel.RSpinBd
import com.persagy.server.datacenter.models.entities.rel.RSpinFl
import com.persagy.server.datacenter.syn.models.tools.EquipUtil
import com.persagy.server.datacenter.syn.models.tools.SpaceUtil
import com.persagy.server.syn.models.api.obj.*
import com.persagy.server.syn.models.tools.*
import com.persagy.server.datacenter.syn.services.BaseDataService
import com.persagy.server.syn.services.dict.DClassDefService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

/**
 * 对象，中台到数据中心   （项目、建筑、楼层、设备、系统、竖井）
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/18 10:06
 */
@Tag(name = "0003、对象-中台到数据中心")
@RestController
@RequestMapping("/rwd/object")
class ObjectFromToControllers {
    companion object {
        // 日志
        private val logger = LoggerFactory.getLogger(ObjectFromToControllers::class.java)
    } // Companion object


    /**
     * 项目信息 - 数据中台存储到数据中心，根据集团获取集团写所有项目
     */
    @Operation(summary = "项目信息-数据中台存储到数据中心，根据集团获取集团写所有项目 ", description = "")
    @PostMapping(value = ["/project-syn"])
    fun projectGroupAll(): SBaseResponse {
        return try {
            /** 集团数据 */
            DClassDefService.groupQuerys()
            /** 项目数据 */
            DClassDefService.dictProject()
            val groupList = BaseDataService.dictProjectService.selectAll().exec()
            for (group in groupList) {
                val projectQueryList = ProjectApi.projectQuery(group.groupCode!!,group.id!!)
                if (projectQueryList.size > 0) {
                    for (projectQuery in projectQueryList) {
                        val projectDataConversion = ProjectUtil.projectDataConversions(group.groupCode!!, projectQuery)
                        logger.debug("项目：  ${projectDataConversion.toJson()} ")
                        BaseDataService.projectService.replace(projectDataConversion)
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }

    }

    /**
     * 项目信息 - 数据中台存储到数据中心，根据集团获取集团写所有项目
     */
    @Operation(summary = "项目信息-数据中台存储到数据中心，根据 id 同步 ", description = "")
    @PostMapping(value = ["/project-syn-id"])
    fun projectId(@RequestParam projectId: String,sign: Boolean = false): SBaseResponse {
        return try {
            /** 集团数据 */
            DClassDefService.groupQuerys()
            /** 项目数据 */
            DClassDefService.dictProject()
            if (!sign){
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id",projectId)).entity()
                val projectQueryList = ProjectApi.projectQuery(dictProject!!.groupCode!!,projectId!!)
                if (projectQueryList.size > 0) {
                    for (projectQuery in projectQueryList) {
                        logger.debug("项目1：  ${projectQuery.toJson()} ")
                        val projectDataConversion = ProjectUtil.projectDataConversions(dictProject!!.groupCode!!, projectQuery)
                        logger.debug("项目：  ${projectDataConversion.toJson()} ")
                        BaseDataService.projectService.replace(projectDataConversion)
                    }
                }
            } else {
                val dictProjectList = BaseDataService.dictProjectService.selectAll().exec()
                for (dictProject in dictProjectList) {
                    val projectQueryList = ProjectApi.projectQuery(dictProject!!.groupCode!!,projectId!!)
                    if (projectQueryList.size > 0) {
                        for (projectQuery in projectQueryList) {
                            logger.debug("项目1：  ${projectQuery.toJson()} ")
                            val projectDataConversion = ProjectUtil.projectDataConversions(dictProject!!.groupCode!!, projectQuery)
                            logger.debug("项目：  ${projectDataConversion.toJson()} ")
                            BaseDataService.projectService.replace(projectDataConversion)
                        }
                    }
                }
            }

            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }

    }

    /**
     * 建筑信息 - 数据中台存储到数据中心
     */
    @Operation(summary = "建筑信息-数据中台存储到数据中心 ", description = "switch true  根据输入的项目 id  同步建筑，false 同步所有项目的 建筑")
    @PostMapping(value = ["/building-syn"])
    fun buildingSyn( @RequestParam projectId: String, @RequestParam switch: Boolean = true): SBaseResponse {
        return try {
            if (switch) {
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                val buildingSynList = BuildingApi.buildingSyn(dictProject!!.groupCode!!,projectId,"building")
                if (buildingSynList.size > 0) {
                    for (buildingSyn in buildingSynList) {
                        val buildingDataConversion =
                            BuildingUtil.buildingDataConversions(projectId, dictProject!!.groupCode!!, buildingSyn)
                        logger.debug("建筑：  ${buildingDataConversion.toJson()} ")
                        BaseDataService.buildingService.replace(buildingDataConversion)

                    }
                }

            } else {
                /** 查询所有的项目 */
                val projectList = BaseDataService.projectService.selectAll().exec()
                for (project in projectList){
                    val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", project.id!!)).entity()
                    val buildingSynList = BuildingApi.buildingSyn(dictProject!!.groupCode!!,projectId,"building")
                    if (buildingSynList.size > 0) {
                        for (buildingSyn in buildingSynList) {
                            val buildingDataConversion =
                                BuildingUtil.buildingDataConversions(project.id!!, dictProject!!.groupCode!!, buildingSyn)
                            logger.debug("建筑：  ${buildingDataConversion.toJson()} ")
                            BaseDataService.buildingService.replace(buildingDataConversion)
                        }
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }

    }

    /**
     * 楼层 - 中台楼层同步到数据中心
     */
    @Operation(summary = "楼层信息-数据中台存储到数据中心 ", description = "switch true  根据输入的项目 id  同步楼层，false 同步所有项目的 楼层")
    @PostMapping(value = ["/floor-syn"])
    fun floorSyn(@RequestParam projectId: String, @RequestParam switch: Boolean = true): SBaseResponse {
        return try {
            if (switch){
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                val floorSynList = BuildingApi.buildingSyn(dictProject!!.groupCode!!,projectId, "floor")
                if (floorSynList.size>0) {
                    for (floorSyn in floorSynList){
                        val floor = FloorUtil.floorSyns(projectId, dictProject.groupCode!!, floorSyn)
                        val floorInBuildingList = FloorApi.floorInBuilding(dictProject.groupCode!!,projectId, floor.id!!)
                        if (floorInBuildingList.size>0){
                            for (floorInBuilding in floorInBuildingList){
                                val containsKey = floorInBuilding.containsKey("objFrom")
                                if (containsKey){
                                    floor.buildingId = floorInBuilding["objFrom"].toString()
                                    break
                                }
                            }
                        }
                        BaseDataService.floorService.replace(floor)
                        val containsKeyOutline = floorSyn.containsKey("outline")
                        if (containsKeyOutline){
                            val id = floorSyn["id"].toString()
//                            val replace1 = floorSyn["outline"].toString().replace("X", "x")
//                            val replace2 = replace1.replace("Y", "y")
//                            val replace3 = replace2.replace("Z", "z")
                            val type = object : TypeToken<ArrayList<PointPosition>>() {
                            }.type
                            var outline=  Gson().fromJson<ArrayList<PointPosition>>(floorSyn["outline"].toString(),type)
//                            floorOutlineService.replace(FloorOutline(id,outline))
                            floor.outline = outline
                        }
                        BaseDataService.floorService.replace(floor)
                    }
                }
            } else {
                /** 查询所有的项目 */
                val projectList = BaseDataService.projectService.selectAll().exec()
                for (project in projectList){
                    val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", project.id!!)).entity()
                    val floorSynList = BuildingApi.buildingSyn(dictProject!!.groupCode!!,projectId, "floor")
                    if (floorSynList.size>0) {
                        for (floorSyn in floorSynList){
                            val floor = FloorUtil.floorSyns(project.id!!, dictProject!!.groupCode!!, floorSyn)
                            val floorInBuildingList = FloorApi.floorInBuilding(dictProject!!.groupCode!!,projectId, floor.id!!)
                            if (floorInBuildingList.size>0){
                                for (floorInBuilding in floorInBuildingList){
                                    val containsKey = floorInBuilding.containsKey("objFrom")
                                    if (containsKey){
                                        floor.buildingId = floorInBuilding["objFrom"].toString()
                                        break
                                    }
                                }
                            }
                            BaseDataService.floorService.replace(floor)
                        }
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }
    }


    /**
     * 设备
     *
     * @param projectId         项目 id
     * @param switch            开关
     * @param isBuildingFloor   是否查询楼层
     * @param page              页码
     * @return 返回值
     */
    @Operation(summary = "设备信息-数据中台存储到数据中心 ", description = "switch true  根据输入的项目 id  同步楼层，false 同步所有项目的 楼层")
    @PostMapping(value = ["/equip-syn"])
    fun equipSyn(@RequestParam projectId: String,@RequestParam switch: Boolean = true,@RequestParam isBuildingFloor: Boolean = true,@RequestParam page: Int = 1): SBaseResponse {
        return try {
            var num = page
            if (switch) {
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                val equipSynApi = EquipApi.equipSynApi(dictProject!!.groupCode!!,projectId, page)
                val equipDataList = equipSynApi.data
                for (equipData in equipDataList!!){
                    val equipSyn = EquipUtil.equipSyns(projectId, dictProject!!.groupCode!!, equipData)
                    logger.debug("设备=${equipSyn.toJson()}")
                    if (isBuildingFloor){
                        /** 查询建筑 */
                        val equipInBuildingList = EquipApi.equipInBuilding(dictProject!!.groupCode!!,projectId, equipSyn.id!!)
                        if (equipInBuildingList.size>0){
                            val hashMap = equipInBuildingList[0]
                            if (hashMap.containsKey("objTo")){
                                equipSyn.buildingId = hashMap["objTo"].toString()
                            }
                        }
                        /** 查询楼层 */
                        val equipInFloorList = EquipApi.equipInFloor(dictProject!!.groupCode!!,projectId, equipSyn.id!!)
                        if (equipInFloorList.size>0){
                            val hashMap = equipInFloorList[0]
                            if (hashMap.containsKey("objTo")){
                                equipSyn.floorId = hashMap["objTo"].toString()
                            }
                        }
                        if (equipSyn.buildingId.isNullOrEmpty()){
                            if (!equipSyn.floorId.isNullOrEmpty()){
                                val floor = BaseDataService.floorService.select(SFilter.eq("id", equipSyn.floorId!!)).entity()
                                if (floor!=null){
                                    equipSyn.buildingId = floor.buildingId
                                }
                            }
                        }
                    }
                    BaseDataService.equipmentService.replace(equipSyn)
                }
                if (equipSynApi.data!!.size>= 1000){
                    num ++
                    equipSyn(projectId,switch,isBuildingFloor,num)
                }
            } else {
                val projectList = BaseDataService.projectService.selectAll().exec()
                for (project in projectList){
                    val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", project.id!!)).entity()
                    val equipSynApi = EquipApi.equipSynApi(dictProject!!.groupCode!!,project.id!!, page)
                    val equipDataList = equipSynApi.data
                    for (equipData in equipDataList!!){
                        val equipSyn = EquipUtil.equipSyns(project.id!!, dictProject.groupCode!!, equipData)
                        if (isBuildingFloor){
                            /** 查询建筑 */
                            val equipInBuildingList = EquipApi.equipInBuilding(dictProject.groupCode!!,project.id!!, equipSyn.id!!)
                            if (equipInBuildingList.size>0){
                                val hashMap = equipInBuildingList[0]
                                if (hashMap.containsKey("objTo")){
                                    equipSyn.buildingId = hashMap["objTo"].toString()
                                }
                            }
                            /** 查询楼层 */
                            val equipInFloorList = EquipApi.equipInFloor(dictProject.groupCode!!,project.id!!, equipSyn.id!!)
                            if (equipInFloorList.size>0){
                                val hashMap = equipInFloorList[0]
                                if (hashMap.containsKey("objTo")){
                                    equipSyn.floorId = hashMap["objTo"].toString()
                                }
                            }
                            if (equipSyn.buildingId.isNullOrEmpty()&&!equipSyn.floorId.isNullOrEmpty()){
                                if (!equipSyn.floorId.isNullOrEmpty()){
                                    val floor = BaseDataService.floorService.select(SFilter.eq("id", equipSyn.floorId!!)).entity()
                                    if (floor!=null){
                                        equipSyn.buildingId = floor.buildingId
                                    }
                                }
                            }
                        }
                        BaseDataService.equipmentService.replace(equipSyn)
                    }
                    if (equipSynApi.data!!.size>= 1000){
                        num ++
                        equipSyn(projectId,switch,isBuildingFloor,num)
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }
    }

    /**
     * 直接拉取数据中台的整个项目下设备
     */
    @Operation(summary = "设备信息-数据中台存储到数据中心 - 直接获取项目下所有设备数据 ", description = "switch true  根据输入的项目 id  同步楼层，false 同步所有项目的 楼层")
    @PostMapping(value = ["/equip-syn-All"])
    fun equipSynAll(@RequestParam projectId: String,@RequestParam switch: Boolean = true,@RequestParam isBuildingFloor: Boolean = true): SBaseResponse {
        return try {
            if (switch) {
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                val equipSynApi = EquipApi.equipSynApiAll(dictProject!!.groupCode!!,projectId)
                logger.debug("数量 = ${equipSynApi.data?.size}")
//                return  SBaseResponse(SResponseType.success)
                val equipDataList = equipSynApi.data
                for (equipData in equipDataList!!){
                    val equipSyn = EquipUtil.equipSyns(projectId, dictProject!!.groupCode!!, equipData)
                    logger.debug("设备=${equipSyn.toJson()}")
                    if (isBuildingFloor){
                        /** 查询建筑 */
                        val equipInBuildingList = EquipApi.equipInBuilding(dictProject!!.groupCode!!,projectId, equipSyn.id!!)
                        if (equipInBuildingList.size>0){
                            val hashMap = equipInBuildingList[0]
                            if (hashMap.containsKey("objTo")){
                                equipSyn.buildingId = hashMap["objTo"].toString()
                            }
                        }
                        /** 查询楼层 */
                        val equipInFloorList = EquipApi.equipInFloor(dictProject!!.groupCode!!,projectId, equipSyn.id!!)
                        if (equipInFloorList.size>0){
                            val hashMap = equipInFloorList[0]
                            if (hashMap.containsKey("objTo")){
                                equipSyn.floorId = hashMap["objTo"].toString()
                            }
                        }
                        if (equipSyn.buildingId.isNullOrEmpty()&&!equipSyn.floorId.isNullOrEmpty()){
                            if (!equipSyn.floorId.isNullOrEmpty()){
                                val floor = BaseDataService.floorService.select(SFilter.eq("id", equipSyn.floorId!!)).entity()
                                if (floor!=null){
                                    equipSyn.buildingId = floor.buildingId
                                }
                            }
                        }
                    }
                    BaseDataService.equipmentService.replace(equipSyn)
                }
            } else {
                val projectList = BaseDataService.projectService.selectAll().exec()
                for (project in projectList){
                    val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", project.id!!)).entity()
                    val equipSynApi = EquipApi.equipSynApiAll(dictProject!!.groupCode!!,project.id!!)
                    val equipDataList = equipSynApi.data
                    for (equipData in equipDataList!!){
                        val equipSyn = EquipUtil.equipSyns(project.id!!, dictProject.groupCode!!, equipData)
                        if (isBuildingFloor){
                            /** 查询建筑 */
                            val equipInBuildingList = EquipApi.equipInBuilding(dictProject.groupCode!!,project.id!!, equipSyn.id!!)
                            if (equipInBuildingList.size>0){
                                val hashMap = equipInBuildingList[0]
                                if (hashMap.containsKey("objTo")){
                                    equipSyn.buildingId = hashMap["objTo"].toString()
                                }
                            }
                            /** 查询楼层 */
                            val equipInFloorList = EquipApi.equipInFloor(dictProject.groupCode!!,project.id!!, equipSyn.id!!)
                            if (equipInFloorList.size>0){
                                val hashMap = equipInFloorList[0]
                                if (hashMap.containsKey("objTo")){
                                    equipSyn.floorId = hashMap["objTo"].toString()
                                }
                            }
                            if (equipSyn.buildingId.isNullOrEmpty()){
                                if (!equipSyn.floorId.isNullOrEmpty()){
                                    val floor = BaseDataService.floorService.select(SFilter.eq("id", equipSyn.floorId!!)).entity()
                                    if (floor!=null){
                                        equipSyn.buildingId = floor.buildingId
                                    }
                                }
                            }
                        }
                        BaseDataService.equipmentService.replace(equipSyn)
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }
    }





    /**
     * 空间
     *
     * @param projectId         项目 id
     * @param switch            开关
     * @param isBuildingFloor   是否查询建筑楼层
     * @return 返回结果
     */
    @Operation(summary = "空间信息-数据中台存储到数据中心 ", description = "switch true  根据输入的项目 id  空间，false 同步所有项目的 空间")
    @PostMapping(value = ["/space-syn"])
    fun spaceSyn(@RequestParam projectId: String,@RequestParam switch: Boolean = true,@RequestParam isBuildingFloor:Boolean = true): SBaseResponse {
        return try {
            if (switch){
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                val spaceSynApiList = SpaceApi.spaceSynApi(dictProject!!.groupCode!!,projectId)
                if (spaceSynApiList.size>0){
                    for (spaceSyn in spaceSynApiList){
                        val spaceDataConversion =
                            SpaceUtil.spaceDataConversions(projectId, dictProject!!.groupCode!!, spaceSyn)
                        logger.debug("打印空间02 = ${spaceDataConversion.toJson()}")
                        BaseDataService.zoneSpaceService.replace(spaceDataConversion)
                        if (isBuildingFloor){
                            val spaceInBuildingList = SpaceApi.spaceInBuilding(dictProject!!.groupCode!!,projectId, spaceDataConversion.id!!)
                            if (spaceInBuildingList.size>0){
                                for (spaceInBuilding in spaceInBuildingList){
                                    val rSpinBd = RSpinBd()
                                    spaceDataConversion.buildingId = spaceInBuilding["objFrom"].toString()
                                    rSpinBd.buildingId = spaceInBuilding["objFrom"].toString()
                                    rSpinBd.projectId = projectId
                                    rSpinBd.sign = 1
                                    rSpinBd.spaceId = spaceDataConversion.id
                                    rSpinBd.objectType = spaceDataConversion.classCode
                                    BaseDataService.rSpinBdService.replace(rSpinBd)
                                }
                            }
                            val spaceInFloorList = SpaceApi.spaceInFloor(dictProject!!.groupCode!!,projectId, spaceDataConversion.id!!)
                            if (spaceInFloorList.size>0){
                                for (spaceInFloor in spaceInFloorList){
                                    val any = spaceInFloor["objFrom"].toString()
                                    val floor =
                                        BaseDataService.floorService.select(SFilter.eq("id", spaceInFloor["objFrom"].toString())).entity()
                                    val rSpinFl = RSpinFl()
                                    spaceDataConversion.floorId = spaceInFloor["objFrom"].toString()

                                    rSpinFl.buildingId = floor?.buildingId
                                    rSpinFl.floorId = spaceInFloor["objFrom"].toString()
                                    rSpinFl.objectType = spaceDataConversion.classCode
                                    rSpinFl.projectId = projectId
                                    rSpinFl.sign = 1
                                    rSpinFl.spaceId = spaceDataConversion.id
                                    logger.debug("空間和楼层关系 = ${rSpinFl.toJson()} *************************************************************************************************************")
                                    BaseDataService.rSpinFlService.replace(rSpinFl)
                                    if (spaceDataConversion.buildingId.isNullOrEmpty()) {
                                        spaceDataConversion.buildingId = floor?.buildingId
                                        val rSpinBd = RSpinBd()
                                        rSpinBd.buildingId = spaceDataConversion.buildingId
                                        rSpinBd.projectId = projectId
                                        rSpinBd.sign = 1
                                        rSpinBd.spaceId = spaceDataConversion.id
                                        rSpinBd.objectType = spaceDataConversion.classCode
                                        BaseDataService. rSpinBdService.replace(rSpinBd)
                                    }
                                }
                            }

                            val containsKey = spaceSyn.containsKey("outline")
                            if (containsKey){
                                val id = spaceDataConversion.id
//                                val replace1 = spaceSyn["outline"].toString().replace("X", "x")
//                                val replace2 = replace1.replace("Y", "y")
//                                val replace3 = replace2.replace("Z", "z")
                                val type = object : TypeToken<ArrayList<ArrayList<ArrayList<PointPosition>>>>() {
                                }.type
                                var outline=  Gson().fromJson<ArrayList<ArrayList<ArrayList<PointPosition>>>>(spaceSyn["outline"].toString(),type)
//                                spaceOutlineService.replace(SpaceOutline(id,outline))
                                spaceDataConversion.outline = outline
                            }
                            BaseDataService.zoneSpaceService.update(spaceDataConversion)
                        }

                    }
                }
            } else {
                /** 查询所有的项目 */
                val projectList = BaseDataService.projectService.selectAll().exec()
                for (project in projectList){
                    val projectId = project.id!!
                    val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                    val spaceSynApiList = SpaceApi.spaceSynApi(dictProject!!.groupCode!!,projectId)
                    if (spaceSynApiList.size>0){
                        for (spaceSyn in spaceSynApiList){
                            val spaceDataConversion =
                                SpaceUtil.spaceDataConversions(projectId, dictProject!!.groupCode!!, spaceSyn)

                            BaseDataService.zoneSpaceService.replace(spaceDataConversion)
                            if (isBuildingFloor){
                                val spaceInBuildingList = SpaceApi.spaceInBuilding(dictProject!!.groupCode!!,projectId, spaceDataConversion.id!!)
                                if (spaceInBuildingList.size>0){
                                    for (spaceInBuilding in spaceInBuildingList){
                                        val rSpinBd = RSpinBd()
                                        spaceDataConversion.buildingId = spaceInBuilding["objFrom"].toString()
                                        rSpinBd.buildingId = spaceInBuilding["objFrom"].toString()
                                        rSpinBd.projectId = projectId
                                        rSpinBd.sign = 1
                                        rSpinBd.spaceId = spaceDataConversion.id
                                        rSpinBd.objectType = spaceDataConversion.classCode
                                        BaseDataService.rSpinBdService.replace(rSpinBd)
                                    }
                                }
                                val spaceInFloorList = SpaceApi.spaceInFloor(dictProject!!.groupCode!!,projectId, spaceDataConversion.id!!)
                                if (spaceInFloorList.size>0){
                                    for (spaceInFloor in spaceInFloorList){
                                        val any = spaceInFloor["objFrom"].toString()
                                        val floor =
                                            BaseDataService.floorService.select(SFilter.eq("id", spaceInFloor["objFrom"].toString())).entity()
                                        spaceDataConversion.floorId = spaceInFloor["objFrom"].toString()
                                        val rSpinFl = RSpinFl()
                                        rSpinFl.buildingId = floor?.buildingId
                                        rSpinFl.floorId = spaceInFloor["objFrom"].toString()
                                        rSpinFl.objectType = spaceDataConversion.classCode
                                        rSpinFl.projectId = projectId
                                        rSpinFl.sign = 1
                                        rSpinFl.spaceId = spaceDataConversion.id
                                        BaseDataService.rSpinFlService.replace(rSpinFl)
                                    }
                                }
                                BaseDataService.zoneSpaceService.update(spaceDataConversion)
                                val containsKey = spaceSyn.containsKey("outline")
                                if (containsKey){
                                    val id = spaceSyn["id"].toString()
                                    val replace1 = spaceSyn["outline"].toString().replace("X", "x")
                                    val replace2 = replace1.replace("Y", "y")
                                    val replace3 = replace2.replace("Z", "z")
                                    val type = object : TypeToken<ArrayList<ArrayList<ArrayList<PointPosition>>>>() {
                                    }.type
                                    var outline=  Gson().fromJson<ArrayList<ArrayList<ArrayList<PointPosition>>>>(replace3,type)
                                    BaseDataService.spaceOutlineService.replace(SpaceOutline(id,outline))
                                }
                            }
                        }
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }
    }

    /**
     * 系统
     *
     * @param projectId         项目 id
     * @param switch            开关
     * @param isBuildingFloor   是否查询建筑楼层
     * @return 返回结果
     */
    @Operation(summary = "系统信息-数据中台存储到数据中心 ", description = "switch true  根据输入的项目 id  系统，false 同步所有项目的 系统")
    @PostMapping(value = ["/system-syn"])
    fun systemSyn(@RequestParam projectId: String,@RequestParam switch: Boolean = true,@RequestParam isBuildingFloor:Boolean = true): SBaseResponse {
        return try {
            if (switch) {
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                val systemQueryList = SystemApi.systemQuery(dictProject!!.groupCode!!,projectId)
                if (systemQueryList.size>0){
                    for (systemQuery in systemQueryList){
                        val spaceDataConversion =
                            SystemUtil.systemDataConversions(projectId, dictProject!!.groupCode!!, systemQuery)
                        BaseDataService.generalSystemService.replace(spaceDataConversion)
                    }
                }
            } else {
                /** 查询所有的项目 */
                val projectList = BaseDataService.projectService.selectAll().exec()
                for (project in projectList){
                    val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", project.id!!)).entity()
                    val systemQueryList = SystemApi.systemQuery(dictProject!!.groupCode!!,project.id!!)
                    if (systemQueryList.size>0){
                        for (systemQuery in systemQueryList){
                            val spaceDataConversion =
                                SystemUtil.systemDataConversions(project.id!!, dictProject!!.groupCode!!, systemQuery)
                            BaseDataService.generalSystemService.replace(spaceDataConversion)
                        }
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }
    }

    /**
     * 竖井
     *
     * @param projectId         项目 id
     * @param switch            开关
     * @param isBuildingFloor   是否查询建筑楼层
     * @return 返回结果
     */
    @Operation(summary = "竖井信息-数据中台存储到数据中心 ", description = "switch true  根据输入的项目 id  竖井，false 同步所有项目的 竖井")
    @PostMapping(value = ["/shaft-syn"])
    fun shaftSyn(@RequestParam projectId: String,@RequestParam switch: Boolean = true,@RequestParam isBuildingFloor:Boolean = true): SBaseResponse {
        return try {
            if (switch){
                val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", projectId)).entity()
                val shaftQueryList = ShaftApi.shaftQuery(dictProject!!.groupCode!!,projectId)
                if (!shaftQueryList.isNullOrEmpty()) {
                    for (shaftQuery in shaftQueryList) {
                        val shaftDataConversion =
                            ShaftUtil.shaftDataConversions(projectId, dictProject?.groupCode!!, shaftQuery)
                        BaseDataService.shaftService.replace(shaftDataConversion)
                    }
                }
            } else {
                /** 查询所有的项目 */
                val projectList = BaseDataService.projectService.selectAll().exec()
                for (project in projectList){
                    val dictProject = BaseDataService.dictProjectService.select(SFilter.eq("id", project.id!!)).entity()
                    val shaftQueryList = ShaftApi.shaftQuery(dictProject!!.groupCode!!,project.id!!)
                    if (!shaftQueryList.isNullOrEmpty()) {
                        for (shaftQuery in shaftQueryList) {
                            val shaftDataConversion =
                                ShaftUtil.shaftDataConversions(project.id!!, dictProject?.groupCode!!, shaftQuery)
                            BaseDataService.shaftService.replace(shaftDataConversion)
                        }
                    }
                }
            }
            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }
    }
}