package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbCourseChapter;
import com.ruoyi.hezhi.mapper.TbCourseChapterMapper;
import com.ruoyi.hezhi.service.ITbCourseChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程章节Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@Service
public class TbCourseChapterServiceImpl implements ITbCourseChapterService
{
    @Autowired
    private TbCourseChapterMapper tbCourseChapterMapper;

    /**
     * 查询课程章节
     *
     * @param chapterId 课程章节主键
     * @return 课程章节
     */
    @Override
    public TbCourseChapter selectTbCourseChapterByChapterId(Long chapterId)
    {
        return tbCourseChapterMapper.selectTbCourseChapterByChapterId(chapterId);
    }

    /**
     * 查询课程章节列表
     *
     * @param tbCourseChapter 课程章节
     * @return 课程章节
     */
    @Override
    public List<TbCourseChapter> selectTbCourseChapterList(TbCourseChapter tbCourseChapter)
    {
        return tbCourseChapterMapper.selectTbCourseChapterList(tbCourseChapter);
    }

    /**
     * 新增课程章节
     *
     * @param tbCourseChapter 课程章节
     * @return 结果
     */
    @Override
    public int insertTbCourseChapter(TbCourseChapter tbCourseChapter)
    {
        tbCourseChapter.setCreateTime(DateUtils.getNowDate());
        return tbCourseChapterMapper.insertTbCourseChapter(tbCourseChapter);
    }

    /**
     * 修改课程章节
     *
     * @param tbCourseChapter 课程章节
     * @return 结果
     */
    @Override
    public int updateTbCourseChapter(TbCourseChapter tbCourseChapter)
    {
        tbCourseChapter.setUpdateTime(DateUtils.getNowDate());
        return tbCourseChapterMapper.updateTbCourseChapter(tbCourseChapter);
    }

    /**
     * 批量删除课程章节
     *
     * @param chapterIds 需要删除的课程章节主键
     * @return 结果
     */
    @Override
    public int deleteTbCourseChapterByChapterIds(Long[] chapterIds)
    {
        return tbCourseChapterMapper.deleteTbCourseChapterByChapterIds(chapterIds);
    }

    /**
     * 删除课程章节信息
     *
     * @param chapterId 课程章节主键
     * @return 结果
     */
    @Override
    public int deleteTbCourseChapterByChapterId(Long chapterId)
    {
        return tbCourseChapterMapper.deleteTbCourseChapterByChapterId(chapterId);
    }

    /**
     * 查询课程章节树列表
     *
     * @param courseId 课程ID
     * @return 课程章节集合
     */
    @Override
    public List<TbCourseChapter> getCourseChapterTreeList(Long courseId) {
        TbCourseChapter tbCourseChapter = new TbCourseChapter();
        tbCourseChapter.setCourseId(courseId);
        tbCourseChapter.setStatus(0);
        tbCourseChapter.setDelFlag(0);
        List<TbCourseChapter> list = tbCourseChapterMapper.selectTbCourseChapterList(tbCourseChapter);
        return this.toTree(list,0L);
    }

    /**
     * 方式一：使用两层循环实现
     * @param treeList
     * @return
     */
    @Override
    public List<TbCourseChapter> toTree(List<TbCourseChapter> treeList, Long pid) {
        List<TbCourseChapter> retList = new ArrayList<TbCourseChapter>();
        for (TbCourseChapter parent : treeList) {
            if (pid.equals(parent.getPid())) {
                retList.add(parent);
            }
            for (TbCourseChapter child : treeList) {
                if (child.getPid().equals(parent.getChapterId())) {
                    if (parent.getChildrens() == null) {
                        parent.setChildrens(new ArrayList<TbCourseChapter>());
                    }
                    parent.getChildrens().add(child);
                }
            }
        }
        return retList;
    }
}
