package com.persagy.server.datacenter.services.relation_calc

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.datacenter.models.entities.graphtype.block.RelationAddRequest
import com.persagy.server.datacenter.datacenter.models.entities.graphtype.rel.RelZoneSpace
import com.persagy.server.datacenter.models.entities.graphtype.RelationTypeProject
import com.persagy.server.datacenter.models.response.ManualRelationCalcSCreateResponse
import com.persagy.server.datacenter.services.objects.BuildingService
import com.persagy.server.datacenter.services.objects.EquipmentService
import com.persagy.server.datacenter.services.objects.GeneralSystemService
import com.persagy.server.datacenter.wanda.rel.REq2Eq
import com.persagy.server.datacenter.wanda.rel.REq2Sp
import com.persagy.server.datacenter.wanda.rel.REq2Sy
import com.persagy.server.datacenter.wanda.rel.RSp2Sp
import com.persagy.server.services.objects.FloorService
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import org.springframework.stereotype.Service
import java.text.SimpleDateFormat
import java.util.*

/**
 * 手动添加关系
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2021/2/18 9:40
 */
@Service
class ManualRelationCalcAddService {


    /** 空间 */
    private val relZoneSpaceService = SObjectService(SMybatisDao(RelZoneSpace::class.java))
    /** 空间和空间的关系 */
    private val rSp2SpService = SObjectService(SMybatisDao(RSp2Sp::class.java))
    /** 设备和系统关系 */
    private val rEq2SyService = SObjectService(SMybatisDao(REq2Sy::class.java))
    /** 设备和空间关系 */
    private val rEq2SpService = SObjectService(SMybatisDao(REq2Sp::class.java))
    /** 阀门限制设备关系 */
    private val rEq2EqService = SObjectService(SMybatisDao(REq2Eq::class.java))


