package com.qmth.distributed.print.business.templete.execute;

import com.qmth.distributed.print.business.entity.GradeBatch;
import com.qmth.distributed.print.business.entity.GradeBatchPaper;
import com.qmth.distributed.print.business.entity.TBSyncTask;
import com.qmth.distributed.print.business.service.DataSyncReportService;
import com.qmth.distributed.print.business.service.GradeBatchService;
import com.qmth.distributed.print.business.service.TBSyncTaskService;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.PushTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 异步同步教研分析
 */
@Service
public class AsyncTeachCloudReportService {

    private static final Logger log = LoggerFactory.getLogger(AsyncTeachCloudReportService.class);

    @Lazy
    @Resource
    private DataSyncReportService dataSyncReportService;

    @Resource
    private TBSyncTaskService tbSyncTaskService;

    @Resource
    private GradeBatchService gradeBatchService;

    /**
     * 同步分析批次
     *
     * @param gradeBatch 分析批次对象
     */
    public void syncGradeBatch(GradeBatch gradeBatch) {
        TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(gradeBatch.getSchoolId(), gradeBatch.getId(), PushTypeEnum.GRADE_BATCH_PUSH);
        dataSyncReportService.syncGradeBatch(gradeBatch, tbSyncTask);
    }

    /**
     * 开始计算
     * @param gradeBatch 分析批次对象
     */
    public void startCalc(GradeBatch gradeBatch) {
        TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(gradeBatch.getSchoolId(), gradeBatch.getId(), PushTypeEnum.CALCULATE);
        dataSyncReportService.startCalc(gradeBatch, tbSyncTask);
    }

    public void publish(GradeBatchPaper gradeBatchPaper) {
        GradeBatch gradeBatch = gradeBatchService.getById(gradeBatchPaper.getBatchId());
        if(gradeBatch == null){
            throw ExceptionResultEnum.ERROR.exception("没有批次信息");
        }
        TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(gradeBatchPaper.getSchoolId(), gradeBatchPaper.getId(), PushTypeEnum.PUBLISH);
        dataSyncReportService.publish(gradeBatch, gradeBatchPaper, tbSyncTask);
    }
}
