package cn.sagacloud.server.algorithm.models.scheduler

import cn.sagacloud.server.algorithm.models.scheduler.items.SagaCheckItem
import cn.sagacloud.server.algorithm.models.excel.IsPassedEnum
import cn.sagacloud.server.algorithm.models.excel.report.PlanNamingReport
import cn.sagacloud.server.algorithm.models.excel.report.BlockingTotalReport
import cn.sagacloud.server.algorithm.services.excel.ExcelService
import com.alibaba.fastjson.annotation.JSONField
import com.sybotan.service.utils.SSpringContextUtil
import org.springframework.stereotype.Component

/**
 *
 *
 * @author jxing
 */
@Component
class SagaCheckResult {

    @JSONField(name = "Name")
    var name : String? = null

    @JSONField(name = "Content")
    var content : ArrayList<SagaCheckItem>? = null

    @JSONField(serialize = false, deserialize = false)
    var report: PlanNamingReport? = null //SSpringContextUtil.getBean(PlanNamingReport::class.java) as PlanNamingReport

    companion object {
        @JSONField(serialize = false, deserialize = false)
        var sheetCheckName = "楼层平面图命名规范检查"
    }
    /**
     * 将当前的检查结果写入excel里
     */
    fun writeReport(excelService: ExcelService, floorName: String?, totalReport: BlockingTotalReport) : Boolean {
        report = SSpringContextUtil.getBean(PlanNamingReport::class.java) as PlanNamingReport
        if(content == null || content!!.isEmpty()) {
            // 异常处理
            totalReport.writeToRow(sheetCheckName, IsPassedEnum.Unpass)
            return false
        }
        val item = content!![0]
        val row = PlanNamingReport.Data()
        row.planName = item.planName?:""
        row.note = item.resultMsg ?: ""
        row.floorName = floorName?: ""
        var isPassed = false
        if("success".equals(item.result, true)){
            isPassed = true
        }
        if(isPassed)
            row.isPassed = IsPassedEnum.Pass
        else
            row.isPassed = IsPassedEnum.Unpass
        report!!.rowValue.add(row)
        excelService.insertReport(report!!)
        totalReport.writeToRow(sheetCheckName, row.isPassed)
        return isPassed
    }
}