package cn.sagacloud.server.algorithm.backstage.state

import cn.sagacloud.server.algorithm.services.modelCheck.TaskService
import cn.sagacloud.server.algorithm.models.config.MyProperties
import cn.sagacloud.server.algorithm.models.modelFile.ModelFile
import cn.sagacloud.server.algorithm.services.BaseDataService
import cn.sagacloud.server.algorithm.services.endpoint.ModelFileService
import com.sybotan.database.SFilter
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
/**
 *
 *
 * @author jxing
 */
@Component
class CheckErrorState_11 : ModelState {
    companion object {
        // 日志
        private val logger = LoggerFactory.getLogger(CheckErrorState_11::class.java)
    } // Companion object

    val dataService = BaseDataService

    @Autowired
    lateinit var taskService : TaskService

    @Autowired
    lateinit var properties : MyProperties

    @Autowired
    lateinit var modelFileService: ModelFileService

    var modelList : ArrayList<ModelFile>? = null

    override fun prepareModel() {
        modelList = dataService.modelFileService.select(SFilter.eq("status", 11), 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)
        }
    }
}

