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

package com.persagy.server.controllers.objects

import com.persagy.server.datacenter.models.entities.dictdatacenter.DictDatacenterProjectcustom
import com.persagy.server.datacenter.models.entities.objects.Building
import com.persagy.server.services.objects.BuildingService
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.requests.SCountRequest
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.requests.SUpdateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCountResponse
import com.persagy.service.models.responses.SCreateResponse
import com.persagy.service.models.responses.SQueryResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.script.Invocable
import javax.script.ScriptEngineManager

/**
 * 建筑接口
 *
 * @author 张维新
 */
@Api(tags = ["002、建筑"],position = 2)
@RestController
@RequestMapping("/object/building")
open class BuildingController {
    companion object {
        // 日志
        private val logger = LoggerFactory.getLogger(BuildingController::class.java)
    } // Companion object

    /**
     * 批量创建建筑
     *
     * @param   request         建筑对象列表
     * @return                  创建结果信息
     */
    @ApiOperation(value = "创建建筑信息", notes = "")
    @PostMapping(value = ["/create"])
    fun create(@RequestBody request: SCreateRequest<Building>): SCreateResponse<Building> {
        logger.debug("building =${request.toJson()}")
        return BuildingService.createList(request)
    } // Function create()

    /**
     * 根据id删除建筑
     *
     * @param   idList      id数组
     * @return              删除的结果信息
     */
    @ApiOperation(value = "根据id删除建筑信息", notes = "")
    @PostMapping(value = ["/delete"])
    fun delete(@RequestBody idList: ArrayList<Building>): SBaseResponse {
        return BuildingService.deleteByKeysList(idList)
    } // Function delete()

    /**
     * 更新建筑信息
     *
     * @param   request     更新的内容对象
     * @return              更新的结果
     */
    @ApiOperation(value = "更新建筑信息", notes = "")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody request: SUpdateRequest<Building>): SBaseResponse {
        logger.debug("buildingUpdate =${request.toJson()}")
        return BuildingService.updateList(request)
    } // Function update()

    /**
     * 查询建筑信息
     *
     * @param   request     查询信息条件
     * @return              查询结果
     */
    @ApiOperation(value = "查询建筑信息", notes = "级联查询的对象：楼层（floor）、项目（project）")
    @PostMapping(value = ["/query"])
    fun query(@RequestBody request: SQueryRequest): SQueryResponse<Building> {
        logger.debug("buildingQuery =${request.toJson()}")
        return BuildingService.pageQuery(request, arrayListOf(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString())))
    } // Function query()

    /**
     * 根据条件查询统计数量
     */
    @ApiOperation(value = "根据条件查询统计数量", notes = "")
    @PostMapping(value = ["/count"])
    fun count(@RequestBody request: SCountRequest): SCountResponse {
        val withFilterList = ArrayList<SFilter>()
        withFilterList.add(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString()))

        /** 服务 */
        val dictRentalservice = SObjectService(SMybatisDao(DictDatacenterProjectcustom::class.java))
        try {
            val exec = dictRentalservice.selectAll().exec()

            System.out.println("打印测试=${exec.toJson()}")

            val manager = ScriptEngineManager()
            val engine = manager.getEngineByName("javascript")
            try {
                engine.eval("function transform(variable){\n" +
                        "const  obj = JSON.parse(variable)"+
                        "if(variable == '4'){\n" +
                        "    return true;\n" +
                        "}else {\n" +
                        "    return false\n" +
                        "  }\n" +
                        "}")
                if (engine is Invocable) {
                    val `in` = engine as Invocable
                    if (`in`.invokeFunction("transform", "20") as Boolean){
                        System.out.println("有")
                    }else{
                        System.out.println("没有")
                    }

                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return BuildingService.count(request,withFilterList)
    } // Function count()

} // Class BuildingController