package com.ssj.service.kmt.kmt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.kmt.kmt.domain.KmtExerciseQuestion;
import com.ssj.bean.kmt.kmt.domain.KmtHomeworkWrongQuestion;
import com.ssj.bean.sys.homework.domain.Homework;
import com.ssj.dao.kmt.kmt.dao.KmtCorrectHomeworkQueryDao;
import com.ssj.dao.kmt.kmt.dao.KmtHomeworkWrongQuestionDao;
import com.ssj.es.service.ExerciseQuestionSearchService;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.kmt.dto.OriginalQuestionDTO;
import com.ssj.service.kmt.service.KmtExerciseService;
import com.ssj.service.kmt.service.KmtHomeworkWrongQuestionService;
import com.ssj.service.mapper.HomeworkMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author yrf
 * @date 2019/1/21 17:19
 */
@Transactional
@Service(version ="1.0")
public class KmtHomeworkWrongQuestionServiceImpl extends BaseServiceImpl<KmtHomeworkWrongQuestion, String> implements KmtHomeworkWrongQuestionService {

    @Autowired
    private KmtHomeworkWrongQuestionDao kmtHomeworkWrongQuestionDao;

    @Autowired
    private KmtCorrectHomeworkQueryDao kmtCorrectHomeworkQueryDao;
    @Autowired
    private KmtExerciseService kmtExerciseService;
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private ExerciseQuestionSearchService exerciseQuestionSearchService;

    @Override
    public PagingAndSortingRepository<KmtHomeworkWrongQuestion, String> getDao() {
        return kmtHomeworkWrongQuestionDao;
    }

    @Override
    public List<KmtHomeworkWrongQuestion> findList(String homeworkPictureId, boolean isDelete) {
        return kmtHomeworkWrongQuestionDao.findByHomeworkPictureIdAndIsDelete(homeworkPictureId, isDelete==false?0:1);
    }

    @Override
    public List<KmtHomeworkWrongQuestion> findByHomeworkId(String homeworkId) {
        return kmtHomeworkWrongQuestionDao.findByHomeworkIdAndIsDelete(homeworkId, false);
    }


    @Override
    public void deleteByHomeworkPictureId(String homeworkPictureId) {
        kmtHomeworkWrongQuestionDao.deleteByHomeworkPictureId(homeworkPictureId);
    }

    @Override
    public Page<Map<String, Object>> findWrongQuestionByPage(Map<String, Object> searchParams, Pageable pageable) {
        return kmtCorrectHomeworkQueryDao.findWrongQuestionByPage(searchParams, pageable);
    }

    @Override
    public Page<Map<String, Object>> findWrongQuestionNewByPage(Map<String, Object> searchParams, Pageable pageable) {
        return kmtCorrectHomeworkQueryDao.findWrongQuestionNewByPage(searchParams, pageable);
    }

    @Override
    public KmtHomeworkWrongQuestion createKmtHomeworkWrongQuestion(Homework homework, Date now, String questionId, Integer wrongType) {
        KmtHomeworkWrongQuestion kmtHomeworkWrongQuestion = new KmtHomeworkWrongQuestion();
        kmtHomeworkWrongQuestion.setHomeworkId(homework.getId());
        kmtHomeworkWrongQuestion.setQuestionId(questionId);
        kmtHomeworkWrongQuestion.setStudentId(homework.getVipId());
        kmtHomeworkWrongQuestion.setType(wrongType);
        kmtHomeworkWrongQuestion.setIsDelete(false);
        kmtHomeworkWrongQuestion.setCreateTime(now);
        return kmtHomeworkWrongQuestion;
    }

    @Override
    public List<KmtHomeworkWrongQuestion> findByHomeworkPictureIdIn(List<String> homeworkPictureIdList) {
        return kmtHomeworkWrongQuestionDao.findByHomeworkPictureIdInAndIsDelete(homeworkPictureIdList, false);
    }

    @Override
    public void deleteWrongQuesByQuestionId(String questionId) {
        kmtHomeworkWrongQuestionDao.deleteWrongQuesByQuestionId(questionId);
    }

    @Override
    public List<Map<String, Object>> findQuestionList(List<String> homeworkPictureIdList) {
        List<Map<String, Object>> wrongQuestionIdHomeworkPictureIdMapList = homeworkMapper.findWrongQuestionIdHomeworkPictureId(homeworkPictureIdList);
        if (CollectionUtils.isEmpty(wrongQuestionIdHomeworkPictureIdMapList)) {
            return Collections.emptyList();
        }
        Map<String, KmtExerciseQuestion> kmtExerciseQuestionMap = new HashMap<>(wrongQuestionIdHomeworkPictureIdMapList.size());
        List<KmtExerciseQuestion> kmtExerciseQuestionList;
        for (Map<String, Object> wrongQuestionIdHomeworkPictureIdMap : wrongQuestionIdHomeworkPictureIdMapList) {
            String questionId = wrongQuestionIdHomeworkPictureIdMap.get("question_id").toString();
            KmtExerciseQuestion kmtExerciseQuestion = kmtExerciseQuestionMap.get(questionId);
            if (kmtExerciseQuestion == null) {
                kmtExerciseQuestionList = exerciseQuestionSearchService.getById(questionId, KmtExerciseQuestion.class);
                if (CollectionUtils.isNotEmpty(kmtExerciseQuestionList)) {
                    kmtExerciseQuestion = kmtExerciseQuestionList.get(0);
                    kmtExerciseQuestionMap.put(kmtExerciseQuestion.getId(), kmtExerciseQuestion);
                }
            }
            if (kmtExerciseQuestion == null) {
                continue;
            }
            wrongQuestionIdHomeworkPictureIdMap.put("question_path", kmtExerciseQuestion.getQuestionPath());
            wrongQuestionIdHomeworkPictureIdMap.put("parent_id", kmtExerciseQuestion.getParentId());
        }

        kmtExerciseService.appendQuestionMapFromJSONFile(wrongQuestionIdHomeworkPictureIdMapList);
        return wrongQuestionIdHomeworkPictureIdMapList;
    }

    @Override
    public List<OriginalQuestionDTO> findQuestionContainParentAndBrotherList(List<String> homeworkPictureIdList) {
        return kmtCorrectHomeworkQueryDao.findQuestionContainParentAndBrotherList(homeworkPictureIdList);
    }

    @Override
    public List<Map<String, Object>> findFullBigQuestion(String questionId) {
        return kmtCorrectHomeworkQueryDao.findFullBigQuestion(questionId);
    }
}
