package com.ruoyi.learn.service.impl;

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

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.learn.mapper.LearnPlanStudyMapper;
import com.ruoyi.learn.domain.LearnPlanStudy;
import com.ruoyi.learn.service.ILearnPlanStudyService;

/**
 * 学生计划执行情况Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-25
 */
@Service
public class LearnPlanStudyServiceImpl implements ILearnPlanStudyService 
{
    @Autowired
    private LearnPlanStudyMapper learnPlanStudyMapper;

    public List<LearnPlanStudy> selectPlanStudyStats(LearnPlanStudy learnPlanStudy) {
        Map<Date, LearnPlanStudy> questionMap = learnPlanStudyMapper.selectQuestionStats(learnPlanStudy).stream().collect(Collectors.toMap(LearnPlanStudy::getReportDate, t -> t));
        Map<Date, LearnPlanStudy> videoMap = learnPlanStudyMapper.selectVideoStats(learnPlanStudy).stream().collect(Collectors.toMap(LearnPlanStudy::getReportDate, t -> t));

        List<LearnPlanStudy> studyList = learnPlanStudyMapper.selectLearnPlanStudyListByMonth(learnPlanStudy);
        for(LearnPlanStudy study : studyList) {
            LearnPlanStudy question = questionMap.get(study.getReportDate());
            LearnPlanStudy video = videoMap.get(study.getReportDate());
            if(null != question) {
                study.setQuestionCount(question.getQuestionCount());
                study.setRightCount(question.getRightCount());
            }
            if(null != video) {
                study.setVideoCount(video.getVideoCount());
                study.setVideoTime(video.getVideoTime());
            }
        }
        return studyList;
    }

    /**
     * 查询学生计划执行情况
     * 
     * @param id 学生计划执行情况主键
     * @return 学生计划执行情况
     */
    @Override
    public LearnPlanStudy selectLearnPlanStudyById(String id)
    {
        return learnPlanStudyMapper.selectLearnPlanStudyById(id);
    }

    /**
     * 查询学生计划执行情况列表
     * 
     * @param learnPlanStudy 学生计划执行情况
     * @return 学生计划执行情况
     */
    @Override
    public List<LearnPlanStudy> selectLearnPlanStudyList(LearnPlanStudy learnPlanStudy)
    {
        return learnPlanStudyMapper.selectLearnPlanStudyList(learnPlanStudy);
    }

    /**
     * 新增学生计划执行情况
     * 
     * @param learnPlanStudy 学生计划执行情况
     * @return 结果
     */
    @Override
    public int insertLearnPlanStudy(LearnPlanStudy learnPlanStudy)
    {
        return learnPlanStudyMapper.insertLearnPlanStudy(learnPlanStudy);
    }

    /**
     * 修改学生计划执行情况
     * 
     * @param learnPlanStudy 学生计划执行情况
     * @return 结果
     */
    @Override
    public int updateLearnPlanStudy(LearnPlanStudy learnPlanStudy)
    {
        learnPlanStudy.setUpdateTime(DateUtils.getNowDate());
        return learnPlanStudyMapper.updateLearnPlanStudy(learnPlanStudy);
    }

    /**
     * 批量删除学生计划执行情况
     * 
     * @param ids 需要删除的学生计划执行情况主键
     * @return 结果
     */
    @Override
    public int deleteLearnPlanStudyByIds(String[] ids)
    {
        return learnPlanStudyMapper.deleteLearnPlanStudyByIds(ids);
    }

    /**
     * 删除学生计划执行情况信息
     * 
     * @param id 学生计划执行情况主键
     * @return 结果
     */
    @Override
    public int deleteLearnPlanStudyById(String id)
    {
        return learnPlanStudyMapper.deleteLearnPlanStudyById(id);
    }
}
