package com.zhongzheng.modules.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.bo.*;
import com.zhongzheng.modules.course.domain.CourseModuleChapter;
import com.zhongzheng.modules.course.mapper.CourseChapterSectionMapper;
import com.zhongzheng.modules.course.mapper.CourseModuleChapterMapper;
import com.zhongzheng.modules.course.service.ICourseModuleChapterService;
import com.zhongzheng.modules.course.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模块与章关系Service业务层处理
 *
 * @author hjl
 * @date 2021-10-11
 */
@Service
public class CourseModuleChapterServiceImpl extends ServiceImpl<CourseModuleChapterMapper, CourseModuleChapter> implements ICourseModuleChapterService {

    @Autowired
    private CourseModuleChapterMapper courseModuleChapterMapper;

    @Override
    public CourseModuleChapterVo queryById(Long id){
        CourseModuleChapter db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, CourseModuleChapterVo.class);
    }

    @Override
    public List<CourseModuleChapterVo> getListById(Long id) {
        return courseModuleChapterMapper.getListById(id);
    }


    @Override
    public List<CourseModuleChapterVo> queryList(CourseModuleChapterQueryBo bo) {
        LambdaQueryWrapper<CourseModuleChapter> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getModuleId() != null, CourseModuleChapter::getModuleId, bo.getModuleId());
        lqw.eq(bo.getChapterId() != null, CourseModuleChapter::getChapterId, bo.getChapterId());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<CourseModuleChapterVo> entity2Vo(Collection<CourseModuleChapter> collection) {
        List<CourseModuleChapterVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CourseModuleChapterVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<CourseModuleChapter> page = (Page<CourseModuleChapter>)collection;
            Page<CourseModuleChapterVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(CourseModuleChapterAddBo bo) {
        CourseModuleChapter add = BeanUtil.toBean(bo, CourseModuleChapter.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(CourseModuleChapterEditBo bo) {
        CourseModuleChapter update = BeanUtil.toBean(bo, CourseModuleChapter.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CourseModuleChapter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<CourseUserModuleChapterVo> chapterList(CourseMenuQueryBo bo) {
        List<CourseUserModuleChapterVo> courseUserModuleChapterVos = baseMapper.chapterList(bo);
        if (bo.getRebuild() != null && bo.getRebuild() == 1){
            for (CourseUserModuleChapterVo courseUserModuleChapterVo : courseUserModuleChapterVos) {
                if (courseUserModuleChapterVo.getModuleId() == null){
                    bo.setModuleId(0L);
                }
                Integer rebuild = baseMapper.rebuildChapterCheck(courseUserModuleChapterVo.getModuleId(),courseUserModuleChapterVo.getChapterId(), courseUserModuleChapterVo.getGradeId(), bo.getUserId());
                if (rebuild > 0) {
                    courseUserModuleChapterVo.setRebuild(1);
                }
            }
            List<CourseUserModuleChapterVo> courseUserModuleChapterVoList = courseUserModuleChapterVos.stream().filter(s ->
                    s.getRebuild() != null && s.getRebuild().equals(1)
            ).collect(Collectors.toList());
            return courseUserModuleChapterVoList;
        }
        return courseUserModuleChapterVos;
    }
}
