/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.datacenter.services.relation_calc

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.Opt
import com.persagy.server.datacenter.datacenter.models.entities.graphtype.block.ConnectedBlock
import com.persagy.server.datacenter.models.entities.graphtype.MPoint
import com.persagy.server.datacenter.models.entities.graphtype.RelationTypeProject
import com.persagy.server.datacenter.services.base.RService
import com.persagy.server.datacenter.services.objects.EquipmentService
import com.persagy.server.datacenter.services.objects.ZoneSpaceBaseService
import com.persagy.server.datacenter.wanda.rel.RSp2Sp
import com.persagy.server.jms.RabbitMqService
import com.persagy.server.mappers.IRelationCalcMapper
import com.persagy.server.datacenter.models.wrapperobj.TwicePoint
import com.persagy.server.datacenter.models.service.BaseDataService
import com.persagy.server.datacenter.models.wrapperobj.ClassCodeEnum
import com.persagy.server.datacenter.wanda.rel.REq2Sp
import com.persagy.server.services.assistant.RUpDateEquipAndSapceService
import com.persagy.server.services.graphtype.ConnectedBlockService
import com.persagy.server.services.objects.FloorService
import com.persagy.server.services.rel.REq2SpService
import com.persagy.server.utils.IdUtils
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 com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import kotlin.collections.ArrayList

/**
 * 自动关系计算
 *
 * @author  庞利祥 <sybotan@126.com>
 */
@Service
open class RelationCalcService {
    /** 实例化 mapper 对象 */
    @Autowired
    lateinit var mapper: IRelationCalcMapper

    @Autowired
    lateinit var calcSpecialService: CalcSpecialService

    /** RabbitMQ消息对象 */
    val rabbitMqService by lazy {
        SSpringContextUtil.getBean(RabbitMqService::class.java) as RabbitMqService
    }
    val connectedBlockService = SObjectService(SMybatisDao(ConnectedBlock::class.java))

