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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qmth.distributed.print.business.bean.dto.SyncExamTaskDto;
import com.qmth.distributed.print.business.bean.marking.GroupInfo;
import com.qmth.distributed.print.business.entity.ExamPaperStructure;
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.contant.SystemConstant;
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.enums.SyncModeEnum;
import com.qmth.teachcloud.common.service.CommonCacheService;
import com.qmth.teachcloud.common.service.SysConfigService;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.apache.commons.lang3.StringUtils;
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.List;

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

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

    @Lazy
    @Resource
    private DataSyncService dataSyncService;

    @Resource
    private TBSyncTaskService tbSyncTaskService;

    /**
     * 同步考试、考生、科目、题卡
     *
     * @param schoolId      学校ID
     * @param objectId      同步对象ID（命题任务Id）
     * @param thirdRelateId 云阅卷考试ID
     */
    public void syncExamAndStudentAndCard(Long schoolId, Long objectId, Long thirdRelateId, String thirdRelateName, String examTime, List<SyncExamTaskDto> params) {
        TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(schoolId, objectId, PushTypeEnum.EXAM_PUSH, JSONObject.toJSONString(params));
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        dataSyncService.syncExamAndStudentAndCard(thirdRelateId, thirdRelateName, examTime, tbSyncTask, sysUser);
    }

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

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

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

    /**
     * 绑定科组长
     *
     * @param examPaperStructure 试卷结构对象
     */
    public void syncMarkLeader(ExamPaperStructure examPaperStructure) {
        if (examPaperStructure == null) {
            throw ExceptionResultEnum.ERROR.exception("请先设置评卷参数");
        }
        TBSyncTask tbSyncTask = tbSyncTaskService.saveTask(examPaperStructure.getSchoolId(), examPaperStructure.getId(), PushTypeEnum.MARK_LEADER_PUSH, null);
        dataSyncService.syncMarkLeader(examPaperStructure, tbSyncTask);
    }

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