package com.hc.web;

import com.hc.business.model.Course;
import com.hc.business.model.CourseChapter;
import com.hc.business.service.CourseChapterService;
import com.hc.business.service.CourseService;
import com.hc.core.utils.redis.RedisUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class CourseChapterServiceTests {

    @Autowired
    CourseChapterService courseChapterService;

    @Autowired
    CourseService courseService;

    @Autowired
    RedisUtil redisUtil;

    private static final String COURSE_CHAPTER_REDIS_KEY_PREFIX ="course:chapter:list:";

    private Course insert_new_course(){
        //外键，首先要插入图书
        Course course = new Course();

        //外键，首先要插入图书
        course.setCourseLogo("test");
        course.setCourseHours(Float.valueOf(11));
        course.setCourseName("test");
        course.setCourseTeacher("william");
        course.setCourseDesc("test");
        courseService.insert(course);

        //先插入章节
        //插入第一章
        CourseChapter child_1 = new CourseChapter();
        child_1.setChapterName("2");
        child_1.setChapterLevel(1);
        child_1.setParentId(0);
        child_1.setCourseId(course.getCourseId());
        courseChapterService.insert(child_1);

        int parent_id  = child_1.getCourseChapterId();

        //插入第二章
        CourseChapter child_2 = new CourseChapter();
        child_2.setChapterName("3");
        child_2.setParentId(0);
        child_2.setChapterLevel(1);
        child_2.setCourseId(course.getCourseId());
        courseChapterService.insert(child_2);

        CourseChapter child_child_1 = new CourseChapter();
        child_child_1.setChapterName("11");
        child_child_1.setParentId(child_2.getCourseChapterId());
        child_child_1.setChapterLevel(2);
        child_child_1.setCourseId(course.getCourseId());
        courseChapterService.insert(child_child_1);

        return course;
    }

    /**
     * 测试根据课程id获取所有子目录id列表,测试访问数据库取值
     * 测试缓存中得数值
     */


    @Test
    void get_course_chapter_list(){
        Course course = insert_new_course();

        String course_key = COURSE_CHAPTER_REDIS_KEY_PREFIX + course.getCourseId();

        //此时缓存中没有course
        assert (true != redisUtil.hasKey(course_key));

        List<CourseChapter> chapter_list = courseChapterService.getCourseChapterChildList(course.getCourseId());
         assert ((null != chapter_list) && (chapter_list.size() == 3));

//         检查缓存中得数据
        assert (true == redisUtil.hasKey(course_key));
        chapter_list = (List<CourseChapter>)redisUtil.get(course_key);
        assert ((null != chapter_list) && (chapter_list.size() == 3));
    }


    /*
    测试用户观看，课程统计信息
     */
    @Test
    void test_course_process(){

    }
}
