package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.ZccmCourseVideoDto;
import com.hc.business.dto.ZccmUserCourseDto;
import com.hc.business.mapper.ZccmCourseChapterVideoMapper;
import com.hc.business.mapper.ZccmCourseMapper;
import com.hc.business.mapper.ZccmUserCourseMapper;
import com.hc.business.mapper.ZccmUserCourseMapper;
import com.hc.business.model.ZccmCourse;
import com.hc.business.model.ZccmCourseChapterVideo;
import com.hc.business.model.ZccmUserCourse;
import com.hc.business.model.ZccmUserVideoPosition;
import com.hc.business.service.ZccmCourseChapterService;
import com.hc.business.service.ZccmCourseService;
import com.hc.business.service.ZccmUserVideoPositionService;
import com.hc.core.utils.status.CodeFlagEnum;
import com.hc.core.utils.tool.Compute;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.*;

/**
 * 课程表(ZccmCourse)表服务实现类
 *
 * @author java
 * @since 2020-11-09 10:04:46
 */

@Service("courseService")
public class ZccmCourseServiceImpl implements ZccmCourseService {

    private final ZccmCourseChapterService courseChapterService;

    private final ZccmCourseChapterVideoMapper courseChapterVideoMapper;

    private final ZccmUserVideoPositionService userVideoPositionService;

    private final ZccmCourseMapper courseMapper;

    private final ZccmUserCourseMapper userZccmCourseMapper;

