package com.persagy.server.services.rel

import com.persagy.server.datacenter.models.entities.rel.RSpInSyBaseList
import com.persagy.server.datacenter.models.entities.rel.RSy2Sp
import com.persagy.server.datacenter.models.entities.rel.RSyInSpBase
import com.persagy.server.datacenter.models.entities.rel.RSyInSpBaseList
import com.persagy.server.services.base.RService
import com.persagy.server.services.objects.EquipmentService
import com.persagy.server.services.objects.ZoneSpaceBaseService
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse
import org.json.JSONObject

/**
 * 系统所在业务空间关系服务
 *
 * @author 付国宝
 */
object RSyInSpBaseService: RService<RSyInSpBase>(SMybatisDao(RSyInSpBase::class.java)) {

    private var rSy2SpService = SObjectService(SMybatisDao(RSy2Sp::class.java))
    /**
     * 创建系统所在业务空间关系
     *
     * @param   rSyInSpBaseList    系统所在业务空间关系对象
     */
    fun create(rSyInSpBaseList: SCreateRequest<RSyInSpBase>): SCreateResponse<RSyInSpBase> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            for (rSyInSpBase in rSyInSpBaseList.content){
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id1 = rSyInSpBase.sysId
                rSy2Sp.id2 = rSyInSpBase.spaceId
                rSy2Sp.sign = 1
                rSy2Sp.zoneType = rSyInSpBase.objectType
                rSy2Sp.type = "sy2sp"
                rSy2Sp.projectId = projectId
                val isInsert = rSy2SpService.insert(rSy2Sp)
                if (isInsert){
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "11")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSyInSpBase.spaceId)
                    jsonObject.put("obj_type", "Sy2Sp")
                    jsonObject.put("linkId",rSyInSpBase.sysId)
                    /** 发送消息 */
                    messageHandler.sendMessage(jsonObject.toString())
                }
