package com.ssj.service.sys.push.service.task;

import com.alibaba.fastjson.JSONObject;
import com.ssj.service.kmt.exercisesys.request.ExerciseTeacherClassRequest;
import com.ssj.service.kmt.service.KmtExerciseQuestionService;
import com.ssj.service.task.SyncRefreshExerciseBookStatusTask;
import com.ssj.taskthread.Task;
import com.ssj.taskthread.ThreadPool;

import java.util.List;

/**
 * 更新练习册题目校对老师数据
 *
 * @author Mount Liu
 * @date 2020/5/12
 */
public class UpdateExerciseQuestionThreadTask extends Task {
    /**
     * 练习册id
     */
    private List<String> exerciseBookIds;

    private KmtExerciseQuestionService kmtExerciseQuestionService;

    /**
     * 分配老师列表, 更新老师类型，1题目加工，2题目审核，3解析加工，4， 解析审核, 8校对
     */
    private List<ExerciseTeacherClassRequest> teachers;

    public UpdateExerciseQuestionThreadTask(List<ExerciseTeacherClassRequest> teachers, List<String> exerciseBookIds, KmtExerciseQuestionService kmtExerciseQuestionService) {
        super();
        this.exerciseBookIds = exerciseBookIds;
        this.kmtExerciseQuestionService = kmtExerciseQuestionService;
        this.teachers = teachers;
    }

    @Override
    public void run() {
        logger.info("开始分配练习册题目给老师teacherId=[{}], exerciseBookIds=[{}]", JSONObject.toJSONString(teachers), exerciseBookIds);
        try {
//            switch (type){
//                case 1: kmtExerciseQuestionService.updateHandleUserIdByExerciseBookIds(teacherId, exerciseBookIds);break;
//                case 2: kmtExerciseQuestionService.updateAuditingUserIdByExerciseBookIds(teacherId, exerciseBookIds);break;
//                case 3: kmtExerciseQuestionService.updateAnalysisUserIdByExerciseBookIds(teacherId, exerciseBookIds);break;
//                case 4: kmtExerciseQuestionService.updateAnalysisAuditerIdByExerciseBookIds(teacherId, exerciseBookIds);break;
//                case 8: kmtExerciseQuestionService.updateAnswerCorrecterIdByExerciseBookIds(teacherId, exerciseBookIds); break;
//                default: break;
//            }
            kmtExerciseQuestionService.updateEsQuestionHandlers(exerciseBookIds, teachers);

        } catch (Exception e) {
            logger.error("teacherId=[{}], exerciseBookIds=[{}]", JSONObject.toJSONString(teachers), exerciseBookIds);
            logger.error("分配练习册题目给老师异常", e);
        }

        logger.info("[{}]结束分配练习册题目给老师(id=[{}])", Thread.currentThread().getName(), JSONObject.toJSONString(teachers));

        boolean dealTeacherUpdate = false;
        for (ExerciseTeacherClassRequest request : teachers){
            if (request.getType() >= 1 && request.getType() <= 4){
                dealTeacherUpdate = true;
                break;
            }
        }
        //只要有加工审核老师的分配， 则更新练习册数据
        if (dealTeacherUpdate){
            ThreadPool.getInstance().addTask(new SyncRefreshExerciseBookStatusTask(exerciseBookIds, 1));
        }
    }
}
