package com.zd.exam.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.exam.domain.ElChapterLearnRecord;
import com.zd.exam.domain.ElCourse;
import com.zd.exam.domain.ElCourseChapter;
import com.zd.exam.domain.vo.ElCourseVo;
import com.zd.exam.service.IElChapterLearnRecordService;
import com.zd.exam.service.IElCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 学习资源Controller
 *
 * @author liubo
 * @date 2021-12-07
 */
@RestController
@Api(tags = "【学习资源】")
@RequestMapping("/el_course")
public class ElCourseController extends BaseController {
    @Autowired
    private IElCourseService elCourseService;
    @Autowired
    private IElChapterLearnRecordService chapterLearnRecordService;

    /**
     * 查询学习资源列表
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EL_COURSE+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询学习资源列表" ,notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.LIST)
    public TableDataInfo<ElCourse> list(ElCourse elCourse) {
        startPage();
        List<ElCourse> list = elCourseService.selectElCourseList(elCourse);
        return getDataTable(list);
    }

    /**
     * 查询学习资源下拉框
     */
    @PostMapping("/queryOption")
    @ApiOperation(value = "查询学习资源下拉框")
    public AjaxResult queryOption(ElCourse elCourse) {
        List<Map<String, Object>> list = elCourseService.queryOption(elCourse);
        return AjaxResult.success(list);
    }

    /**
     * 查询学习记录列表
     */
    @ApiOperation(value = "查询学习记录列表")
    @GetMapping(value = "/learnList")
    public TableDataInfo allLearnList(ElCourseVo courseVo) {
        startPage();
        List<ElCourseVo> list = chapterLearnRecordService.learnList(courseVo);
        return getDataTable(list);
    }

    /**
     * 管理端获取学习资源详细信息
     */
    @ApiOperation(value = "管理端获取学习资源详细信息")
    @GetMapping(value = "/{userId}/{id}")
    public R<ElCourse> getCourseInfoByUser(@PathVariable("userId") Long userId, @PathVariable("id") Long id,HttpServletRequest request) {
        return R.ok(elCourseService.getCourseInfoByUser(userId,id,request));
    }

    /**
     * 获取学习资源详细信息 "exam:el_course:query"
     */
    @ApiOperation(value = "获取学习资源详细信息",notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.QUERY)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_COURSE+ PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,HttpServletRequest request) {
        return AjaxResult.success(elCourseService.selectElCourseById(id,request));
    }

    /**
     * 新增学习资源 "exam:el_course:add"
     */
    @ApiOperation(value = "新增学习资源",notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.ADD)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_COURSE+ PerFun.ADD )
    @Log(title = "学习资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElCourse elCourse) {
        return AjaxResult.success(elCourseService.insertElCourse(elCourse));
    }

    /**
     * 修改学习资源 -- 只能修改自己的课程
     */
    @ApiOperation(value = "修改学习资源",notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.EDIT)
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EL_COURSE+ PerFun.EDIT)
    @Log(title = "学习资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElCourse elCourse) {
        return AjaxResult.success(elCourseService.updateElCourse(elCourse));
    }

    /**
     * 删除学习资源  "exam:el_course:remove"
     */
    @ApiOperation(value = "删除学习资源",notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.REMOVE)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_COURSE+ PerFun.REMOVE)
    @Log(title = "学习资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(elCourseService.deleteElCourseByIds(ids));
    }

    /**--------------------------个人学习相关--------------------------------*/

    /**
     * 学生端查询学习资源列表
     */
    @GetMapping("/course/list")
    @ApiOperation(value = "学生端查询学习资源列表")
    public TableDataInfo<ElCourse> courseList(ElCourse elCourse) {
        startPage();
        List<ElCourse> list = elCourseService.selectElCourseListByUser(elCourse);
        return getDataTable(list);
    }

    /**
     * 个人学习记录接口
     */
    @ApiOperation(value = "个人学习记录接口")
    @PostMapping(value = "/learn/list")
    public TableDataInfo learnList(@RequestBody ElCourseVo courseVo) {
        courseVo.setJoinUserId(SecurityUtils.getUserId());
        startPage();
        List<ElCourseVo> list = chapterLearnRecordService.learnList(courseVo);
        return getDataTable(list);
    }

    /**
     * 个人学习资源详细信息
     */
    @ApiOperation(value = "获取学习资源详细信息")
    @GetMapping(value = "/learn/{id}")
    public AjaxResult getCourseInfo(@PathVariable("id") Long id, HttpServletRequest request) {
        return AjaxResult.success(elCourseService.selectUserCourseById(id,request));
    }

    /**
     * 学习资源 开始学习接口
     */
    @ApiOperation(value = "学习资源 开始学习接口")
    @PostMapping(value = "/learn/start")
    public AjaxResult learnStart(@Valid @RequestBody ElChapterLearnRecord learnRecord) {
        chapterLearnRecordService.learnStart(learnRecord);
        return AjaxResult.success();
    }

    /**
     * 学习资源 完成学习接口
     */
    @ApiOperation(value = "学习资源 完成学习接口")
    @PostMapping(value = "/learn/finish")
    public R<Map<String, Object>> learnFinish(@Valid @RequestBody ElChapterLearnRecord learnRecord) {
        return R.ok(chapterLearnRecordService.learnFinish(learnRecord));
    }
}
