package com.persagy.server.syn.services.jms

import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DictProject
import com.persagy.server.datacenter.datacenter.models.entities.objects.Tenant
import com.persagy.server.datacenter.models.jms.SynchronousMessage
import com.persagy.server.datacenter.syn.services.jms.DataCenterBuilding
import com.persagy.server.syn.Configure
import com.persagy.server.syn.models.api.obj.EquipApi
import com.persagy.server.syn.models.tools.VOTnUtil
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

/**
 * 租户
 */
@Service
class DataCenterVOTn {

    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(DataCenterBuilding::class.java)
    } // Companion object
    /** 租户服务 */
    private val tenantService = SBaseService(SMybatisDao(Tenant::class.java))
    /** 缓存对象数据服务 - 数据中心到数据中台  */
    var synchronousMessageObjService = SObjectService(SMybatisDao(SynchronousMessage::class.java))
    /** 字典项目服务 */
    private val dictProjectService = SBaseService(SMybatisDao(DictProject::class.java))
    /**
     * 处理 数据中心到 - 数据中台的租户
     */
    fun tenantType(message: SynchronousMessage) {
        val dictProject = dictProjectService.select(SFilter.eq("id", message.projectId!!)).entity()
        logger.debug(" 处理租户***********************************")
        when (message.type) {
            Configure.CREATE -> {
                /**查询租户信息 */
                val tenant = tenantService.select(
                        SFilter.eq("projectId", message.projectId!!),
                        SFilter.eq("id", message.objId!!)
                ).entity()
                logger.debug(" 处理租户对象${tenant?.toJson()}***********************************")
                /** 租户不为空 */
                if (tenant!=null) {
                    val tenantList = VOTnUtil.equipTools(message.projectId!!, tenant)
                    val sign = EquipApi.createObj(dictProject!!.groupCode!!,message.projectId!!, tenantList)
                    if (sign){
                        /** 成功 */
                        message.sign = 0
                    }else {
                        /** 失败 */
                        message.sign = 2
                    }
                    /** 更改标记 */
                    synchronousMessageObjService.update(message, arrayListOf("sign"))
                }
            }
            Configure.UPDATE -> {
                /** 修改 */
                /**查询租户信息 */
                val tenant = tenantService.select(
                        SFilter.eq("projectId", message.projectId!!),
                        SFilter.eq("id", message.objId!!)
                ).entity()
                /** 租户不为空 */
                if (tenant!=null) {
                    val equipList = VOTnUtil.equipTools(message.projectId!!, tenant)
                    val sign = EquipApi.updateObj(dictProject!!.groupCode!!,message.projectId!!, equipList)
                    if (sign){
                        /** 成功 */
                        message.sign = 0
                    }else {
                        /** 失败 */
                        message.sign = 2
                    }
                    /** 更改标记 */
                    synchronousMessageObjService.update(message, arrayListOf("sign"))
                }
            }
            Configure.DELETE -> {
                /** 删除 */
                val sign = EquipApi.deleteObj(dictProject!!.groupCode!!,message.projectId!!, message.objId!!)
                if (sign){
                    /** 成功 */
                    message.sign = 0
                }else {
                    /** 失败 */
                    message.sign = 2
                }
                /** 更改标记 */
                synchronousMessageObjService.update(message, arrayListOf("sign"))
            }
        }
    }





}