package com.ssj.service.kmt.service;


import com.ssj.bean.kmt.kmt.domain.KmtExerciseBook;
import com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion;
import com.ssj.framework.core.common.service.BaseService;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.conch.parents.v2.dto.TrainRecommendQuestionDTO;
import com.ssj.service.kmt.dto.ExerciseBookCourseClassDto;
import com.ssj.service.kmt.dto.ExerciseQuestionAnswer4CorrectDTO;
import com.ssj.service.kmt.dto.ExerciseQuestionDto;
import com.ssj.service.kmt.dto.ExerciseQuetionDto;
import com.ssj.service.kmt.dto.exercise.ExerciseQuestionRollbacks;
import com.ssj.service.kmt.dto.exercise.QuestionAttributeFilterDto;
import com.ssj.service.kmt.exercisesys.request.ExerciseTeacherClassRequest;
import com.ssj.service.kmt.request.AnswerSearchRequest;
import com.ssj.service.kmt.request.AnswerSearchV1Request;
import com.ssj.service.kmt.request.AnswerSearchV2Request;
import com.ssj.service.kmt.request.ExerciseQuestionsRequest;

import java.util.List;
import java.util.Map;

public interface KmtExerciseQuestionService extends BaseService<KmtExerciseQuestion, String> {

    /**
     * 根据课时id查询题目
     * @param periodId 课时id
     */
    List<KmtExerciseQuestion> findByBookCourseId(String periodId);

    /**
     * 根据单元id查询题目
     * @param unitId 单元id
     */
    List<KmtExerciseQuestion> findByUnitId(String unitId);

    /**
     * 根据练习册id查询题目
     * @param bookId 练习册id
     */
    List<KmtExerciseQuestion> findByBookId(String bookId);

    /**
     * 查询练习册题目
     * @param userId 操作人id
     * @param request 请求实体
     * @return
     */
    public ResponseEntity findExerciseQuestions(String userId, ExerciseQuestionsRequest request);

    /**
     * 题目整成二级结构，添加知识点
     * @param list
     * @return
     */
    List<ExerciseBookCourseClassDto> getQuestionGroupList(List<ExerciseQuetionDto> list, boolean hasBigQuestionIndex);

    /**
     * 查询简化的分类
     * @param list
     * @param hasBigQuestionIndex
     * @return
     */
    List<ExerciseBookCourseClassDto> getSimplifyQuestionGroupList (List<ExerciseQuetionDto> list, boolean hasBigQuestionIndex);

    /**
     * kmt题目整成二级结构，添加知识点
     * @param list
     * @return
     */
    List<ExerciseBookCourseClassDto> getKmtQualityQuestionGroupList (List<ExerciseQuetionDto> list);

    /**
     * 查询角色名称
     * @param type
     * @return
     */
    String getAuthorityByType(int type);

    /**
     * 查询课时下的所有题目, 返回给前端
     * @param courseId
     * @return
     */
    public List<ExerciseBookCourseClassDto> findQusetionsByCourseId(String courseId);

    /**
     * 查询课时内所有题目的答案
     */
    Map<String, List<ExerciseQuestionDto>> findByExerciseBookCourseId(AnswerSearchRequest request);

    /**
     * 查询课时内所有题目的答案
     * @return
     */
    List<ExerciseQuestionAnswer4CorrectDTO> findByExerciseBookCourseIdV2(AnswerSearchRequest request);

    /**
     * 查询课时内所有题目的答案
     * @param request
     * @return
     */
    List<ExerciseQuestionAnswer4CorrectDTO> findByExerciseBookCourseIdV3(AnswerSearchV1Request request);

    /**
     * 查询课时内所有题目的答案
     * @param request
     * @return
     */
    Map<String, Object> findByExerciseBookCourseIdV4(AnswerSearchV2Request request);

    /**
     * 查询课时内所有已发布题目的答案
     * @param request
     * @return
     */
    List<ExerciseQuestionAnswer4CorrectDTO> findByExerciseBookPeriodId(AnswerSearchV2Request request);

