package com.persagy.server.services.rel

import com.persagy.base.extensions.toJson
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.rel.RBdForEqList
import com.persagy.server.datacenter.models.entities.rel.REq2Bd
import com.persagy.server.datacenter.models.entities.rel.REqForBdList
import com.persagy.server.services.base.RService
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 REqForBdService: RService<REq2Bd>(SMybatisDao(REq2Bd::class.java)) {


    /**
     * 创建设备服务于建筑关系
     *
     * @param   req2Bd    设备服务于建筑关系对象
     */
    fun create(req2Bd: REq2Bd): SCreateResponse<REq2Bd> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            req2Bd.projectId = projectId
            req2Bd.type = "eq2bd_for"
            val isInsert = insert(req2Bd)
            if (isInsert){
                val jsonObject = JSONObject()
                jsonObject.put("type", "11")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", req2Bd.id1)
                jsonObject.put("obj_type", "Eq2Bd")
                jsonObject.put("linkId",req2Bd.id2)

                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

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

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

                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

                val sBaseResponse = SBaseResponse(SResponseType.success)
                sBaseResponse.message = req2Bd.toJson()
                sBaseResponse
            } else {
                SBaseResponse(SResponseType.failure)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function deletes()

    /**
     * 创建设备服务于建筑关系
     *
     * @param    rEqForBdList     设备和建筑关系的对象列表
     */
    fun createEq(rEqForBdList: REqForBdList): SCreateResponse<REq2Bd> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("equipID = '${rEqForBdList.equipId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (buildingId in rEqForBdList.buildingIdList!!){
                val rEqForBd = REq2Bd()
                rEqForBd.id1 = rEqForBdList.equipId
                rEqForBd.id2 = buildingId
                rEqForBd.projectId = projectId
                rEqForBd.type = "eq2bd_for"
                val replace = replace(rEqForBd)
                if (replace){
                    successList.add(rEqForBd)
                }else{
                    failureList.add(rEqForBd)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForBdList.equipId)
                jsonObject.put("obj_type", "Eq2Bd")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

                val sCreateResponse = SCreateResponse<REq2Bd>(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    rEqForBdList    设备和建筑关系的对象列表
     */
    fun createsBd(rEqForBdList: RBdForEqList): SCreateResponse<REq2Bd> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("buildingID = '${rEqForBdList.buildingId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (equipId in rEqForBdList.equipIdList!!){
                val rEqForBd = REq2Bd()
                rEqForBd.id2 = rEqForBdList.buildingId
                rEqForBd.id1 = equipId
                rEqForBd.projectId = projectId
                rEqForBd.type = "eq2bd_for"
                val replace = replace(rEqForBd)
                if (replace){
                    successList.add(rEqForBd)
                }else{
                    failureList.add(rEqForBd)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForBdList.buildingId)
                jsonObject.put("obj_type", "Eq2Bd")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

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

} // Class REqForBdService