//                if (rSyInSpBase.objectType == "HeatingZone"){
//                    RSyInSpZoneHeatingService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "CleanZone"){
//                    RSyInSpZoneCleanService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "DomesticWaterSupplyZone"){
//                    RSyInSpZoneDomesticWaterSupplyService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "NetworkZone" ){
//                    RSyInSpZoneNetworkService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "TenantZone"){
//                    RSyInSpZoneTenantService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "AirConditioningZone"){
//                    RSyInSpZoneAirConditioningService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "FunctionZone"){
//                    RSyInSpZoneFunctionService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "FireZone"){
//                    RSyInSpZoneFireService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "SecurityZone"){
//                    RSyInSpZoneSecurityService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "GeneralZone"){
//                    RSyInSpZoneGeneralService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "PowerSupplyZone"){
//                    RSyInSpZonePowerSupplyService.create(rSyInSpBase)
//                }else if (rSyInSpBase.objectType == "LightingZone"){
//                    RSyInSpZoneLightingService.create(rSyInSpBase)
//                }else{
//                    val sCreateResponse = SCreateResponse<RSyInSpBase>(SResponseType.failure)
//                    sCreateResponse.message = "空间分区类型不存在：" + rSyInSpBase.objectType
//                    sCreateResponse
//                }
            }
            try {
                /** 处理关系标记 */
                val listType = ArrayList<String>()
                listType.add("sh2bd")
                listType.add("eq2sh")
                for (type in listType){
                    val entity = EquipmentService.relationTypeProjectservice.select(SFilter.eq("projectId", projectId!!), SFilter.eq("relationType", type)).entity()
                    if (entity!=null){
                        entity.computationalState = 2
                        EquipmentService.relationTypeProjectservice.update(entity)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            val sCreateResponse = SCreateResponse<RSyInSpBase>(SResponseType.success)
            sCreateResponse
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function create()

    /**
     * 根据对象删除
     *
     * @param   rSyInSpBaseList    关系对象实例
     * @return
     */
    fun deletes(type: String, rSyInSpBaseList: ArrayList<RSyInSpBase>): SBaseResponse {
        return try {

            val projectId = SPageContext.getHeader("ProjectId")
            for (rSyInSpBase in rSyInSpBaseList){
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id2 = rSyInSpBase.spaceId
                rSy2Sp.id1 = rSyInSpBase.sysId
                rSy2Sp.projectId = projectId
                rSy2Sp.type = "sy2sp"
                rSy2Sp.zoneType = type
                val isDelete = rSy2SpService.delete(rSy2Sp)
                if (isDelete) {
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "12")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSyInSpBase.spaceId)
                    jsonObject.put("obj_type", "Sy2Sp")
                    jsonObject.put("linkId",rSyInSpBase.sysId)
                    /** 发送消息 */
                    messageHandler.sendMessage(jsonObject.toString())
                }
            }
            try {
                /** 处理关系标记 */
                val listType = ArrayList<String>()
                listType.add("sh2bd")
                listType.add("eq2sh")
                for (type in listType){
                    val entity = EquipmentService.relationTypeProjectservice.select(SFilter.eq("projectId", projectId!!), SFilter.eq("relationType", type)).entity()
                    if (entity!=null){
                        entity.computationalState = 2
                        EquipmentService.relationTypeProjectservice.update(entity)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

            val sBaseResponse = SBaseResponse(SResponseType.success)
            sBaseResponse
//            if (type == "HeatingZone"){
//                    RSyInSpZoneHeatingService.deletes(rSyInSpBaseList)
//            }else if (type == "CleanZone"){
//                RSyInSpZoneCleanService.deletes(rSyInSpBaseList)
//            }else if (type == "DomesticWaterSupplyZone"){
//                RSyInSpZoneDomesticWaterSupplyService.deletes(rSyInSpBaseList)
//            }else if (type == "NetworkZone" ){
//                RSyInSpZoneNetworkService.deletes(rSyInSpBaseList)
//            }else if (type == "TenantZone"){
//                RSyInSpZoneTenantService.deletes(rSyInSpBaseList)
//            }else if (type == "AirConditioningZone"){
//                RSyInSpZoneAirConditioningService.deletes(rSyInSpBaseList)
//            }else if (type == "FunctionZone"){
//                RSyInSpZoneFunctionService.deletes(rSyInSpBaseList)
//            }else if (type == "FireZone"){
//                RSyInSpZoneFireService.deletes(rSyInSpBaseList)
//            }else if (type == "SecurityZone"){
//                RSyInSpZoneSecurityService.deletes(rSyInSpBaseList)
//            }else if (type == "GeneralZone"){
//                RSyInSpZoneGeneralService.deletes(rSyInSpBaseList)
//            }else if (type == "PowerSupplyZone"){
//                RSyInSpZonePowerSupplyService.deletes(rSyInSpBaseList)
//            }else if (type == "LightingZone"){
//                RSyInSpZoneLightingService.deletes(rSyInSpBaseList)
//            }else{
//                val sCreateResponse = SCreateResponse<RSyInSpBase>(SResponseType.failure)
//                sCreateResponse.message = "空间分区类型不存在：$type"
//                sCreateResponse
//            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function deletes()

    /**
     * 创建系统所在业务空间关系
     *
     * @param    rSyInSpBaseList    系统所在业务空间关系对象列表
     */
    fun createSy(rSyInSpBaseList: RSyInSpBaseList): SCreateResponse<RSyInSpBase> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            rSy2SpService.delete("id1 = '${rSyInSpBaseList.sysId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (spaceId in rSyInSpBaseList.spaceIdList!!){
                val entity = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", spaceId!!)).entity()
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id1 = rSyInSpBaseList.sysId
                rSy2Sp.id2 = spaceId
                rSy2Sp.sign = 1
                rSy2Sp.zoneType = entity!!.objectType
                rSy2Sp.type = "sy2sp"
                rSy2Sp.projectId = projectId
                val replace = rSy2SpService.replace(rSy2Sp)
                if (replace){
                    successList.add(rSy2Sp)
                }else{
                    failureList.add(rSy2Sp)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rSyInSpBaseList.sysId)
                jsonObject.put("obj_type", "Sy2Sp")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

                val sCreateResponse = SCreateResponse<RSyInSpBase>(SResponseType.success)
                if (failureList.size>0){
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                }
                sCreateResponse
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createSy()

    /**
     * 创建所在业务空间的系统关系
     *
     * @param    rSyInSpBaseList    所在业务空间的系统关系对象列表
     */
    fun createsSp(rSyInSpBaseList: RSpInSyBaseList): SCreateResponse<RSyInSpBase> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            val projectId = SPageContext.getHeader("ProjectId")
            delete("id2 = '${rSyInSpBaseList.spaceId}'")
            val entity = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", rSyInSpBaseList.spaceId!!)).entity()

            for (sysId in rSyInSpBaseList.sysIdList!!){
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id2 = rSyInSpBaseList.spaceId
                rSy2Sp.id1 = sysId
                rSy2Sp.projectId = projectId
                rSy2Sp.sign = 1
                rSy2Sp.zoneType = entity!!.objectType
                rSy2Sp.type = "sy2sp"
                val replace = rSy2SpService.replace(rSy2Sp)
                if (replace){
                    successList.add(rSy2Sp)
                }else{
                    failureList.add(rSy2Sp)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rSyInSpBaseList.spaceId)
                jsonObject.put("obj_type", "Sy2Sp")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

                if (failureList.size>0){
                    val sCreateResponse = SCreateResponse<RSyInSpBase>(SResponseType.success)
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                    sCreateResponse
                }else{
                    val sCreateResponse = SCreateResponse<RSyInSpBase>(SResponseType.success)
                    sCreateResponse
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsSp()

    /**
     * 创建所在业务空间的系统关系
     *
     * @param    rSyInSpBaseList    所在业务空间的系统关系对象列表
     */
    fun createsSpSys(type: String, rSyInSpBase: RSpInSyBaseList): SCreateResponse<RSpInSyBaseList> {
        return try {


            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            val projectId = SPageContext.getHeader("ProjectId")
            for (sysId in rSyInSpBase.sysIdList!!){
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id2 = rSyInSpBase.spaceId
                rSy2Sp.id1 = sysId
                rSy2Sp.projectId = projectId
                rSy2Sp.sign = 1
                rSy2Sp.zoneType = type
                rSy2Sp.type = "sy2sp"
                val replace = rSy2SpService.replace(rSy2Sp)
                if (replace){
                    successList.add(rSy2Sp)
                }else{
                    failureList.add(rSy2Sp)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "14")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rSyInSpBase.spaceId)
                jsonObject.put("obj_type", "Sy2Sp")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

                if (failureList.size>0){
                    val sCreateResponse = SCreateResponse<RSpInSyBaseList>(SResponseType.success)
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                    sCreateResponse
                }else{
                    val sCreateResponse = SCreateResponse<RSpInSyBaseList>(SResponseType.success)
                    sCreateResponse
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }


//            if (type == "HeatingZone"){
//                 RSyInSpZoneHeatingService.createsSpSys(rSyInSpBase)
//            }else if (type == "CleanZone"){
//                 RSyInSpZoneCleanService.createsSpSys(rSyInSpBase)
//            }else if (type == "DomesticWaterSupplyZone"){
//                 RSyInSpZoneDomesticWaterSupplyService.createsSpSys(rSyInSpBase)
//            }else if (type == "NetworkZone" ){
//                 RSyInSpZoneNetworkService.createsSpSys(rSyInSpBase)
//            }else if (type == "TenantZone"){
//                 RSyInSpZoneTenantService.createsSpSys(rSyInSpBase)
//            }else if (type == "AirConditioningZone"){
//                 RSyInSpZoneAirConditioningService.createsSpSys(rSyInSpBase)
//            }else if (type == "FunctionZone"){
//                 RSyInSpZoneFunctionService.createsSpSys(rSyInSpBase)
//            }else if (type == "FireZone"){
//                  RSyInSpZoneFireService.createsSpSys(rSyInSpBase)
//            }else if (type == "SecurityZone"){
//                 RSyInSpZoneSecurityService.createsSpSys(rSyInSpBase)
//            }else if (type == "GeneralZone"){
//                 RSyInSpZoneGeneralService.createsSpSys(rSyInSpBase)
//            }else if (type == "PowerSupplyZone"){
//                 RSyInSpZonePowerSupplyService.createsSpSys(rSyInSpBase)
//            }else if (type == "LightingZone"){
//                RSyInSpZoneLightingService.createsSpSys(rSyInSpBase)
//            }else{
//                val sCreateResponse = SCreateResponse<RSpInSyBaseList>(SResponseType.failure)
//                sCreateResponse.message = "空间分区类型不存在：$type"
//                sCreateResponse
//            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsSp()


    /**
     * 创建系统所在业务空间关系
     *
     * @param    rSyInSpBaseList    系统所在业务空间关系对象列表
     */
    fun createSySps(type: String, rSyInSpBase: RSyInSpBaseList): SCreateResponse<RSyInSpBaseList> {
        return try {


            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            val projectId = SPageContext.getHeader("ProjectId")
            for (spaceId in rSyInSpBase.spaceIdList!!){
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id1 = rSyInSpBase.sysId
                rSy2Sp.id2 = spaceId
                rSy2Sp.projectId = projectId
                rSy2Sp.sign = 1
                rSy2Sp.zoneType = type
                rSy2Sp.type = "sy2sp"
                val replace = rSy2SpService.replace(rSy2Sp)
                if (replace){
                    successList.add(rSy2Sp)
                }else{
                    failureList.add(rSy2Sp)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "14")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rSyInSpBase.sysId)
                jsonObject.put("obj_type", "Sy2Sp")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())


                val sCreateResponse = SCreateResponse<RSyInSpBaseList>(SResponseType.success)
                if (failureList.size>0){
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                }
                sCreateResponse
            }else{
                SCreateResponse(SResponseType.failure)
            }

//            if (type == "HeatingZone"){
//                RSyInSpZoneHeatingService.createSySps(rSyInSpBase)
//            }else if (type == "CleanZone"){
//                RSyInSpZoneCleanService.createSySps(rSyInSpBase)
//            }else if (type == "DomesticWaterSupplyZone"){
//                RSyInSpZoneDomesticWaterSupplyService.createSySps(rSyInSpBase)
//            }else if (type == "NetworkZone" ){
//                RSyInSpZoneNetworkService.createSySps(rSyInSpBase)
//            }else if (type == "TenantZone"){
//                RSyInSpZoneTenantService.createSySps(rSyInSpBase)
//            }else if (type == "AirConditioningZone"){
//                RSyInSpZoneAirConditioningService.createSySps(rSyInSpBase)
//            }else if (type == "FunctionZone"){
//                RSyInSpZoneFunctionService.createSySps(rSyInSpBase)
//            }else if (type == "FireZone"){
//                RSyInSpZoneFireService.createSySps(rSyInSpBase)
//            }else if (type == "SecurityZone"){
//                RSyInSpZoneSecurityService.createSySps(rSyInSpBase)
//            }else if (type == "GeneralZone"){
//                RSyInSpZoneGeneralService.createSySps(rSyInSpBase)
//            }else if (type == "PowerSupplyZone"){
//                RSyInSpZonePowerSupplyService.createSySps(rSyInSpBase)
//            }else if (type == "LightingZone"){
//                RSyInSpZoneLightingService.createSySps(rSyInSpBase)
//            }else{
//                val sCreateResponse = SCreateResponse<RSyInSpBaseList>(SResponseType.failure)
//                sCreateResponse.message = "空间分区类型不存在：$type"
//                sCreateResponse
//            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createSySps()




} // Object RSyInSpBaseService