    /**
     * 添加关系
     */
    fun addRel(relationAddRequest: RelationAddRequest): ManualRelationCalcSCreateResponse {
        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")

        val sCreateResponse = ManualRelationCalcSCreateResponse()

        if (relationAddRequest.relType.isNullOrEmpty()) {
            /** 关系类型不能为空 */
            sCreateResponse.result = SResponseType.failure
            sCreateResponse.message = "关系类型不能为空"
            return sCreateResponse
        }
        if (relationAddRequest.type.isNullOrEmpty()) {
            /** 编码类型不能为空 */
            sCreateResponse.result = SResponseType.failure
            sCreateResponse.message = "识别编码不能为空"
            return sCreateResponse
        }
        if (relationAddRequest.mainContent.isNullOrEmpty()) {
            /** 主对象内容不能为空 */
            sCreateResponse.result = SResponseType.failure
            sCreateResponse.message = "主对象编码不能为空"
            sCreateResponse.errorType = "1"
            return sCreateResponse
        }
        if (relationAddRequest.fromContent.isNullOrEmpty()) {
            /** 从对象内容不能为空 */
            sCreateResponse.result = SResponseType.failure
            sCreateResponse.message = "从对象编码不能为空"
            sCreateResponse.errorType = "2"
            return sCreateResponse
        }
        if (!relationAddRequest.fromContent.isNullOrEmpty() && relationAddRequest.fromContent!!.size < 1) {
            /** 从对象内容不能为空 */
            sCreateResponse.result = SResponseType.failure
            sCreateResponse.message = "从对象编码不能为空"
            sCreateResponse.errorType = "2"
            return sCreateResponse
        }
        
        try {
            val relationTypeProjectService = SObjectService(SMybatisDao(RelationTypeProject::class.java))
            val relationType = relationTypeProjectService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("graphicType", relationAddRequest.graphicType!!), SFilter.eq("relationType", relationAddRequest.relType!!)).entity()
            if (relationType != null) {
                relationType.computingTime = time()
                relationTypeProjectService.update(relationType)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }


        if (relationAddRequest.graphicType == "ArchSubset" && relationAddRequest.relType == "Bd2Sp") {

            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "主从对象不存在CADID编码"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            val buildingList = BuildingService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (buildingList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (buildingList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            for (fromContent in relationAddRequest.fromContent!!) {
                val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!)).exec()
                if (relZoneSpaceList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (relZoneSpaceList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
            }

            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }
            /** 查询空间 */
            val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.inList(relationAddRequest.type!!, setCode.toList())).exec()
            /** 遍历 */
            for (relZoneSpace in relZoneSpaceList) {
                relZoneSpace.buildingId = buildingList[0].id
                relZoneSpaceService.update(relZoneSpace)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse
        } else if (relationAddRequest.graphicType == "ArchSubset" && relationAddRequest.relType == "Fl2Sp") {
            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "主从对象不存在CADID编码"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            val floorList = FloorService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (floorList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (floorList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            for (fromContent in relationAddRequest.fromContent!!) {
                val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!)).exec()
                if (relZoneSpaceList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (relZoneSpaceList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
            }

            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }
            /** 查询空间 */
            val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.inList(relationAddRequest.type!!, setCode.toList())).exec()
            /** 遍历 */
            for (relZoneSpace in relZoneSpaceList) {
                relZoneSpace.floorId = floorList[0].id
                relZoneSpace.buildingId = floorList[0].buildingId
                relZoneSpaceService.update(relZoneSpace)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse

        } else if (relationAddRequest.graphicType == "MechInArch" && relationAddRequest.relType == "Eq2Bd") {
            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "从对象不存在CADID编码"
                sCreateResponse.errorType = "2"
                return sCreateResponse
            }

            val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (equipmentList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (equipmentList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            for (fromContent in relationAddRequest.fromContent!!) {
                val buildingList = BuildingService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!)).exec()
                if (buildingList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (buildingList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
            }
            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }

            /** 查询空间 */
            val buildingList = BuildingService.select(SFilter.eq("projectId", projectId), SFilter.inList(relationAddRequest.type!!, setCode.toList())).exec()
            /** 遍历 */
            for (building in buildingList) {
                val equipment = equipmentList[0]
                equipment.buildingId = building.id
                EquipmentService.update(equipment)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse
        } else if (relationAddRequest.graphicType == "MechInArch" && relationAddRequest.relType == "Eq2Fl") {
            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "从对象不存在CADID编码"
                sCreateResponse.errorType = "2"
                return sCreateResponse
            }

            val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (equipmentList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (equipmentList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            for (fromContent in relationAddRequest.fromContent!!) {
                val floorList = FloorService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!)).exec()
                if (floorList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (floorList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
            }
            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }

            /** 查询空间 */
            val floorList = FloorService.select(SFilter.eq("projectId", projectId), SFilter.inList(relationAddRequest.type!!, setCode.toList())).exec()

            /** 遍历 */
            for (floor in floorList) {
                val equipment = equipmentList[0]
                equipment.buildingId = floor.buildingId
                equipment.floorId = floor.id
                EquipmentService.update(equipment)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse
        }
        /** 阀门限制设备 */
        else if (relationAddRequest.graphicType == "ValveRelationship" && relationAddRequest.relType == "Vv2Eq") {
            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "从对象不存在CADID编码"
                sCreateResponse.errorType = "2"
                return sCreateResponse
            }

            val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (equipmentList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (equipmentList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            for (fromContent in relationAddRequest.fromContent!!) {
                val equipList = EquipmentService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!)).exec()
                if (equipList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (equipList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
            }
            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }

            /** 查询空间 */
            val equipList = EquipmentService.select(SFilter.eq("projectId", projectId), SFilter.inList(relationAddRequest.type!!, setCode.toList())).exec()

            /** 遍历 */
            for (equipment in equipList) {

                val rEq2Eq = REq2Eq()
                rEq2Eq.id1 = equipmentList[0].id
                rEq2Eq.id2 = equipment.id
                rEq2Eq.graphCode = relationAddRequest.graphicType
                rEq2Eq.type = relationAddRequest.relType
                rEq2Eq.sign = 1
                rEq2Eq.projectId = projectId
                rEq2EqService.replace(rEq2Eq)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse
        }
        else if (relationAddRequest.graphicType == "ArchForArch" && relationAddRequest.relType == "Sp2Sp") {

            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "主从对象不存在CADID编码"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            val relZoneSpaceOneList = relZoneSpaceService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (relZoneSpaceOneList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (relZoneSpaceOneList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            for (fromContent in relationAddRequest.fromContent!!) {
                val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!)).exec()
                if (relZoneSpaceList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (relZoneSpaceList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
            }

            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }
            /** 查询空间 */
            val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.inList(relationAddRequest.type!!, setCode.toList())).exec()
            /** 遍历 */
            for (relZoneSpace in relZoneSpaceList) {
                val rSp2Sp = RSp2Sp()
                rSp2Sp.id1 = relZoneSpaceOneList[0].id
                rSp2Sp.id2 = relZoneSpace.id
                rSp2Sp.graphCode = relationAddRequest.graphicType
                rSp2Sp.type = relationAddRequest.relType
                rSp2Sp.projectId = projectId
                rSp2Sp.zoneType = relZoneSpace.classCode
                rSp2Sp.sign = 1
                rSp2SpService.replace(rSp2Sp)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse
        } else if (relationAddRequest.graphicType == "MechSubset" && relationAddRequest.relType == "Sy2Eq") {

            val systemList = GeneralSystemService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (systemList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (systemList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            for (fromContent in relationAddRequest.fromContent!!) {
                val equipList = EquipmentService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!)).exec()
                if (equipList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (equipList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
            }
            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }
            /** 查询设备 */
            val equipList = EquipmentService.select(SFilter.eq("projectId", projectId), SFilter.inList(relationAddRequest.type!!, setCode.toList())).exec()
            /** 遍历 */
            for (equip in equipList) {
                val rEq2Sy = REq2Sy()
                rEq2Sy.id1 = systemList[0].id
                rEq2Sy.id2 = equip.id
                rEq2Sy.graphCode = relationAddRequest.graphicType
                rEq2Sy.type = relationAddRequest.relType
                rEq2Sy.sign = 1
                rEq2Sy.projectId = projectId
                rEq2SyService.replace(rEq2Sy)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse

        } else if (relationAddRequest.graphicType == "MechInArch" && relationAddRequest.relType == "Eq2Sp") {
            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "主从对象不存在CADID编码"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (relationAddRequest.zoneType.isNullOrEmpty()){
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "空间类型不能为空"
                sCreateResponse.errorType = "2"
                return sCreateResponse
            }

            val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (equipmentList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (equipmentList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            val relZoneSpaceLists = ArrayList<RelZoneSpace>()
            for (fromContent in relationAddRequest.fromContent!!) {
                val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!), SFilter.eq("classCode", relationAddRequest.zoneType!!)).exec()
                if (relZoneSpaceList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (relZoneSpaceList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                relZoneSpaceLists.addAll(relZoneSpaceList)
            }

            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }
            /** 查询空间 */
//            val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.fromContent!!),
//                    SFilter.eq("classCode", relationAddRequest.zoneType!!)).exec()
            /** 遍历 */
            for (zoneSpace in relZoneSpaceLists) {
                val rEq2Sp = REq2Sp()
                rEq2Sp.id1 = equipmentList[0].id
                rEq2Sp.id2 = zoneSpace.id
                rEq2Sp.graphCode = relationAddRequest.graphicType
                rEq2Sp.type = relationAddRequest.relType
                rEq2Sp.sign = 1
                rEq2Sp.projectId = projectId
                rEq2Sp.zoneType = zoneSpace.classCode
                rEq2SpService.replace(rEq2Sp)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse

        } else if (relationAddRequest.graphicType == "MechForArch" && relationAddRequest.relType == "Eq2Sp") {
            if (!relationAddRequest.type.isNullOrEmpty() && relationAddRequest.type == "cadId") {
                /** 从对象不存在cadId编码 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "主从对象不存在CADID编码"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (relationAddRequest.zoneType.isNullOrEmpty()){
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "空间类型不能为空"
                sCreateResponse.errorType = "2"
                return sCreateResponse
            }

            val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!)).exec()
            if (equipmentList.size < 1) {
                /** 主对象不存在 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":不存在"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            if (equipmentList.size > 1) {
                /** 主对象编码识别到多个实例 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = relationAddRequest.mainContent + ":存在多个实例"
                sCreateResponse.errorType = "1"
                return sCreateResponse
            }
            val relZoneSpaceLists = ArrayList<RelZoneSpace>()
            for (fromContent in relationAddRequest.fromContent!!) {
                val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq(relationAddRequest.type!!, fromContent!!), SFilter.eq("classCode", relationAddRequest.zoneType!!)).exec()
                if (relZoneSpaceList.size < 1) {
                    /** 从对象不存在 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":不存在"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                if (relZoneSpaceList.size > 1) {
                    /** 从对象编码识别到多个实例 */
                    sCreateResponse.result = SResponseType.failure
                    sCreateResponse.message = fromContent + ":存在多个实例"
                    sCreateResponse.errorType = "2"
                    return sCreateResponse
                }
                relZoneSpaceLists.addAll(relZoneSpaceList)
            }

            /** 处理关系 */
            val setCode = HashSet<String>()
            for (fromContent in relationAddRequest.fromContent!!) {
                setCode.add(fromContent)
            }
//            /** 查询空间 */
//            val relZoneSpaceList = relZoneSpaceService.select(SFilter.eq("projectId", projectId!!), SFilter.eq(relationAddRequest.type!!, relationAddRequest.mainContent!!),
//                    SFilter.eq("classCode", relationAddRequest.zoneType!!)).exec()
            /** 遍历 */
            for (zoneSpace in relZoneSpaceLists) {
                val rEq2Sp = REq2Sp()
                rEq2Sp.id1 = equipmentList[0].id
                rEq2Sp.id2 = zoneSpace.id
                rEq2Sp.graphCode = relationAddRequest.graphicType
                rEq2Sp.type = relationAddRequest.relType
                rEq2Sp.sign = 1
                rEq2Sp.projectId = projectId
                rEq2Sp.zoneType = zoneSpace.classCode
                rEq2SpService.replace(rEq2Sp)
            }
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse
        }else{
            sCreateResponse.result = SResponseType.success
            sCreateResponse.message = "添加成功"
            return sCreateResponse
        }
    }

    fun time():String {
        val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //设置日期格式
        println(df.format(Date())) // new Date()为获取当前系统时间
        return df.format(Date())
    }
}