package com.ruoyi.learn.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.learn.domain.LearnPlanStudy;
import com.ruoyi.learn.mapper.LearnPlanStudyMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.learn.mapper.LearnPlanMapper;
import com.ruoyi.learn.domain.LearnPlan;
import com.ruoyi.learn.service.ILearnPlanService;

/**
 * 学习计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-25
 */
@Service
public class LearnPlanServiceImpl implements ILearnPlanService 
{
    @Autowired
    private LearnPlanMapper learnPlanMapper;
    @Autowired
    private LearnPlanStudyMapper learnPlanStudyMapper;

    public void updateLearnPlan(Long studentId) {
        LearnPlan curr = getCurrLearnPlan();
        if(null == curr) {
            return;
        }
        LearnPlanStudy cond = new LearnPlanStudy();
        cond.setStudentId(studentId);
        cond.setPlanId(curr.getId());
        cond.setReportDate(DateUtils.truncate(new Date(), Calendar.DATE));
        List<LearnPlanStudy> learnPlanStudyList = learnPlanStudyMapper.selectLearnPlanStudyList(cond);
        if(CollectionUtils.isEmpty(learnPlanStudyList)) {
            cond.setQuestionPlan(curr.getQuestionCnt());
            cond.setVideoPlan(curr.getVideoTime());
            learnPlanStudyMapper.insertLearnPlanStudy(cond);
        }

    }

    public LearnPlan getCurrLearnPlan() {
        LearnPlan cond = new LearnPlan();
        cond.setStudentId(SecurityUtils.getLoginUser().getUserId());
        cond.setStatus(1);
        List<LearnPlan> planList = learnPlanMapper.selectLearnPlanList(cond);
        if(CollectionUtils.isNotEmpty(planList)) {
            return planList.get(0);
        }
        return null;
    }

    /**
     * 查询学习计划
     * 
     * @param id 学习计划主键
     * @return 学习计划
     */
    @Override
    public LearnPlan selectLearnPlanById(String id)
    {
        return learnPlanMapper.selectLearnPlanById(id);
    }

    /**
     * 查询学习计划列表
     * 
     * @param learnPlan 学习计划
     * @return 学习计划
     */
    @Override
    public List<LearnPlan> selectLearnPlanList(LearnPlan learnPlan)
    {
        return learnPlanMapper.selectLearnPlanList(learnPlan);
    }

    /**
     * 新增学习计划
     * 
     * @param learnPlan 学习计划
     * @return 结果
     */
    @Override
    public int insertLearnPlan(LearnPlan learnPlan)
    {
        learnPlan.setCreateTime(DateUtils.getNowDate());
        return learnPlanMapper.insertLearnPlan(learnPlan);
    }

    /**
     * 修改学习计划
     * 
     * @param learnPlan 学习计划
     * @return 结果
     */
    @Override
    public int updateLearnPlan(LearnPlan learnPlan)
    {
        return learnPlanMapper.updateLearnPlan(learnPlan);
    }

    /**
     * 批量删除学习计划
     * 
     * @param ids 需要删除的学习计划主键
     * @return 结果
     */
    @Override
    public int deleteLearnPlanByIds(String[] ids)
    {
        return learnPlanMapper.deleteLearnPlanByIds(ids);
    }

    /**
     * 删除学习计划信息
     * 
     * @param id 学习计划主键
     * @return 结果
     */
    @Override
    public int deleteLearnPlanById(String id)
    {
        return learnPlanMapper.deleteLearnPlanById(id);
    }
}
