/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

package com.persagy.server.service

import com.persagy.base.extensions.rootCause
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.database.SQueryBuilder
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dict.EquipCategory
import com.persagy.server.datacenter.models.entities.dict.EquipFamily
import com.persagy.server.datacenter.models.entities.dict.Family
import com.persagy.server.datacenter.models.entities.dictnew.DefClass
import com.persagy.server.datacenter.models.entities.graphtype.RelationTypeProject
import com.persagy.server.datacenter.models.entities.objects.*
import com.persagy.server.datacenter.models.entities.rel.REq2Sp
import com.persagy.server.service.base.Service
import com.persagy.server.service.objects.PropertyService
import com.persagy.server.utils.IdUtils
import com.persagy.server.utils.MatchingProperty
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.requests.SUpdateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SQueryResponse
import org.slf4j.LoggerFactory
import java.util.*
import kotlin.collections.ArrayList

/**
 * 设备和部件信息服务
 *
 * @author 张维新
 */
object EquipmentComService : Service<Equipment>(SMybatisDao(Equipment::class.java)) {
    /** 日志 */
    private val logger = LoggerFactory.getLogger(EquipmentComService::class.java)

    /** 建筑服务 */
    val buildingService = SObjectService(SMybatisDao(Building::class.java))

    /** 楼层服务 */
    val floorService = SObjectService(SMybatisDao(Floor::class.java))
    /** 关系总览服务 */
    val relationTypeProjectservice = SBaseService(SMybatisDao(RelationTypeProject::class.java))
    /** 字典对象类型 */
    private val defClassService = SBaseService(SMybatisDao(DefClass::class.java))

    /**
     * 查询之前操作
     */
    override fun onQueryBefore(queryBuilder: SQueryBuilder<Equipment>) {
        queryBuilder.tableName = "v_equip_property_component"

        return
    }

