package com.persagy.server.datacenter.syn.controllers.obj

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DictProject
import com.persagy.server.datacenter.wanda.obj.Equipment
import com.persagy.server.datacenter.models.entities.rel.*
import com.persagy.server.datacenter.wanda.obj.Floor
import com.persagy.server.datacenter.wanda.obj.ZoneSpace
import com.persagy.server.datacenter.wanda.rel.REq2Eq
import com.persagy.server.datacenter.wanda.rel.REq2Sp
import com.persagy.server.datacenter.wanda.rel.RSp2Sp
import com.persagy.server.datacenter.wanda.rel.RSy2Sp
import com.persagy.server.syn.models.api.obj.EquipApi
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
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.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

/**
 * 数据中台关系导入数据中心
 */
@Tag(name = "0004、关系-中台到数据中心")
@RestController
@RequestMapping("/rwd/rel")
class RelFromToControllers {

    /** 字典项目服务 */
    private val dictProjectService = SBaseService(SMybatisDao(DictProject::class.java))
    /** 设备和空间关系 */
    private val rEq2SpService = SBaseService(SMybatisDao(REq2Sp::class.java))
    /** 空间对象服务 */
    private val zoneSpaceService = SBaseService(SMybatisDao(ZoneSpace::class.java))
    /** 系统下的设备设施 */
    val rSyEqService = SBaseService(SMybatisDao(RSyEq::class.java))
    /** 设备和设备关系 */
    val rEq2EqService = SBaseService(SMybatisDao(REq2Eq::class.java))
    /** 设备服务楼层 */
    private val rEqForFlService = SBaseService(SMybatisDao(REqForFl::class.java))
    /** 设备服务建筑 */
    private val rEqForBdService = SBaseService(SMybatisDao(REqForBd::class.java))
    /** 设备服务于竖井 */
    private val rEqForShService = SBaseService(SMybatisDao(REqForSh::class.java))
    /** 设备所在竖井 */
    private val rEqInShService = SBaseService(SMybatisDao(REqInSh::class.java))
    /** 数据中心系统所在竖井关系 */
    private val syInShService = SObjectService(SMybatisDao(RSyInSh::class.java))
    /** 数据中心系统服务于竖井关系 */
    private val rSyForShService = SObjectService(SMybatisDao(RSyForSh::class.java))
    /** 数据中心竖井下的业务空间关系 */
    private val rSh2SpService = SObjectService(SMybatisDao(RSh2Sp::class.java))
    /** 数据中心建筑下的竖井关系 */
    private val rShInBdService = SObjectService(SMybatisDao(RShInBd::class.java))
    /** 数据中心系统所在楼层关系 */
    private val rSyInFlService = SObjectService(SMybatisDao(RSyInFl::class.java))
    /** 数据中心系统服务于楼层关系 */
    private val rSyForFlService = SObjectService(SMybatisDao(RSyForFl::class.java))
    /** 数据中心系统所在的建筑体关系 */
    private val rSyInBdService = SObjectService(SMybatisDao(RSyInBd::class.java))
    /** 数据中心系统服务于楼层关系 */
    private val rSyForBdService = SObjectService(SMybatisDao(RSyForBd::class.java))
    /** 数据中心楼层下的业务空间关系 */
    private val rSpinFlService = SObjectService(SMybatisDao(RSpinFl::class.java))
    /** 数据中心建筑体下的业务空间关系 */
    private val rSpinBdService = SObjectService(SMybatisDao(RSpinBd::class.java))
    /** 数据中心系统和空间关系 */
    private val rSy2SpService = SObjectService(SMybatisDao(RSy2Sp::class.java))
    /** 数据中心竖井贯通关系 */
    private val rShaftThroughShaftService = SObjectService(SMybatisDao(RShaftThroughShaft::class.java))
    /** 数据中心楼层贯通关系 */
    private val rFlThroughFlService = SObjectService(SMybatisDao(RFlThroughFl::class.java))
    /** 空间和空间的关系 */
    private val rSp2SpService = SObjectService(SMybatisDao(RSp2Sp::class.java))
    /** 数据中心设备服务 */
    private val equipmentService = SObjectService(SMybatisDao(Equipment::class.java))
    /** 数据中心楼层服务 */
    private val floorService = SObjectService(SMybatisDao(Floor::class.java))

