package com.persagy.server.services.dict

import com.persagy.database.SFilter
import com.persagy.database.SPageQueryInfo
import com.persagy.database.SQueryBuilder
import com.persagy.database.functions.SLength
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dict.DClassDef
import com.persagy.server.services.base.Service
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SQueryResponse

/**
 * 字典头部信息服务
 *
 * @author 张维新
 */
object DClassDefService : Service<DClassDef>(SMybatisDao(DClassDef::class.java)){

    /**
     * 分页查询
     *
     * @param   type     查询条件
     * @return              查询结果
     */
     fun equipQuery(type:String): SQueryResponse<DClassDef> {
        return try {
            var queryBuilder : SQueryBuilder<DClassDef>? = null
            var pageInfo : SPageQueryInfo<DClassDef>? = null
            when (type) {
                "Equipment" -> {
                    queryBuilder = select(SFilter.eq(SLength("id"), 4), SFilter.not(SFilter.isNull("parentId")))
                    pageInfo = queryBuilder.pageQuery(1, 1000)
                }
                "Component" -> {
                    queryBuilder = select(SFilter.eq(SLength("id"), 6), SFilter.not(SFilter.isNull("parentId")),
                            SFilter.not(SFilter.eq("parentId","Space")),SFilter.not(SFilter.eq("name","租户"))
                    )
                    pageInfo = queryBuilder.pageQuery(1, 1000)
                }
                "System" -> {
                    queryBuilder = select(SFilter.eq(SLength("id"), 2), SFilter.not(SFilter.eq("parentId","AllProfessionalSystemEquipment")))
                    pageInfo = queryBuilder.pageQuery(1, 1000)
                }
                "Major" -> {
                    queryBuilder = select(SFilter.eq(SLength("id"), 2), SFilter.eq("parentId","AllProfessionalSystemEquipment"))
                    pageInfo = queryBuilder.pageQuery(1, 1000)
                }
                "Major-System" -> {
                    val listClassDef = DClassDefService.select(SFilter.eq("parentId", "AllProfessionalSystemEquipment")).exec()
                    for (classDef in listClassDef){
                        val exec = DClassDefService.select(SFilter.eq("parentId", classDef.id!!)).exec()
                        classDef.listClassDef = exec
                    }
                    pageInfo = SPageQueryInfo(1, 1000)
                    pageInfo.content  = listClassDef
                }
            }
            SQueryResponse(pageInfo!!)
        } catch (e: Exception) {
            e.printStackTrace()
            SQueryResponse(SResponseType.failure, e.message!!)
        }
    } // Function query()

    /**
     * 查询设备部件类型
     */
    fun equipCommType(): SQueryResponse<DClassDef>{
        val sQueryResponse = SQueryResponse<DClassDef>()
        try {
            val dClassDefList = select(SFilter.or(SFilter.eq(SLength("id"), 4),
                    SFilter.eq(SLength("id"), 6)), SFilter.not(SFilter.isNull("parentId")),
                    SFilter.not(SFilter.eq("id","Tenant")),SFilter.not(SFilter.eq("id","Ispace"))).exec()
            sQueryResponse.content = dClassDefList
            sQueryResponse.total = dClassDefList.size.toLong()
        } catch (e: Exception) {
            e.toString()
        }
        return sQueryResponse

    } // Function equipCommType()



} // Object DClassDefService
