package com.xunwang.education.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.education.entity.TeacherCourses;
import com.xunwang.education.entity.vo.TeacherCoursesVo;
import com.xunwang.education.entity.vo.TeacherList;
import com.xunwang.education.entity.vo.TeacherListQuery;
import com.xunwang.education.service.TeacherCoursesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 老师课程发布 前端控制器
 * </p>
 *
 * @author Long
 * @since 2022-12-04
 */
@ApiSupport(author = "Long")
@Api("老师课程发布")
@RestController
@RequestMapping("/education/teacher-courses")
public class TeacherCoursesController {

    @Autowired
    private TeacherCoursesService teacherCoursesService;

    @ApiOperation("课程发布")
    @PostMapping("publishCourse")
    public R publishCourse(@RequestBody TeacherCoursesVo teacherCoursesVo, HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        int i = teacherCoursesService.addTeacherCourses(teacherCoursesVo, uid);
        if (i > 0) {
            return R.ok().message("发布成功,请耐心等待审核");
        } else {
            return R.ok().message("发布失败！");
        }
    }

    @ApiOperation("查询个人已发布的课程")
    @GetMapping("showPersonCourse")
    public R showPersonCourse(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        List<TeacherCourses> teacherCourses = teacherCoursesService.showPersonCourse(uid);
        return R.ok().data("courses", teacherCourses);
    }

    @ApiOperation("删除已发布的课程")
    @GetMapping("deletePersonCourse")
    public R deletePersonCourse(HttpServletRequest request) {
        String courseId = request.getParameter("courseId");
        Integer id = Integer.valueOf(courseId);
        int i = teacherCoursesService.deletePersonCourse(id);
        if (i > 0) {
            return R.ok().message("删除成功！");
        } else {
            return R.error().message("删除失败！");
        }
    }

    @ApiOperation("查询全部课程")
    @PostMapping("showCourse")
    public R showCourse(@RequestBody TeacherListQuery queryObj) {
        List<TeacherList> teacherLists = teacherCoursesService.queryAllByConditions(queryObj);
        return R.ok().data("course", teacherLists);
    }
}

