package com.hc.web.controller.browser.back;

import com.hc.business.model.Course;
import com.hc.business.service.CourseChapterVideoService;
import com.hc.business.service.CourseService;
import com.hc.core.utils.result.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 课程表(Course)表控制层
 *
 * @author java
 * @since 2020-11-09 10:04:46
 */

@RestController
@RequestMapping("/new/api/back/course")
public class BackCourseController {

    /**
     * 服务对象
     */
    private final CourseService courseService;
    private final CourseChapterVideoService videoService;

    public BackCourseController(CourseService courseService,
                                CourseChapterVideoService videoService) {
        this.courseService = courseService;
        this.videoService = videoService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "依据主键获取 Course", response = Result.class)
    public Result selectBackDetail(@PathVariable("id") Integer id) {
        return Result.success(this.courseService.queryById(id));
    }

    @PostMapping("/insert")
    @ApiOperation(value = "新增 Course", response = Result.class)
    public Result addBackCourse(@Valid @RequestBody Course course) {
        return Result.success(courseService.insert(course));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改 Course", response = Result.class)
    public Result updateBackCourse(@RequestBody Course course) {
        return Result.success(courseService.update(course));
    }

//    @DeleteMapping("/delete/{id}")
//    @ApiOperation(value = "依据主键删除 Course", response = Result.class)
//    public Result deleteBackCourse(@PathVariable("id") Integer id) {
//        return Result.success(courseService.deleteById(id));
//    }

    @GetMapping("/list")
    @ApiOperation(value = "获取 Course列表(分页)", response = Result.class)
    public Result listBackCourse(@RequestParam(defaultValue = "0") Integer page,
                                 @RequestParam(defaultValue = "0") Integer size,
                                 @RequestParam(required = false) Integer valid) {
        return Result.success(courseService.queryAllByLimit(page, size, valid));
    }


    @GetMapping("/list/teacher")
    @ApiOperation(value = "依据老师名称获取 Course列表(分页)", response = Result.class)
    public Result queryTeacherName(@ApiParam(value = "老师名称") @RequestParam String name) {
        return Result.success(courseService.queryTeacherName(name));
    }

    @GetMapping("/list/type")
    @ApiOperation(value = "获取 type分类下的 Course列表(分页)", response = Result.class)
    public Result queryAllByLimitAndTypeId(@RequestParam(defaultValue = "0") Integer page,
                                           @RequestParam(defaultValue = "0") Integer size,
                                           @ApiParam(value = "类别id") @RequestParam Integer typeId) {
        return Result.success(courseService.queryAllByLimitAndTypeId(page, size, typeId));
    }

    @GetMapping("/video")
    @ApiOperation(value = "获取一个课程下所有的视频以及某个用户的播放进度", response = Result.class)
    public Result getCourseChapterAndVideoPosition(@RequestParam Integer courseId) {
        return Result.success(videoService.queryByCourseId(courseId));
    }

}