package com.ruoyi.learn.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dz.domain.DzSubject;
import com.ruoyi.dz.mapper.DzSubjectMapper;
import com.ruoyi.enums.QuestionType;
import com.ruoyi.learn.domain.LearnQuestions;
import com.ruoyi.learn.mapper.LearnKnowledgeTreeMapper;
import com.ruoyi.learn.mapper.LearnQuestionsMapper;
import com.ruoyi.learn.service.ILearnQuestionsService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.learn.mapper.LearnWrongBookMapper;
import com.ruoyi.learn.domain.LearnWrongBook;
import com.ruoyi.learn.service.ILearnWrongBookService;

/**
 * 学生错题本Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-25
 */
@Service
public class LearnWrongBookServiceImpl implements ILearnWrongBookService
{
    @Autowired
    private LearnWrongBookMapper learnWrongBookMapper;
    @Autowired
    private DzSubjectMapper subjectMapper;
    @Autowired
    private LearnKnowledgeTreeMapper knowledgeTreeMapper;
    @Autowired
    private ILearnQuestionsService questionsService;

    /**
     * 查询学生错题本
     *
     * @param wrongId 学生错题本主键
     * @return 学生错题本
     */
    @Override
    public LearnWrongBook selectLearnWrongBookByWrongId(Long wrongId)
    {
        return learnWrongBookMapper.selectLearnWrongBookByWrongId(wrongId);
    }

    /**
     * 查询学生错题本列表
     *
     * @param learnWrongBook 学生错题本
     * @return 学生错题本
     */
    @Override
    public List<LearnWrongBook> selectLearnWrongBookList(LearnWrongBook learnWrongBook)
    {
        return learnWrongBookMapper.selectLearnWrongBookList(learnWrongBook);
    }

    /**
     * 新增学生错题本
     *
     * @param learnWrongBook 学生错题本
     * @return 结果
     */
    @Override
    public int insertLearnWrongBook(LearnWrongBook learnWrongBook)
    {
        return learnWrongBookMapper.insertLearnWrongBook(learnWrongBook);
    }

    /**
     * 修改学生错题本
     *
     * @param learnWrongBook 学生错题本
     * @return 结果
     */
    @Override
    public int updateLearnWrongBook(LearnWrongBook learnWrongBook)
    {
        return learnWrongBookMapper.updateLearnWrongBook(learnWrongBook);
    }

    /**
     * 批量删除学生错题本
     *
     * @param wrongIds 需要删除的学生错题本主键
     * @return 结果
     */
    @Override
    public int deleteLearnWrongBookByWrongIds(Long[] wrongIds)
    {
        return learnWrongBookMapper.deleteLearnWrongBookByWrongIds(wrongIds);
    }

    /**
     * 删除学生错题本信息
     *
     * @param wrongId 学生错题本主键
     * @return 结果
     */
    @Override
    public int deleteLearnWrongBookByWrongId(Long wrongId)
    {
        return learnWrongBookMapper.deleteLearnWrongBookByWrongId(wrongId);
    }

    public List<DzSubject> findSubject() {
        return subjectMapper.selectWrongSubjectList(SecurityUtils.getUserId());
    }

    public List<LearnWrongBook> findWrongQuestions(Long subjectId, Date start, Date end) {
        Map cond = Maps.newHashMap();
        cond.put("subjectId", subjectId);
        cond.put("studentId", SecurityUtils.getUserId());
        cond.put("start", start);
        cond.put("end", end);
        List<LearnWrongBook> wrongBookList = learnWrongBookMapper.findWrongQuestions(cond);
        if(wrongBookList.size() > 0) {
            Map<Long, LearnQuestions> qMap = questionsService.selectLearnQuestionsByIds(wrongBookList.stream().map(LearnWrongBook::getQuestionId).toArray(Long[]::new)).stream().collect(Collectors.toMap(LearnQuestions::getId, t -> t));
            wrongBookList.stream().forEach(t -> {
                LearnQuestions q = qMap.get(t.getQuestionId());
                t.setTitle(q.getTitle());
                t.setParse(q.getParse());
                t.setType(q.getQtpye());
                t.setTypeId(QuestionType.of(q.getQtpye()).getVal());
                t.setAnswer1(q.getAnswer1());
                t.setAnswer2(q.getAnswer2());
                t.setAnswers(StringUtils.isNotBlank(t.getAnswer()) ? Arrays.asList(t.getAnswer().split(",")) : Lists.newArrayList());
                t.setOptions(StringUtils.getOptions(q.getOptionA(), q.getOptionB(), q.getOptionC(), q.getOptionD(), q.getOptionE(), q.getOptionF(), q.getOptionG()));
            });
        }
        return wrongBookList;
    }

    @Override
    public List<LearnWrongBook> findWrongExaminees(Long wrongId) {
        return learnWrongBookMapper.findWrongExaminees(wrongId);
    }

    @Override
    public int deleteByQuestion(Long studentId, Long questionId) {
        LearnWrongBook b = new LearnWrongBook();
        b.setStudentId(studentId);
        b.setQuestionId(questionId);
        return learnWrongBookMapper.deleteByQuestion(b);
    }


}
