package com.zd.exam.controller;

import com.zd.common.core.domain.R;
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.exam.domain.ElCourseChapter;
import com.zd.exam.domain.ElPaper;
import com.zd.exam.service.IElCourseChapterQuService;
import com.zd.exam.service.IElCourseChapterService;
import com.zd.exam.service.IElPaperService;
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.validation.Valid;
import java.util.Map;

/**
 * 学习资源章节Controller
 *
 * @author liubo
 * @date 2021-12-08
 */
@RestController
@Api(tags = "【学习资源章节】")
@RequestMapping("/el_chapter")
public class ElCourseChapterController extends BaseController {
    @Autowired
    private IElCourseChapterService elCourseChapterService;
    @Autowired
    private IElCourseChapterQuService courseChapterQuService;
    @Autowired
    private IElPaperService elPaperService;

    /**
     * 查询章节考题列表
     */
    @ApiOperation(value = "查询章节考题列表")
    @PostMapping("/queryChapterQu/{id}")
    public AjaxResult queryChapterQu(@PathVariable("id") Long id) {
        return AjaxResult.success(courseChapterQuService.queryQuByChapterId(id));
    }

    /**
     * 新增学习资源章节
     */
    @ApiOperation(value = "新增学习资源章节")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody ElCourseChapter elCourseChapter) {
        return AjaxResult.success(elCourseChapterService.insertElCourseChapter(elCourseChapter));
    }

    /**
     * 修改学习资源章节
     */
    @ApiOperation(value = "修改学习资源章节")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody ElCourseChapter elCourseChapter) {
        if(elCourseChapter.getId() == null){
            return error("ID 不能为空！");
        }
        return AjaxResult.success(elCourseChapterService.updateElCourseChapter(elCourseChapter));
    }

    /**
     * 删除学习资源章节
     */
    @ApiOperation(value = "删除学习资源章节")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(elCourseChapterService.deleteElCourseChapterByIds(ids));
    }

    /**----------------------------------考核功能-------------------------------------*/

    /**
     * 考核列表 -- 章节ID
     */
    @PostMapping("/assessList/{id}")
    @ApiOperation(value = "考核列表")
    public TableDataInfo assessList(@PathVariable ("id") Long courseChapterId) {
        startPage();
        return getDataTable(elPaperService.allAssessList(courseChapterId));
    }

    /**
     * 考核详情 -- 试卷ID
     */
    @PostMapping("/assessDetail/{id}")
    @ApiOperation(value = "考核详情")
    public R<ElPaper> assessDetail(@PathVariable ("id") Long paperId) {
        return R.ok(elPaperService.assessDetail(paperId));
    }

    /**
     * 开始考核 -- 章节ID
     */
    @PostMapping("/assessStart/{id}")
    @ApiOperation(value = "开始考核")
    public R<ElPaper> startAssess(@PathVariable ("id") Long courseChapterId) {

        return R.ok(elPaperService.startAssess(courseChapterId));
    }

    /**
     * 考核提交返回结果 -- 考核ID(试卷ID)
     */
    @PostMapping("/assessFinish/{id}")
    @ApiOperation(value = "考核结果")
    public R<Map<String, Object>> finishAssess(@PathVariable ("id") Long paperId) {

        return R.ok(elPaperService.finishAssess(paperId));
    }


}
