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

import cn.sagacloud.server.algorithm.models.scheduler.items.UnitCheckItem
import cn.sagacloud.server.algorithm.models.excel.IsPassedEnum
import cn.sagacloud.server.algorithm.models.excel.report.ProjectLengthUnitReport
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 UnitCheckResult {

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

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

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

    companion object {
        @JSONField(serialize = false, deserialize = false)
        var sheetCheckName = "项目长度单位检查"
    }
    fun writeReport(excelService: ExcelService, floorName: String?, totalReport: BlockingTotalReport) : Boolean {
        report = SSpringContextUtil.getBean(ProjectLengthUnitReport::class.java) as ProjectLengthUnitReport
        if(content == null || content!!.isEmpty()) {
            // 异常处理
            totalReport.writeToRow(sheetCheckName, IsPassedEnum.Unpass)
            return false
        }
        val item = content!![0]
        val row = ProjectLengthUnitReport.Data()
        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
    }
}