package com.ruoyi.hezhi.api.course;

import com.ruoyi.common.annotation.RateLimiter;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.qiniu.QiNiuUploadUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hezhi.domain.TbCourseChapter;
import com.ruoyi.hezhi.domain.dto.CourseDTO;
import com.ruoyi.hezhi.domain.dto.CourseEvaluateDTO;
import com.ruoyi.hezhi.service.ITbCourseChapterService;
import com.ruoyi.hezhi.service.ITbCourseEvaluateService;
import com.ruoyi.hezhi.service.ITbCourseService;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: LCL
 * @Date: 2024/9/14
 * @Description:课程API
 */
@Api("课程API")
@RestController
@RequestMapping("/api/course")
public class ApiCourseController {

    @Resource
    private QiNiuUploadUtils qiNiuUploadUtils;
    @Resource
    private ITbCourseService courseService;
    @Resource
    private ITbCourseEvaluateService courseEvaluateService;
    @Resource
    private ITbCourseChapterService courseChapterService;


    /**
     * 查询课程列表
     *
     * @param params classType 课程类型（1-思政示范课程，2-精品在线课程）
     *               classId 课程分类ID
     *               beginTime 开始时间
     *               endTime 结束时间
     *               searchValue 搜索词
     * @return success
     */
    @GetMapping("/getCourseList")
    public AjaxResult getCourseList(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> courseList = courseService.getCourseList(params);
        return AjaxResult.success(courseList);
    }

    /**
     * 查询课程详情
     *
     * @param params courseId 课程ID
     * @return success
     */
    @GetMapping("/getCourseInfo")
    public AjaxResult getCourseInfo(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("courseId") || StringUtils.isBlank(params.get("courseId").toString())) {
            return AjaxResult.error("缺少courseId");
        }
        // 浏览量+1
        courseService.addViewsNum(Long.parseLong(params.get("courseId").toString()));
        Map<String, Object> courseInfo = courseService.getCourseInfo(params);
        return AjaxResult.success(courseInfo);
    }

    /**
     * 查询课程评价列表
     *
     * @param params courseId 课程ID
     *               sort 排序（0或不传-倒序，1-正序）
     * @return success
     */
    @GetMapping("/getCourseEvaluateList")
    public AjaxResult getCourseEvaluateList(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> courseEvaluateList = courseEvaluateService.getCourseEvaluateList(params);
        return AjaxResult.success(courseEvaluateList);
    }

    /**
     * 查询课程评价评分
     *
     * @param params courseId 课程ID
     * @return success
     */
    @GetMapping("/getCourseEvaluateScore")
    public AjaxResult getCourseEvaluateScore(@RequestParam Map<String, Object> params) {
        Map<String, Object> evaluateScore = courseEvaluateService.getCourseEvaluateScore(params);
        return AjaxResult.success(evaluateScore);
    }

    /**
     * 提交评价
     *
     * @param courseEvaluateDTO
     * @return ok
     */
    @RepeatSubmit
    @RateLimiter(key = "API_submitEvaluation", time = 5, count = 50)
    @PostMapping("/submitEvaluation")
    public AjaxResult submitEvaluation(@RequestBody @Validated({CourseEvaluateDTO.submitEvaluation.class}) CourseEvaluateDTO courseEvaluateDTO, BindingResult bindingResult) {
        // 参数校验
        if (bindingResult.hasErrors()) {
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try {
            return courseEvaluateService.submitEvaluation(courseEvaluateDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("提交评价异常");
        }
    }

    /**
     * 立即参加
     *
     * @param courseDTO
     * @return ok
     */
    @RepeatSubmit
    @RateLimiter(key = "API_joinNow", time = 5, count = 50)
    @PostMapping("/joinNow")
    public AjaxResult joinNow(@RequestBody @Validated({CourseDTO.joinNow.class}) CourseDTO courseDTO, BindingResult bindingResult) {
        // 参数校验
        if (bindingResult.hasErrors()) {
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try {
            return courseService.joinNow(courseDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("立即参加异常");
        }
    }

    /**
     * 查询课程章节列表
     *
     * @param params courseId 课程ID
     * @return success
     */
    @GetMapping("/getCourseChapterTreeList")
    public AjaxResult getCourseChapterTreeList(@RequestParam Map<String, Object> params) {
        List<TbCourseChapter> chapterTreeList = courseChapterService.getCourseChapterTreeList(Long.parseLong(params.get("courseId").toString()));
        return AjaxResult.success(chapterTreeList);
    }


}
