package com.persagy.revit.controllers.modelFile

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializeConfig
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.revit.models.modelFile.FloorModelView
import com.persagy.revit.models.modelFile.ModelFileFolder
import com.persagy.revit.models.responses.DataCheckListResponse
import com.persagy.revit.services.BaseDataService
import com.persagy.revit.services.comparator.FloorComparator
import com.persagy.revit.services.endpoint.ReportService
import com.persagy.revit.services.excel.ExcelService
import com.persagy.revit.services.modelCheck.TaskService
import com.persagy.service.SBaseService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SQueryResponse
import com.persagy.service.utils.SSpringContextUtil
import io.swagger.v3.oas.annotations.tags.Tag
import io.swagger.v3.oas.annotations.Operation
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.net.URLEncoder
import javax.servlet.http.HttpServletResponse

/**
 *
 *
 * @author jxing
 */
@Tag(name = "报告接口")
@RestController
@RequestMapping("/report")
class ReportController {

    val dataService = BaseDataService

    @Autowired
    lateinit var floorComparator: FloorComparator

    @Autowired
    lateinit var taskService: TaskService

    @Autowired
    lateinit var reportService : ReportService

    /**
     * 根据模型id获取模型所属楼层名, 所属文件夹名
     */
    @Operation(summary = "非阻塞性报告列表", description =  "填文件夹Id和项目id")
    @PostMapping(value = ["/list/non-blocking"])
    fun getNonBlockingReportList(@RequestBody(required = true) request: ModelFileFolder) : SBaseResponse{
        val errorResponse = SBaseResponse(SResponseType.failure)
        val sQueryResponse = SQueryResponse<DataCheckListResponse>()
        try {
            if(request.id.isNullOrBlank() || request.projectId.isNullOrBlank()){
                errorResponse.message = "Id或ProjectId不能为空"
                return errorResponse
            }
//            val list = floorMapper.getFinishedFloor(request.projectId!!, request.id!!)
            val service = SBaseService(SMybatisDao(FloorModelView::class.java))
            val select = service.select(SFilter.eq("folderId", request.id!!), SFilter.Companion.eq("projectId", request.projectId!!), SFilter.Companion.eq("status", 4))
            select.tableName = "revit.model_floor_file_func('${SPageContext.getHeader("ProjectId")}')"
            var list = select.exec()
            if(list.isEmpty()){
                //errorResponse.message = "暂时没有可用楼层"
                return sQueryResponse
            }
            list = ArrayList(list.sortedWith(floorComparator))
            val reportMap = taskService.getAllDataCheckResult(list)
            val content = ArrayList<DataCheckListResponse>()
            sQueryResponse.content = content
            reportMap.forEach {
                val dataCheckListResponse = DataCheckListResponse()
                dataCheckListResponse.floorName = it.key
                reportService.doNonBlockingStatistics(it.value, dataCheckListResponse)
                content.add(dataCheckListResponse)
            }
            return sQueryResponse
        }catch (e : Exception){
            e.printStackTrace()
            return errorResponse
        }
    }

    /**
     * 根据模型id获取模型所属楼层名, 所属文件夹名
     */
    @Operation(summary = "下载非阻塞性报告", description =  "填文件夹Id和项目id")
    @PostMapping(value = ["/non-blocking"])
    fun getNonBlockingReport(response : HttpServletResponse, @RequestBody(required = true) request: ModelFileFolder){
        val errorResponse = SBaseResponse(SResponseType.failure)
        try {
            if(request.id.isNullOrBlank() || request.projectId.isNullOrBlank()){
                errorResponse.message = "Id或ProjectId不能为空"
                response.contentType = "application/json;charset=utf-8"
                response.flushBuffer()
                response.outputStream.write(JSON.toJSONString(errorResponse, SSpringContextUtil.getBean("jsonMessageConverterConfig") as SerializeConfig).toByteArray())
                return
            }
//            val list = floorMapper.getFinishedFloor(request.projectId!!, request.id!!)
            val service = SBaseService(SMybatisDao(FloorModelView::class.java))
            val select = service.select(SFilter.eq("folderId", request.id!!), SFilter.Companion.eq("projectId", request.projectId!!), SFilter.Companion.eq("status", 4))
            select.tableName = "revit.model_floor_file_func('${SPageContext.getHeader("ProjectId")}')"
            var list = select.exec()
            if(list.isEmpty()){
                errorResponse.message = "没有可以下载的报告"
                response.contentType = "application/json;charset=utf-8"
                response.flushBuffer()
                response.outputStream.write(JSON.toJSONString(errorResponse, SSpringContextUtil.getBean("jsonMessageConverterConfig") as SerializeConfig).toByteArray())
                return
            }
            val folder = dataService.folderService.select(SFilter.Companion.eq("id", request.id!!)).entity()
            list = ArrayList(list.sortedWith(floorComparator))
            val reportMap = taskService.getAllDataCheckResult(list)
            val excelService = SSpringContextUtil.getBean(ExcelService::class.java) as ExcelService
            excelService.readNonBlockingExcel2007()
            reportService.writeNonBlockingReportToExcel(list, reportMap, excelService)
            response.contentType = "application/vnd.ms-excel;charset=utf-8"
            var str = (folder?.name + "检查报告.xlsx")
            response.setHeader("Content-disposition",
                    "attachment;filename=${URLEncoder.encode(str, "UTF-8")}")
            excelService.workBook!!.write(response.outputStream)
            excelService.workBook!!.close()
            response.flushBuffer()
        }catch (e : Exception){
            errorResponse.message = "异常: ${e.message}"
            response.contentType = "application/json;charset=utf-8"
            response.flushBuffer()
            response.outputStream.write(JSON.toJSONString(errorResponse, SSpringContextUtil.getBean("jsonMessageConverterConfig") as SerializeConfig).toByteArray())
            e.printStackTrace()
        }finally {
            response.outputStream.flush()
            response.outputStream.close()
        }
    }
}