package com.persagy.server.services.rel

import com.persagy.server.services.base.RService

import com.persagy.server.datacenter.models.entities.rel.RFlThroughFl
import com.persagy.server.datacenter.models.entities.rel.RFloorOtherThroughFloorList
import com.persagy.server.datacenter.models.entities.rel.RFloorThroughFloorOtherList
import com.persagy.base.extensions.toJson
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse
import org.json.JSONObject

/**
 * 楼层的贯通关系服务
 *
 * @author 付国宝
 */
object RFlThroughFlService : RService<RFlThroughFl>(SMybatisDao(RFlThroughFl::class.java)) {

    /**
     * 创建楼层的贯通关系
     *
     * @param   rFlThroughFl    楼层的贯通关系对象
     */
    fun create(rFlThroughFl: RFlThroughFl): SCreateResponse<RFlThroughFl> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rFlThroughFl.projectId = projectId
            val isInsert = insert(rFlThroughFl)
            if (isInsert){
                val jsonObject = JSONObject()
                jsonObject.put("type", "11")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rFlThroughFl.floorId)
                jsonObject.put("obj_type", "Fl2Fl")
                jsonObject.put("linkId",rFlThroughFl.floorOtherId)

                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())
//                kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Fl2Fl", "11", IdUtils.uuidCreate(),  rFlThroughFl.floorId!!, rFlThroughFl.floorOtherId!!),false)
                SCreateResponse(SResponseType.success)
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Tenant create()

    /**
     * 根据对象删除
     *
     * @param   rFlThroughFl    关系对象实例
     */
    fun deletes(rFlThroughFl: RFlThroughFl): SBaseResponse {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            val isDelete = delete(rFlThroughFl)
            /** 删除成功 */
            if (isDelete) {
                val jsonObject = JSONObject()
                jsonObject.put("type", "12")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rFlThroughFl.floorId)
                jsonObject.put("obj_type", "Fl2Fl")
                jsonObject.put("linkId",rFlThroughFl.floorOtherId)

                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())
//                kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Fl2Fl", "12", IdUtils.uuidCreate(),  rFlThroughFl.floorId!!, rFlThroughFl.floorOtherId!!),false)
                val sBaseResponse = SBaseResponse(SResponseType.success)
                sBaseResponse.message = rFlThroughFl.toJson()
                sBaseResponse
            } else {
                SBaseResponse(SResponseType.failure)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Tenant deletes()

    /**
     * 创建楼层的贯通关系
     *
     * @param   rFloorThroughFloorOtherList    楼层和楼层列表关系对象
     */
    fun createFl(rFloorThroughFloorOtherList: RFloorThroughFloorOtherList): SCreateResponse<RFlThroughFl> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()

            try {
                delete("floorId = '${rFloorThroughFloorOtherList.floorId}'")
                delete("floorOtherId = '${rFloorThroughFloorOtherList.floorId}'")
            } catch (e: Exception) {
                e.printStackTrace()
            }
            val projectId = SPageContext.getHeader("ProjectId")
            if (rFloorThroughFloorOtherList.floorOtherIdList!=null&&rFloorThroughFloorOtherList.floorOtherIdList!!.isNotEmpty()){
                for (floorOtherId in rFloorThroughFloorOtherList.floorOtherIdList!!){
                    val rFlThroughFl = RFlThroughFl()
                    rFlThroughFl.floorId = rFloorThroughFloorOtherList.floorId
                    rFlThroughFl.floorOtherId = floorOtherId
                    rFlThroughFl.projectId = projectId
                    val replace = replace(rFlThroughFl)
                    if (replace){
                        successList.add(rFlThroughFl)
                    }else{
                        failureList.add(rFlThroughFl)
                    }
                }
                if (successList.size>0){
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "13")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rFloorThroughFloorOtherList.floorId)
                    jsonObject.put("obj_type", "Fl2Fl")
                    jsonObject.put("linkId",successList.toJson())
                    /** 发送消息 */
                    messageHandler.sendMessage(jsonObject.toString())
//                    kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Fl2Fl", "13", IdUtils.uuidCreate(),  rFloorThroughFloorOtherList.floorId!!, null),false)
                    val sCreateResponse = SCreateResponse<RFlThroughFl>(SResponseType.success)
                    if (failureList.size>0){
                        sCreateResponse.message ="失败对象："+failureList.toJson()
                    }
                    sCreateResponse
                }else{
                    SCreateResponse(SResponseType.failure)
                }
            }else{
                SCreateResponse(SResponseType.success)
            }

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

    /**
     * 创建楼层的贯通关系
     *
     * @param   rFollrOtherThroughFloorList    楼层列表和楼层关系对象
     */
    fun createsFl(rFollrOtherThroughFloorList: RFloorOtherThroughFloorList): SCreateResponse<RFlThroughFl> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()

            try {
                delete("floorId = '${rFollrOtherThroughFloorList.floorOtherId}'")
                delete("floorOtherId = '${rFollrOtherThroughFloorList.floorOtherId}'")
            } catch (e: Exception) {
                e.printStackTrace()
            }
            val projectId = SPageContext.getHeader("ProjectId")
            if (rFollrOtherThroughFloorList.floorIdList!=null&&rFollrOtherThroughFloorList.floorIdList!!.isNotEmpty()){
                for (floorId in rFollrOtherThroughFloorList.floorIdList!!){
                    val rFlThroughFl = RFlThroughFl()
                    rFlThroughFl.floorOtherId = rFollrOtherThroughFloorList.floorOtherId
                    rFlThroughFl.floorId = floorId
                    rFlThroughFl.projectId = projectId
                    val replace = replace(rFlThroughFl)
                    if (replace){
                        successList.add(rFlThroughFl)
                    }else{
                        failureList.add(rFlThroughFl)
                    }
                }
                if (successList.size>0){
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "13")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rFollrOtherThroughFloorList.floorOtherId)
                    jsonObject.put("obj_type", "Fl2Fl")
                    jsonObject.put("linkId",successList.toJson())
                    /** 发送消息 */
                    messageHandler.sendMessage(jsonObject.toString())
//                    kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Fl2Fl", "13", IdUtils.uuidCreate(),  rFollrOtherThroughFloorList.floorOtherId!!, null),false)
                    if (failureList.size>0){
                        val sCreateResponse = SCreateResponse<RFlThroughFl>(SResponseType.success)
                        sCreateResponse.message ="失败对象："+failureList.toJson()
                        sCreateResponse
                    }else{
                        val sCreateResponse = SCreateResponse<RFlThroughFl>(SResponseType.success)
                        sCreateResponse
                    }
                }else{
                    SCreateResponse(SResponseType.failure)
                }
            }else{
                SCreateResponse(SResponseType.success)
            }

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

} // Object RFlThroughFlService()