package com.persagy.server.utils

import com.persagy.database.SFilter
import com.persagy.database.SPageQueryInfo
import com.persagy.database.functions.SLength
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.assistant.ViewEquipSystem
import com.persagy.server.datacenter.models.entities.dict.ExplainAllCategories
import com.persagy.server.datacenter.models.entities.objects.GeneralSystem
import com.persagy.server.datacenter.models.response.EquipAssetComponentsReqponse
import com.persagy.server.service.objects.EquipmentService
import com.persagy.service.SBaseService

/**
 * 数据校验
 *
 * @author 张维新
 */
object DataVerification {

    /**
     * 查询 设备关联的资产 部件数量帮助接口
     */
    fun getEquipAssetComponentsTools(equipAssetComponentsList: SPageQueryInfo<EquipAssetComponentsReqponse>): ArrayList<EquipAssetComponentsReqponse> {
        /** 设备关联系统服务 */
        val viewEquipSystemservice = SBaseService(SMybatisDao(ViewEquipSystem::class.java))
        val generalSystemService = SBaseService(SMybatisDao(GeneralSystem::class.java))
        val list = ArrayList<EquipAssetComponentsReqponse>()
        if (equipAssetComponentsList.total!! > 0) {
            val contentList = equipAssetComponentsList.content
            for (content in contentList!!) {

                val exec = EquipmentService.select(SFilter.lte(SLength("category"), 6), SFilter.eq("parentId", content.id!!)).exec()

                content.count = exec.size
//                var equipAssetComponentsReqponse = EquipAssetComponentsReqponse()
//                equipAssetComponentsReqponse.id = content.id
//                equipAssetComponentsReqponse.linkId = content.linkId
//                equipAssetComponentsReqponse.linkEquipName = content.linkName
//                equipAssetComponentsReqponse.linkFamily = content.linkFamily
//                equipAssetComponentsReqponse.linkEquipLocalName = content.linkLocalName
//                equipAssetComponentsReqponse.count = exec.size
//                equipAssetComponentsReqponse.name = content.name
//                equipAssetComponentsReqponse.customParam = content.customParam
//                equipAssetComponentsReqponse.alram = content.alram
//                equipAssetComponentsReqponse.settingParam = content.settingParam
//                equipAssetComponentsReqponse.runParam = content.runParam
//                equipAssetComponentsReqponse.techParam = content.techParam
//                equipAssetComponentsReqponse.category = content.category
//                equipAssetComponentsReqponse.buildingId = content.buildingId
//                equipAssetComponentsReqponse.projectId = content.projectId
//                equipAssetComponentsReqponse.floorId = content.floorId
//                equipAssetComponentsReqponse.bimId = content.bimId
//                equipAssetComponentsReqponse.bimLocation = content.bimLocation
//                equipAssetComponentsReqponse.cadId = content.cadId
//                equipAssetComponentsReqponse.commParam = content.commParam
//                equipAssetComponentsReqponse.ledgerParam = content.ledgerParam
//                equipAssetComponentsReqponse.localId = content.localId
//                equipAssetComponentsReqponse.localName = content.localName
//                equipAssetComponentsReqponse.qrCode = content.qrCode
//                equipAssetComponentsReqponse.dpInsurerID = content.dpInsurerID
//                equipAssetComponentsReqponse.dpMaintainerID = content.dpMaintainerID
//                equipAssetComponentsReqponse.dpManufacturerID = content.dpManufacturerID
//                equipAssetComponentsReqponse.dpSupplierID = content.dpSupplierID
//                equipAssetComponentsReqponse.techParamGeneral = content.techParamGeneral
//                equipAssetComponentsReqponse.runParamGeneral = content.runParamGeneral

                val generalSystemList = ArrayList<GeneralSystem>()
                val systemList = viewEquipSystemservice.select(SFilter.eq("equipId", content.id!!)).exec()
                try {
                    if (!systemList.isEmpty()) {

                        for (system in systemList) {
                            if (!system.id.isNullOrEmpty()){
                                val systemEntity = generalSystemService.select(SFilter.eq("id", system.id!!)).entity()
                                if (systemEntity!=null){
                                    val generalSystem =GeneralSystem()
                                    generalSystem.id = system.id
                                    generalSystem.name = systemEntity.name
                                    generalSystem.localName = systemEntity.localName
                                    generalSystem.category = system.category
                                    generalSystemList.add(generalSystem)
                                }
                            }
                        }
                    }
                    content.linkSystem = generalSystemList
                    list.add(content)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        return list
    }

    /**
     * 查询 设备关联的资产 部件数量帮助接口
     */
    fun getEquipAssetComponentsToolsTwo(equipAssetComponentsList: List<EquipAssetComponentsReqponse>): ArrayList<EquipAssetComponentsReqponse> {
        /** 设备关联系统服务 */
        val viewEquipSystemservice = SBaseService(SMybatisDao(ViewEquipSystem::class.java))
        val generalSystemService = SBaseService(SMybatisDao(GeneralSystem::class.java))
        val explainAllCategoriesService = SBaseService(SMybatisDao(ExplainAllCategories::class.java))

        val list = ArrayList<EquipAssetComponentsReqponse>()
        if (equipAssetComponentsList.size!! > 0) {

            for (content in equipAssetComponentsList!!) {

                val exec = EquipmentService.select(SFilter.lte(SLength("category"), 6), SFilter.eq("parentId", content.id!!)).exec()

                content.count = exec.size

//                val exec1 = explainAllCategoriesService.select(SFilter.eq("parentId", content.category!!)).exec()
//                content.componentCategory = exec1

//                var equipAssetComponentsReqponse = EquipAssetComponentsReqponse()
//                equipAssetComponentsReqponse.id = content.id
//                equipAssetComponentsReqponse.linkId = content.linkId
//                equipAssetComponentsReqponse.linkEquipName = content.linkName
//                equipAssetComponentsReqponse.linkFamily = content.linkFamily
//                equipAssetComponentsReqponse.linkEquipLocalName = content.linkLocalName
//                equipAssetComponentsReqponse.count = exec.size
//                equipAssetComponentsReqponse.name = content.name
//                equipAssetComponentsReqponse.customParam = content.customParam
//                equipAssetComponentsReqponse.alram = content.alram
//                equipAssetComponentsReqponse.settingParam = content.settingParam
//                equipAssetComponentsReqponse.runParam = content.runParam
//                equipAssetComponentsReqponse.techParam = content.techParam
//                equipAssetComponentsReqponse.category = content.category
//                equipAssetComponentsReqponse.buildingId = content.buildingId
//                equipAssetComponentsReqponse.projectId = content.projectId
//                equipAssetComponentsReqponse.floorId = content.floorId
//                equipAssetComponentsReqponse.bimId = content.bimId
//                equipAssetComponentsReqponse.bimLocation = content.bimLocation
//                equipAssetComponentsReqponse.cadId = content.cadId
//                equipAssetComponentsReqponse.commParam = content.commParam
//                equipAssetComponentsReqponse.ledgerParam = content.ledgerParam
//                equipAssetComponentsReqponse.localId = content.localId
//                equipAssetComponentsReqponse.localName = content.localName
//                equipAssetComponentsReqponse.qrCode = content.qrCode
//                equipAssetComponentsReqponse.dpInsurerID = content.dpInsurerID
//                equipAssetComponentsReqponse.dpMaintainerID = content.dpMaintainerID
//                equipAssetComponentsReqponse.dpManufacturerID = content.dpManufacturerID
//                equipAssetComponentsReqponse.dpSupplierID = content.dpSupplierID
//                equipAssetComponentsReqponse.techParamGeneral = content.techParamGeneral
//                equipAssetComponentsReqponse.runParamGeneral = content.runParamGeneral

                val generalSystemList = ArrayList<GeneralSystem>()
                val systemList = viewEquipSystemservice.select(SFilter.eq("equipId", content.id!!)).exec()
                try {
                    if (!systemList.isEmpty()) {

                        for (system in systemList) {
                            if (!system.id.isNullOrEmpty()){
                                val systemEntity = generalSystemService.select(SFilter.eq("id", system.id!!)).entity()
                                if (systemEntity!=null){
                                    val generalSystem = GeneralSystem()
                                    generalSystem.id = system.id
                                    generalSystem.name = systemEntity.name
                                    generalSystem.localName = systemEntity.localName
                                    generalSystem.category = system.category
                                    generalSystemList.add(generalSystem)
                                }
                            }
                        }
                    }
                    content.linkSystem = generalSystemList
                    list.add(content)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        return list
    }

} // Object DataVerification