package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.ThirdCourseBack;
import com.hc.business.dto.ThirdCourseDto;
import com.hc.business.mapper.CourseChapterVideoMapper;
import com.hc.business.mapper.CourseMapper;
import com.hc.business.mapper.ThirdCourseMapper;
import com.hc.business.mapper.ThirdMapper;
import com.hc.business.model.Course;
import com.hc.business.model.CourseChapterVideo;
import com.hc.business.model.Third;
import com.hc.business.model.ThirdCourse;
import com.hc.business.service.ThirdCourseService;
import com.hc.core.utils.enums.InfoEnum;
import com.hc.core.utils.exception.ResponseException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 第三方用户课程配置表(ThirdCourse)表服务实现类
 *
 * @author java
 * @since 2020-06-17 17:29:30
 */

@Service("thirdCourseService")
public class ThirdCourseServiceImpl implements ThirdCourseService {

    private final ThirdCourseMapper thirdCourseMapper;

    private final ThirdMapper thirdMapper;

    private final CourseChapterVideoMapper courseChapterVideoMapper;

    private final CourseMapper courseMapper ;

    public ThirdCourseServiceImpl(ThirdCourseMapper thirdCourseMapper, ThirdMapper thirdMapper, CourseChapterVideoMapper courseChapterVideoMapper,
                                  CourseMapper courseMapper) {
        this.thirdCourseMapper = thirdCourseMapper;
        this.thirdMapper = thirdMapper;
        this.courseChapterVideoMapper = courseChapterVideoMapper;
        this.courseMapper = courseMapper;
    }

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

    /**
     * 新增数据
     *
     * @param thirdCourse 实例对象
     * @return 实例对象
     */
    @Override
    public ThirdCourse insert(ThirdCourse thirdCourse) {
        this.thirdCourseMapper.insert(thirdCourse);
        return thirdCourse;
    }

    /**
     * 修改数据
     *
     * @param thirdCourse 实例对象
     * @return 实例对象
     */
    @Override
    public ThirdCourse update(ThirdCourse thirdCourse) {
        this.thirdCourseMapper.updateByPrimaryKeySelective(thirdCourse);
        return this.queryById(thirdCourse.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteById(Integer id) {
        return this.thirdCourseMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 分页查询全部数据
     *
     * @param page
     * @param size
     * @return 实例对象数组
     */
    @Override
    public PageInfo queryPageAll(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<ThirdCourse> list = this.thirdCourseMapper.selectAll();
        return new PageInfo(list);
    }

    @Override
    public List<ThirdCourseDto> queryThreadCourseDto(String token) {
        //查询单位
        List<Third> thirds = thirdMapper.queryThirdByToken(token);
        if (CollectionUtils.isEmpty(thirds)) {
            throw new ResponseException(InfoEnum.TOKEN_ERROR);
        }
        Third third = thirds.get(0);
        Integer thirdId = third.getThirdId();

        List<ThirdCourseDto> thirdCourseDtoList = new ArrayList<>();
        List<Course> courses = thirdCourseMapper.queryThreadCourse(thirdId);
        if (CollectionUtils.isEmpty(courses)) {
            return null;
        }
        courses.forEach(course -> {
            ThirdCourseDto thirdCourseDto = new ThirdCourseDto();
            Integer courseId = course.getCourseId();
            List<CourseChapterVideo> courseChapterVideos
                    = courseChapterVideoMapper.selectVideoByCourseId(courseId);
            thirdCourseDto.setCourse(course);
            thirdCourseDto.setCourseChapterVideoList(courseChapterVideos);
            thirdCourseDtoList.add(thirdCourseDto);
        });
        return thirdCourseDtoList;
    }

    @Override
    public List<ThirdCourseBack> queryThirdCourseDtoById(Integer thirdId) {

        List<ThirdCourse>  list = thirdCourseMapper.selectAll();
        List<ThirdCourseBack> backList = new ArrayList<ThirdCourseBack>();
        for (ThirdCourse tc:
             list) {

            ThirdCourseBack back = new ThirdCourseBack();
            Course course = courseMapper.selectByPrimaryKey(tc.getCourseId());
            if(null == course){
                continue;
            }

            back.setThirdId(thirdId);
            back.setId(tc.getId());
            back.setCourseId(tc.getCourseId());
            back.setCourseName(course.getCourseName());
            back.setCourseLogo(course.getCourseLogo());

            backList.add(back);

        }

        return backList;
    }
}