/*
 * ********************************************************************************************************************
 *
 *                     :*$@@%$*:                         ;:                ;;    ;;
 *                   :@@%!  :!@@%:                       %!             ;%%@@%$ =@@@@@@@%;     @%@@@%%%%@@@@@
 *                  :@%;       :$=                       %%$$$%$$         ;$$  ;$@=   !@$
 *                  =@!                                  %!              @ $=;%   !@@@%:      !$$$$$$$$$$$$$$=
 *                  =@*                                  %!              @ $= % %@=   =%@!      %=
 *             *$%%! @@=        ;=$%%%$*:                %!              @ $= % =%%%%%%@$      *%:         =%
 *           %@@!:    !@@@%=$@@@@%!  :*@@$:              %!              @ $= % $*     ;@      @*          :%*
 *         ;@@!          ;!!!;:         ;@%:      =======@%========*     @ $$ % $%*****$@     :@$=*********=@$
 *         $@*   ;@@@%=!:                *@*
 *         =@$    ;;;!=%@@@@=!           =@!
 *          %@$:      =@%: :*@@@*       %@=                    Copyright (c) 2016-2019.  北京上格云技术有限公司
 *           ;%@@$=$@@%*       *@@@$=%@@%;
 *              ::;::             ::;::                                              All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.persagy.server.utils

import com.persagy.server.datacenter.models.entities.dict.*
import com.persagy.server.services.dict.DictBuildingFunctionalService
import com.persagy.server.services.dict.DictClimateService
import com.persagy.server.services.dict.DictDevelopService
import com.persagy.server.services.dict.DictSpaceFunctionalTypesService
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SBaseService

/**
 * 递归查询
 *
 * @author 张维新
 */
object RecursiveQuery {
    /** 租赁业态类型 */
    private val dictRentalservice = SBaseService(SMybatisDao(DictRental::class.java))

    /**
     * 气候区 递归查询
     *
     * @param   parent  外键关系值
     * @param   hasChildren 是否进行递归查询
     */
    fun treeClimate( parent: String, hasChildren: Boolean = true): ArrayList<DictClimate> {
        val content = DictClimateService.select("parent" to parent).exec()
        if (hasChildren) {
            for (item in content) {
                try {
                    val children = treeClimate(item.id!!)
                    if (children.size > 0) {
                        item.content = children
                    }
                } catch (e: Exception) {
                    // DO NOTHING
                }
            }
        }
        return content
    } // Function treeClimate()

    /**
     * 发展水平递归查询
     *
     * @param   parent  外键关系值
     * @param   hasChildren 是否进行递归查询
     */
    fun treeDevelop( parent: String, hasChildren: Boolean = true): ArrayList<DictDevelop> {
        val content = DictDevelopService.select("parent" to parent).exec()
        if (hasChildren) {
            for (item in content) {
                try {
                    val children = treeDevelop(item.id!!)
                    if (children.size > 0) {
                        item.content = children
                    }
                } catch (e: Exception) {
                    // DO NOTHING
                }
            }
        }
        return content
    } // Function treeDevelop()

    /**
     *建筑功能类型递归查询
     *
     * @param   parent  外键关系值
     * @param   hasChildren 是否进行递归查询
     */
    fun treeBuildingFunctional( parent: String, hasChildren: Boolean = true): ArrayList<DictBuildingFunctional> {
        val content = DictBuildingFunctionalService.select("parent" to parent).exec()
        if (hasChildren) {
            for (item in content) {
                try {
                    val children = treeBuildingFunctional(item.id!!)
                    if (children.size > 0) {
                        item.content = children
                    }
                } catch (e: Exception) {
                    // DO NOTHING
                }
            }
        }
        return content
    } // Function treeBuildingFunctional()

    /**
     *建筑功能类型递归查询
     *
     * @param   parent  外键关系值
     * @param   hasChildren 是否进行递归查询
     */
    fun treeSpaceFunctional( parent: String, hasChildren: Boolean = true): ArrayList<DictSpaceFunctionalTypes> {
        val content = DictSpaceFunctionalTypesService.select("parent" to parent).exec()
        if (hasChildren) {
            for (item in content) {
                try {
                    val children = treeSpaceFunctional(item.id!!)
                    if (children.size > 0) {
                        item.content = children
                    }
                } catch (e: Exception) {
                    // DO NOTHING
                }
            }
        }
        return content
    } // Function treeBuildingFunctional()

    /**
     * 租赁业态类型递归查询
     *
     * @param   parent  外键关系值
     * @param   hasChildren 是否进行递归查询
     */
    fun treeDictRental( parent: String, hasChildren: Boolean = true): ArrayList<DictRental> {

        val content = dictRentalservice.select("parent" to parent).exec()
        if (hasChildren) {
            for (item in content) {
                try {
                    item.parent = null
                    val children = treeDictRental(item.id!!)
                    if (children.size > 0) {
                        item.content = children
                    }
                } catch (e: Exception) {
                    // DO NOTHING
                }
            }
        }
        return content
    } // Function treeDictRental()

    /**
     * 查询空间功能树结构
     *
     * @param   parent       父节点
     * @param   hasChildren  是否循环递归
     */
    fun dictSpaceFunctionalTreeDictRental(parent: String, hasChildren: Boolean = true): ArrayList<DictSpaceFunctionalTypes> {

        val content = DictSpaceFunctionalTypesService.select("parent" to parent).exec()
        if (hasChildren) {
            for (item in content) {
                try {
                    item.parent = null
                    item.createTime = null
                    item.lastUpdate = null
                    val children = dictSpaceFunctionalTreeDictRental(item.id!!)
                    if (children.size > 0) {
                        item.content = children
                    }
                } catch (e: Exception) {
                    // DO NOTHING
                    e.printStackTrace()
                }
            }
        }
        return content
    } // Function dictSpaceFunctionalTreeDictRental()

} // object RecursiveQuery