package com.persagy.server.services.graphtype

import com.persagy.server.services.base.RService
import com.persagy.server.datacenter.models.entities.graphtype.block.ConnectedBlockFamilyItem
import com.persagy.server.datacenter.models.entities.graphtype.block.ConnectedBlockItem
import com.persagy.server.datacenter.models.entities.graphtype.block.ConnectedBlockResponse
import com.persagy.server.services.dict.EquipFamilyService
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SPageContext
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.responses.SQueryResponse
import org.slf4j.LoggerFactory

/**
 * 源末端
 *
 * @author  邓婷婷
 */
object ConnectedBlockService : RService<ConnectedBlockFamilyItem>(SMybatisDao(ConnectedBlockFamilyItem::class.java))  {

    // 日志
    private val logger = LoggerFactory.getLogger(ConnectedBlockService::class.java)


    fun getConnectedBlock( request: SQueryRequest): SQueryResponse<ConnectedBlockResponse> {
        var projectId = SPageContext.getHeader("ProjectId")
        val responseList = ArrayList<ConnectedBlockResponse>()
        val result = SQueryResponse<ConnectedBlockResponse>()
        val list1 = ConnectedBlockService.select(request.filters).filter(SFilter.eq("projectId",projectId!!)).projection("blockId").distinct().exec()
        for(block in list1){
            val response = ConnectedBlockResponse()
            val familyItem = ArrayList<ConnectedBlockItem>()

            response.blockId = block.blockId

            val list2 = ConnectedBlockService.select(request.filters).filter(SFilter.eq("projectId",projectId!!),SFilter.eq("blockId",block.blockId!!)).projection("family").distinct().exec()
            for(family in list2){
                val f = ConnectedBlockItem()
                f.family = family.family
                val equipFamily = EquipFamilyService.select(SFilter.eq("equipCode",family.family!!)).entity()
                f.familyName = equipFamily!!.equipName
                f.items = ConnectedBlockService.select(request.filters).filter(SFilter.eq("projectId",projectId!!), SFilter.eq("blockId", block.blockId!!), SFilter.eq("family", family.family!!)).exec()
                familyItem.add(f)
            }
            response.items = familyItem
            responseList.add(response)
        }
        result.content = responseList
        return result
    } // Fun

} // Class ConnectedBlockService