package com.ruoyi.ems.task.taskiml;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.common.core.enums.ResultStatus;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.ems.domain.AdmOpInspectionReport;
import com.ruoyi.ems.service.impl.AdmOpInspectionReportServiceImpl;
import com.ruoyi.ems.service.impl.EmsFacsServiceImpl;
import com.ruoyi.ems.task.BaseTask;
import com.ruoyi.quartz.domain.SysJob;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("facInspectionTask")
public class FacTask extends BaseTask {

    @Autowired
    private EmsFacsServiceImpl emsFacsService;

    @Autowired
    private AdmOpInspectionReportServiceImpl admOpInspectionReportService;

    @Override
    public void run(SysJob taskProperties) {
        List<String> errorReport = emsFacsService.inspectFacIndex();
        AdmOpInspectionReport facInspectionTask = new AdmOpInspectionReport() {
            {
                setTaskCode(taskProperties.getJobGroup() + "." + taskProperties.getJobId());
                setResultStatus(ResultStatus.NORMAL.getCode());
                setSubTime(DateUtils.getNowDate());
                setFinishTime(DateUtils.getNowDate());
                setSubmitter("facInspectionTask");
            }
        };
        if (!errorReport.isEmpty()) {
            log.error("facs index error report: {}", errorReport);
            facInspectionTask.setResultStatus(ResultStatus.ABNORMAL.getCode());
            facInspectionTask.setSubTime(DateUtils.getNowDate());
            facInspectionTask.setFinishTime(DateUtils.getNowDate());
            facInspectionTask.setResultMsg(StringUtils.join(errorReport, "\\r\\n"));
        }
        admOpInspectionReportService.insertAdmOpInspectionReport(facInspectionTask);
    }
}
