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.CourseChapterSection;
import com.zhongzheng.modules.course.domain.CourseStreamingBusiness;
import com.zhongzheng.modules.course.mapper.CourseChapterMapper;
import com.zhongzheng.modules.course.mapper.CourseChapterSectionMapper;
import com.zhongzheng.modules.course.service.ICourseChapterSectionService;
import com.zhongzheng.modules.course.vo.CourseChapterSectionVo;
import com.zhongzheng.modules.course.vo.CourseUserChapterSectionVo;
import com.zhongzheng.modules.course.vo.CourseUserMenuVo;
import com.zhongzheng.modules.course.vo.CourseUserVo;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * 章与节关系Service业务层处理
 *
 * @author hjl
 * @date 2021-10-12
 */
@Service
public class CourseChapterSectionServiceImpl extends ServiceImpl<CourseChapterSectionMapper, CourseChapterSection> implements ICourseChapterSectionService {

    @Autowired
    private CourseChapterSectionMapper courseChapterSectionMapper;

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

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

    @Override
    public Long getListTotalTime(Long id) {
        return courseChapterSectionMapper.getListTotalTime(id);
    }

    @Override
    public List<CourseChapterSectionVo> queryList(CourseChapterSectionQueryBo bo) {
        LambdaQueryWrapper<CourseChapterSection> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSectionId() != null, CourseChapterSection::getSectionId, bo.getSectionId());
        lqw.eq(bo.getChapterId() != null, CourseChapterSection::getChapterId, bo.getChapterId());
        return entity2Vo(this.list(lqw));
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(CourseChapterSectionAddBo bo) {
        Boolean result = false;
        if(bo.getSectionIdList()!=null){
            this.remove(new LambdaQueryWrapper<CourseChapterSection>().eq(CourseChapterSection::getChapterId, bo.getChapterId()));
            Collection<CourseChapterSection> coll = new HashSet<>();
            for(int i=0;i<bo.getSectionIdList().size();i++){
                Long sectionId = bo.getSectionIdList().get(i).getSectionId();
                Long sort = bo.getSectionIdList().get(i).getSort();
                CourseChapterSection add = new CourseChapterSection();
                add.setChapterId(bo.getChapterId());
                add.setSectionId(sectionId);
                add.setSort(sort);
                coll.add(add);
            }
            this.saveBatch(coll);
        }

        return result;
    }

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

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

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

    @Override
    public List<CourseUserChapterSectionVo> sectionList(CourseMenuQueryBo bo) {
        List<CourseUserChapterSectionVo> courseUserChapterSectionVos = courseChapterSectionMapper.sectionList(bo);
        courseUserChapterSectionVos.addAll(courseChapterSectionMapper.examList(bo));
        if (bo.getUserId() != null) {
            for (CourseUserChapterSectionVo courseMenuVo : courseUserChapterSectionVos) {
                if (courseMenuVo.getType().equals(1)) {
                    Integer rebuild = baseMapper.rebuildCheck(courseMenuVo.getTypeId(), courseMenuVo.getGradeId(), bo.getUserId(), bo.getModuleId(), bo.getChapterId(), 0L);
                    if (rebuild > 0) {
                        courseMenuVo.setRebuild(1);
                    }
                } else {
                    Integer rebuild = baseMapper.rebuildCheck(0L, courseMenuVo.getGradeId(), bo.getUserId(), 0L, bo.getChapterId(), courseMenuVo.getTypeId());
                    if (rebuild > 0) {
                        courseMenuVo.setRebuild(1);
                    }
                }
            }
        }
        return courseUserChapterSectionVos;
    }
}