    /**
     * 设备所在的业务空间
     */
    @Operation(summary= "设备所在的业务空间-同步到数据中心", description = "")
    @PostMapping("/eq2spIn")
    fun eq2SpIn(@RequestParam projectId: String): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "MechInArch", "Eq2Sp")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val rEq2Sp = REq2Sp()
                            rEq2Sp.id1 = fromId
                            rEq2Sp.id2 = toId
                            rEq2Sp.projectId = projectId
                            rEq2Sp.type = "eq2sp_in"
                            val containsKey = rel.containsKey("relValue")
                            if (containsKey) {
                                rEq2Sp.zoneType = rel["relValue"].toString()
                            } else {
                                val zoneSpace = zoneSpaceService.select(
                                    SFilter.eq("projectId", projectId),
                                    SFilter.eq("id", rEq2Sp.id2!!)
                                ).entity()
                                if (zoneSpace!=null) {
                                    rEq2Sp.zoneType = zoneSpace.classCode
                                }
                            }
                            rEq2SpService.replace(rEq2Sp)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }

    /**
     * 设备设施服务业务空间
     */
    @Operation(summary= "设备设施服务业务空间-同步到数据中心", description = "")
    @PostMapping("/eq2SpFor")
    fun eq2SpFor(@RequestParam projectId: String): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "MechForArch", "Eq2Sp")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val rEq2Sp = REq2Sp()
                            rEq2Sp.id1 = fromId
                            rEq2Sp.id2 = toId
                            rEq2Sp.projectId = projectId
                            rEq2Sp.type = "eq2sp_for"
                            val containsKey = rel.containsKey("relValue")
                            if (containsKey) {
                                rEq2Sp.zoneType = rel["relValue"].toString()
                            } else {
                                val zoneSpace = zoneSpaceService.select(
                                    SFilter.eq("projectId", projectId),
                                    SFilter.eq("id", rEq2Sp.id2!!)
                                ).entity()
                                if (zoneSpace!=null) {
                                    rEq2Sp.zoneType = zoneSpace.classCode
                                }
                            }
                            rEq2SpService.replace(rEq2Sp)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }

    /**
     * 系统下的设备设施
     */
    @Operation(summary= "系统下的设备设施-同步到数据中心", description = "")
    @PostMapping("/sy2EqIn")
    fun sy2EqIn(@RequestParam projectId: String): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "MechSubset", "Sy2Eq")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val rSyEq = RSyEq()
                            rSyEq.sysId = fromId
                            rSyEq.equipId = toId
                            rSyEq.projectId = projectId
                            rSyEq.graphCode = "MechSubset"
                            rSyEq.type = "Sy2Eq"
                            rSyEqService.replace(rSyEq)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }

    /**
     * 建筑下的竖井
     */
    @Operation(summary= "建筑下的竖井-同步到数据中心", description = "")
    @PostMapping("/bd2ShIn")
    fun bd2ShIn(@RequestParam projectId: String): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "ArchSubset", "Bd2Sh")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val rShInBd = RShInBd()
                            rShInBd.buildingId = fromId
                            rShInBd.shaftId = toId
                            rShInBd.projectId = projectId
                            rShInBd.graphCode = "ArchSubset"
                            rShInBdService.replace(rShInBd)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }

    /**
     * 建筑下的业务空间
     */
    @Operation(summary= "建筑下的业务空间-同步到数据中心", description = "")
    @PostMapping("/bd2SpIn")
    fun bd2SpIn(@RequestParam projectId: String): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "ArchSubset", "Bd2Sp")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val rSpinBd = RSpinBd()
                            rSpinBd.buildingId = fromId
                            rSpinBd.spaceId = toId
                            rSpinBd.projectId = projectId
                            val containsKey = rel.containsKey("relValue")
                            if (containsKey) {
                                rSpinBd.objectType = rel["relValue"].toString()
                            } else {
                                val zoneSpace = zoneSpaceService.select(
                                    SFilter.eq("projectId", projectId),
                                    SFilter.eq("id", rSpinBd.spaceId!!)
                                ).entity()
                                if (zoneSpace!=null) {
                                    rSpinBd.objectType = zoneSpace.classCode
                                }
                            }
                            rSpinBdService.replace(rSpinBd)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }

    /**
     * 系统所在建筑
     */
    @Operation(summary= "系统所在建筑-同步到数据中心", description = "")
    @PostMapping("/sy2BdIn")
    fun sy2BdIn(@RequestParam projectId: String): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "MechInArch", "Sy2Bd")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val rSyInBd = RSyInBd()
                            rSyInBd.sysId = fromId
                            rSyInBd.buildingId = toId
                            rSyInBd.projectId = projectId
                            rSyInBdService.replace(rSyInBd)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }


    /**
     * 楼层下的业务空间
     */
    @Operation(summary= "楼层下的业务空间 - 同步到数据中心", description = "")
    @PostMapping("/fl2SpIn")
    fun fl2SpIn(@RequestParam projectId: String): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "ArchSubset", "Fl2Sp")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val entity = floorService.select(SFilter.eq("id", fromId)).entity()
                            val rSpinFl = RSpinFl()
                            rSpinFl.floorId = fromId
                            rSpinFl.spaceId = toId
                            rSpinFl.projectId = projectId
                            if (entity!=null) {
                                rSpinFl.buildingId = entity.buildingId
                            }
                            val containsKey = rel.containsKey("relValue")
                            if (containsKey) {
                                rSpinFl.objectType = rel["relValue"].toString()
                            } else {
                                val zoneSpace = zoneSpaceService.select(
                                    SFilter.eq("projectId", projectId),
                                    SFilter.eq("id", rSpinFl.spaceId!!)
                                ).entity()
                                if (zoneSpace!=null) {
                                    rSpinFl.objectType = zoneSpace.classCode
                                }
                            }
                            rSpinFlService.replace(rSpinFl)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }

    /**
     * 竖井下的业务空间
     */
    fun sh2SpIn(@RequestParam projectId: String): SBaseResponse{
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (dictProject!=null) {
                val relList = EquipApi.getRel(dictProject.groupCode!!, projectId, "ArchSubset", "Sh2Sp")
                if (!relList.isNullOrEmpty()){
                    for ( rel in relList) {
                        try {
                            val fromId = rel["objFrom"].toString()
                            val toId = rel["objTo"].toString()
                            val rSh2Sp = RSh2Sp()
                            rSh2Sp.id1 = fromId
                            rSh2Sp.id2 = toId
                            rSh2Sp.projectId = projectId
                            rSh2Sp.type = "sh2sp"
                            val containsKey = rel.containsKey("relValue")
                            if (containsKey) {
                                rSh2Sp.zoneType = rel["relValue"].toString()
                            } else {
                                val zoneSpace = zoneSpaceService.select(
                                    SFilter.eq("projectId", projectId),
                                    SFilter.eq("id", rSh2Sp.id2!!)
                                ).entity()
                                if (zoneSpace!=null) {
                                    rSh2Sp.zoneType = zoneSpace.classCode
                                }
                            }
                            rSh2SpService.replace(rSh2Sp)
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }
    }


}