package com.zd.exam.controller;

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.utils.poi.ExcelUtil;
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.ElPaper;
import com.zd.exam.domain.ElPaperQu;
import com.zd.exam.domain.vo.ElPaperVO;
import com.zd.exam.service.IElPaperQuService;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 试卷管理Controller
 *
 * @author liubo
 * @date 2021-08-31
 */
@RestController
@RequestMapping("/paper")
@Api(tags = {"试卷管理"})
public class ElPaperController extends BaseController {
    @Autowired
    private IElPaperService elPaperService;
    @Autowired
    private IElPaperQuService elPaperQuService;

    /**
     * 查询试卷管理列表  "exam:paper:list"
     */
    @ApiOperation(value = "查询试卷管理列表 " ,notes="权限字符："+PerPrefix.EXAM_PAPER+ PerFun.LIST)
//    @PreAuthorize(hasPermi = PerPrefix.EXAM_PAPER+ PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(ElPaper elPaper) {
        startPage();
        List<ElPaper> list = elPaperService.selectElPaperList(elPaper);
        return getDataTable(list);
    }

    /**
     * 查询试卷管理列表 - 个人数据
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(ElPaper elPaper) {
        elPaper.setJoinUserId(SecurityUtils.getUserId());
        startPage();
        List<ElPaper> list = elPaperService.selectElPaperList(elPaper);
        return getDataTable(list);
    }

    /**
     * 导出试卷管理列表  "exam:paper:export"
     */
    @PreAuthorize(hasPermi =PerPrefix.EXAM_PAPER+ PerFun.EXPORT)
    @Log(title = "试卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElPaper elPaper) throws IOException {
        List<ElPaper> list = elPaperService.selectElPaperList(elPaper);
        ExcelUtil<ElPaper> util = new ExcelUtil<ElPaper>(ElPaper.class);
        util.exportExcel(response, list, "试卷管理数据");
    }

    /**
     * 获取试卷管理详细信息（答题卡信息）-开始考试
     */
//    @PreAuthorize(hasPermi = "exam:paper:query")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "开始考试")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(elPaperService.selectElPaperById(id));
    }

    /**
     * 考试过程 查询当前考题的正确和错误数量
     */
    @ApiOperation(value = "考试过程 查询当前考题的正确和错误数量")
    @GetMapping(value = "/paperStatistics/{id}")
    public AjaxResult getPaperStatistics(@PathVariable("id") Long id) {
        return AjaxResult.success(elPaperService.getPaperStatistics(id));
    }

    /**
     * 答题过程根据 paperId 和 quId 查询问题和选项
     */
    @ApiOperation(value = "查询问题和选项")
    @PostMapping(value = "/paperDetail")
    public AjaxResult queryQuDetail(@RequestBody ElPaperQu paperQu) {
        if (paperQu.getPaperId() == null || paperQu.getQuId() == null) {
            return error("参数无效");
        }
        paperQu = elPaperQuService.queryQuDetail(paperQu);
        return AjaxResult.success(paperQu);
    }

    /**
     * 填充答案 下一题实时保存上题答案
     */
    @ApiOperation(value = "保存用户答案")
    @PostMapping(value = "/fillAnswer")
    public AjaxResult fillAnswer(@RequestBody ElPaperQu paperQu) {
        if (paperQu.getPaperId() == null || paperQu.getQuId() == null) {
            return error("参数异常！");
        }
        elPaperQuService.fillAnswer(paperQu);
        return success();
    }

    /**
     * 交卷操作
     */
    @ApiOperation(value = "交卷操作")
    @PostMapping(value = "/handPaper")
    public AjaxResult handPaper(@RequestBody ElPaper elPaper, HttpServletRequest request) {
        return AjaxResult.success(elPaperService.handPaper(elPaper,request));
    }

    /**
     * 超时弃考
     */
    @ApiOperation(value = "超时弃考")
    @PostMapping(value = "/abandon")
    public void abandonPaper() {
        elPaperService.abandonPaper();
    }

    /**
     * 交卷查询考试结果
     */
    @ApiOperation(value = "查询考试结果")
    @PostMapping(value = "/paperResult")
    public AjaxResult paperResult(@RequestBody ElPaper elPaper) {
        elPaper = elPaperService.paperResult(elPaper);
        return AjaxResult.success(elPaper);
    }

//    /**
//     * 新增试卷管理
//     */
//    @PreAuthorize(hasPermi = "exam:paper:add")
//    @Log(title = "试卷管理", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody ElPaper elPaper) {
//        return toAjax(elPaperService.insertElPaper(elPaper));
//    }

//    /**
//     * 修改试卷管理
//     */
//    @PreAuthorize(hasPermi = "exam:paper:edit")
//    @Log(title = "试卷管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody ElPaper elPaper) {
//        return toAjax(elPaperService.updateElPaper(elPaper));
//    }

//    /**
//     * 删除试卷管理
//     */
//    @PreAuthorize(hasPermi = "exam:paper:remove")
//    @Log(title = "试卷管理", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(elPaperService.deleteElPaperByIds(ids));
//    }


    /**
     * 去考试自动创建试卷
     */
    @ApiOperation(value = "创建试卷")
    @PostMapping(value = "/create-paper")
    public AjaxResult createPaper(@RequestBody ElPaper elPaper) {
        Long paperId = elPaperService.createPaper(elPaper.getExamId());
        return AjaxResult.success(paperId);
    }


    /**
     * 小程序查询试卷管理列表
     *
     * @return
     */
    @RequestMapping("/appListAll")
    public TableDataInfo appListAll(@RequestBody ElPaperVO elPaper) {
//        if(elPaper.getScopeType() != null && elPaper.getScopeType() == 1){
//            elPaper.setJoinUserId(SecurityUtils.getUserId());
//        }
        startPage(elPaper.getAppPageNum(),elPaper.getAppPageSize());
        List<ElPaper> list = elPaperService.selectElPaperList(elPaper);
        return getDataTable(list);
    }
}
