package com.persagy.server.services.dict.dictnew

import com.persagy.database.SFilter
import com.persagy.database.SPageQueryInfo
import com.persagy.database.SQueryBuilder
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.*
import com.persagy.server.datacenter.models.requests.CategoryRequest
import com.persagy.server.services.assistant.SchemeService
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SQueryResponse
import com.persagy.service.utils.SSpringContextUtil

/**
 * 字典类型
 * @author  wx
 * @date  2020/9/23 15:43
 */
object DictDefClassService : SObjectService<DefClass>(SMybatisDao(DefClass::class.java)){

    /** 自定义SQL 对象 */
    val schemeService by lazy {
        SSpringContextUtil.getBean(SchemeService::class.java) as SchemeService
    }

        /** 方案和项目的关系 */
        private val dictSchemeAndProjectService = SObjectService(SMybatisDao(DictSchemeAndProject::class.java))
        /** 方案下的类型 */
        private val dictSchemeCategotyService = SObjectService(SMybatisDao(DictSchemeCategoty::class.java))
        /** 项目所属的集团 */
        private val dictProjectService = SObjectService(SMybatisDao(DictProject::class.java))
        /** 平台集团对象类型 */
        private val defClassService = SObjectService(SMybatisDao(DefClass::class.java))
        /** 专业 */
        private val dictMajorService = SObjectService(SMybatisDao(DictMajor::class.java))
        /** 信息点 */
        private val dictDefFuncId = SObjectService(SMybatisDao(DefFuncId::class.java))



