package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.SubjectDao;
import com.yonge.cooleshow.biz.dal.dao.VideoLessonAuthRecordDao;
import com.yonge.cooleshow.biz.dal.dao.VideoLessonGroupDetailDao;
import com.yonge.cooleshow.biz.dal.dto.search.VideoGroupSearch;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonGroupSearch;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonSearch;
import com.yonge.cooleshow.biz.dal.entity.*;
import com.yonge.cooleshow.biz.dal.dao.VideoLessonGroupDao;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupService;
import com.yonge.cooleshow.biz.dal.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * VideoLessonGroupService服务实现类
 *
 * @author yzp
 * @version v1.0
 * @date 2022-03-26 00:01:38
 **/
@Service
public class VideoLessonGroupServiceImpl extends ServiceImpl<VideoLessonGroupDao, VideoLessonGroup> implements VideoLessonGroupService {

    private final static Logger log = LoggerFactory.getLogger(VideoLessonGroupServiceImpl.class);

    @Autowired
    private VideoLessonGroupDao videoLessonGroupDao;
    @Autowired
    private VideoLessonGroupDetailDao videoLessonGroupDetailDao;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private VideoLessonAuthRecordDao recordDao;

    public VideoLessonGroupDao getDao() {
        return videoLessonGroupDao;
    }

    /**
     * @Description: 查询视频课组
     * @Author: cy
     * @Date: 2022/3/31
     */
    @Override
    public IPage<VideoLessonGroupVo> selectPage(IPage<VideoLessonGroupVo> page, VideoLessonGroupSearch query) {
        return page.setRecords(baseMapper.selectPage(page, query));
    }

    /**
     * @Description: 根据组id查详情
     * @Author: cy
     * @Date: 2022/4/11
     */
    public VideoLessonGroup selectGroup(Long groupId) {
        VideoLessonGroup lessonGroup = videoLessonGroupDao.selectById(groupId);
        Subject subject = subjectDao.selectSubjectById(lessonGroup.getLessonSubject());
        lessonGroup.setLessonSubjectName(subject.getName());
        return lessonGroup;
    }

    /**
     * @Description: 修改视频课组
     * @Author: cy
     * @Date: 2022/3/31
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGroup(VideoLessonGroupUpVo lessonGroupUpVo) {
        videoLessonGroupDao.updateGroup(lessonGroupUpVo);
    }

    /**
     * @Description: 审核-修改视频课组审核状态
     * @Author: cy
     * @Date: 2022/4/24
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateGroup(VideoLessonExamineVo examineVo) {
        //改主表
        videoLessonGroupDao.updateExamine(examineVo);

        //插从表
        VideoLessonAuthRecord record = new VideoLessonAuthRecord();
        record.setAuditStatus(examineVo.getAuditStatus());
        record.setVideoGroupId(examineVo.getId());
        record.setAuditId(examineVo.getAuditId());
        record.setAuditName(examineVo.getAuditName());
        record.setRemarks(examineVo.getRemarks());
        recordDao.insert(record);

        //清状态
        recordDao.delete(Wrappers.<VideoLessonAuthRecord>lambdaQuery()
                .eq(VideoLessonAuthRecord::getVideoGroupId, examineVo.getId())
                .eq(VideoLessonAuthRecord::getAuditStatus, AuthStatusEnum.DOING));
    }

    /**
     * @Description: 删除视频课
     * @Author: cy
     * @Date: 2022/3/31
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGroup(Long id) {
        videoLessonGroupDao.deleteById(id);
        videoLessonGroupDetailDao.deleteByGroupId(id);
    }

    /**
     * @Description: 新增视频课组&视频课
     * @Author: cy
     * @Date: 2022/4/1
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(VideoLessonVo lessonVo, SysUser sysUser) {
        VideoLessonGroup lessonGroup = lessonVo.getLessonGroup();
        List<VideoLessonGroupDetail> lessonList = lessonVo.getLessonList();

        //添加组
        lessonGroup.setTeacherId(sysUser.getId());
        videoLessonGroupDao.insert(lessonGroup);

        for (VideoLessonGroupDetail detail : lessonList) {
            detail.setVideoLessonGroupId(lessonGroup.getId());
            detail.setTeacherId(sysUser.getId());
        }

        //添加课程
        videoLessonGroupDetailDao.insertVideoLesson(lessonList);

        //刷新统计
        VideoLessonGroupUpVo groupUpVo = new VideoLessonGroupUpVo();
        groupUpVo.setId(lessonGroup.getId());
        videoLessonGroupDao.updateGroup(groupUpVo);

        //记录video_lesson_auth_record
        VideoLessonAuthRecord record = new VideoLessonAuthRecord();
        record.setVideoGroupId(lessonGroup.getId());
        record.setAuditStatus(AuthStatusEnum.DOING.getCode());
        recordDao.insert(record);
    }

    /**
     * @Description: 学员详情-查询视频课列表
     * @Author: cy
     * @Date: 2022/4/2
     */
    @Override
    public IPage<VideoLessonStudentDetailVo> selectStudentPage(IPage<VideoLessonStudentDetailVo> page, VideoLessonSearch search) {
        return page.setRecords(baseMapper.selectStudentPage(page, search));
    }

