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

import cn.sagacloud.server.algorithm.dao.mapper.FloorMapper
import cn.sagacloud.server.algorithm.models.modelFile.FloorFolderName
import cn.sagacloud.server.algorithm.models.modelFile.ModelFile
import cn.sagacloud.server.algorithm.models.modelFile.ModelFileFolder
import cn.sagacloud.server.algorithm.services.BaseDataService
import cn.sagacloud.server.algorithm.services.CommonService
import cn.sagacloud.server.algorithm.services.endpoint.ModelFileFolderService
import com.sybotan.database.SFilter
import com.sybotan.database.enums.SDirection
import com.sybotan.mybatis.SMybatisDao
import com.sybotan.service.SObjectService
import com.sybotan.service.SPageContext
import com.sybotan.service.models.enums.SResponseType
import com.sybotan.service.models.requests.SCountRequest
import com.sybotan.service.models.requests.SCreateRequest
import com.sybotan.service.models.requests.SQueryRequest
import com.sybotan.service.models.requests.SUpdateRequest
import com.sybotan.service.models.responses.SBaseResponse
import com.sybotan.service.models.responses.SCountResponse
import com.sybotan.service.models.responses.SCreateResponse
import com.sybotan.service.models.responses.SQueryResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
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 java.lang.StringBuilder

/**
 *
 *
 * @author jxing
 */
@Api(tags = ["模型文件夹接口"])
@RestController
@RequestMapping("/model-folder")
class ModelFileFolderController {
    companion object {
        // 日志
        private val logger = LoggerFactory.getLogger(ModelFileFolderController::class.java)
    } // Companion object

    val dataService = BaseDataService

    @Autowired
    lateinit var floorMapper: FloorMapper

    /**
     * 创建模型文件夹
     *
     * @param   entity     实体
     * @return  创建结果应答信息
     */
    @ApiOperation(value = "创建模型文件夹", notes = "")
    @PostMapping(value = ["/create"])
    fun create(@RequestBody entity: SCreateRequest<ModelFileFolder>): SCreateResponse<ModelFileFolder> {
        entity.content.stream().forEach { x ->
            x.id = CommonService.generateUUID("", true)
            x.projectId = SPageContext.getHeader("ProjectId")
        }
        return dataService.folderService.createList(entity)
    } // Function create()

    /**
     * 删除模型文件夹
     *
     * @param   idList      删除模型文件夹ID列表
     * @return  删除结果应答信息
     */
    @ApiOperation(value = "删除模型文件夹", notes = "")
    @PostMapping(value = ["/delete"])
    fun delete(@RequestBody idList: ArrayList<ModelFileFolder>): SBaseResponse {
        return ModelFileFolderService.deleteFolder(idList)
    } // Function delete()

    /**
     * 更新模型文件夹名字
     *
     * @param   request     更新请求
     * @return  更新结果应答信息
     */
    @ApiOperation(value = "更新模型文件夹名字", notes = "")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody request: SUpdateRequest<ModelFileFolder>): SBaseResponse {
        return dataService.folderService.updateList(request)
    } // Function update()

    /**
     * 查询模型文件夹
     *
     * @param   request     查询请求
     * @return  查询结果应答信息
     */
    @ApiOperation(value = "查询模型文件夹", notes = "")
    @PostMapping(value = ["/query"])
    fun query(@RequestBody request: SQueryRequest): SQueryResponse<ModelFileFolder> {
        var sb = StringBuilder()
        if(request.filters == null){
            sb.append("ProjectId = '${SPageContext.getHeader("ProjectId")}'")
        }else{
            sb.append(request.filters)
            if(!sb.trim().endsWith(";"))
                sb.append(" and ")
            sb.append("ProjectId = '${SPageContext.getHeader("ProjectId")}'")
        }
        request.filters = sb.toString()
        return dataService.folderService.pageQuery(request)
    } // Function query()

    /**
     * 根据条件查询统计数量
     */
    @ApiOperation(value = "根据条件查询统计数量", notes = "")
    @PostMapping(value = ["/count"])
    fun count(@RequestBody request: SCountRequest): SCountResponse {
        val sb = StringBuilder()
        if(request.filters == null){
            sb.append("ProjectId = '${SPageContext.getHeader("ProjectId")}'")
        }else{
            sb.append(request.filters)
            if(!sb.trim().endsWith(";"))
                sb.append(" and ")
            sb.append("ProjectId = '${SPageContext.getHeader("ProjectId")}'")
        }
        request.filters = sb.toString()
        return dataService.folderService.count(request)
    } // Function count()


    /**
     * 根据模型id获取模型所属楼层名, 所属文件夹名
     */
    @ApiOperation(value = "根据楼层id获取楼层名, 所属文件夹名", notes = "只填楼层Id")
    @PostMapping(value = ["/get-name"])
    fun getName(@RequestBody request: ModelFile): SBaseResponse {
        if(request.id.isNullOrBlank()){
            return SBaseResponse(SResponseType.failure, "Id不能为空")
        }
        val floorFolderName = floorMapper.getFloorFolderName(request.id!!)
                ?: return SBaseResponse(SResponseType.failure, "没有此模型")
        floorFolderName.result = SResponseType.success
        return floorFolderName
    } // Function getName()
}