package com.persagy.revit.controllers.modelFile

import com.persagy.revit.models.config.MyProperties
import com.persagy.revit.services.endpoint.ModelFileService
import com.persagy.revit.services.endpoint.UploadService
import com.persagy.revit.services.json.JSONAnalyzerService
import com.persagy.revit.services.monitor.ModelMonitorService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import io.swagger.v3.oas.annotations.tags.Tag
import io.swagger.v3.oas.annotations.Operation
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import java.util.*


/**
 *
 *
 * @author jxing
 */
@Tag(name = "上传json-zip接口")
@RestController
@RequestMapping("/upload-json-zip")
class UploadController {

    companion object {
        // 日志
        private val logger = LoggerFactory.getLogger(UploadController::class.java)
    } // Companion object

    @Autowired
    lateinit var modelFileService: ModelFileService

    @Autowired
    lateinit var myProperties : MyProperties

    @Autowired
    lateinit var jsonAnalyzerService: JSONAnalyzerService

    @Autowired
    lateinit var monitorService: ModelMonitorService

    /**
     * 上传Zip文件, 只保存到文件服务器
     *
     * @param   file     文件
     * @return  创建结果应答信息
     */
    @PostMapping("/upload")
    @ResponseBody
    @Operation(summary = "上传Zip文件", description =  "")
    fun upload(@RequestParam("file") file: MultipartFile): SBaseResponse {
        val beginTime = Date()
        var modelId : String? = null
        var success = false
        try {
            if(file.isEmpty|| file.bytes.size.toLong() <= 0L){
                return SBaseResponse(SResponseType.failure, "empty file")
            }
            val map = UploadService.getJSONObjectMap(file.inputStream)
            val iterator = map.iterator()
            if(iterator.hasNext()) {
                modelId = jsonAnalyzerService.getModelIdInJSON(iterator.next().value)
                val ret = modelFileService.uploadFile(file, myProperties.fileService.exportPath + modelId + ".zip")
                if(ret){
                    success = true
                    return SBaseResponse(SResponseType.success)
                }else{
                    return SBaseResponse(SResponseType.failure, "上传到文件服务器出错!")
                }
            }else{
                return SBaseResponse(SResponseType.failure, "上传的文件出错!")
            }
        } catch (e: Exception) {
            return SBaseResponse(SResponseType.failure,"error:" + e.message)
        }finally {
            if(success) {
                monitorService.exportJsonz(modelId!!, beginTime, file.bytes.size.toLong())
            }
        }
    }

//    /**
//     * 上传Zip文件, 不保存, 直接解析入库
//     *
//     * @param   file     文件
//     * @return  创建结果应答信息
//     */
//    @PostMapping("/upload_old")
//    @ResponseBody
//    @Operation(summary = "上传Zip文件", description =  "")
//    fun upload_old(@RequestParam("file") file: MultipartFile): SBaseResponse {
//        try {
//            val map = UploadService.getJSONObjectMap(file.inputStream)
//            val iterator = map.iterator()
//            if(iterator.hasNext()) {
//                val ret = jsonAnalyzerService.handleSingleJSON(iterator.next().value)
//                if(ret == null)
//                    return SBaseResponse(SResponseType.success, "")
//                else
//                    return SBaseResponse(SResponseType.failure, ret)
//            }else{
//                return SBaseResponse(SResponseType.failure, "上传文件出错!")
//            }
//        } catch (e: Exception) {
//            return SBaseResponse(SResponseType.failure,"error:" + e.message)
//        }
//    }
}