    /** 关系总览服务 */
    val relationTypeProjectservice = SBaseService(SMybatisDao(RelationTypeProject::class.java))

    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(RelationCalcService::class.java)

    } // Companion object

    /**
     * 建筑下的业务空间
     *
     * @param   projectId   项目id
     * @param   tables      参与计算的表的全名称, 带schema名, 以英文逗号隔开
     */
    fun calcBd2Sp(tables: String?, projectId: String): SBaseResponse {
        try {
            var tablesName = ""
            if (tables == null) {
                tablesName = "public.zone_air_conditioning," +
                        "public.zone_clean," +
                        "public.zone_domestic_water_supply," +
                        "public.zone_fire," +
                        "public.zone_function," +
                        "public.zone_general," +
                        "public.zone_heating," +
                        "public.zone_lighting," +
                        "public.zone_network," +
                        "public.zone_power_supply," +
                        "public.zone_security," +
                        "public.zone_tenant"
            } else {
                val nameList = tables.split(",")
                for (type in nameList) {
                    when (type) {
                        "HeatingZone" -> {
                            tablesName = tablesName + "public.zone_heating,"
                        }
                        "CleanZone" -> {
                            tablesName = tablesName + "public.zone_clean,"
                        }
                        "DomesticWaterSupplyZone" -> {
                            tablesName = tablesName + "public.zone_domestic_water_supply,"
                        }
                        "NetworkZone" -> {
                            tablesName = tablesName + "public.zone_network,"
                        }
                        "TenantZone" -> {
                            tablesName = tablesName + "public.zone_tenant,"
                        }
                        "AirConditioningZone" -> {
                            tablesName = tablesName + "public.zone_air_conditioning,"
                        }
                        "FunctionZone" -> {
                            tablesName = tablesName + "public.zone_function,"
                        }
                        "FireZone" -> {
                            tablesName = tablesName + "public.zone_fire,"
                        }
                        "SecurityZone" -> {
                            tablesName = tablesName + "public.zone_security,"
                        }
                        "GeneralZone" -> {
                            tablesName = tablesName + "public.zone_general,"
                        }
                        "PowerSupplyZone" -> {
                            tablesName = tablesName + "public.zone_power_supply,"
                        }
                        "LightingZone" -> {
                            tablesName = tablesName + "public.zone_lighting,"
                        }
                        else -> {
                            return SBaseResponse(SResponseType.failure, "空间分区类型不存在：$type")
                        }
                    }
                }
                if (tablesName != "" && !tablesName.equals("")) {
                    tablesName = tablesName.substring(0, tablesName.length - 1)
                }
            }
            upDateBiaoji(projectId, "bd2sp", 3)
            val result = mapper.calcBd2Sp(tablesName, projectId)
            if (result != null && result == 0) {
                upDate(projectId, "bd2sp")
//                upDate(projectId,"fl2sp")
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Bd2Sp", "20", "123", "456")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "bd2sp", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcBd2Sp()

    /**
     * 设备所在建筑
     * @param   projectId       项目id
     */
    fun calcEq2Bd(projectId: String): SBaseResponse {
        try {
            val result = mapper.calcEq2Bd(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Eq2Bd_in", "20", "123", "456")
                return SBaseResponse(SResponseType.success)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcEq2Bd()

    /**
     * 设备所在楼层
     * @param   projectId       项目id
     */
    fun calcEq2Fl(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "eq2fl", 3)
            val result = mapper.calcEq2Fl(projectId)
            if (result != null && result == 0) {
                upDate(projectId, "eq2fl")
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Eq2Bd_in", "20", "123", "456")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "eq2fl", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcEq2Fl()

    /**
     * 设备所在竖井
     * @param   projectId   项目id
     */
    fun calcEq2Sh(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "eq2sh", 3)
            upDateBiaoji(projectId, "eq2fl", 3)
            upDateBiaoji(projectId, "fl2sp", 3)
            val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", "eq2sp_in")).entity()
            if (entity != null) {
                entity.computationalState = 3
                relationTypeProjectservice.update(entity)
            }
            val result = mapper.calcEq2Sh(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Eq2Sh_in", "20", "123", "456")
                upDate(projectId, "eq2sh")
                upDate(projectId, "eq2sp_in")
                upDate(projectId, "fl2sp")
                upDate(projectId, "eq2fl")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "eq2sh", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
                upDateBiaoji(projectId, "fl2sp", 5)
                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", "eq2sp_in")).entity()
                if (entity != null) {
                    entity.computationalState = 5
                    relationTypeProjectservice.update(entity)
                }
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcEq2Sh()

    /**
     * 设备所在业务空间
     *
     *  @param  tables      参与计算的业务空间(为空表示所有业务空间类型)，以英文逗号隔开 HeatingZone,CleanZone,DomesticWaterSupplyZone,NetworkZone,TenantZone,AirConditioningZone FunctionZone,FireZone,SecurityZone,GeneralZone,PowerSupplyZone,LightingZone
     *  @param  projectId   项目id
     *  @param  sign        是否要删除自动维护的关系（不填默认为false）, boolean型, 值为true或false (true表示删除自动和手动维护的关系, false表示只删除手动计算的的关系)
     */
    fun calcEq2Sp(tables: String?, projectId: String, sign: Boolean,graphCode: String,relType: String): SBaseResponse {
        try {
            var tablesName = ""
            var outTablesName = ""
            var sign1Int: Int = 2
            var sign2Int: Int = 2
            if (tables == null) {
                tablesName = "public.zone_air_conditioning," +
                        "public.zone_clean," +
                        "public.zone_domestic_water_supply," +
                        "public.zone_fire," +
                        "public.zone_function," +
                        "public.zone_general," +
                        "public.zone_heating," +
                        "public.zone_lighting," +
                        "public.zone_network," +
                        "public.zone_power_supply," +
                        "public.zone_security," +
                        "public.zone_tenant"
                outTablesName = "public.r_eq_in_sp_zone_air_conditioning," +
                        "public.r_eq_in_sp_zone_clean," +
                        "public.r_eq_in_sp_zone_domestic_water_supply," +
                        "public.r_eq_in_sp_zone_fire," +
                        "public.r_eq_in_sp_zone_function," +
                        "public.r_eq_in_sp_zone_general," +
                        "public.r_eq_in_sp_zone_heating," +
                        "public.r_eq_in_sp_zone_lighting," +
                        "public.r_eq_in_sp_zone_network," +
                        "public.r_eq_in_sp_zone_power_supply," +
                        "public.r_eq_in_sp_zone_security," +
                        "public.r_eq_in_sp_zone_tenant"
            } else {
                val nameList = tables.split(",")
                for (type in nameList) {
                    when (type) {
                        "HeatingZone" -> {
                            tablesName = tablesName + "public.zone_heating,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_heating,"
                        }
                        "CleanZone" -> {
                            tablesName = tablesName + "public.zone_clean,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_clean,"
                        }
                        "DomesticWaterSupplyZone" -> {
                            tablesName = tablesName + "public.zone_domestic_water_supply,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_domestic_water_supply,"
                        }
                        "NetworkZone" -> {
                            tablesName = tablesName + "public.zone_network,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_network,"
                        }
                        "TenantZone" -> {
                            tablesName = tablesName + "public.zone_tenant,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_tenant,"
                        }
                        "AirConditioningZone" -> {
                            tablesName = tablesName + "public.zone_air_conditioning,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_air_conditioning,"
                        }
                        "FunctionZone" -> {
                            tablesName = tablesName + "public.zone_function,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_function,"
                        }
                        "FireZone" -> {
                            tablesName = tablesName + "public.zone_fire,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_fire,"
                        }
                        "SecurityZone" -> {
                            tablesName = tablesName + "public.zone_security,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_security,"
                        }
                        "GeneralZone" -> {
                            tablesName = tablesName + "public.zone_general,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_general,"
                        }
                        "PowerSupplyZone" -> {
                            tablesName = tablesName + "public.zone_power_supply,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_power_supply,"
                        }
                        "LightingZone" -> {
                            tablesName = tablesName + "public.zone_lighting,"
                            outTablesName = outTablesName + "public.r_eq_in_sp_zone_lighting,"
                        }
                        else -> {
                            return SBaseResponse(SResponseType.failure, "空间分区类型不存在：$type")
                        }
                    }
                }
                if (tablesName != "" && !tablesName.equals("")) {
                    tablesName = tablesName.substring(0, tablesName.length - 1)
                    outTablesName = outTablesName.substring(0, outTablesName.length - 1)
                }
            }
            if (sign) {
                sign1Int = 1
                sign2Int = 2
            }

            try {
//                upDateBiaoji(projectId, "eq2fl", 3)
//                upDateBiaoji(projectId, "fl2sp", 3)

                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", relType
                ), SFilter.eq("zoneType", tables!!), SFilter.eq("graphicType",graphCode)).entity()
                if (entity != null) {
                    entity.computationalState = 3
                    entity.computingTime = IdUtils.time()
                    relationTypeProjectservice.update(entity)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            val result = mapper.calcEq2Sp(projectId)
            if (result != null && result == 0) {
                try {
                    /** 发送消息 */
                    rabbitMqService.sendRel(Opt.projectId!!, "Eq2Sp_in", "20", tables!!, "456")
                    upDate(projectId, "fl2sp")
                    upDate(projectId, "eq2fl")
                    val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", relType
                    ), SFilter.eq("zoneType", tables!!), SFilter.eq("graphicType",graphCode)).entity()
                    if (entity != null) {
                        entity.computationalState = 1
                        entity.computingTime = IdUtils.time()
                        relationTypeProjectservice.update(entity)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return SBaseResponse(SResponseType.success)
            } else {
//                upDateBiaoji(projectId, "eq2fl", 5)
//                upDateBiaoji(projectId, "fl2sp", 5)
                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", relType
                ), SFilter.eq("zoneType", tables!!), SFilter.eq("graphicType",graphCode)).entity()
                if (entity != null) {
                    entity.computationalState = 5
                    entity.computingTime = IdUtils.time()
                    relationTypeProjectservice.update(entity)
                }
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcEq2Sp()

    /**
     * 楼层贯通关系
     *
     * @param   projectId   项目id
     * @param   sign1       表示r_fl_through_fl表中要被删除的sign值
     * @param   sign2       同上 与sign1是逻辑或的关系
     */
    fun calcFl2Fl(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "fl2fl", 3)
            val result = mapper.calcFl2Fl(projectId, 2, 2)
            if (result != null && result == 0) {
                try {
                    /** 发送消息 */
                    rabbitMqService.sendRel(Opt.projectId!!, "Fl2Fl_Through", "20", "123", "456")
                    val entity = RUpDateEquipAndSapceService.relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationTypeCode", "Fl2Fl")).entity()
                    if (entity != null) {
                        entity.computationalState = 1
                        RUpDateEquipAndSapceService.relationTypeProjectservice.update(entity)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "fl2fl", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcFl2Fl()

    /**
     * 楼层下的业务空间
     *
     * @param   projectId   项目id
     * @param   tables      参与计算的表的全名称, 带schema名, 以英文逗号隔开
     */
    fun calcFl2Sp(tables: String?, projectId: String): SBaseResponse {
        try {
            var tablesName = ""
            if (tables == null) {
                tablesName = "public.zone_air_conditioning," +
                        "public.zone_clean," +
                        "public.zone_domestic_water_supply," +
                        "public.zone_fire," +
                        "public.zone_function," +
                        "public.zone_general," +
                        "public.zone_heating," +
                        "public.zone_lighting," +
                        "public.zone_network," +
                        "public.zone_power_supply," +
                        "public.zone_security," +
                        "public.zone_tenant"
            } else {
                val nameList = tables.split(",")
                for (type in nameList) {
                    when (type) {
                        "HeatingZone" -> {
                            tablesName = tablesName + "public.zone_heating,"
                        }
                        "CleanZone" -> {
                            tablesName = tablesName + "public.zone_clean,"
                        }
                        "DomesticWaterSupplyZone" -> {
                            tablesName = tablesName + "public.zone_domestic_water_supply,"
                        }
                        "NetworkZone" -> {
                            tablesName = tablesName + "public.zone_network,"
                        }
                        "TenantZone" -> {
                            tablesName = tablesName + "public.zone_tenant,"
                        }
                        "AirConditioningZone" -> {
                            tablesName = tablesName + "public.zone_air_conditioning,"
                        }
                        "FunctionZone" -> {
                            tablesName = tablesName + "public.zone_function,"
                        }
                        "FireZone" -> {
                            tablesName = tablesName + "public.zone_fire,"
                        }
                        "SecurityZone" -> {
                            tablesName = tablesName + "public.zone_security,"
                        }
                        "GeneralZone" -> {
                            tablesName = tablesName + "public.zone_general,"
                        }
                        "PowerSupplyZone" -> {
                            tablesName = tablesName + "public.zone_power_supply,"
                        }
                        "LightingZone" -> {
                            tablesName = tablesName + "public.zone_lighting,"
                        }
                        else -> {
                            return SBaseResponse(SResponseType.failure, "空间分区类型不存在：$type")
                        }
                    }
                }
                if (tablesName != "" && !tablesName.equals("")) {
                    tablesName = tablesName.substring(0, tablesName.length - 1)
                }
            }
            upDateBiaoji(projectId, "fl2sp", 3)
            val result = mapper.calcFl2Sp(tablesName, projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Fl2Sp_in", "20", tables!!, "456")
                upDate(projectId, "fl2sp")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "fl2sp", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcFl2Sp()

    /**
     * 资产所在建筑
     *
     * @param   projectId        项目id
     */
    fun calcPe2Bd(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "pe2bd", 3)
            upDateBiaoji(projectId, "eq2fl", 3)
            val result = mapper.calcPe2Bd(projectId)
            if (result != null && result == 0) {
                upDate(projectId, "pe2bd")
                upDate(projectId, "eq2fl")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "pe2bd", 5)
                upDateBiaoji(projectId, "eq2fl", 2)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcPe2Bd()

    /**
     * 资产所在楼层
     *
     * @param   projectId     项目id
     */
    fun calcPe2Fl(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "pe2fl", 3)
            upDateBiaoji(projectId, "eq2fl", 3)
            val result = mapper.calcPe2Fl(projectId)
            if (result != null && result == 0) {
                upDate(projectId, "pe2fl")
                upDate(projectId, "eq2fl")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "pe2fl", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcPe2Fl()

    /**
     * 资产所在竖井
     *
     * @param   projectId       项目id
     */
    fun calcPe2Sh(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "pe2sh", 3)
            upDateBiaoji(projectId, "fl2sp", 3)
            upDateBiaoji(projectId, "eq2sp_in", 3)
            upDateBiaoji(projectId, "pe2sp", 3)
            upDateBiaoji(projectId, "eq2sh", 3)
            upDateBiaoji(projectId, "eq2fl", 3)
            val result = mapper.calcPe2Sh(projectId)
            if (result != null && result == 0) {
                upDate(projectId, "pe2sh")
                upDate(projectId, "fl2sp")
                upDate(projectId, "eq2sp_in")
                upDate(projectId, "pe2sp")
                upDate(projectId, "eq2sh")
                upDate(projectId, "eq2fl")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "pe2sh", 5)
                upDateBiaoji(projectId, "fl2sp", 5)
                upDateBiaoji(projectId, "eq2sp_in", 5)
                upDateBiaoji(projectId, "pe2sp", 5)
                upDateBiaoji(projectId, "eq2sh", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcPe2Sh()

    /**
     * 资产所在业务空间
     *
     * @param   projectId       项目id
     */
    fun calcPe2Sp(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "eq2fl", 3)
            upDateBiaoji(projectId, "fl2sp", 3)
            upDateBiaoji(projectId, "eq2sp_in", 3)
            upDateBiaoji(projectId, "pe2sp", 3)
            val result = mapper.calcPe2Sp(projectId)
            if (result != null && result == 0) {
                upDate(projectId, "pe2sp")
                upDate(projectId, "eq2fl")
                upDate(projectId, "fl2sp")
                upDate(projectId, "eq2sp_in")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "pe2sp", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
                upDateBiaoji(projectId, "fl2sp", 5)
                upDateBiaoji(projectId, "eq2sp_in", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcPe2Sp()

    /**
     * 建筑下的竖井
     *
     * @param   projectId       项目id
     */
    fun calcSh2Bd(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sh2bd", 3)
            val result = mapper.calcSh2Bd(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Bd2Sh_in", "20", "123", "456")
                upDate(projectId, "sh2bd")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sh2bd", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcSh2Bd()

    /**
     * 竖井贯通
     *
     * @param   projectId       项目id
     */
    fun calcSh2Sh(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sh2sh", 3)
            val result = mapper.calcSh2Sh(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Sh2Sh_Through", "20", "123", "456")
                upDate(projectId, "sh2sh")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sh2sh", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcPe2Sp()

    /**
     * 建筑下的系统设备部件
     *
     * @param   projectId       项目id
     */
    fun calcSyEq2Bd(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "syeq2bd", 3)
            upDateBiaoji(projectId, "eq2fl", 3)
            val resultEq = mapper.calcEq2Bd(projectId)
            val resultSy = mapper.calcSy2Bd(projectId)

            logger.debug("设备= ${resultEq}")
            logger.debug("系统= ${resultSy}")
            if (resultEq != null && resultSy != null && resultEq == 0 && resultSy == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "syeq2bd", "20", "123", "456")
                upDate(projectId, "syeq2bd")
                upDate(projectId, "eq2fl")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "syeq2bd", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcSyEq2Bd()

    /**
     * 系统所在建筑
     *
     * @param   projectId       项目id
     */
    fun calcSy2Bd(projectId: String): SBaseResponse {
        try {
            val result = mapper.calcSy2Bd(projectId)
            if (result != null && result == 0) {
//                upDate(projectId,"sh2sh")
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "syeq2bd", "20", "123", "456")
                return SBaseResponse(SResponseType.success)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcSy2Bd()

    /**
     * 系统所在楼层
     *
     * @param   projectId       项目id
     */
    fun calcSy2Fl(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sy2fl", 3)
            upDateBiaoji(projectId, "eq2fl", 3)
            val result = mapper.calcSy2Fl(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Sy2Fl_in", "20", "123", "456")
                upDate(projectId, "sy2fl")
                upDate(projectId, "eq2fl")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sy2fl", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcSy2Fl()

    /**
     * 系统所在竖井
     *
     * @param   projectId       项目id
     */
    fun calcSy2Sh(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sy2sh", 3)
            upDateBiaoji(projectId, "eq2sh", 3)
            upDateBiaoji(projectId, "eq2fl", 3)
            upDateBiaoji(projectId, "fl2sp", 3)
            upDateBiaoji(projectId, "eq2sp_in", 3)
            val result = mapper.calcSy2Sh(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Sy2Sh_in", "20", "123", "456")
                upDate(projectId, "sy2sh")
                upDate(projectId, "eq2sh")
                upDate(projectId, "eq2fl")
                upDate(projectId, "fl2sp")
                upDate(projectId, "eq2sp_in")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sy2sh", 5)
                upDateBiaoji(projectId, "eq2sh", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
                upDateBiaoji(projectId, "fl2sp", 5)
                upDateBiaoji(projectId, "eq2sp_in", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcSy2Sh()

    /**
     * 系统所在业务空间
     *
     * @param   projectId       项目id
     */
    fun calcSy2Sp(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "eq2fl", 3)
            upDateBiaoji(projectId, "fl2sp", 3)
            upDateBiaoji(projectId, "sy2sp", 3)
            upDateBiaoji(projectId, "eq2sp_in", 3)
            val result = mapper.calcSy2Sp(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "Sy2Sp_in", "20", "123", "456")
                upDate(projectId, "sy2sp")
                upDate(projectId, "fl2sp")
                upDate(projectId, "sy2sp")
                upDate(projectId, "eq2sp_in")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sy2sp", 5)
                upDateBiaoji(projectId, "eq2fl", 5)
                upDateBiaoji(projectId, "fl2sp", 5)
                upDateBiaoji(projectId, "eq2sp_in", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function calcSy2Sp()


    /**
     * 普通交通
     */
    fun sp2spTransport(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sp2sp_TrafficNetwork_1", 3)
            val result = mapper.calcsp2spTransport(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "sp2sp_TrafficNetwork_1", "20", "123", "456")
                upDate(projectId, "sp2sp_TrafficNetwork_1")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sp2sp_TrafficNetwork_1", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Fun

    /**
     * 光照连通
     */
    fun sp2spLight(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sp2sp_RadiationNetwork_1", 3)
            val result = mapper.calcsp2spLight(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "sp2sp_RadiationNetwork_1", "20", "123", "456")
                upDate(projectId, "sp2sp_RadiationNetwork_1")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sp2sp_RadiationNetwork_1", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Fun

    /**
     * 空气自然对流
     */
    fun sp2spConvection(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sp2sp_ConvectionNetwork_1", 3)
            val result = mapper.calcsp2spConvection(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "sp2sp_ConvectionNetwork_1", "20", "123", "456")
                upDate(projectId, "sp2sp_ConvectionNetwork_1")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sp2sp_ConvectionNetwork_1", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    }

    /**
     * 空间连通
     */
    fun sp2spV2(projectId: String): SBaseResponse {
        try {
            upDateBiaoji(projectId, "sp2sp_SpaceNeighborhood_5", 3)
            val result = mapper.calcsp2spV2(projectId)
            if (result != null && result == 0) {
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "sp2sp_SpaceNeighborhood_5", "20", "123", "456")
                upDate(projectId, "sp2sp_SpaceNeighborhood_5")
                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "sp2sp_SpaceNeighborhood_5", 5)
            }
            return SBaseResponse(SResponseType.failure)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    }


    /**
     * 修改标记
     */
    fun upDate(projectId: String, type: String) {
        try {

            val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", type)).entity()
            if (entity != null) {
                entity.computationalState = 1
                entity.computingTime = IdUtils.time()
                RUpDateEquipAndSapceService.relationTypeProjectservice.update(entity)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } // Fun

    /**
     * 计算中标记
     */
    fun upDateBiaoji(projectId: String, type: String, sing: Int) {
        try {
            val list = ArrayList<String>()
            list.add("ComputationalState")
            list.add("ComputingTime")
            if (sing == 3) {
                logger.debug("type1= $type********************************************")
                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", type)).entity()
                if (entity != null) {
                    entity.computationalState = 3
                    entity.computingTime = IdUtils.time()
                    logger.debug("computingTime= ${entity.computingTime},graphic_id =${entity.graphicId} ,code = ${entity.relationTypeCode},id= ${entity.id},project_id = ${entity.projectId}********************************************")
                    val update = relationTypeProjectservice.update(entity, list)
                    logger.debug("update= ${update}********************************************")
                }
                logger.debug("type= $type********************************************")
            } else {
                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", type)).entity()
                if (entity != null) {
                    entity.computationalState = 5
                    entity.computingTime = IdUtils.time()
                    relationTypeProjectservice.update(entity, list)
                }
                logger.debug("type3= $type********************************************")
            }
            logger.debug("type2= $type********************************************")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } // Fun

    /**
     * 设置标记
     */
    fun setMarks(projectId: String): SBaseResponse {
        try {
            /** 模型重新上传做标记 */
            val list = ArrayList<String>()
            list.add("eq2fl")
            list.add("fl2sp")
            list.add("bd2sp")
            list.add("eq2sp_in")
            for (type in list) {
                val entityList = RUpDateEquipAndSapceService.relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", type)).exec()
                if (entityList.size > 0) {
                    for (entity in entityList) {
                        entity.computationalState = 2
                        RUpDateEquipAndSapceService.relationTypeProjectservice.update(entity)
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }

    } // Fun


    /**
     * 设备所在业务空间  温度传感器（SETP）、湿度传感器（SERH）、浓度传感器（SECT）  传感器测量设备关系
     *
     *  @param  tables      参与计算的业务空间(为空表示所有业务空间类型)，以英文逗号隔开 HeatingZone,CleanZone,DomesticWaterSupplyZone,NetworkZone,TenantZone,AirConditioningZone FunctionZone,FireZone,SecurityZone,GeneralZone,PowerSupplyZone,LightingZone
     *  @param  projectId   项目id
     *  @param  sign        是否要删除自动维护的关系（不填默认为false）, boolean型, 值为true或false (true表示删除自动和手动维护的关系, false表示只删除手动计算的的关系)
     */
    fun calcss2Sp(projectId: String,graphCode: String): SBaseResponse {
        try {

            try {
                upDateBiaoji(projectId, "eq2fl", 3)
                upDateBiaoji(projectId, "fl2sp", 3)

                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", "eq2sp_SensorRelationship_ss2sp")).entity()
                if (entity != null) {
                    entity.computationalState = 3
                    relationTypeProjectservice.update(entity)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            /** 启动计算 */
            val result = mapper.calcEq2Sp(projectId!!)
            if (result != null && result == 0) {
                try {
                    /** 发送消息 */
                    rabbitMqService.sendRel(Opt.projectId!!, "eq2eq_SensorRelationship_ss2eq", "20", "123", "456")
                    upDate(projectId, "fl2sp")
                    upDate(projectId, "eq2fl")
                    val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", "eq2sp_SensorRelationship_ss2sp")).entity()
                    if (entity != null) {
                        entity.computationalState = 1
                        relationTypeProjectservice.update(entity)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }


                /** 处理关系 */
                val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId),
                        SFilter.inList("classCode",
                                arrayListOf("SETP", "SERH", "SECT"))).exec()
                logger.debug("设备长度: ${equipmentList.size}")
                logger.debug("设备长度: ${equipmentList.size}")
                if (equipmentList.size > 0) {
                    var listId = ArrayList<String>()
                    for (equipment in equipmentList) {
                        listId.add(equipment.id!!)
                        logger.debug("设备id: ${equipment.id}")
                    }
                    val rEq2SpList = REq2SpService.select(SFilter.eq("projectId", projectId),
                            SFilter.inList("id1", listId)
                            , SFilter.eq("type", "eq2sp_in"), SFilter.eq("type", "AirConditioningZone")).exec()
                    logger.debug("关系长度: ${rEq2SpList.size}")
                    if (rEq2SpList.size > 0) {
                        for (rEq2Sp in rEq2SpList) {
                            rEq2Sp.type = "eq2sp_SensorRelationship_ss2sp"
                            REq2SpService.replace(rEq2Sp)
                        }
                    }
                }

                return SBaseResponse(SResponseType.success)
            } else {
                upDateBiaoji(projectId, "eq2fl", 5)
                upDateBiaoji(projectId, "fl2sp", 5)
                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", "eq2sp_SensorRelationship_ss2sp")).entity()
                if (entity != null) {
                    entity.computationalState = 5
                    relationTypeProjectservice.update(entity)
                }
            }

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

    /**
     * 设备服务业务空间  风口（空调风口、通风风口）
     *
     *  @param  tables      参与计算的业务空间(为空表示所有业务空间类型)，以英文逗号隔开 HeatingZone,CleanZone,DomesticWaterSupplyZone,NetworkZone,TenantZone,AirConditioningZone FunctionZone,FireZone,SecurityZone,GeneralZone,PowerSupplyZone,LightingZone
     *  @param  projectId   项目id
     *  @param  sign        是否要删除自动维护的关系（不填默认为false）, boolean型, 值为true或false (true表示删除自动和手动维护的关系, false表示只删除手动计算的的关系)
     */
    fun calcEqForSp(tables: String?, projectId: String,graphCode: String,relType: String): SBaseResponse {


        try {

            calcSpecialService.calcNetworks(projectId)
            val codeList= ClassCodeEnum.toSourceList(1)

            val connectedBlocks = connectedBlockService.select(SFilter.eq("projectId",projectId), SFilter.or(SFilter.inList("typeCode1",codeList), SFilter.inList("typeCode2",codeList))).exec()

            var sqlBuilder = arrayListOf(SFilter.eq("projectId", projectId), SFilter.eq("relationType", relType
            ), SFilter.eq("graphicType",graphCode))

            if(tables != null){
                sqlBuilder.add(SFilter.eq("zoneType", tables!!))
            }
            val entity = relationTypeProjectservice.select(sqlBuilder).entity()
            if (entity != null) {
                entity.computationalState = 3
                relationTypeProjectservice.update(entity)
            }
            /** 启动计算   ******************************/
            val result = mapper.calcEq2Sp(projectId)
            logger.debug("计算结果= ${result}")

            /*************************上面添加数据**********************************/

            if (result != null && result == 0) {

                if (entity != null) {
                    entity.computationalState = 1
                    entity.computingTime = IdUtils.time()
                    relationTypeProjectservice.update(entity)
                }

                val sourceList = ClassCodeEnum.toSourceList(0)
                /** 处理关系 */
                val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId),
                        SFilter.inList("classCode",
                                arrayListOf("ACATIO", "ACVTIO","ACATFC","ACVTAC","FFSCIO"))).exec()
                val typeMap = HashMap<String,String>();
                if (equipmentList.size > 0) {
                    var listId = ArrayList<String>()
                    for (equipment in equipmentList) {
                        listId.add(equipment.id!!)
                        typeMap[equipment.id!!] = equipment.classCode!!
                    }
                    val rbuilder = arrayListOf(SFilter.eq("projectId", projectId),
                    SFilter.inList("id1", listId)
                    , SFilter.eq("graphCode", "MechInArch"), SFilter.eq("type",relType))

                    if(tables != null){
                        rbuilder.add( SFilter.eq("zoneType",tables!!))
                    }

                    val rEq2SpList = REq2SpService.select(rbuilder).exec()
                    val rEq2Sps = ArrayList<REq2Sp>()
                    if (rEq2SpList.size > 0) {
                        for (rEq2Sp in rEq2SpList) {
                            rEq2Sp.type = relType
                            rEq2Sp.graphCode = graphCode
                            rEq2Sps.add(rEq2Sp)
//                            REq2SpService.replace(rEq2Sp)
                            //获取设备类型
                            val typeCode = typeMap[rEq2Sp.id1]
                            //如果是风口才计算关系
                            if( typeCode!= null && sourceList.contains(typeCode)){
                                for (connectedBlock in connectedBlocks) {
                                    val typeCode1 = connectedBlock.typeCode1
                                    val typeCode2 = connectedBlock.typeCode2
                                    if(codeList.contains(typeCode2) && connectedBlock.id1 == rEq2Sp.id1){
                                        val fullREq2Sp = REq2Sp()
                                        fullREq2Sp.type = relType
                                        fullREq2Sp.graphCode = graphCode
                                        fullREq2Sp.projectId = projectId
                                        fullREq2Sp.id1 = connectedBlock.id2
                                        fullREq2Sp.id2 = rEq2Sp.id2
                                        fullREq2Sp.zoneType = rEq2Sp.zoneType
                                        fullREq2Sp.sign =2
                                        rEq2Sps.add(fullREq2Sp)
                                    }else if(codeList.contains(typeCode1)  && connectedBlock.id2 == rEq2Sp.id1){
                                        val fullREq2Sp = REq2Sp()
                                        fullREq2Sp.type = relType
                                        fullREq2Sp.graphCode = graphCode
                                        fullREq2Sp.projectId = projectId
                                        fullREq2Sp.id1 = connectedBlock.id1
                                        fullREq2Sp.id2 = rEq2Sp.id2
                                        fullREq2Sp.zoneType = rEq2Sp.zoneType
                                        fullREq2Sp.sign =2
                                        rEq2Sps.add(fullREq2Sp)
                                    }
                                }
                            }

                        }
                    }
                    for (rEq2Sp in rEq2Sps) {
                        REq2SpService.replace(rEq2Sp)
                    }

                }
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "fengkou_for", "20", "123", "456")
                return SBaseResponse(SResponseType.success)
            } else {
                if (entity != null) {
                    entity.computationalState = 5
                    entity.computingTime = IdUtils.time()
                    relationTypeProjectservice.update(entity)
                }
            }

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


    /**
     * @Description: 空间交通关系
     * @Param:
     * @Return:
     */
    fun calcSp2Sp(projectId: String,buildingId: String,objectType: String,type: String):SBaseResponse{
        val distance = 100.00
        val floors = FloorService.select(SFilter.eq("projectId",projectId), SFilter.eq("buildingId",buildingId)).exec()
        val rSp2Sps =  ArrayList<RSp2Sp>()
        for (floor in floors) {
            if(!floor.modelId.isNullOrEmpty()){
                val zoneSpaces = ZoneSpaceBaseService.select(SFilter.eq("projectId",projectId), SFilter.eq("floorId",floor.id!!), SFilter.eq("classCode",objectType)).exec()
                val doors = BaseDataService.doorService.select(SFilter.eq("modelId",floor.modelId!!)).exec()
                val pointList = ArrayList<TwicePoint>()

                for (door in doors) {
                    val outline = door.outline
                    val point = door!!.location!!.points?.get(0)!!
                    if(!outline.isNullOrEmpty()){
                        val point1 = outline[0]!![0]
                        val point2 = outline[0]!![1]
                        val k = get_points(point1.x,point1.y,point2.x,point2.y)
                        var twicePoint: TwicePoint
                        if(k== null){
                            twicePoint = TwicePoint(point.x, point.y + distance, point.x, point.y - distance)
                        }else{
//                            val distance = get_segment_distance(point1.x,point1.y,point2.x,point2.y)
                            twicePoint = get_point_by_distance(point.x,point.y,k,distance*2)
                        }
                        pointList.add(twicePoint)
                    }
                }

                val map = HashMap<Int,ArrayList<String>>()
                for (index in 0 until pointList.size) {
                    val mPoint1= MPoint(pointList[index].x1!!,pointList[index].y1!!,0.00)
                    val mPoint2= MPoint(pointList[index].x2!!,pointList[index].y2!!,0.00)
                    var isLocal1: Boolean
                    var isLocal2: Boolean

                    for (zoneSpace in zoneSpaces) {
                        val zoneOutline = zoneSpace.outline
                        if(zoneOutline.isNullOrEmpty()){
                            continue
                        }
                        isLocal1 = CommonService.isPointInPolygonCheckWithJdkGeneralPath(mPoint1, zoneOutline?.get(0)?.get(0)!!)
                        isLocal2 = CommonService.isPointInPolygonCheckWithJdkGeneralPath(mPoint2, zoneOutline?.get(0)?.get(0)!!)
                        val spaceId = zoneSpace.id!!
                        if(isLocal1 || isLocal2)
                            if(map.containsKey(index)){
                                val list = map[index]!!
                                list.add(spaceId)
                                map[index] = list
                            }else{
                                map[index] = arrayListOf(spaceId)
                            }

                    }

                }
                //map遍历
                for (v in map.values) {

                    val key = v[0] !!
                    if(v.size > 1){
                        for (index in  1 until v.size) {
                            val rSp2SP = RSp2Sp()
                            rSp2SP.id1 = key
                            rSp2SP.id2 = v[index]
                            rSp2SP.projectId = projectId
                            rSp2SP.sign = 1
                            rSp2SP.type = type
                            rSp2SP.zoneType = objectType
                            if(!rSp2Sps.contains(rSp2SP)){
                                rSp2Sps.add(rSp2SP)
                            }
                        }
                    }



                }
            }
            val rSp2SpService = RService(SMybatisDao(RSp2Sp::class.java))
            for (rSp2Sp in rSp2Sps) {
                rSp2SpService.replace(rSp2Sp)
            }
        }
        return SBaseResponse(SResponseType.success,"")
    }


    fun get_points(x1: Double, y1: Double , x2: Double, y2: Double): Double?{
        if (x1 == y1 && x2 == y2){
            return null
        }
        var k:Double? = null
        // 计算当前线段的 k
        if (x1 == x2){

        }else{
            k = (y2 - y1) / (x2 - x1)
        }
        var k2:Double?
        //计算垂直方向的k
        if (k == null){
            k2 = 0.00
        }else if(k == 0.00){
            k2 = null
        }else{
            k2 = 1/k
        }
        return k2
    }

    //获取两点之间的距离
    fun get_segment_distance(x1: Double, y1: Double , x2: Double, y2: Double): Double{
        val x_diff = x2 - x1
        val y_diff = y2 - y1
        return Math.sqrt(x_diff * x_diff + y_diff * y_diff)
    }

    //获取坐标
    fun  get_point_by_distance(base_x: Double, base_y: Double, k: Double, distance: Double): TwicePoint {
        if(k == null){
            return TwicePoint(base_x, base_y + distance, base_x, base_y - distance)
        }
        val vector_x1 = Math.sqrt(distance * distance / (1 + k * k))
        val vector_x2 = -vector_x1
        val vector_y1 = k * vector_x1
        val vector_y2 = k * vector_x2
        return TwicePoint(base_x + vector_x1, base_y + vector_y1, base_x + vector_x2, base_y + vector_y2)
    }

} // Class RelationCalcService