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

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.Opt
import com.persagy.server.datacenter.models.entities.graphtype.RelationTypeProject
import com.persagy.server.datacenter.models.entities.rel.REq2Sp
import com.persagy.server.jms.RabbitMqService
import com.persagy.server.mappers.IRelationCalcMapper
import com.persagy.server.services.assistant.RUpDateEquipAndSapceService
import com.persagy.server.services.objects.EquipmentService
import com.persagy.server.services.objects.ZoneSpaceBaseService
import com.persagy.server.services.rel.REq2SpService
import com.persagy.server.utils.IdUtils
import com.persagy.service.SBaseService
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 java.util.*
import kotlin.collections.ArrayList

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

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

    /** 关系总览服务 */
    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): 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", "eq2sp_in"), SFilter.eq("zoneType", tables!!)).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", "eq2sp_in"), SFilter.eq("zoneType", tables!!)).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", "eq2sp_in"), SFilter.eq("zoneType", tables!!)).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): 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): SBaseResponse {


        try {

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

                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", "eq2sp_for"), SFilter.eq("zoneType", tables!!)).entity()
                if (entity != null) {
                    entity.computationalState = 3
                    relationTypeProjectservice.update(entity)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            /** 启动计算   ******************************/
            val result = mapper.calcEq2Sp(projectId)
//            var result = 0
            logger.debug("计算结果= ${result}")

            var equipment = EquipmentService.select(SFilter.eq("localId", "20.30.40.50.10.0001"), SFilter.contain("bimId", "3353659"), SFilter.eq("projectId", projectId)).entity()
            var space = ZoneSpaceBaseService.select(SFilter.eq("localName", "F1-补风机房-01"), SFilter.eq("projectId", projectId)).entity()
            val list = ArrayList<REq2Sp>()
            if (equipment != null && space != null) {
                val req = REq2Sp()
                req.id1 = equipment.id
                req.id2 = space.id
                req.projectId = projectId
                req.sign = 1
                req.type = "eq2sp_for"
                req.zoneType = "GeneralZone"
                req.lastUpdate = Date()
                list.add(req)
            }

            var equipment2 = EquipmentService.select(SFilter.eq("localId", "20.30.30.30.10.10.0860"), SFilter.contain("bimId", "3360171"), SFilter.eq("projectId", projectId)).entity()
            var space2 = ZoneSpaceBaseService.select(SFilter.eq("localName", "F1-女卫生间-02"), SFilter.eq("projectId", projectId)).entity()
            if (equipment2 != null && space2 != null) {
                val req = REq2Sp()
                req.id1 = equipment2.id
                req.id2 = space2.id
                req.projectId = projectId
                req.sign = 1
                req.type = "eq2sp_for"
                req.zoneType = "GeneralZone"
                req.lastUpdate = Date()
                list.add(req)
                if(equipment != null){
                    val req = REq2Sp()
                    req.id1 = equipment.id
                    req.id2 = space2.id
                    req.projectId = projectId
                    req.sign = 1
                    req.type = "eq2sp_for"
                    req.zoneType = "GeneralZone"
                    req.lastUpdate = Date()
                    list.add(req)
                }
            }


            var equipment3 = EquipmentService.select(SFilter.eq("localId", "20.30.30.30.10.10.0861"), SFilter.contain("bimId", "3360429"), SFilter.eq("projectId", projectId)).entity()
            var equipment4 = EquipmentService.select(SFilter.eq("localId", "20.30.30.30.10.10.0862"), SFilter.contain("bimId", "3360571"), SFilter.eq("projectId", projectId)).entity()
            var space3 = ZoneSpaceBaseService.select(SFilter.eq("localName", "F1-卫生间前室-02"), SFilter.eq("projectId", projectId)).entity()
            if (equipment3 != null && space3 != null) {
                val req = REq2Sp()
                req.id1 = equipment3.id
                req.id2 = space3.id
                req.projectId = projectId
                req.sign = 1
                req.type = "eq2sp_for"
                req.zoneType = "GeneralZone"
                req.lastUpdate = Date()
                list.add(req)
                if(equipment != null){
                    val req = REq2Sp()
                    req.id1 = equipment.id
                    req.id2 = space3.id
                    req.projectId = projectId
                    req.sign = 1
                    req.type = "eq2sp_for"
                    req.zoneType = "GeneralZone"
                    req.lastUpdate = Date()
                    list.add(req)
                }
            }

            if (equipment4 != null && space3 != null) {
                val req = REq2Sp()
                req.id1 = equipment4.id
                req.id2 = space3.id
                req.projectId = projectId
                req.sign = 1
                req.type = "eq2sp_for"
                req.zoneType = "GeneralZone"
                req.lastUpdate = Date()
                list.add(req)
            }

            var equipment5 = EquipmentService.select(SFilter.eq("localId", "20.30.30.30.10.10.0863"), SFilter.contain("bimId", "3360088"), SFilter.eq("projectId", projectId)).entity()
            var equipment6 = EquipmentService.select(SFilter.eq("localId", "20.30.30.30.10.10.0864"), SFilter.contain("bimId", "3359745"), SFilter.eq("projectId", projectId)).entity()
            var space4 = ZoneSpaceBaseService.select(SFilter.eq("localName", "F1-男卫生间-02"), SFilter.eq("projectId", projectId)).entity()

            if (equipment5 != null && space4 != null && equipment6 != null) {
                val req1 = REq2Sp()
                req1.id1 = equipment5.id
                req1.id2 = space4.id
                req1.projectId = projectId
                req1.sign = 1
                req1.type = "eq2sp_for"
                req1.zoneType = "GeneralZone"
                req1.lastUpdate = Date()
                list.add(req1)
                val req2 = REq2Sp()
                req2.id1 = equipment6.id
                req2.id2 = space4.id
                req2.projectId = projectId
                req2.sign = 1
                req2.type = "eq2sp_for"
                req2.zoneType = "GeneralZone"
                req2.lastUpdate = Date()
                list.add(req2)
                if(equipment != null){
                    val req = REq2Sp()
                    req.id1 = equipment.id
                    req.id2 = space4.id
                    req.projectId = projectId
                    req.sign = 1
                    req.type = "eq2sp_for"
                    req.zoneType = "GeneralZone"
                    req.lastUpdate = Date()
                    list.add(req)
                }
            }

//            for (rEq2Sp in list) {
//
//            }
////                val element = REq2SpService.select(SFilter.eq("id1",mapKey), SFilter.eq("id2",mapValue)).exec()
////                if(element != null && element.size >0){
////                    continue
////                }
//                val req = REq2Sp()
//                req.id1 = mapKey
//                req.id2 = mapValue
//                req.projectId = projectId
//                req.sign = 1
//                req.type = "eq2sp_for"
//                req.zoneType = "GeneralZone"
//                req.lastUpdate = Date()
            for (rEq2Sp in list) {
                REq2SpService.replace(rEq2Sp)
            }

            Thread.sleep(2000000);

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


            try {
// //               val r1 = mapper.deletesp(projectId)
//                val r1 = REq2SpService.delete(SFilter.eq("projectId", projectId), SFilter.eq("sign0", 3))
//                val r2 = mapper.insertsp(projectId)
//                logger.debug("设备关联业务空间计算结果= ${r1}+${r2}")
            } catch (e: Exception) {
                e.printStackTrace()
            }


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

                    upDate(projectId, "fl2sp")
                    upDate(projectId, "eq2fl")
                    val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId), SFilter.eq("relationType", "eq2sp_for"), SFilter.eq("zoneType", tables!!)).entity()
                    if (entity != null) {
                        entity.computationalState = 1
                        entity.computingTime = IdUtils.time()
                        relationTypeProjectservice.update(entity)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }


                /** 处理关系 */
                val equipmentList = EquipmentService.select(SFilter.eq("projectId", projectId),
                        SFilter.inList("classCode",
                                arrayListOf("ACATIO", "ACVTIO","ACATFC","ACVTAC"))).exec()
                if (equipmentList.size > 0) {
                    var listId = ArrayList<String>()
                    for (equipment in equipmentList) {
                        listId.add(equipment.id!!)
                    }
                    val rEq2SpList = REq2SpService.select(SFilter.eq("projectId", projectId),
                            SFilter.inList("id1", listId)
                            , SFilter.eq("type", "eq2sp_in")).exec()
                    if (rEq2SpList.size > 0) {
                        for (rEq2Sp in rEq2SpList) {
                            rEq2Sp.type = "eq2sp_for"
                            REq2SpService.replace(rEq2Sp)
                        }
                    }
                }
                /** 发送消息 */
                rabbitMqService.sendRel(Opt.projectId!!, "fengkou_for", "20", "123", "456")
                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_for"), SFilter.eq("zoneType", tables!!)).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!!)
        }
    }

} // Class RelationCalcService