    /**
     *  批量处理设备关联资产、 查询接口
     *
     *  @param      category        设备或部件类型
     *  @param      request         条件
     *  @param      spaceIdList     空间id列表
     *  @param      spacrType       空间类型
     *  */
    fun equipProperty(category:String, request: SQueryRequest, spaceIdList: ArrayList<String>?, spacrType: String?):SQueryResponse<Equipment> {

        return try {
            /** 设备类型或设备族类型 */
            val rEq2SpService = SBaseService(SMybatisDao(REq2Sp::class.java))
        val equipFamilyService = SBaseService(SMybatisDao(EquipFamily::class.java))
        val projectId = SPageContext.getHeader("projectId")
        if (spaceIdList!=null&&spaceIdList.size>0){
            logger.debug("空间*************** ")
            /** 获取到空间关联的设备 */
            val rEqInSpBaseList = rEq2SpService.select(SFilter.inList("id2", spaceIdList), SFilter.eq("type","eq2sp_in")).exec()
            if ( !rEqInSpBaseList.isEmpty()){
                val setId = HashSet<String>()
                for (rEqInSpBase in rEqInSpBaseList){
                    setId.add(rEqInSpBase.id1!!)
                }
                val sQueryResponse = SQueryResponse<Equipment>()

                val equipList = select(SFilter.inList("id", setId.toList())).exec()
                sQueryResponse.total = equipList.size.toLong()
                val iterator = equipList.iterator()
                while (iterator.hasNext()) {
                    var equipment = iterator.next()
                    if (category != equipment.classCode){
                        iterator.remove()
                    }
                }

                if (equipList.size>0){
                    val iterator = equipList.iterator()
                    while (iterator.hasNext()) {
                        var equipment = iterator.next()
                        val equipFamily = equipFamilyService.select(SFilter.eq("equipCode", equipment.classCode!!)).entity()
                        var propertyList = ArrayList<Property>()
//                        if (!equipment.buildingId.isNullOrEmpty()&&!equipment.floorId.isNullOrEmpty()){
//                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("floorId", equipment.floorId!!), SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                        }else if (!equipment.buildingId.isNullOrEmpty()&&equipment.floorId.isNullOrEmpty()){
//                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("buildingId", equipment.buildingId!!),SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                        }else{
                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("family", equipFamily!!.family!!),SFilter.isNull("equipId")).exec()
//                        }
                        logger.debug("category = ${equipment.classCode}")
                        logger.debug("Family = ${equipFamily.family}")
                        logger.debug("匹配长度 = ${propertyList.size}")
                        if (propertyList.size>0){
                            /** 计算匹配资产 */
                            val equipMatchingProperty = MatchingProperty.equipMatchingProperty(equipment, propertyList)
                            if (equipMatchingProperty.size>0){
                                equipment.propertyList = equipMatchingProperty
                            }else{
                                iterator.remove()
                            }
                        }
                        /** 编码审查 */
                        IdUtils.codeExanine()
                    }
                }
                sQueryResponse.result= SResponseType.success
                sQueryResponse.content = equipList
                sQueryResponse
            }else{
                val sQueryResponse = SQueryResponse<Equipment>()
                sQueryResponse.total = 0
                sQueryResponse.result=SResponseType.success
                sQueryResponse.content = ArrayList<Equipment>()
                sQueryResponse
            }
        }else if(!spacrType.isNullOrEmpty()&&spacrType == "noKnow"){
            logger.debug("空间类型*************** ")
            var floorId: String? = null
            var buildingId: String? = null
            val queryBuilder = select(request.filters)
            val filterList = queryBuilder.filterList
            for (filter in filterList){
                if (filter.first.toString() == "buildingId"){
                    buildingId = filter.second.toString()
                }else if (filter.first.toString() == "floorId"){
                    floorId = filter.second.toString()
                }
            }

            val rEqInSpBaseList =rEq2SpService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("type","eq2sp_in")).exec()
//            val rEqInSpBaseList = REqInSpBaseService.select(SFilter.eq("projectId", projectId!!)).exec()
            val setId = HashSet<String>()
                for (rEqInSpBase in rEqInSpBaseList){
                    setId.add(rEqInSpBase.id1!!)
                }

            val equipList = select(SFilter.eq("projectId", projectId!!), SFilter.eq("classCode", category), SFilter.eq("buildingId", buildingId!!),
                    SFilter.eq("floorId", floorId!!), SFilter.not(SFilter.inList("id", setId.toList()))).exec()
            if (!equipList.isEmpty()){
                val sQueryResponse = SQueryResponse<Equipment>()

                sQueryResponse.total = equipList.size.toLong()
                val iterator = equipList.iterator()
                while (iterator.hasNext()) {
                    var equipment = iterator.next()
                    val equipFamily = equipFamilyService.select(SFilter.eq("equipCode", equipment.classCode!!)).entity()
                    var propertyList = ArrayList<Property>()
//                    if (!equipment.buildingId.isNullOrEmpty()&&!equipment.floorId.isNullOrEmpty()){
                        propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("floorId", equipment.floorId!!), SFilter.isNull("equipId"), SFilter.eq("family", equipFamily!!.family!!)).exec()
//                    }else if (!equipment.buildingId.isNullOrEmpty()&&equipment.floorId.isNullOrEmpty()){
//                        propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("buildingId", equipment.buildingId!!),SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                    }else{
                        propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("family", equipFamily!!.family!!),SFilter.isNull("equipId")).exec()
//                    }
                    if (propertyList.size>0){
                        /** 计算匹配资产 */
                        val equipMatchingProperty = MatchingProperty.equipMatchingProperty(equipment, propertyList)
                        if (equipMatchingProperty.size>0){
                            equipment.propertyList = equipMatchingProperty
                        }else{
                            iterator.remove()
                        }
                    }

                    /** 编码审查 */
                    IdUtils.codeExanine()
                }

