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

package com.persagy.server.services.objects

import cn.sagacloud.server.datacenter.utils.IdUtils
import cn.sagacloud.server.datacenter.utils.ShaftNameUtil
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.objects.Building
import com.persagy.server.services.assistant.ObjInfoService
import com.persagy.server.services.base.Service
import com.persagy.service.SPageContext
import java.util.*

/**
 * 建筑信息服务
 *
 * @author 张维新
 */
object BuildingService : Service<Building>(SMybatisDao(Building::class.java)) {

    /**
     * 创建对象前回调函数
     *
     * @param   building      创建前的回调函数
     * @return  返回true,可以创建对象
     */
    override fun onCreateBefore(building: Building): Boolean  {
        val projectId = SPageContext.getHeader("ProjectId")
        building.projectId = projectId
        if (!building.id.isNullOrEmpty()) {
            val isWith = building.id!!.startsWith("Bd")
            if (!isWith) {
                building.id = IdUtils.buildIdCreate(projectId!!)
            }
        } else {
            building.id = IdUtils.buildIdCreate(projectId!!)
        }

        if (building.name.isNullOrEmpty()){
            val buildingName = ShaftNameUtil.shaftName("建筑-")
            building.name = buildingName
        }
        return true
    } // Function onCreateBefore()


    /**
     * 删除之前
     */
    override fun onDeleteBefore(entity: Building): Boolean {
        try {
            val projectId = SPageContext.getHeader("ProjectId")
            entity.projectId = projectId

            val exec = FloorService.select(SFilter.eq("buildId", entity.id!!)).exec()

//            object : Thread() {
//                override fun run() {
//                    val  building = BuildingService.select(SFilter.eq("id", entity.id!!)).entity()
//                    /** 日志 */
//                    val dataCenterLogs = DataCenterLogs()
//                    dataCenterLogs.id = IdUtils.uuidCreate()
//                    dataCenterLogs.author = "dataCenter"
//                    dataCenterLogs.projectId = projectId
//                    dataCenterLogs.objectType = entity.objectType
//                    dataCenterLogs.type = "delete"
//                    dataCenterLogs.objectId = building!!.id
//                    val hashMap = HashMap<String, Any?>()
//                    hashMap[entity.objectType!!] = building
//                    dataCenterLogs.dataSource = hashMap
//                    logService.insert(dataCenterLogs)
//                }
//            }.start()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return super.onDeleteBefore(entity)
    } // Function onDeleteBefore()

    /**
     * @Description:创建之后
     * @Param:
     * @Return:
     */
    override fun onCreateSuccess(entityList: ArrayList<Building>) {
        super.onCreateSuccess(entityList)
        try {
            for (building in entityList) {
                ObjInfoService.createInfo(building,true,"Bd","Building")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

//    /**
//     * @Description: 创建或更新信息点
//     * @Param: objType,projectId,objId,list,map,flag
//     */
//    fun createInfo(objType: String,projectId: String,objId: String,lastUpdate: Date?,list: ArrayList<HashMap<String,String?>>,map: HashMap<String,Any?>?,flag: Boolean){
//        val SIMPLEDATEFORMAT_DC = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
//        for (itemMap in list) {
//            var objInfos = ObjInfos()
//            objInfos.objType = objType
//            objInfos.projectId = projectId
//            objInfos.objId = objId
//            objInfos.infoId = itemMap["infoPointCode"]
//
//            if(itemMap["secondTag"] !=null){
//                val value = (map!![itemMap["secondTag"]] as HashMap<String,Any>)[itemMap["infoPointCode"]].toString()
//                objInfos.value = value
//            }else{
//                val value = map!![itemMap["infoPointCode"]].toString()
//                objInfos.value = value
//            }
//            val oService = SBaseService(SMybatisDao(ObjInfos::class.java))
//            if(flag){
//                objInfos.time = SIMPLEDATEFORMAT_DC.format(Date())
//                oService.insert(objInfos)
//            }else{
//                objInfos.time = SIMPLEDATEFORMAT_DC.format(lastUpdate)
//                oService.update(objInfos)
//            }
//
//        }
//    }
//
//    /**
//     * @Description: 查找信息点选择创建或修改
//     * @Param: entityList,flag
//     */
//    fun findInfo(entityList: ArrayList<Building>,flag: Boolean){
//        val prodefs = PropertyDefProjectService.select(SFilter.eq("projectId",entityList.get(0).projectId!!),SFilter.eq("type","Building"), SFilter.or(SFilter.eq("inputMode","L"), SFilter.eq("inputMode","M"))).exec()
//        var consList = ArrayList<HashMap<String,String?>>()
//        var buildList = ArrayList<HashMap<String,String?>>()
//        var peopleList = ArrayList<HashMap<String,String?>>()
//        for (prodef in prodefs) {
//            var itemMap = HashMap<String,String?>()
//            itemMap["firstTag"] = prodef.firstTag
//            itemMap["secondTag"] = prodef.secondTag
//            itemMap["infoPointCode"] = prodef.infoPointCode
//            when {
//                prodef.firstTag.equals("ConsumptionInfo") -> {
//                    consList.add(itemMap)
//                }
//                prodef.firstTag.equals("BuildingInfo") -> {
//                    buildList.add(itemMap)
//                }
//                prodef.firstTag.equals("PeopleInfo") -> {
//                    peopleList.add(itemMap)
//                }
//            }
//        }
//        for (building in entityList) {
//            val lastUpdate = building.lastUpdate
//            if(building.buildingInfo!=null){
//                createInfo("Bd",building.projectId!!,building.id!!,lastUpdate,buildList,building.buildingInfo,flag)
//            }
//            if(building.consumptionInfo!=null){
//                createInfo("Bd",building.projectId!!,building.id!!,lastUpdate,consList,building.consumptionInfo,flag)
//            }
//            if(building.peopleInfo!=null){
//                createInfo("Bd",building.projectId!!,building.id!!,lastUpdate,peopleList,building.peopleInfo,flag)
//            }
//        }
//    }

    /**
     * @Description: 更新之后
     * @Param: entityList
     */
    override fun onUpdateSuccess(entityList: ArrayList<Building>) {
        super.onUpdateSuccess(entityList)
        try {
            for (building in entityList) {
                ObjInfoService.createInfo(building,false,"Bd","Building")
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    /**
     * @Description: 删除之后
     * @Param: entityList
     */
    override fun onDeleteSuccess(entityList: ArrayList<Building>) {
        super.onDeleteSuccess(entityList)
        try {
            val projectId = SPageContext.getHeader("ProjectId")
            for (building in entityList) {
                ObjInfoService.delete(SFilter.eq("projectId",projectId!!), SFilter.eq("objId",building.id!!), SFilter.eq("objType","Bd"))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


} // Object BuildingService
