/*
 * ********************************************************************************************************************
 *
 *                     :*$@@%$*:                         ;:                ;;    ;;
 *                   :@@%!  :!@@%:                       %!             ;%%@@%$ =@@@@@@@%;     @%@@@%%%%@@@@@
 *                  :@%;       :$=                       %%$$$%$$         ;$$  ;$@=   !@$
 *                  =@!                                  %!              @ $=;%   !@@@%:      !$$$$$$$$$$$$$$=
 *                  =@*                                  %!              @ $= % %@=   =%@!      %=
 *             *$%%! @@=        ;=$%%%$*:                %!              @ $= % =%%%%%%@$      *%:         =%
 *           %@@!:    !@@@%=$@@@@%!  :*@@$:              %!              @ $= % $*     ;@      @*          :%*
 *         ;@@!          ;!!!;:         ;@%:      =======@%========*     @ $$ % $%*****$@     :@$=*********=@$
 *         $@*   ;@@@%=!:                *@*
 *         =@$    ;;;!=%@@@@=!           =@!
 *          %@$:      =@%: :*@@@*       %@=                    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.database.SQueryBuilder
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.logs.DataCenterLogs
import com.persagy.server.datacenter.models.entities.objects.Equipment
import com.persagy.server.services.assistant.ObjInfoService
import com.persagy.server.services.base.Service
import com.persagy.service.SPageContext

/**
 * 部件信息服务
 *
 * @author 张维新
 */
object ComponentService : Service<Equipment>(SMybatisDao(Equipment::class.java)) {

    /**
     * 创建对象前回调函数
     *
     * @param   component      创建前的回调函数
     */
    override fun onCreateBefore(component: Equipment): Boolean  {
        val projectId = SPageContext.getHeader("ProjectId")
        component.projectId = projectId
        if (!component.id.isNullOrEmpty()) {
            val isStartsWith = component.id.toString().startsWith("Ec")
            if (!isStartsWith) {
                /** 部件id */
                component.id = IdUtils.equipmentComponentIdCreate(projectId!!)
            }
        } else {
            /** 部件id */
            component.id = IdUtils.equipmentComponentIdCreate(projectId!!)
        }
        /** 项目id */
        component.projectId = projectId
        /** 部件名字 */
        if (component.name.isNullOrEmpty()) {
            component.name = ShaftNameUtil.shaftName("部件-")
        }
        component.objectType = "Component"
        return true
    } // Function onCreateBefore()

    /**
     * 查询之前的操作
     *
     * @param   queryBuilder    查询条件
     */
    override fun onQueryBefore(queryBuilder: SQueryBuilder<Equipment>) {
        queryBuilder.tableName = "v_component"
       return
    } // Function onQueryBefore()

    /**
     * 查询统计之前操作
     *
     * @param   queryBuilder    查询条件
     */
    override fun onCountBefore(queryBuilder: SQueryBuilder<Equipment>) {
        queryBuilder.tableName = "v_component"
       return
    } // Function onCountBefore()

    override fun onDeleteBefore(cquipment: Equipment): Boolean {
        try {
            cquipment.objectType = "Component"
            val  entity = this.select(SFilter.eq("id", cquipment.id!!)).entity()

            val projectId = SPageContext.getHeader("ProjectId")
            val coming = SPageContext.getHeader("Coming")
            val account = SPageContext.getHeader("Account")
            /** 日志 */
            val dataCenterLogs = DataCenterLogs()
            dataCenterLogs.id = IdUtils.uuidCreate()
            dataCenterLogs.author = "dataCenter"
            dataCenterLogs.projectId = projectId
            dataCenterLogs.objectType = cquipment.objectType
            dataCenterLogs.type = "delete"
            dataCenterLogs.coming = coming
            dataCenterLogs.account = account
            dataCenterLogs.objectId = entity!!.id
            val hashMap = HashMap<String, Any?>()
            hashMap[cquipment.objectType!!] = entity
            dataCenterLogs.dataSource = hashMap
            logService.insert(dataCenterLogs)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return super.onDeleteBefore(cquipment)
    }

    override fun onUpdateBefore(entity: Equipment): Boolean {
        entity.objectType = "Component"
        return super.onUpdateBefore(entity)
    }

    /**
     * @Description:添加成功之后
     */
    override fun onCreateSuccess(entityList: ArrayList<Equipment>) {
        super.onCreateSuccess(entityList)
        for (equipment in entityList) {
            ObjInfoService.createInfo(equipment,true,"Ec",equipment.category!!)
        }
    }

    /**
     * @Description:更新成功后
     */
    override fun onUpdateSuccess(entityList: ArrayList<Equipment>) {
        super.onUpdateSuccess(entityList)
        for (equipment in entityList) {
            ObjInfoService.createInfo(equipment,false,"Ec",equipment.category!!)
        }
    }

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

} // Object ComponentService