package com.persagy.revit.controllers


import com.persagy.revit.models.excel.IsPassedEnum
import com.persagy.revit.models.excel.PreciseCellValue
import com.persagy.revit.models.excel.nonBlockingReport.EquipNameCodeReport
import com.persagy.revit.services.BaseDataService
import com.persagy.revit.services.excel.ExcelService
import com.persagy.service.utils.SSpringContextUtil
import io.swagger.v3.oas.annotations.tags.Tag
import io.swagger.v3.oas.annotations.Operation
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.servlet.http.HttpServletResponse



/**
 *
 *
 * @author jxing
 */
@Tag(name = "楼层模型接口")
@RestController
@RequestMapping("/log")
class LogController {
    companion object {
        // 日志
        private val logger = LoggerFactory.getLogger(LogController::class.java)
    } // Companion object

    val dataService = BaseDataService


    /**
    * 获取检查建筑文件夹下的楼层的日志(excel)
    *
    * @param   response     返回结果 excel
    * @param   buildingCheckRequest     请求建筑文件夹id
    * @return  创建结果应答信息
    */
    @Operation(summary = "检查建筑文件夹下的楼层", description =  "")
    @PostMapping(value = ["/building-check/excel"])
//    fun buildingCheckExcel(response : HttpServletResponse, @RequestBody(required = true) buildingCheckRequest: BuildingCheckRequest) : SBaseResponse{
    fun buildingCheckExcel(response : HttpServletResponse, @RequestBody(required = true) buildingCheckRequest: PreciseCellValue){
        try {
//            val excelService = SSpringContextUtil.getBean("excelService") as ExcelService
            val excelService = SSpringContextUtil.getBean(ExcelService::class.java) as ExcelService
            excelService.readBlockingExcel2007()
            var report = EquipNameCodeReport()
            for(i in 0..99 step 1) {
                var row = EquipNameCodeReport.Data()
                row.floorName = "F${i}"
                row.isPassed = IsPassedEnum.Pass
                row.familyName = "Name $i"
                row.note = "aaa"
                report.rowValue.add(row)
            }
            excelService.insertReport(report)

            response.contentType = "application/vnd.ms-excel;charset=utf-8"
            response.setHeader("Content-disposition", "attachment;filename=aaa.xlsx")
            val outputStream = response.outputStream
            excelService.workBook!!.write(response.outputStream)
            excelService.workBook!!.close()
            response.flushBuffer()
            outputStream.flush()
            outputStream.close()
        } catch (e : Exception) {
            println(e.toString())
            response.contentType = "application/json;charset=utf-8"
//            response.setHeader("Content-disposition", null)
            response.flushBuffer()
//            excelService.randomWrite(0, buildingCheckRequest)
            val outputStream = response.outputStream
            outputStream.write("{\"a\":\"fff\"}".toByteArray())
            outputStream.flush()
            outputStream.close()
        }
    } // Function buildingCheckExcel()
}