package com.persagy.labsl.services

import cn.hutool.core.collection.CollUtil
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.persagy.labsl.Opts
import com.persagy.labsl.client.DmpRwdClient
import com.persagy.labsl.models.entities.pbs.Building
import com.persagy.labsl.models.entities.pbs.Floor
import com.persagy.labsl.models.request.InstanceUrlParam
import com.persagy.labsl.models.response.DmpResult
import com.persagy.labsl.utils.AdmEntityTransferUtil
import com.persagy.service.utils.SSpringContextUtil
import org.springframework.stereotype.Service

/**
 * 物理世界对象查询
 * @author:linhuili
 * @date:2021/10/19
 */
@Service
class DmpBaseService {

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

    /* public List<T> queryDmpObject(JSONObject params, Class<T> clazz){
        //封装基础查询参数
        InstanceUrlParam instanceUrlParam = new InstanceUrlParam();
        instanceUrlParam.setGroupCode(Opts.INSTANCE.getGroupCode());
        instanceUrlParam.setProjectId(Opts.INSTANCE.getProjectId());
        //查询对象信息
        DmpResult<JSONArray> dmpResult = rwdClient.queryObject(instanceUrlParam, params);
        if(dmpResult == null){
            return new ArrayList<>();
        }
        return AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), null, clazz);
    }*/


    /**
     * 查询建筑
     * @param params
     * @return
     */
    fun queryBuilding(params: JSONObject?): List<Building>? {
        //封装基础查询参数
        val instanceUrlParam = InstanceUrlParam()
        instanceUrlParam.groupCode = Opts.groupCode
        instanceUrlParam.projectId = Opts.projectId
        //查询对象信息
        val dmpResult = rwdClient!!.queryObject(instanceUrlParam, params) ?: return ArrayList()
        if(dmpResult.data == null){
            return ArrayList<Building>()
        }
        return AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.data, null, Building::class.java)
    }



    /**
     * 查询楼层
     * @param params
     * @return
     */
    fun queryFloor(params: JSONObject?): List<Floor>? {
        //封装基础查询参数
        val instanceUrlParam = InstanceUrlParam()
        instanceUrlParam.groupCode = Opts.groupCode
        instanceUrlParam.projectId = Opts.projectId
        //查询对象信息
        val dmpResult = rwdClient!!.queryObject(instanceUrlParam, params) ?: return ArrayList()
        if(dmpResult.data == null){
            return ArrayList<Floor>()
        }
        var floorList = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.data, null, Floor::class.java)
        if(floorList!=null){
            queryFlInBd(instanceUrlParam,floorList)
        }
        return floorList
    }


    /**
     * 查询楼层所在建筑
     * @param context
     * @param floorList
     */
    fun queryFlInBd(context: InstanceUrlParam?, floorList: List<Floor>) {
        //查询楼层所在建筑
        for (floor in floorList) {
            val param = JSONObject()
            val criteria = JSONObject()
            criteria["graphCode"] = "ArchSubset"
            criteria["relCode"] = "Bd2Fl"
            criteria["objTo"] = floor.id
            param["criteria"] = criteria
            val dmpResult: DmpResult<JSONArray?>? = rwdClient.queryObjectRelation(context, param)
            val data = dmpResult?.data
            if (CollUtil.isNotEmpty(data)) {
                val build = data!!.getJSONObject(0)
                if (CollUtil.isNotEmpty(build)) {
                    floor.buildingId = build.getString("objFrom")
                }
            }
        }
    }


}