package com.persagy.revit.backstage.state

import com.persagy.database.SFilter
import com.persagy.revit.models.config.MyProperties
import com.persagy.revit.models.modelFile.ModelFile
import com.persagy.revit.services.BaseDataService
import com.persagy.revit.services.endpoint.ModelFileService
import com.persagy.revit.services.modelCheck.TaskService
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
class ExportErrorState_21 : ModelState {

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

    val dataService = BaseDataService

    @Autowired
    lateinit var modelFileService: ModelFileService

    @Autowired
    lateinit var taskService : TaskService

    @Autowired
    lateinit var properties : MyProperties

    var modelList : ArrayList<ModelFile>? = null

    /**
     * 准备数据
     */
    override fun prepareModel() {
        modelList = dataService.modelFileService.select(SFilter.eq("status", 21), SFilter.eq("removed", false)).exec()
    }

    /**
     * 做删除回退版本操作
     */
    override fun handle() {
        try {
            if (modelList.isNullOrEmpty()) {
                return
            }
            modelList!!.forEach {
                // 删除模型文件
                modelFileService.removeFileFromFileServer(it)
                // 删除记录并将楼层的模型文件退回上个版本
                modelFileService.removeAndGoBack(it, properties, true, isMarkDelete = true)
            }
        }catch (e : Exception){
            e.printStackTrace()
            logger.error(e.message)
        }
    }
}