package com.ruoyi.learn.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.learn.mapper.LearnQuestionsMapper;
import com.ruoyi.learn.domain.LearnQuestions;
import com.ruoyi.learn.service.ILearnQuestionsService;

/**
 * 试题Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-18
 */
@Service
public class LearnQuestionsServiceImpl implements ILearnQuestionsService
{
    @Autowired
    private LearnQuestionsMapper learnQuestionsMapper;

    /**
     * 查询试题
     *
     * @param id 试题主键
     * @return 试题
     */
    @Override
    public LearnQuestions selectLearnQuestionsById(Long id)
    {
        return learnQuestionsMapper.selectLearnQuestionsById(id);
    }

    /**
     * 查询试题列表
     *
     * @param learnQuestions 试题
     * @return 试题
     */
    @Override
    public List<LearnQuestions> selectLearnQuestionsList(LearnQuestions learnQuestions)
    {
        return learnQuestionsMapper.selectLearnQuestionsList(learnQuestions);
    }

    /**
     * 新增试题
     *
     * @param learnQuestions 试题
     * @return 结果
     */
    @Override
    public int insertLearnQuestions(LearnQuestions learnQuestions)
    {
        learnQuestions.setCreateTime(DateUtils.getNowDate());
        return learnQuestionsMapper.insertLearnQuestions(learnQuestions);
    }

    /**
     * 修改试题
     *
     * @param learnQuestions 试题
     * @return 结果
     */
    @Override
    public int updateLearnQuestions(LearnQuestions learnQuestions)
    {
        return learnQuestionsMapper.updateLearnQuestions(learnQuestions);
    }

    /**
     * 批量删除试题
     *
     * @param ids 需要删除的试题主键
     * @return 结果
     */
    @Override
    public int deleteLearnQuestionsByIds(Long[] ids)
    {
        return learnQuestionsMapper.deleteLearnQuestionsByIds(ids);
    }

    @Override
    public int updateQuestionType(String type, Collection<Long> ids)
    {
        return learnQuestionsMapper.updateQuestionType(type, ids);
    }


    /**
     * 删除试题信息
     *
     * @param id 试题主键
     * @return 结果
     */
    @Override
    public int deleteLearnQuestionsById(Long id)
    {
        return learnQuestionsMapper.deleteLearnQuestionsById(id);
    }

    @Override
    public List<LearnQuestions> selectCollectedList(LearnQuestions questions) {
        return learnQuestionsMapper.selectCollectedList(questions);
    }

    @Override
    public void fillCollectInfo(Long userId, List<LearnQuestions> questions) {
        if (null == userId || CollectionUtils.isEmpty(questions)) {
            return;
        }
        List<Long> ids = questions.stream().map(LearnQuestions::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(ids)){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("userId", userId);
            map.put("ids", ids);
            List<LearnQuestions> collectInfos = learnQuestionsMapper.selectCollectInfo(map);
            Set<Long> collected = collectInfos.stream().map(LearnQuestions::getId).collect(Collectors.toSet());
            questions.forEach(e -> {
                e.setCollect(collected.contains(e.getId()));
            });
        }
    }
}