    /**
     * 查询设备、部件、系统、专业类型
     *
     * @param   categoryRequest     类型查询对象
     */
    fun equipQuery(categoryRequest: CategoryRequest): SQueryResponse<DefClass> {
        val sQueryResponse = SQueryResponse<DefClass>()
        if (!categoryRequest.type.isNullOrEmpty()){
            val projectId = SPageContext.getHeader("ProjectId")
            var dictSchemeCategoryList = ArrayList<DefClass>()
            // 查询项目所在的集团 然后取集团字典
//        val dictSchemeAndProject = dictSchemeAndProjectService.select(SFilter.eq("projectId", projectId!!)).entity()
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId!!)).entity()
            if (dictProject != null){
                var queryBuilder : SQueryBuilder<DefClass>? = null
                var pageInfo : SPageQueryInfo<DefClass>? = null
                when (categoryRequest.type) {
                    "equipment" -> {
//                    queryBuilder = select(SFilter.eq(SLength("id"), 4), SFilter.not(SFilter.isNull("parentId")))
//                    pageInfo = queryBuilder.pageQuery(1, 1000)

                        dictSchemeCategoryList = defClassService.select(
                            SFilter.eq("parentCode","equipment"), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()

                    }
                    "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)
                        dictSchemeCategoryList = defClassService.select(
                            SFilter.eq("parentCode","component"), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                    }
                    "system" -> {
//                    queryBuilder = select(SFilter.eq(SLength("id"), 2), SFilter.not(SFilter.eq("parentId","AllProfessionalSystemEquipment")))
//                    pageInfo = queryBuilder.pageQuery(1, 1000)
                        dictSchemeCategoryList = defClassService.select(
                            SFilter.eq("parentCode","system"), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                    }
                    "major" -> {
//                    queryBuilder = select(SFilter.eq(SLength("id"), 2), SFilter.eq("parentId","AllProfessionalSystemEquipment"))
////                    pageInfo = queryBuilder.pageQuery(1, 1000)
                        val dictMajorList = dictMajorService.selectAll().exec()
                        if (dictMajorList.size>0){
                            for (dictMajor in dictMajorList){
                                val defClass = DefClass()
                                defClass.code = dictMajor.code
                                defClass.name = dictMajor.name
                                defClass.aliasName = dictMajor.aliasName
                                defClass.aliasCode = dictMajor.aliasCode
                                dictSchemeCategoryList.add(defClass)
                            }
                        }
                    }
                    "majorSystem" -> {
//                    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
                        val dictMajorList = dictMajorService.selectAll().exec()
                        if (dictMajorList.size>0){
                            for (dictMajor in dictMajorList){
                                val defClass = DefClass()
                                defClass.code = dictMajor.code
                                defClass.name = dictMajor.name
                                defClass.aliasName = dictMajor.aliasName
                                defClass.aliasCode = dictMajor.aliasCode
                                val defClassList = defClassService.select(
                                    SFilter.eq("parentCode", "system"), SFilter.eq("type", "group"),
                                    SFilter.eq("groupId", dictProject.groupId!!), SFilter.eq("majorCode", defClass.code!!)
                                ).exec()
                                defClass.children = defClassList
                                dictSchemeCategoryList.add(defClass)
                            }
                        }
                    }
                    "space" -> {
                        /** 业务空间类型 */
                        dictSchemeCategoryList = defClassService.select(
                            SFilter.eq("parentCode","space"), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                    }
                    "majorEquipment" -> {
                        /** 专业下的设备 */
                        val dictMajorList = dictMajorService.selectAll().exec()
                        if (dictMajorList.size>0){
                            for (dictMajor in dictMajorList){
                                val defClass = DefClass()
                                defClass.code = dictMajor.code
                                defClass.name = dictMajor.name
                                defClass.aliasName = dictMajor.aliasName
                                defClass.aliasCode = dictMajor.aliasCode
                                val defClassList = defClassService.select(
                                    SFilter.eq("parentCode", "equipment"), SFilter.eq("type", "group"),
                                    SFilter.eq("groupId", dictProject.groupId!!),SFilter.eq("majorCode", defClass.code!!)
                                ).exec()
                                defClass.children = defClassList
                                dictSchemeCategoryList.add(defClass)
                            }
                        }
                    }
                    "equipmentAndComponent" -> {
                        /** 设备和部件 */
                        dictSchemeCategoryList = defClassService.select(
                            SFilter.or(SFilter.eq("parentCode","component"),SFilter.eq("parentCode","equipment")), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                    }
                    "equipmentComponent" ->{
                        /** 设备下的部件 */
                        dictSchemeCategoryList = defClassService.select(
                            SFilter.eq("parentCode","equipment"), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                        val defClassList = defClassService.select(
                            SFilter.eq("parentCode", "component"),
                            SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                        if (dictSchemeCategoryList.size > 0){
                            for (dictSchemeCategory in dictSchemeCategoryList){
                                var list = ArrayList<DefClass>()
                                for (defClass in defClassList){
                                    if (defClass.equipmentCode == dictSchemeCategory.code){
                                        list.add(defClass)
                                    }
                                }
                                if (list.size>0){
                                    dictSchemeCategory.children = list
                                }
                            }
                        }
                    }
                    "majorSystemEquip" -> {
                        /** 专业下的系统，系统下的设备 */
                        val dictMajorList = dictMajorService.selectAll().exec()
                        if (dictMajorList.size>0){
                            for (dictMajor in dictMajorList){
                                val defClass = DefClass()
                                defClass.code = dictMajor.code
                                defClass.name = dictMajor.name
                                defClass.aliasName = dictMajor.aliasName
                                defClass.aliasCode = dictMajor.aliasCode
                                dictSchemeCategoryList.add(defClass)
                            }
                        }
                        /** 系统 */
                        val systemList = defClassService.select(
                            SFilter.eq("parentCode", "system"), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                        /** 设备 */
                       val equipmentList = defClassService.select(
                            SFilter.eq("parentCode","equipment"), SFilter.eq("type", "group"),
                            SFilter.eq("groupId", dictProject.groupId!!)
                        ).exec()
                        /** 系统下的设备 */
                        for (system in systemList){
                            for (equipment in equipmentList){
                                if (system.code == equipment.systemCode){
                                    if (system.children.isNullOrEmpty()){
                                        system.children = ArrayList<DefClass>()
                                    }
                                    system.children!!.add(equipment)
                                }
                            }
                        }
                        /** 专业下的系统 */
                        for (system in systemList){
                            for (dictMajor in dictSchemeCategoryList){
                                if (dictMajor.code == system.majorCode ){
                                    if (dictMajor.children.isNullOrEmpty()){
                                        dictMajor.children = ArrayList<DefClass>()
                                    }
                                    dictMajor.children!!.add(system)
                                }
                            }
                        }
                    }
                }
                sQueryResponse.result = SResponseType.success
                sQueryResponse.content = dictSchemeCategoryList
                sQueryResponse.total = dictSchemeCategoryList.size.toLong()
            }
        }else{
            sQueryResponse.result = SResponseType.failure
            sQueryResponse.message = "请输入查询类型"
        }
        return sQueryResponse
    } // Fun equipQuery()

    /**
     * 查询项目下对象类型对应的信息点
     */
    fun categoryInfoQuery(categoryRequest: CategoryRequest): SQueryResponse<DefFuncId> {
        val projectId = SPageContext.getHeader("ProjectId")
        val sQueryResponse = SQueryResponse<DefFuncId>()
        if (!categoryRequest.type.isNullOrEmpty()){
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId!!)).entity()
            if (dictProject!=null){
                val dictDefFuncBuilder = dictDefFuncId.select(
                    SFilter.eq("projectId", projectId),
                    SFilter.eq("groupId", dictProject.groupId!!),
                    SFilter.eq("classCode", categoryRequest.type!!)
                )
                val dictDefFuncList = dictDefFuncBuilder.table("dict.def_funcid_project").order("sort").exec()
                if (dictDefFuncList.size>0){
                    sQueryResponse.content = dictDefFuncList
                    sQueryResponse.total = dictDefFuncList.size.toLong()
                }else{
                    schemeService.dictInfo(projectId,dictProject.groupId!!,categoryRequest.type!!)
                    val dictDefFuncBuilder = dictDefFuncId.select(
                        SFilter.eq("projectId", projectId),
                        SFilter.eq("groupId", dictProject.groupId!!),
                        SFilter.eq("classCode", categoryRequest.type!!)
                    )
                    val dictDefFuncList = dictDefFuncBuilder.table("dict.def_funcid_project").order("sort").exec()
                    sQueryResponse.content = dictDefFuncList
                    sQueryResponse.total = dictDefFuncList.size.toLong()
                }
                sQueryResponse.result = SResponseType.success
            }else{
                sQueryResponse.result = SResponseType.failure
            }
        }else {
            sQueryResponse.result = SResponseType.failure
        }
        return sQueryResponse
    } // Fun categoryInfoQuery()




} // Object DictDefClassService