package com.persagy.server.service

import com.persagy.database.SFilter
import com.persagy.database.extensions.keyValue
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.objects.Property
import com.persagy.server.datacenter.models.entities.rel.RSyEq
import com.persagy.server.models.EquipmentProSy
import com.persagy.server.service.base.Service
import com.persagy.server.service.objects.PropertyService
import com.persagy.server.synchronize.MessageHandler
import com.persagy.server.utils.IdUtils
import com.persagy.server.utils.ShaftNameUtil
import com.persagy.service.SPageContext
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.utils.SSpringContextUtil
import org.json.JSONObject
import org.slf4j.LoggerFactory

/**
 * 设备系统服务
 *
 * @author wx
 */
object EquipmentProSyService : Service<EquipmentProSy>(SMybatisDao(EquipmentProSy::class.java)) {

    /** 日志 */
    private val logger = LoggerFactory.getLogger(Service::class.java)

    /** MQ消息对象 */
    private val messageHandler by lazy {
        SSpringContextUtil.getBean(MessageHandler::class.java) as MessageHandler
    }


    override fun onCreateBefore(equip: EquipmentProSy): Boolean {
        try {
            /** 查询准备类型 */
            val projectId = SPageContext.getHeader("ProjectId")
            if (!equip.id.isNullOrEmpty()) {
                val isStartsWith = equip.id.toString().startsWith("Eq")
                if (!isStartsWith) {
                    /** 资产id */
                    equip.id = IdUtils.equipmentIdCreate(projectId!!)
                }
            } else {
                /** 资产id */
                equip.id = IdUtils.equipmentIdCreate(projectId!!)
            }
            /** 项目id */
            equip.projectId = projectId
            /** 设备名字 */
            if (equip.name.isNullOrEmpty()) {
                equip.name = ShaftNameUtil.shaftName("设备-")
            }

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

    /**
     * 创建成功回调函数
     *
     * @param   entityList          创建成功对象
     */
    override fun onCreateSuccess(entityList: ArrayList<EquipmentProSy>) {
        try {
            entityList.forEach {
                val jsonObject = JSONObject()
                jsonObject.put("type", "1")
                jsonObject.put("projectId",it.keyValue("projectId"))
                jsonObject.put("id", it.keyValue("id"))
                /** 发送消息 */
                messageHandler!!.sendMessage(jsonObject.toString())
            }
            logger.debug("设备创建完成 =******************************************************************")
            for (entity in entityList){
                if (entity.propertyId!!){
                    if (entity.classCode.isNullOrEmpty()){
                        continue
                    }
                    val property = Property()
                    property.projectId = entity.projectId
                    property.buildingId = entity.buildingId
                    property.floorId = entity.floorId
                    val equipFamily = EquipFamilyService.select(SFilter.eq("equipCode", entity.classCode!!)).entity()
                    property.family = equipFamily!!.family!!

                    val sCreateRequest = SCreateRequest<Property>()
                    var list = ArrayList<Property>()
                    property.equipId = entity.id
                    list.add(property)
                    sCreateRequest.content = list
                    PropertyService.createList(sCreateRequest)
                }
                if (!entity.systemList.isNullOrEmpty()){
                    for (syId in entity.systemList!!){
                        val rSyEq = RSyEq()
                        rSyEq.equipId = entity.id
                        rSyEq.sysId = syId
                        RSyEqService.create(rSyEq)
                    }
                }
            }
            logger.debug("资产创建完成 =******************************************************************")
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return
    } // Function onCreateSuccess()




} // Object EquipmentProSyService