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.VideoLessonGroupDao;
import com.yonge.cooleshow.biz.dal.dao.VideoLessonPurchaseRecordDao;
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.VideoLessonGroup;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonGroupDetail;
import com.yonge.cooleshow.biz.dal.dao.VideoLessonGroupDetailDao;

import com.yonge.cooleshow.biz.dal.entity.VideoLessonPurchaseRecord;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupDetailService;
import com.yonge.cooleshow.biz.dal.vo.*;
import com.yonge.cooleshow.common.exception.BizException;
import com.yonge.toolset.utils.obj.ObjectUtil;
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.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

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

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

    @Autowired
    private VideoLessonGroupDetailDao videoLessonGroupDetailDao;
    @Autowired
    private VideoLessonGroupDao videoLessonGroupDao;
    @Autowired
    private VideoLessonPurchaseRecordDao videoLessonPurchaseRecordDao;

    public VideoLessonGroupDetailDao getDao() {
        return videoLessonGroupDetailDao;
    }

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

    /**
    * @Description: 根据组id查询购买用户
    * @Author: cy
    * @Date: 2022/4/1
    */
    @Override
    public IPage<VideoLessonPurchaseStudent> selectPageStudent(IPage<VideoLessonPurchaseStudent> page, VideoLessonSearch query) {
        return page.setRecords(baseMapper.selectPageStudent(page,query));
    }

    /**
    * @Description: 新增视频课
    * @Author: cy
    * @Date: 2022/3/31
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertVideoLesson(List<VideoLessonGroupDetail> videoLessonList, SysUser sysUser) {
        Set<Long> synSet = Collections.synchronizedSet(new HashSet<>());
        for (VideoLessonGroupDetail detail : videoLessonList) {
            detail.setTeacherId(sysUser.getId());
            synSet.add(detail.getVideoLessonGroupId());
        }
        if (synSet.size()!=1){
            throw new RuntimeException("创建课程必须属于同组");
        }

        VideoLessonGroup lessonGroup = videoLessonGroupDao.selectById(synSet.iterator().next());
        if (lessonGroup==null){
            throw new RuntimeException("视频课组不存在");
        }

        //添加课程
        videoLessonGroupDetailDao.insertVideoLesson(videoLessonList);
        //刷新统计
        refreshLessonCount(synSet.iterator().next());
    }

    /**
    * @Description: 修改视频课
    * @Author: cy
    * @Date: 2022/3/31
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDetail(VideoLessonGroupDetailUpVo detailUpVo, SysUser sysUser) {
        //跟新课程
        detailUpVo.setUpdateBy(sysUser.getId());
        videoLessonGroupDetailDao.updateDetail(detailUpVo);

        //刷新统计
        if (detailUpVo.getVideoLessonGroupId()!=null){
            refreshLessonCount(detailUpVo.getVideoLessonGroupId());
        }
    }

    /**
    * @Description: 删除视频课
    * @Author: cy
    * @Date: 2022/3/31
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDetail(Long id) {
        VideoLessonGroupDetail videoLessonDetail = videoLessonGroupDetailDao.selectById(id);
        if (videoLessonDetail !=null){
            videoLessonGroupDetailDao.deleteById(id);
            refreshLessonCount(videoLessonDetail.getVideoLessonGroupId());
        }
    }

    /**
    * @Description: 根据组id查询视频课&组信息-学生端
    * @Author: cy
    * @Date: 2022/4/2
    */
    @Override
    public VideoLessonStudentVo selectVideoLesson(Long groupId,Long userId) {
        VideoLessonStudentVo lessonStudentVo = new VideoLessonStudentVo();

        //校验课程是否购买
        VideoLessonPurchaseRecord isAlreadyBuy= videoLessonPurchaseRecordDao.selectOne(Wrappers.<VideoLessonPurchaseRecord>lambdaQuery()
                .eq(VideoLessonPurchaseRecord::getStudentId, userId)
                .eq(VideoLessonPurchaseRecord::getVideoLessonGroupId, groupId));
        lessonStudentVo.setAlreadyBuy(!ObjectUtil.isEmpty(isAlreadyBuy));

        VideoLessonGroupSearch query = new VideoLessonGroupSearch();
        //筛选已审核的视频课
        query.setAuditStatus(AuthStatusEnum.PASS);
        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: 刷新组内lesson_count_
    * @Author: cy 
    * @Date: 2022/3/31 
    */
    public void refreshLessonCount(Long groupId){
        VideoLessonGroupUpVo groupUpVo = new VideoLessonGroupUpVo();
        groupUpVo.setId(groupId);
        videoLessonGroupDao.updateGroup(groupUpVo);
    }
}