    public ZccmCourseServiceImpl(ZccmCourseMapper courseMapper,
                                 ZccmUserCourseMapper userZccmCourseMapper,
                             ZccmCourseChapterVideoMapper courseChapterVideoMapper,
                                 ZccmUserVideoPositionService userVideoPositionService,
                             ZccmCourseChapterService courseChapterService) {
        this.courseMapper = courseMapper;
        this.userZccmCourseMapper = userZccmCourseMapper;
        this.courseChapterVideoMapper = courseChapterVideoMapper;
        this.userVideoPositionService = userVideoPositionService;
        this.courseChapterService = courseChapterService;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param courseId 主键
     * @return 实例对象
     */
    @Override
    public ZccmCourse queryById(Integer courseId) {
        return this.courseMapper.selectByPrimaryKey(courseId);
    }

    /**
     * 新增数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmCourse insert(ZccmCourse course) {
        course.setIsValid(CodeFlagEnum.OBJECT_ENABLE_CODE.getCode());
        this.courseMapper.insert(course);
        return course;
    }

    /**
     * 修改数据
     *
     * @param course 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmCourse update(ZccmCourse course) {
        this.courseMapper.updateByPrimaryKeySelective(course);
        return this.queryById(course.getCourseId());
    }

    /**
     * 通过主键删除数据
     *
     * @param courseId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer courseId) {
//        return this.courseMapper.deleteByPrimaryKey(courseId) > 0;
        ZccmCourse course = queryById(courseId);
        if (!Objects.isNull(course)) {
            course.setIsValid(CodeFlagEnum.OBJECT_DISABLED_CODE.getCode());
            this.courseMapper.updateByPrimaryKeySelective(course);
            return true;
        }
        return false;
    }

    @Override
    public List<ZccmCourse> queryTeacherName(String name) {
        return this.courseMapper.queryTeacherName(name);
    }

    @Override
    public List<ZccmCourse> queryCourseName(String name) {
        return this.courseMapper.queryCourseName(name);
    }

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size, Integer valid) {
        PageHelper.startPage(page, size);
        List<ZccmCourse> courses = this.courseMapper.queryAll(valid);
        return new PageInfo(courses);
    }

    @Override
    public PageInfo queryAllByLimitAndTypeId(int page, int size, Integer typeId) {
        PageHelper.startPage(page, size);
        List<ZccmCourse> courses = this.courseMapper.queryByTypeId(typeId);
        return new PageInfo(courses);
    }


    @Override
    public List<ZccmCourseChapterVideo> getAllVideos(Integer courseId){
        return courseChapterVideoMapper.selectVideoByCourseId(courseId);
    }

    //重写课程，缓存以及计算用户总体进度的方法和结构,
    //继续教育不区分章节，但是支持多个视频
    @Override
    public List<ZccmCourseVideoDto> getCourseChapterAndVideoPosition(Integer courseId, Integer userId, Integer classId) {

        boolean update = false;
        List<ZccmCourseVideoDto> videoListDto = new ArrayList<ZccmCourseVideoDto>();

        List<ZccmCourseChapterVideo> videoList = courseChapterVideoMapper.selectVideoByCourseId(courseId);

        Double totalPercent = Double.valueOf("0");
        if (videoList.size() > 0) {

            for (int vindex = 0; vindex < videoList.size(); vindex++) {

                ZccmCourseVideoDto dto = new ZccmCourseVideoDto();
                dto.setVideo(videoList.get(vindex));

                Map<String, String> map = userVideoPositionService.findVideoPosition(userId, videoList.get(vindex).getVideoId(), courseId, classId);
                // 填入播放记录
                if ((null != map) && (map.size() > 0)) {
                    ZccmUserVideoPosition userVideoPosition = new ZccmUserVideoPosition();
                    Double videotime = null;
                    Double position = null;
                    if (map.containsKey("update")) {
                        update = true;
                    }
                    try {
                        videotime = Double.valueOf(map.get("videotime"));
                        position = Double.valueOf(map.get("position"));

                    } catch (Exception e) {
                        //视频位置，长度有问题，处理吓一条
                        //视频进度有问题，不能直接跳过。否则对于 已经缓存了但是没有正确位置信息的视频就会漏掉
                        //这类数据按照位置0处理
                        userVideoPosition.setVideoTime("0");
                        userVideoPosition.setVideoPosition("0");
                        userVideoPosition.setVideoId(videoList.get(0).getVideoId());
                        userVideoPosition.setUserId(userId);
                        //没有正确数据的视频，进度按照0计算，返回结构体
                        //totalPercent = Compute.add(totalPercent, Compute.div(position, videotime, 2));
                        dto.setPosition(userVideoPosition);

                        //解析得视频进度数据不正确，但是依然要返回一个空得结构体。否则会造成丢掉数据有问题得视频
                        videoListDto.add(dto);
                        continue;
                    }

                    userVideoPosition.setVideoTime(map.get("videotime"));
                    userVideoPosition.setVideoPosition(map.get("position"));
                    userVideoPosition.setVideoId(videoList.get(0).getVideoId());
                    userVideoPosition.setUserId(userId);
                    totalPercent = Compute.add(totalPercent, Compute.div(position, videotime, 2));
                    dto.setPosition(userVideoPosition);
                }

                videoListDto.add(dto);
            }


            //计算平均进度
            //计算平均进度
            totalPercent = Compute.div(totalPercent, videoList.size(), 2);
            if (true == update) {
                //如果章节进度有更新，需要从新计算课程总体进度，并更新到数据库
                //TODO 查询课程进度
                List<ZccmUserCourseDto> cardZccmCourseList = userZccmCourseMapper.selectUserCourseProgress(userId, classId, courseId);
                if (cardZccmCourseList.size() > 0) {
                    //课程进度有变化时 将查询的课程进度写入数据库
                    if (!totalPercent.equals(cardZccmCourseList.get(0).getCoursePercent())) {
                        ZccmUserCourse userZccmCourse = new ZccmUserCourse();
                        BeanUtils.copyProperties(cardZccmCourseList.get(0), userZccmCourse);
                        userZccmCourse.setCoursePercent(totalPercent);
                        userZccmCourseMapper.updateByPrimaryKeySelective(userZccmCourse);
                    }
                } else {
                    //如果没有课程得记录，则增加记录
                    ZccmUserCourse userZccmCourse = new ZccmUserCourse();
                    userZccmCourse.setCoursePercent(totalPercent);
                    userZccmCourse.setCourseId(courseId);
                    userZccmCourse.setUserId(userId);
                    userZccmCourse.setClassId(classId);
                    userZccmCourseMapper.insert(userZccmCourse);
                }

            }
        }
        return videoListDto;
    }
}
