package com.zd.exam.service.impl;

import java.util.List;

import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.exam.domain.ElCourse;
import com.zd.exam.domain.ElCourseChapterQu;
import com.zd.exam.mapper.ElCourseMapper;
import com.zd.exam.service.IElCourseChapterQuService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.exam.mapper.ElCourseChapterMapper;
import com.zd.exam.domain.ElCourseChapter;
import com.zd.exam.service.IElCourseChapterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学习资源章节Service业务层处理
 *
 * @author liubo
 * @date 2021-12-07
 */
@Service
public class ElCourseChapterServiceImpl implements IElCourseChapterService {

    @Autowired
    private ElCourseMapper elCourseMapper;
    @Autowired
    private ElCourseChapterMapper elCourseChapterMapper;
    @Autowired
    private IElCourseChapterQuService courseChapterQuService;

    /**
     * 查询学习资源章节
     *
     * @param id 学习资源章节主键
     * @return 学习资源章节
     */
    @Override
    public ElCourseChapter selectElCourseChapterById(Long id) {
        return elCourseChapterMapper.selectElCourseChapterById(id);
    }

    /**
     * 查询学习资源章节列表
     *
     * @param elCourseChapter 学习资源章节
     * @return 学习资源章节
     */
    @Override
    public List<ElCourseChapter> selectElCourseChapterList(ElCourseChapter elCourseChapter) {
        return elCourseChapterMapper.selectElCourseChapterList(elCourseChapter);
    }

    /**
     * 新增学习资源章节
     *
     * @param elCourseChapter 学习资源章节
     * @return 结果
     */
    @Override
    @Transactional
    public ElCourseChapter insertElCourseChapter(ElCourseChapter elCourseChapter) {
        ElCourse course = elCourseMapper.selectElCourseById(elCourseChapter.getCourseId());
        if(course == null || !SecurityUtils.getUserId().equals(course.getUserId())){
            throw new ServiceException("操作失败，不是自己创建的课程不能修改！");
        }

        //设置其他公共字段
        SaveUtil.setCommonAttr(elCourseChapter);

        elCourseChapter.setIsAssess(elCourseChapter.getIsAssess() == null ? 0 : elCourseChapter.getIsAssess());

        int num = elCourseChapterMapper.insertElCourseChapter(elCourseChapter);
        if(num == 0){
            throw new ServiceException("新增失败，请重试！");
        }
        if(elCourseChapter.getIsAssess() == 1){
            checkAssessDetail(elCourseChapter.getId(), elCourseChapter.getChapterQuList());
        }
        return elCourseChapter;
    }

    /**
     * 修改学习资源章节
     *
     * @param elCourseChapter 学习资源章节
     * @return 结果
     */
    @Override
    @Transactional
    public int updateElCourseChapter(ElCourseChapter elCourseChapter) {

        elCourseChapter.setIsAssess(elCourseChapter.getIsAssess() == null ? 0 : elCourseChapter.getIsAssess());

        elCourseChapter.setUpdateTime(DateUtils.getNowDate());
        elCourseChapter.setUpdateBy(SecurityUtils.getUsername());
        int num = elCourseChapterMapper.updateElCourseChapter(elCourseChapter);
        if(num == 0){
            throw new ServiceException("修改失败！ID无效");
        }
        if(elCourseChapter.getIsAssess() == 1){
            checkAssessDetail(elCourseChapter.getId(), elCourseChapter.getChapterQuList());
        }
        return num;
    }

    /**
     * 如果添加了考核，进行数据验证，并保存
     * @param chapterId 章节ID
     * @param chapterQuList 选题集合 有效数据 quId
     */
    private void checkAssessDetail(long chapterId, List<ElCourseChapterQu> chapterQuList) {
        if(CollectionUtils.isEmpty(chapterQuList)){
            throw new ServiceException("考核题目内容不能为空！");
        }
        courseChapterQuService.insertListByChapterId(chapterId, chapterQuList);
    }

    /**
     * 批量删除学习资源章节
     *
     * @param ids 需要删除的学习资源章节主键
     * @return 结果
     */
    @Override
    public int deleteElCourseChapterByIds(Long[] ids) {
        ElCourseChapter elCourseChapter = new ElCourseChapter();
        List<ElCourseChapter> list;
        for (Long id : ids) {
            elCourseChapter.setParentId(id);
            list = elCourseChapterMapper.selectElCourseChapterList(elCourseChapter);
            if(CollectionUtils.isNotEmpty(list)){
                throw new ServiceException("该章节已有子章节，删除失败！");
            }
        }
        return elCourseChapterMapper.deleteElCourseChapterByIds(ids);
    }

    /**
     * 删除学习资源章节信息
     *
     * @param id 学习资源章节主键
     * @return 结果
     */
    @Override
    public int deleteElCourseChapterById(Long id) {
        return elCourseChapterMapper.deleteElCourseChapterById(id);
    }
}