    /**
     * 根据题目id集合查询题目
     */
    List<KmtExerciseQuestion> findByIdIn(List<String> wrongQuestionIdList);

    /**
     * 根据题目的id查询所关联的练习册的版本
     */
    Integer findKnowledgeVersionById(String questionId);

    /**
     * 根据大题id查询题目及大题本身
     * @param parentQuestionIdList 大题id
     * @return
     */
    List<String> findListAndParentByParentId(List<String> parentQuestionIdList);


    List<ExerciseQuestionDto> findQuestionByBookIdAndPage(String bookId, String unitId, String bookCourseId,
                                                          List<Integer> pageIndex, int bookYear);

    List<ExerciseQuestionDto> findQuestionByBookIdAndPageByAll(String bookId, List<Integer> pageIndex);


    List<ExerciseQuestionDto> findAnswerByExerciseBookCourseId(String exerciseBookCourseId, int bookYear);


    /**
     * 判断查询条件的练习册数据是否都已发布， 只要有一道题目未发布，则返回true
     * @param data
     * @return
     */
    boolean findIsNotPublish(AnswerSearchV2Request data);

    /**
     * 查询题目的打回记录
     * @param questionId
     * @return
     */
    List<ExerciseQuestionRollbacks> findQuestionRollbackRecord(String questionId);

    /**
     * 查询题目的属性筛选项
     * @return
     */
    List<QuestionAttributeFilterDto> findQuestionAttributeFilter();


    List<ExerciseQuestionDto> findQuestionByQuestionIds(List<String> questionIds);


    void refreshAllToEs(String date);

    boolean saveToEs(KmtExerciseQuestion kmtExerciseQuestion);

    /**
     * 批量保存kmt题目
     * @param kmtExerciseQuestions
     */
    void bulkKmtExerciseQuestions(List<KmtExerciseQuestion> kmtExerciseQuestions);

    /**
     * 保存练习册题目
     * @param kmtExerciseBookIds
     */
    void bulkBookExerciseQuestions(List<String> kmtExerciseBookIds);

    /**
     * 更新题目es, 传入练习册对象
     * @param
     */
    void updateEsQuestionHandler(List<String> kmtExerciseBookIds, String teacherId, int type);

    /**
     * 批量修改加工审核人
     * @param kmtExerciseBookIds
     * @param teachers
     */
    void updateEsQuestionHandlers(List<String> kmtExerciseBookIds, List<ExerciseTeacherClassRequest> teachers);

    /**
     * 保存es实时更新
     * @param kmtExerciseQuestion
     */
    void save2EsImmediate(KmtExerciseQuestion kmtExerciseQuestion);

    /**
     * 查询单题的es记录
     * @param id
     * @return
     */
    KmtExerciseQuestion getEsById(String id);

    /**
     * 查询所有小题
     * @param parentId 大题id
     * @param quantity
     * @return 小题列表
     */
    List<TrainRecommendQuestionDTO> findChildTrainRecommendQuestionDTO(String parentId, int quantity);

    void updateAnswerCorrecterIdByExerciseBookId(String teacherId, String exerciseBookId);

    void updateAnswerCorrecterIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    /**
     * 更新提交加工人
     * @param teacherId
     * @param exerciseBookIds
     */
    void updateHandleUserIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    /**
     * 更新题目审核人
     * @param teacherId
     * @param exerciseBookIds
     */
    void updateAuditingUserIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    /**
     * 更新解析加工人
     * @param teacherId
     * @param exerciseBookIds
     */
    void updateAnalysisUserIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);

    /**
     * 更新解析审核人
     * @param teacherId
     * @param exerciseBookIds
     */
    void updateAnalysisAuditerIdByExerciseBookIds(String teacherId, List<String> exerciseBookIds);


    /**
     * 保存题目更新至es(包括kmt_exercise_questions、kmt_exercise_question_know索引)
     * 如果失败，最多尝试5次
     *
     * @param kmtExerciseQuestionList 练习册题目
     */
    void save2EsBatch(List<KmtExerciseQuestion> kmtExerciseQuestionList);
}