    /**
     * @Description: 老师详情-查询视频课列表
     * @Author: cy
     * @Date: 2022/4/6
     */
    @Override
    public IPage<VideoLessonTeacherVo> selectTeacherPage(IPage<VideoLessonTeacherVo> page, VideoLessonSearch search) {
        return page.setRecords(baseMapper.selectTeacherPage(page, search));
    }

    /**
     * @Description: 老师详情-视频课详情-查询购买学员列表
     * @Author: cy
     * @Date: 2022/4/6
     */
    @Override
    public IPage<VideoLessonPurchaseVo> selectPurchaseStudent(IPage<VideoLessonPurchaseVo> page, VideoLessonSearch search) {
        return page.setRecords(baseMapper.selectPurchaseStudent(page, search));
    }

    /**
     * @Description: 根据组id查询视频课&视频课组
     * @Author: cy
     * @Date: 2022/4/7
     */
    @Override
    public VideoLessonStudentVo selectVideoLesson(Long groupId) {
        VideoLessonStudentVo lessonStudentVo = new VideoLessonStudentVo();

        VideoLessonGroupSearch query = new VideoLessonGroupSearch();
        query.setGroupId(groupId);
        List<VideoLessonGroupVo> lessonGroup = videoLessonGroupDao.selectPage(null, query);
        if (lessonGroup.isEmpty()) {
            return lessonStudentVo;
        }
        lessonStudentVo.setLessonGroup(lessonGroup.get(0));

        VideoLessonSearch search = new VideoLessonSearch();
        search.setVideoLessonGroupId(groupId);
        lessonStudentVo.setDetailList(videoLessonGroupDetailDao.selectPage(null, search));
        return lessonStudentVo;
    }

    /**
     * @Description: 修改视频课组&视频课
     * @Author: cy
     * @Date: 2022/4/7
     */
    @Override
    public void update(VideoLessonVo lessonVo, SysUser sysUser) {
        VideoLessonGroup lessonGroup = lessonVo.getLessonGroup();
        List<VideoLessonGroupDetail> lessonList = lessonVo.getLessonList();

        //查询原评审信息
        List<VideoLessonAuthRecord> recordList = recordDao.selectList(Wrappers.<VideoLessonAuthRecord>lambdaQuery().eq(VideoLessonAuthRecord::getVideoGroupId, lessonGroup.getId()));
        List<Long> recoreIds = recordList.stream().map(VideoLessonAuthRecord::getId).collect(Collectors.toList());

        //清除原有组&课
        videoLessonGroupDao.deleteById(lessonGroup.getId());
        videoLessonGroupDetailDao.deleteByGroupId(lessonGroup.getId());

        //添加组
        lessonGroup.setTeacherId(sysUser.getId());
        videoLessonGroupDao.insert(lessonGroup);

        for (VideoLessonGroupDetail detail : lessonList) {
            detail.setVideoLessonGroupId(lessonGroup.getId());
            detail.setTeacherId(sysUser.getId());
        }

        //添加课程
        videoLessonGroupDetailDao.insertVideoLesson(lessonList);

        //刷新统计
        VideoLessonGroupUpVo groupUpVo = new VideoLessonGroupUpVo();
        groupUpVo.setId(lessonGroup.getId());
        videoLessonGroupDao.updateGroup(groupUpVo);

        //更新历史记录的组id
        recordDao.updateIds(recoreIds, lessonGroup.getId());

        //重新提审
        VideoLessonAuthRecord record = new VideoLessonAuthRecord();
        record.setVideoGroupId(lessonGroup.getId());
        record.setAuditStatus(AuthStatusEnum.DOING.getCode());
        recordDao.insert(record);
    }

    /**
     * @Description: 查询视频课组
     * @Author: cy
     * @Date: 2022/4/11
     */
    @Override
    public IPage<LessonGroupVo> selectLessonGroup(IPage<LessonGroupVo> page, VideoLessonGroupSearch query) {
        query.setAuditStatus(AuthStatusEnum.PASS);
        return page.setRecords(baseMapper.selectLessonGroup(page, query));
    }

    /**
     * @Description: 查询所有视频课组
     * @Author: cy
     * @Date: 2022/4/20
     */
    @Override
    public IPage<VideoLessonAuthGroup> queryGroupList(IPage<VideoLessonAuthGroup> page, VideoGroupSearch query) {
        return page.setRecords(baseMapper.queryGroupList(page, query));
    }

    /**
     * @Description: 学生端-我的-我的课程-购买的视频课组
     * @Author: cy
     * @Date: 2022/4/20
     */
    @Override
    public IPage<LessonGroupVo> selectLessonGroupById(IPage<LessonGroupVo> page, VideoLessonGroupSearch query) {
        return page.setRecords(baseMapper.selectLessonGroup(page, query));
    }
}