package com.persagy.server.services.rel

import com.persagy.server.services.base.RService
import com.persagy.server.datacenter.models.entities.rel.RIspaceAdjacentIspace
import com.persagy.server.services.objects.SpaceService
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
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 java.util.*

/**
 * 元空间邻接关系
 *
 * @author 张维新
 */
object RIspaceAdjacentIspaceService : RService<RIspaceAdjacentIspace>(SMybatisDao(RIspaceAdjacentIspace::class.java)) {


    /**
     * 元空间邻接关系
     */
    fun createLists(rIspaceAdjacentIspace: SCreateRequest<RIspaceAdjacentIspace>): SCreateResponse<RIspaceAdjacentIspace> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rIspaceAdjacentIspace.content
            for (rIspaceIspace in rIspaceAdjacentIspace.content) {
                rIspaceIspace.projectId = projectId
                try {
                    val insert = insert(rIspaceIspace)
//                    if (insert){
//                        kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Fl2Fl", "11", IdUtils.uuidCreate(),  rIspaceIspace.ispace!!,
//                                rIspaceIspace.otherIspaceId),false)
//                    }

                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
            SCreateResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure, e.message.toString())
        }

    } // Function createLists()

    /**
     * 删除元空间邻接关系
     */
    fun delectId(floorId: String): SBaseResponse {

        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            val ispaceList = SpaceService.select(SFilter.eq("floorId", floorId)).exec()

            val list = ArrayList<String>()
            ispaceList.stream().forEach {
                list.add(it.id!!)
            }
            val delete = delete(SFilter.or(SFilter.inList("ispace", list), SFilter.inList("otherIspaceId", list)))
//            if (delete){
//                kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Fl2Fl", "11", IdUtils.uuidCreate(),  rIspaceIspace.ispace!!,
//                        rIspaceIspace.otherIspaceId),false)
//            }


            SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function delectId()


} // Object RIspaceAdjacentIspaceService