package com.persagy.server.services.rel

import com.persagy.server.datacenter.models.entities.rel.REqForSh
import com.persagy.server.datacenter.models.entities.rel.REqForShList
import com.persagy.server.datacenter.models.entities.rel.RShForEqList
import com.persagy.server.services.base.RService
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 REqForShService: RService<REqForSh>(SMybatisDao(REqForSh::class.java)) {

    /**
     * 创建设备服务于竖井关系
     *
     * @param     rEqForSh     设备服务于竖井关系对象
     */
    fun create(rEqForSh: REqForSh): SCreateResponse<REqForSh> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rEqForSh.projectId = projectId
            val isInsert = insert(rEqForSh)
            if (isInsert){
                val jsonObject = JSONObject()
                jsonObject.put("type", "11")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForSh.equipId)
                jsonObject.put("obj_type", "Eq2Sh")
                jsonObject.put("linkId",rEqForSh.shaftId)

                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())
//                kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Eq2Sh", "11", IdUtils.uuidCreate(), rEqForSh.equipId!!,rEqForSh.shaftId!!),false)
                SCreateResponse(SResponseType.success)
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } //Function create()

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

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

    /**
     * 创建设备服务于竖井关系
     *
     * @param    rEqForShList    设备和竖井关系的对象列表
     */
    fun createEq(rEqForShList: REqForShList): SCreateResponse<REqForSh> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("equipId = '${rEqForShList.equipId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (shaftId in rEqForShList.shaftIdList!!){
                val rEqForSh = REqForSh()
                rEqForSh.equipId = rEqForShList.equipId
                rEqForSh.shaftId = shaftId
                rEqForSh.projectId = projectId
                val replace = replace(rEqForSh)
                if (replace){
                    successList.add(rEqForSh)
                }else{
                    failureList.add(rEqForSh)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForShList.equipId)
                jsonObject.put("obj_type", "Eq2Sh")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())
//                kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Eq2Sh", "13", IdUtils.uuidCreate(), rEqForShList.equipId!!,null),false)
                val sCreateResponse = SCreateResponse<REqForSh>(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 createEq()

    /**
     * 创建服务于竖井的设备关系
     *
     * @param    rEqForShList    设备和竖井关系的对象列表
     */
    fun createsSh(rEqForShList: RShForEqList): SCreateResponse<REqForSh> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("shaftID = '${rEqForShList.shaftId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (equipId in rEqForShList.equipIdList!!){
                val rEqForSh = REqForSh()
                rEqForSh.shaftId = rEqForShList.shaftId
                rEqForSh.equipId = equipId
                rEqForSh.projectId = projectId
                val replace = replace(rEqForSh)
                if (replace){
                    successList.add(rEqForSh)
                }else{
                    failureList.add(rEqForSh)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForShList.shaftId)
                jsonObject.put("obj_type", "Eq2Sh")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())
//                kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "Eq2Sh", "13", IdUtils.uuidCreate(), rEqForShList.shaftId!!,null),false)
                if (failureList.size>0){
                    val sCreateResponse = SCreateResponse<REqForSh>(SResponseType.success)
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                    sCreateResponse
                }else{
                    val sCreateResponse = SCreateResponse<REqForSh>(SResponseType.success)
                    sCreateResponse
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsSh()

} // Object REqForShService