package com.persagy.server.datacenter.syn.services.jms.rel

import com.alibaba.fastjson.JSONArray
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DictProject
import com.persagy.server.datacenter.models.jms.SynchronousMessageRel
import com.persagy.server.datacenter.wanda.obj.ZoneSpace
import com.persagy.server.syn.Configure
import com.persagy.server.syn.models.api.obj.EquipApi
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

/**
 * 设备所在的业务空间
 */
@Service
class EquipinSpaceSync {

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

    /** 缓存对象数据服务 - 数据中心到数据中台  */
    var synchronousMessageRelService = SObjectService(SMybatisDao(SynchronousMessageRel::class.java))
    /** 项目所属的集团 */
    private val dictProjectService = SObjectService(SMybatisDao(DictProject::class.java))
    /** 空间对象服务 */
    private val zoneSpaceService = SBaseService(SMybatisDao(ZoneSpace::class.java))

    /**
     * 创建关系
     *
     * @param synchronousMessageRel     关系对象
     * @param graphCode                 图类型
     * @param relCode                   关系类型
     */
    fun equipInSpaceCreate(synchronousMessageRel: SynchronousMessageRel,graphCode: String,relCode: String,fromIds:ArrayList<String>?){
        try {

            val dictProject = dictProjectService.select(SFilter.eq("id", synchronousMessageRel.projectId!!)).entity()
            if (dictProject!=null) {
                synchronousMessageRel.sign = 0
                val toJsonObjArray :JSONArray
                    if (!fromIds.isNullOrEmpty()){
                        /** 关系列表 */
                        val zoneSpace = zoneSpaceService.select(
                            SFilter.eq("projectId", synchronousMessageRel.projectId!!),
                            SFilter.eq("id", synchronousMessageRel.fromId!!)
                        ).entity()
                        toJsonObjArray= Configure.toJsonObjFromList(graphCode, relCode,fromIds, synchronousMessageRel.fromId!!,zoneSpace?.classCode)
                } else {
                        val zoneSpace = zoneSpaceService.select(
                            SFilter.eq("projectId", synchronousMessageRel.projectId!!),
                            SFilter.eq("id", synchronousMessageRel.toId!!)
                        ).entity()
                        /** 关系列表 */
                        toJsonObjArray = Configure.toJsonObjArray(graphCode, relCode, synchronousMessageRel.fromId!!, synchronousMessageRel.toId!!,zoneSpace?.classCode)
                }

                val createRel = EquipApi.createRel(dictProject.groupCode!!, synchronousMessageRel.projectId!!, toJsonObjArray)
                if (!createRel ){
                    synchronousMessageRel.sign = 2
                    synchronousMessageRel.count +1
                }
            } else {
                synchronousMessageRel.sign = 2
                synchronousMessageRel.count +1
            }
            /** 更改标记 */
            synchronousMessageRelService.update(synchronousMessageRel)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 创建关系
     *
     * @param synchronousMessageRel     关系对象
     * @param graphCode                 图类型
     * @param relCode                   关系类型
     */
    fun equipInSpaceCreateTwo(synchronousMessageRel: SynchronousMessageRel,graphCode: String,relCode: String,fromIds:ArrayList<String>?){
        try {

            val dictProject = dictProjectService.select(SFilter.eq("id", synchronousMessageRel.projectId!!)).entity()
            if (dictProject!=null) {
                synchronousMessageRel.sign = 0
                val toJsonObjArray :JSONArray
                if (!fromIds.isNullOrEmpty()){
                    /** 关系列表 */
                    toJsonObjArray= Configure.toJsonObjFromList(graphCode, relCode, fromIds, synchronousMessageRel.fromId!!)
                } else {
                    /** 关系列表 */
                    toJsonObjArray = Configure.toJsonObjArray(graphCode, relCode, synchronousMessageRel.fromId!!, synchronousMessageRel.toId!!)
                }

                val createRel = EquipApi.createRel(dictProject.groupCode!!, synchronousMessageRel.projectId!!, toJsonObjArray)
                if (!createRel ){
                    synchronousMessageRel.sign = 2
                    synchronousMessageRel.count +1
                }
            } else {
                synchronousMessageRel.sign = 2
                synchronousMessageRel.count +1
            }
            /** 更改标记 */
            synchronousMessageRelService.update(synchronousMessageRel)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }



    /**
     * 删除关系
     *
     * @param synchronousMessageRel     关系对象
     */
    fun delRel(synchronousMessageRel: SynchronousMessageRel,graphCode: String,relCode: String){
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", synchronousMessageRel.projectId!!)).entity()
            if (dictProject!=null) {
                synchronousMessageRel.sign = 0
                val toJsonObj = Configure.toJsonObj(graphCode, relCode, synchronousMessageRel.fromId!!, synchronousMessageRel.toId!!)
                val delRel = EquipApi.delRel(dictProject.groupCode!!, synchronousMessageRel.projectId!!, toJsonObj)
                if (!delRel){
                    synchronousMessageRel.sign = 2
                    synchronousMessageRel.count +1
                }
            } else {
                synchronousMessageRel.sign = 2
                synchronousMessageRel.count +1
            }
            /** 更改标记 */
            synchronousMessageRelService.update(synchronousMessageRel)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 累计叠加关系
     */
    fun addRelSuperposition(synchronousMessageRel: SynchronousMessageRel,toIds: ArrayList<String>,graphCode: String,relCode: String){

        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", synchronousMessageRel.projectId!!)).entity()
            if (dictProject!=null) {
                synchronousMessageRel.sign = 0

                val toJsonObj = Configure.toJsonObj(graphCode, relCode, synchronousMessageRel.fromId!!, null)
                val delRel = EquipApi.delRel(dictProject.groupCode!!, synchronousMessageRel.projectId!!, toJsonObj)
                if (delRel) {
                    val zoneSpace = zoneSpaceService.select(
                        SFilter.eq("projectId", synchronousMessageRel.projectId!!),
                        SFilter.eq("id", toIds[0])
                    ).entity()
                    /** 关系列表 */
                    val toJsonObjArray =
                            Configure.toJsonObjList(graphCode, relCode, synchronousMessageRel.fromId!!, toIds,zoneSpace?.classCode)

                    val createRel = EquipApi.createRel(dictProject.groupCode!!, synchronousMessageRel.projectId!!, toJsonObjArray)
                    if (!createRel ){
                        synchronousMessageRel.sign = 2
                        synchronousMessageRel.count +1
                    }
                } else {
                    synchronousMessageRel.sign = 2
                    synchronousMessageRel.count +1
                }


            } else {
                synchronousMessageRel.sign = 2
                synchronousMessageRel.count +1
            }
        } catch (e: Exception) {
            e.printStackTrace()
            synchronousMessageRel.sign = 2
            synchronousMessageRel.count +1
        }
        /** 更改标记 */
        synchronousMessageRelService.update(synchronousMessageRel)
    }

    /**
     * 主对象和从对象相反的替换函数
     */
    fun addRelSuperpositionFan(synchronousMessageRel: SynchronousMessageRel,fromIds: ArrayList<String>,graphCode: String,relCode: String){

        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", synchronousMessageRel.projectId!!)).entity()
            if (dictProject!=null) {
                synchronousMessageRel.sign = 0
                for (id in fromIds){
                    val toJsonObj = Configure.toJsonObj(graphCode, relCode, id, null)
                    val delRel = EquipApi.delRel(dictProject.groupCode!!, synchronousMessageRel.projectId!!, toJsonObj)
                    if (!delRel) {
                        synchronousMessageRel.sign = 2
                        synchronousMessageRel.count +1
                        break
                    }
                }
                if (synchronousMessageRel.sign == 0){
                    val zoneSpace = zoneSpaceService.select(
                        SFilter.eq("projectId", synchronousMessageRel.projectId!!),
                        SFilter.eq("id", synchronousMessageRel.toId!!)
                    ).entity()
                    /** 关系列表 */
                    val toJsonObjArray =
                            Configure.toJsonObjFromList(graphCode, relCode, fromIds, synchronousMessageRel.toId!!,zoneSpace?.classCode)

                    val createRel = EquipApi.createRel(dictProject.groupCode!!, synchronousMessageRel.projectId!!, toJsonObjArray)
                    if (!createRel ){
                        synchronousMessageRel.sign = 2
                        synchronousMessageRel.count +1
                    }
                }

                /** 更改标记 */
                synchronousMessageRelService.update(synchronousMessageRel)

            } else {
                synchronousMessageRel.sign = 2
                synchronousMessageRel.count +1
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }



}