package com.persagy.server.services.rel

import com.persagy.server.datacenter.models.entities.rel.REqForFl
import com.persagy.server.datacenter.models.entities.rel.REqForFlList
import com.persagy.server.datacenter.models.entities.rel.RFlForEqList
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 REqForFlService: RService<REqForFl>(SMybatisDao(REqForFl::class.java)) {

    /**
     * 创建设备服务于楼层关系
     *
     * @param    rEqForFl      设备服务于楼层关系对象
     */
    fun create(rEqForFl: REqForFl): SCreateResponse<REqForFl> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rEqForFl.projectId = projectId
            val isInsert = insert(rEqForFl)
            if (isInsert){
                val jsonObject = JSONObject()
                jsonObject.put("type", "11")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForFl.equipId)
                jsonObject.put("obj_type", "Eq2Fl")
                jsonObject.put("linkId",rEqForFl.floorId)

                /** 发送消息 */
                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    rEqForFl     关系对象实例
     */
    fun deletes(rEqForFl: REqForFl): SBaseResponse {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            val isDelete = delete(rEqForFl)
            /** 删除成功 */
            if (isDelete) {
                val jsonObject = JSONObject()
                jsonObject.put("type", "12")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForFl.equipId)
                jsonObject.put("obj_type", "Eq2Fl")
                jsonObject.put("linkId",rEqForFl.floorId)

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

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

    /**
     * 创建设备服务于楼层关系
     *
     * @param   rEqForFlList    设备和楼层关系的对象列表
     */
    fun createEq(rEqForFlList: REqForFlList): SCreateResponse<REqForFl> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("equipId = '${rEqForFlList.equipId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (floorId in rEqForFlList.floorIdList!!){
                val rEqForFl = REqForFl()
                rEqForFl.equipId = rEqForFlList.equipId
                rEqForFl.floorId = floorId
                rEqForFl.projectId = projectId
                val replace = replace(rEqForFl)
                if (replace){
                    successList.add(rEqForFl)
                }else{
                    failureList.add(rEqForFl)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForFlList.equipId)
                jsonObject.put("obj_type", "Eq2Fl")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())
                
                val sCreateResponse = SCreateResponse<REqForFl>(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    rEqForFlList     设备和楼层关系的对象列表
     */
    fun createsFl(rEqForFlList: RFlForEqList): SCreateResponse<REqForFl> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("floorID = '${rEqForFlList.floorId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (equipId in rEqForFlList.equipIdList!!){
                val rEqForFl = REqForFl()
                rEqForFl.floorId = rEqForFlList.floorId
                rEqForFl.equipId = equipId
                rEqForFl.projectId = projectId
                val replace = replace(rEqForFl)
                if (replace){
                    successList.add(rEqForFl)
                }else{
                    failureList.add(rEqForFl)
                }
            }
            if (successList.size>0){
                val jsonObject = JSONObject()
                jsonObject.put("type", "13")
                jsonObject.put("projectId", projectId)
                jsonObject.put("id", rEqForFlList.floorId)
                jsonObject.put("obj_type", "Eq2Fl")
                jsonObject.put("linkId",successList.toJson())
                /** 发送消息 */
                messageHandler.sendMessage(jsonObject.toString())

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

} // Object REqForFlService