package com.hc.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hc.business.mapper.CourseChapterMapper;
import com.hc.business.model.CourseChapter;
import com.hc.business.service.CourseChapterService;
import com.hc.core.utils.redis.RedisUtil;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程章节表(CourseChapter)表服务实现类
 *
 * @author java
 * @since 2020-11-09 10:04:47
 */

@Service("courseChapterService")
public class CourseChapterServiceImpl implements CourseChapterService {

    private final CourseChapterMapper courseChapterMapper;


    /**
     * 课程以及章节缓存
     */
    private static final String COURSE_CHAPTER_REDIS_KEY_PREFIX = "course:chapter:list:";

    private final RedisUtil redisUtil;

    public CourseChapterServiceImpl(CourseChapterMapper courseChapterMapper, RedisUtil redisUtil) {
        this.courseChapterMapper = courseChapterMapper;
        this.redisUtil = redisUtil;
    }


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

    /**
     * 新增数据
     *
     * @param courseChapter 实例对象
     * @return 实例对象
     */
    @Override
    public CourseChapter insert(CourseChapter courseChapter) {
        this.courseChapterMapper.insert(courseChapter);
        return courseChapter;
    }

    /**
     * 修改数据
     *
     * @param courseChapter 实例对象
     * @return 实例对象
     */
    @Override
    public CourseChapter update(CourseChapter courseChapter) {
        this.courseChapterMapper.updateByPrimaryKeySelective(courseChapter);
        return this.queryById(courseChapter.getCourseChapterId());
    }

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

    /**
     * 分页查询多条数据
     *
     * @param page 页码
     * @param size 查询条数
     * @return 对象列表
     */
    @Override
    public PageInfo queryAllByLimit(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(this.courseChapterMapper::selectAll);
    }


    /**
     * @param courseId
     * @return
     */
    @Override
    public List<CourseChapter> getCourseChapterChildList(Integer courseId) {

        //首先查找redis缓存保存的课程 章节的数据
        String course_key = COURSE_CHAPTER_REDIS_KEY_PREFIX + courseId.toString();
        //目前只支持到章
        if (true == redisUtil.hasKey(course_key)) {
            return (List<CourseChapter>) redisUtil.get(course_key);
        } else {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("courseId", courseId);
            List<CourseChapter> list = courseChapterMapper.getCourseChapterChildList(map);
            redisUtil.set(course_key, list, 24 * 60 * 60);
            return list;
        }


    }
}