package com.persagy.server.services

import cn.hutool.core.collection.CollUtil
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.persagy.server.Opts
import com.persagy.server.client.AdmMiddlewareClient
import com.persagy.server.datacenter.models.entities.objects.Building
import com.persagy.server.datacenter.models.entities.objects.Component
import com.persagy.server.datacenter.models.entities.objects.Equipment
import com.persagy.server.datacenter.models.entities.objects.Property

import com.persagy.service.models.SCascadeQuery
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.requests.SUpdateRequest
import com.persagy.service.utils.SSpringContextUtil
import org.springframework.stereotype.Service
import java.util.*
import kotlin.collections.ArrayList

/**
 * ADM基础接口查询
 * @author:linhuili
 * @date:2021/10/19
 */
@Service
class AdmBaseService {

    /** 自定义 sql */
    private val admMiddlewareClient by lazy {
        SSpringContextUtil.getBean(AdmMiddlewareClient::class.java) as AdmMiddlewareClient
    }

    /**
     * 查询建筑
     * @param params
     * @return
     */
    fun queryBuilding(criteria: SQueryRequest?): List<Building>? {
        //查询对象信息
        val result = admMiddlewareClient!!.queryBuilding(Opts.groupCode,Opts.projectId,criteria) ?: return ArrayList()
        if(CollUtil.isEmpty(result.content)){
            return ArrayList<Building>()
        }
        return result.content
    }

    /**
     * 查询设备
     */
    fun queryEquip(criteria: SQueryRequest?): List<Equipment>? {
        //查询对象信息
        val result = admMiddlewareClient!!.queryEquip(Opts.groupCode,Opts.projectId,criteria) ?: return ArrayList()
        if(CollUtil.isEmpty(result.content)){
            return ArrayList<Equipment>()
        }
        return result.content?.let { entityAttributeConvert(it) }
    }


    /**
     * 更新设备
     */
    fun updateEquip(equipments: ArrayList<Equipment>){
        val sUpdateRequest = SUpdateRequest<Equipment>()
        sUpdateRequest.content = equipments
        val updateEquip = admMiddlewareClient.updateEquip(Opts.groupCode, Opts.projectId, sUpdateRequest)
    }


    /**
     * 更新部件
     */
    fun updateComponent(components: ArrayList<Component>){
        val sUpdateRequest = SUpdateRequest<Component>()
        sUpdateRequest.content = components
        val updateEquip = admMiddlewareClient.updateComponent(Opts.groupCode, Opts.projectId, sUpdateRequest)
    }

    /**
     * 查询资产
     */
    fun queryProperty(criteria: SQueryRequest?): List<Property>? {
        //查询对象信息
        val result = admMiddlewareClient!!.queryProperty(Opts.groupCode,Opts.projectId,criteria) ?: return ArrayList()
        if(CollUtil.isEmpty(result.content)){
            return ArrayList<Property>()
        }
        return result.content?.let { propertyAttributeConvert(it) }
    }


    /**
     * 更新资产
     */
    fun updateProperty(properties: ArrayList<Property>){
        val sUpdateRequest = SUpdateRequest<Property>()
        sUpdateRequest.content = properties
        val updateEquip = admMiddlewareClient.updateProperty(Opts.groupCode, Opts.projectId, sUpdateRequest)
    }


    /**
     * 实体属性转换
     */
    fun entityAttributeConvert(objects:List<JSONObject>): List<Equipment>? {
        val result = ArrayList<Equipment>()
        if(objects == null){
            return result
        }
        for(obj in objects){
            val equipment = JSON.toJavaObject(obj, Equipment::class.java)
            result.add(equipment)
        }
        return result
    }


    /**
     * 根据设备id查询部件
     */
    fun queryComponyByEquipId(id:String): ArrayList<Component>? {
        var equipRequest = SQueryRequest()
        equipRequest.filters = "id = '"+id+"'"
        //级联查询部件
        var cascade = SCascadeQuery()
        cascade.name = "component"
        equipRequest.cascade = arrayListOf(cascade)

        val equipmentList = queryEquip(equipRequest)
        if(CollUtil.isEmpty(equipmentList)){
            return ArrayList();
        }
        val equipment = equipmentList?.get(0)

        return equipment?.component
    }

    /**
     * 根据设备id查询设备信息
     */
    fun queryEquipById(id:String): List<Equipment>? {
        var equipRequest = SQueryRequest()
        equipRequest.filters = "id = '"+id+"'"
        return queryEquip(equipRequest)
    }

    /**
     * 实体属性转换
     */
    fun propertyAttributeConvert(objects:List<JSONObject>): List<Property>? {
        val result = ArrayList<Property>()
        if(objects == null){
            return result
        }
        for(obj in objects){
            val equipment = JSON.toJavaObject(obj, Property::class.java)
            result.add(equipment)
        }
        return result
    }

    /**
     * 根据设备id查询设备信息
     */
    fun queryPropertyById(id:String): List<Property>? {
        var equipRequest = SQueryRequest()
        equipRequest.filters = "id = '"+id+"'"
        return queryProperty(equipRequest)
    }
}