package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.dto.ZccmThirdCourseBack;
import com.hc.business.dto.ZccmThirdCourseDto;
import com.hc.business.mapper.ZccmCourseChapterVideoMapper;
import com.hc.business.mapper.ZccmCourseMapper;
import com.hc.business.mapper.ZccmThirdCourseMapper;
import com.hc.business.mapper.ZccmThirdMapper;
import com.hc.business.model.ZccmCourse;
import com.hc.business.model.ZccmCourseChapterVideo;
import com.hc.business.model.ZccmThird;
import com.hc.business.model.ZccmThirdCourse;
import com.hc.business.service.ZccmThirdCourseService;
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;

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

@Service("thirdCourseService")
public class ZccmThirdCourseServiceImpl implements ZccmThirdCourseService {

    private final ZccmThirdCourseMapper thirdCourseMapper;

    private final ZccmThirdMapper thirdMapper;

    private final ZccmCourseChapterVideoMapper courseChapterVideoMapper;

    private final ZccmCourseMapper courseMapper ;

    public ZccmThirdCourseServiceImpl(ZccmThirdCourseMapper thirdCourseMapper, ZccmThirdMapper thirdMapper, ZccmCourseChapterVideoMapper courseChapterVideoMapper,
                                      ZccmCourseMapper courseMapper) {
        this.thirdCourseMapper = thirdCourseMapper;
        this.thirdMapper = thirdMapper;
        this.courseChapterVideoMapper = courseChapterVideoMapper;
        this.courseMapper = courseMapper;
    }

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

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

    /**
     * 修改数据
     *
     * @param thirdCourse 实例对象
     * @return 实例对象
     */
    @Override
    public ZccmThirdCourse update(ZccmThirdCourse 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<ZccmThirdCourse> list = this.thirdCourseMapper.selectAll();
        return new PageInfo(list);
    }

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

        List<ZccmThirdCourseDto> zccmZccmThirdCourseDtoList = new ArrayList<>();
        List<ZccmCourse> courses = thirdCourseMapper.queryThreadCourse(thirdId);
        if (CollectionUtils.isEmpty(courses)) {
            return null;
        }
        courses.forEach(course -> {
            ZccmThirdCourseDto zccmZccmThirdCourseDto = new ZccmThirdCourseDto();
            Integer courseId = course.getCourseId();
            List<ZccmCourseChapterVideo> courseChapterVideos
                    = courseChapterVideoMapper.selectVideoByCourseId(courseId);
            zccmZccmThirdCourseDto.setCourse(course);
            zccmZccmThirdCourseDto.setCourseChapterVideoList(courseChapterVideos);
            zccmZccmThirdCourseDtoList.add(zccmZccmThirdCourseDto);
        });
        return zccmZccmThirdCourseDtoList;
    }

    @Override
    public List<ZccmThirdCourseBack> queryZccmThirdCourseDtoById(Integer thirdId) {

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

            ZccmThirdCourseBack back = new ZccmThirdCourseBack();
            ZccmCourse 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;
    }
}