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

import com.qmth.distributed.print.business.entity.ExamPaperStructure;
import com.qmth.distributed.print.business.entity.ExamPrintPlan;
import com.qmth.distributed.print.business.entity.TBSyncTask;
import com.qmth.distributed.print.business.service.DataSyncService;
import com.qmth.distributed.print.business.service.TBSyncTaskService;
import com.qmth.teachcloud.common.entity.SysConfig;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.PushTypeEnum;
import com.qmth.teachcloud.common.service.SysConfigService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 异步同步云阅卷
 */
@Service
public class AsyncCloudMarkingTaskService {

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

    @Resource
    private SysConfigService sysConfigService;

    @Lazy
    @Resource
    private DataSyncService dataSyncService;

    @Resource
    private TBSyncTaskService tbSyncTaskService;

    private boolean isSync() {
        SysConfig sysConfig = sysConfigService.getByKey("sys.sync.enable");
        return sysConfig != null && "true".equals(sysConfig.getConfigValue());
    }

    /**
     * 同步考试、考生、科目、题卡
     *
     * @param examPrintPlan 印刷计划
     * @param thirdRelateId 云阅卷考试ID
     */
    public void syncExamAndStudentAndCard(ExamPrintPlan examPrintPlan, Long thirdRelateId, String thirdRelateName) {
        if (isSync()) {
            TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(examPrintPlan.getSchoolId(), examPrintPlan.getId(), PushTypeEnum.EXAM_PUSH);

            Long relateId = Objects.isNull(thirdRelateId) ? examPrintPlan.getThirdRelateId() : thirdRelateId;
            thirdRelateName = Objects.isNull(examPrintPlan.getThirdRelateId()) ? examPrintPlan.getName() : examPrintPlan.getThirdRelateName();
            SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
            dataSyncService.syncExamAndStudentAndCard(relateId, thirdRelateName, examPrintPlan.getExamEndTime(), tbSyncTask ,sysUser);
        }
    }

    /**
     * 同步试卷结构、分组信息、评卷员绑定关系
     *
     * @param examPaperStructure 试卷结构对象
     */
    public void syncPaperStructureAndGroup(ExamPaperStructure examPaperStructure) {
        if (examPaperStructure == null) {
            throw ExceptionResultEnum.ERROR.exception("未找到同步数据");
        }
        if (isSync()) {
            TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(examPaperStructure.getSchoolId(), examPaperStructure.getId(), PushTypeEnum.STRUCTURE_GROUP_PUSH);
            dataSyncService.syncPaperStructureAndGroup(examPaperStructure, tbSyncTask);
        }
    }

    /**
     * 同步试卷文件、标签文件
     *
     * @param examPaperStructure 试卷结构对象
     */
    public void syncPaperAndAnswer(ExamPaperStructure examPaperStructure) {
        if (examPaperStructure == null) {
            throw ExceptionResultEnum.ERROR.exception("请先设置评卷参数");
        }
        if (isSync()) {
            TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(examPaperStructure.getSchoolId(), examPaperStructure.getId(), PushTypeEnum.PAPER_ANSWER_FILE_PUSH);
            dataSyncService.syncPaperAndAnswer(examPaperStructure, tbSyncTask);
        }
    }

    /**
     * 同步客观题答案
     *
     * @param examPaperStructure 试卷结构对象
     */
    public void syncObjectiveStructure(ExamPaperStructure examPaperStructure) {
        if (examPaperStructure == null) {
            throw ExceptionResultEnum.ERROR.exception("请先设置评卷参数");
        }
        if (isSync()) {
            TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(examPaperStructure.getSchoolId(), examPaperStructure.getId(), PushTypeEnum.OBJECTIVE_ANSWER_PUSH);
            dataSyncService.syncObjectiveStructure(examPaperStructure, tbSyncTask);
        }
    }
}
