package com.zd.app.controller;

import com.zd.common.core.domain.R;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.exam.RemoteExamService;
import com.zd.system.api.exam.domain.ElPaperQuDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhoupan
 * @Date: 2021/09/03/16:11
 * @Description:
 */
@RestController
@RequestMapping("/exam")
@Api(tags = {"考试相关"})
public class ExamApi {

    @Autowired
    private RemoteExamService remoteExamService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询在线考试列表（用户相关，区分列表接口）
     */
    @ApiOperation(value = "我的在线考试列表")
    @GetMapping(value = "/onlineExamList")
    public TableDataInfo queryMyExamList(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) Integer scopeType) {
        Long userId = SecurityUtils.getUserId();
        Map map = new HashMap(3);
        map.put("userId", userId);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("scopeType", scopeType);
        return remoteExamService.list(map);
    }

    /**
     * 去考试自动创建试卷
     */
    @ApiOperation(value = "去考试自动创建试卷")
    @PostMapping(value = "/create-paper/{examId}")
    public R<Object> createPaper(@PathVariable Long examId) {
        Map map = new HashMap(1);
        map.put("examId", examId);
        return remoteExamService.createPaper(map);
    }

    /**
     * 获取试卷管理详细信息（答题卡信息）
     */
    @ApiOperation(value = "获取试卷管理详细信息（答题卡信息）")
    @GetMapping(value = "/paper/{id}")
    public R<Object> getInfo(@PathVariable("id") Long id) {

        return remoteExamService.getInfo(id);
    }

    /**
     * 答题过程根据 paperId 和 quId 查询问题和选项
     */
    @ApiOperation(value = "查询问题和选项")
    @PostMapping(value = "/paper/paperDetail/{paperId}/{quId}")
    public R<Object> queryQuDetail(@PathVariable Long paperId, @PathVariable Long quId) {
        Map map = new HashMap(2);
        map.put("paperId", paperId);
        map.put("quId", quId);
        return remoteExamService.queryQuDetail(map);
    }

    /**
     * 填充答案 下一题实时保存上题答案
     */
    @ApiOperation(value = "保存用户答案")
    @PostMapping(value = "/paper/fillAnswer")
    public R<Object> fillAnswer(@RequestBody Map<String,Object> paperQuMap) {
        return remoteExamService.fillAnswer(paperQuMap);
    }

    /**
     * 交卷操作
     */
    @ApiOperation(value = "交卷操作")
    @PostMapping(value = "/paper/handPaper/{id}")
    public R<Object> handPaper(@PathVariable Long id,@RequestParam(required = false) Long violationId) {
        Map map = new HashMap(1);
        map.put("id", id);
        map.put("violationId", violationId);
        return remoteExamService.handPaper(map);
    }

    /**
     * 查询当前用户考试记录列表
     */
    @ApiOperation(value = "查询当前用户考试记录列表")
    @GetMapping("/my/exam/list")
    public TableDataInfo list(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Long userId = tokenService.getLoginUser().getUserid();
        Map map = new HashMap(3);
        map.put("joinUserId", userId);
        map.put("appPageNum", pageNum);
        map.put("appPageSize", pageSize);
        return remoteExamService.myExamResultList(map);
    }


    /**
     * 交卷查询考试结果（没有调用，暂时不用）
     */
    @ApiOperation(value = "查询考试结果")
    @GetMapping(value = "/paper/paperResult/{id}")
    public R<Map> paperResult(@PathVariable Long id) {

        Map map = new HashMap(1);
        map.put("id", id);
        return remoteExamService.paperResult(map);
    }

    /**
     * 根据登录用户查询 我的证书列表
     */
    @PostMapping("/exam/queryMyCert")
    @ApiOperation(value = "根据登录用户查询 我的证书列表")
    public TableDataInfo queryMyCert(@RequestBody Map<String, Object> userCert) {
        return remoteExamService.queryMyCert(userCert);
    }

    /**
     * 考试过程 查询当前考题的正确和错误数量(暂时不用)
     */
    @ApiOperation(value = "考试过程 查询当前考题的正确和错误数量")
    @GetMapping(value = "/paperStatistics/{id}")
    public R<Object> getPaperStatistics(@PathVariable("id") Long id) {
        return remoteExamService.getPaperStatistics(id);
    }


    /**
     * 根据登录用户查询 我的积分列表
     */
    @PostMapping("/points/record/list/myApp")
    @ApiOperation(value = "根据登录用户查询 我的积分列表")
    public TableDataInfo recordMyApp(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Map<String, Object> myApp = new HashMap <>();
        myApp.put("appPageNum",pageNum);
        myApp.put("appPageSize",pageSize);
        return remoteExamService.recordMyApp(myApp);
    }

    /**
     * 根据登录用户查询 我的积分统计
     */
    @ApiOperation(value = "根据登录用户查询 我的积分统计")
    @GetMapping(value = "/points/record/count/myApp")
    public R<Object> recordMyAppCount() {
        return remoteExamService.recordMyAppCount();
    }

}