                if (!equipList.isEmpty()){

                    for (equip in equipList){
                        if (!equip.buildingId.isNullOrEmpty()){


                            val building = buildingService.select(SFilter.eq("id", equip.buildingId!!)).entity()
                            if (building!=null){
                                equip.building =  building
                            }
                        }
                        if (!equip.floorId.isNullOrEmpty()){
                            val floor = floorService.select(SFilter.eq("id", equip.floorId!!)).entity()
                            if (floor!=null){
                                equip.floor = floor
                            }

                        }
                        if (!equip.classCode.isNullOrEmpty()){
                            val equipFamily = defClassService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("code", equip.classCode!!)).entity()

                            if (equipFamily!=null){
                                val equipCategory = EquipCategory()
                                equipCategory.code = equipFamily.code
                                equipCategory.name = equipFamily.name
                                equip.equipCategory = equipCategory
                            }
                        }
                        if (!equip.propertyList!!.isEmpty()){
                            for ( property in equip.propertyList!!){
                                if (!property.buildingId.isNullOrEmpty()){


                                    val building = buildingService.select(SFilter.eq("id", property.buildingId!!)).entity()
                                    if (building!=null){
                                        property.building =  building
                                    }
                                }
                                if (!property.floorId.isNullOrEmpty()){
                                    val floor = floorService.select(SFilter.eq("id", property.floorId!!)).entity()
                                    if (floor!=null){
                                        property.floor = floor
                                    }

                                }
                                if (!property.family.isNullOrEmpty()){
                                    val equipFamily = equipFamilyService.select(SFilter.eq("family", property.family!!)).entity()
                                    if (equipFamily!=null){
                                        val family = Family()
                                        family.code = equipFamily.family
                                        family.name = equipFamily.familyName
                                        property.familyName  = family
                                    }
                                }
                            }
                        }
                    }
                }
                sQueryResponse.result=SResponseType.success
                sQueryResponse.content = equipList
                sQueryResponse
            }else{
                val sQueryResponse = SQueryResponse<Equipment>()
                sQueryResponse.total = 0
                sQueryResponse.result=SResponseType.success
                sQueryResponse.content = ArrayList<Equipment>()
                sQueryResponse
            }
        }else if (!spacrType.isNullOrEmpty()){
//            logger.debug("空间类型2*************** ")
            var floorId: String? = null
            var buildingId: String? = null
            val zoneSpaceBaseTable = SObjectService(SMybatisDao(ZoneSpace::class.java))
            val queryBuilder = select(request.filters)
            val filterList = queryBuilder.filterList
                for (filter in filterList){
                    if (filter.first.toString() == "buildingId"){
                        buildingId = filter.second.toString()
                    }else if (filter.first.toString() == "floorId"){
                        floorId = filter.second.toString()
                    }
                }
            /** 查询空间数据 */
            val zoneSpaceBaseList = zoneSpaceBaseTable.select(SFilter.eq("objectType", spacrType), SFilter.eq("projectId", projectId!!),
                    SFilter.eq("buildingId", buildingId!!), SFilter.eq("floorId", floorId!!)).exec()

            if (!zoneSpaceBaseList.isEmpty()){
                val listId = ArrayList<String>()
                for (zoneSpaceBase in zoneSpaceBaseList){
                    listId.add(zoneSpaceBase.id!!)
                }

                /** 获取到空间关联的设备 */
                val rEqInSpBaseList =  rEq2SpService.select(SFilter.inList("id2", listId), SFilter.eq("type","eq2sp_in")).exec()
//                val rEqInSpBaseList = REqInSpBaseService.select(SFilter.inList("spaceId", listId)).exec()
                if ( !rEqInSpBaseList.isEmpty()){
                    val setId = HashSet<String>()
                    for (rEqInSpBase in rEqInSpBaseList){
                        setId.add(rEqInSpBase.id1!!)
                    }
                    val sQueryResponse = SQueryResponse<Equipment>()

                    val equipList = select(SFilter.inList("id", setId.toList())).exec()
                    sQueryResponse.total = equipList.size.toLong()
                    val iterator = equipList.iterator()
                    while (iterator.hasNext()) {
                        var equipment = iterator.next()
                        if (category != equipment.classCode){
                            iterator.remove()
                        }
                    }

                    if (equipList.size>0){
                        val iterator = equipList.iterator()
                        while (iterator.hasNext()) {
                            var equipment = iterator.next()
                            val equipFamily = equipFamilyService.select(SFilter.eq("equipCode", equipment.classCode!!)).entity()
                            var propertyList = ArrayList<Property>()
//                            if (!equipment.buildingId.isNullOrEmpty()&&!equipment.floorId.isNullOrEmpty()){
//                                propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("floorId", equipment.floorId!!), SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                            }else if (!equipment.buildingId.isNullOrEmpty()&&equipment.floorId.isNullOrEmpty()){
//                                propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("buildingId", equipment.buildingId!!),SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                            }else{
                                propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("family", equipFamily!!.family!!),SFilter.isNull("equipId")).exec()
//                            }
                            if (propertyList.size>0){
                                /** 计算匹配资产 */
                                val equipMatchingProperty = MatchingProperty.equipMatchingProperty(equipment, propertyList)
                                if (equipMatchingProperty.size>0){
                                    equipment.propertyList = equipMatchingProperty
                                }else{
                                    iterator.remove()
                                }
                            }
                            /** 编码审查 */
                            IdUtils.codeExanine()
                        }
                    }



                    if (!equipList.isEmpty()){

                        for (equip in equipList){
                            if (!equip.buildingId.isNullOrEmpty()){


                                val building = buildingService.select(SFilter.eq("id", equip.buildingId!!)).entity()
                                if (building!=null){
                                    equip.building =  building
                                }
                            }
                            if (!equip.floorId.isNullOrEmpty()){
                                val floor = floorService.select(SFilter.eq("id", equip.floorId!!)).entity()
                                if (floor!=null){
                                    equip.floor = floor
                                }

                            }
                            if (!equip.classCode.isNullOrEmpty()){
                                val equipFamily = equipFamilyService.select(SFilter.eq("equipCode", equip.classCode!!)).entity()
                                if (equipFamily!=null){
                                    val equipCategory = EquipCategory()
                                    equipCategory.code = equipFamily.equipCode
                                    equipCategory.name = equipFamily.equipName
                                    equip.equipCategory   = equipCategory
                                }
                            }
                            if (!equip.propertyList!!.isEmpty()){
                                for ( property in equip.propertyList!!){
                                    if (!property.buildingId.isNullOrEmpty()){


                                        val building = buildingService.select(SFilter.eq("id", property.buildingId!!)).entity()
                                        if (building!=null){
                                            property.building =  building
                                        }
                                    }
                                    if (!property.floorId.isNullOrEmpty()){
                                        val floor = floorService.select(SFilter.eq("id", property.floorId!!)).entity()
                                        if (floor!=null){
                                            property.floor = floor
                                        }

                                    }
                                    if (!property.family.isNullOrEmpty()){
                                        val equipFamily = equipFamilyService.select(SFilter.eq("family", property.family!!)).entity()
                                        if (equipFamily!=null){
                                            val family = Family()
                                            family.code = equipFamily.family
                                            family.name = equipFamily.familyName
                                            property.familyName  = family
                                        }
                                    }

                                }

                            }
                        }
                    }


                    sQueryResponse.result=SResponseType.success
                    sQueryResponse.content = equipList
                    sQueryResponse
                }else{
                    val sQueryResponse = SQueryResponse<Equipment>()
                    sQueryResponse.total = 0
                    sQueryResponse.result=SResponseType.success
                    sQueryResponse.content = ArrayList<Equipment>()
                    sQueryResponse
                }


            }else{
                val sQueryResponse = SQueryResponse<Equipment>()
                sQueryResponse.total = 0
                sQueryResponse.result=SResponseType.success
                sQueryResponse.content = ArrayList<Equipment>()
                sQueryResponse
            }
        }else{
            logger.debug("设备*************** ")
            request.pageSize = 100000
            SQueryRequest.maxRow = 100000
            val pageQuery = pageQuery(request, arrayListOf(SFilter.eq("classCode", category),
                    SFilter.isNull("propertyId"),SFilter.eq("projectId", projectId!!)))
            var contentList :ArrayList<Equipment>? = null
            logger.debug("设备***************${pageQuery.toJson()} ")
            if (pageQuery.content!=null&&!pageQuery.content!!.isNullOrEmpty()){
                contentList = pageQuery.content as ArrayList<Equipment>
            }else{
                contentList = ArrayList<Equipment>()
            }

            if (contentList!=null&&!contentList.isEmpty()){

                try {
//                    logger.debug("设备***************11111111111111111111111111")
                    val iterator = contentList.iterator()
                    while (iterator.hasNext()){
                        var equipment = iterator.next()
                        val equipFamily = equipFamilyService.select(SFilter.eq("equipCode", equipment.classCode!!)).entity()
                        var propertyList = ArrayList<Property>()
//                        if (!equipment.buildingId.isNullOrEmpty()&&!equipment.floorId.isNullOrEmpty()){
//                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("floorId", equipment.floorId!!), SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                        }else if (!equipment.buildingId.isNullOrEmpty()&&equipment.floorId.isNullOrEmpty()){
//                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("buildingId", equipment.buildingId!!),SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                        }else{
                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("family", equipFamily!!.family!!),SFilter.isNull("equipId")).exec()
//                        }
//                        logger.debug("category = ${equipment.category}")
//                        logger.debug("Family = ${equipFamily.family}")
//                        logger.debug("匹配长度 = ${propertyList.size}")
                        if (propertyList.size>0){
                            /** 计算匹配资产 */
                            val equipMatchingProperty = MatchingProperty.equipMatchingProperty(equipment, propertyList)
                            logger.debug("匹配到的数据 = ${equipMatchingProperty.size}")
                            if (equipMatchingProperty.size>0){
                                equipment.propertyList = equipMatchingProperty
                            }else{
                                iterator.remove()
                            }
                            /** 编码审查 */
                            IdUtils.codeExanine()
                        }else{
                            iterator.remove()
                        }
                    }


//                    for (equipment in contentList){
//                        val equipFamily = equipFamilyService.select(SFilter.eq("equipCode", equipment.category!!)).entity()
//                        var propertyList = ArrayList<Property>()
//                        if (!equipment.buildingId.isNullOrEmpty()&&!equipment.floorId.isNullOrEmpty()){
//                             propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("floorId", equipment.floorId!!), SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                        }else if (!equipment.buildingId.isNullOrEmpty()&&equipment.floorId.isNullOrEmpty()){
//                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("buildingId", equipment.buildingId!!),SFilter.isNull("EquipmentId"), SFilter.eq("Family", equipFamily!!.family!!)).exec()
//                        }else{
//                            propertyList = PropertyService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("Family", equipFamily!!.family!!),SFilter.isNull("EquipmentId")).exec()
//                        }
//                        logger.debug("资产个数 =${propertyList.size}")
//                        if (propertyList.size>0){
//                            /** 计算匹配资产 */
//                            val equipMatchingProperty = MatchingProperty.equipMatchingProperty(equipment, propertyList)
//
//                            if (equipMatchingProperty.size>0){
//                                equipment.propertyList = equipMatchingProperty
//                            }else{
//                                contentList.remove(equipment)
//                            }
//                            try {
//                                Thread.sleep(500)
//                            } catch (e: InterruptedException) {
//                                e.printStackTrace()
//                            }
//                        }else{
//                            contentList.remove(equipment)
//                        }
//                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }



            if (!contentList.isEmpty()){

                for (equip in contentList){

                    if (!equip.buildingId.isNullOrEmpty()){


                        val building = buildingService.select(SFilter.eq("id", equip.buildingId!!)).entity()
                        if (building!=null){
                            equip.building =  building
                        }
                    }
                    if (!equip.floorId.isNullOrEmpty()){
                        try {
                            val floor = floorService.select(SFilter.eq("id", equip.floorId!!)).entity()
                            if (floor!= null){
                                equip.floor = floor
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                    if (!equip.classCode.isNullOrEmpty()){
                        val equipFamily = equipFamilyService.select(SFilter.eq("equipCode", equip.classCode!!)).entity()
                        if (equipFamily!=null){
                            val equipCategory = EquipCategory()
                            equipCategory.code = equipFamily.equipCode
                            equipCategory.name = equipFamily.equipName
                            equip.equipCategory   = equipCategory
                        }
                    }
                    if (!equip.propertyList!!.isEmpty()){
                        for ( property in equip.propertyList!!){
                            if (!property.buildingId.isNullOrEmpty()){

                                val building = buildingService.select(SFilter.eq("id", property.buildingId!!)).entity()
                                if (building!=null){
                                    property.building =  building
                                }
                            }
                            if (!property.floorId.isNullOrEmpty()){
                                val floor = floorService.select(SFilter.eq("id", property.floorId!!)).entity()
                                if (floor!=null){
                                    property.floor = floor
                                }

                            }
                            if (!property.family.isNullOrEmpty()){
                                val equipFamily = equipFamilyService.select(SFilter.eq("family", property.family!!)).entity()
                                if (equipFamily!=null){
                                    val family = Family()
                                    family.code = equipFamily.family
                                    family.name = equipFamily.familyName
                                    property.familyName  = family
                                }
                            }
                        }
                    }
                }
            }
            pageQuery.content = contentList
            pageQuery
        }
        } catch (e : Exception){
            e.printStackTrace()
            SQueryResponse(SResponseType.failure, e.rootCause()?.message ?: e.message ?: "未知异常！")
        }

    }

    /**
     * 创建设备和资产的关系
     */
    fun equipPro(request: SUpdateRequest<Property>): SBaseResponse {

       return try {
           val projectId = SPageContext.getHeader("projectId")
            var projection = request.projection

            if (projection == null ){
                projection = ArrayList()
                projection.add("equipId")
            }
            if (projection!=null&&projection!!.isEmpty()){
                projection.add("equipId")
            }
           if (projection!=null&&!projection.isEmpty()){
               projection.clear()
               projection.add("equipId")
           }
            val updateList = PropertyService.updateList(request)

           try {
               /** 处理关系标记 */
               val listtype = ArrayList<String>()
               listtype.add("pe2bd")
               listtype.add("pe2fl")
               listtype.add("pe2sh")
               listtype.add("pe2sp")
               for (type in listtype){
                   val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId!!), SFilter.eq("relationType", type)).entity()
                   if (entity!=null){
                       entity.computationalState = 2
                       relationTypeProjectservice.update(entity)
                   }
               }
           } catch (e: Exception) {
               e.printStackTrace()
           }

            updateList
        } catch (e: Exception) {
            e.printStackTrace()
            SBaseResponse(SResponseType.failure, e.message.toString())
        }
    }

} // Object EquipmentService