package com.ynfy.buss.exam.examreview.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynfy.buss.exam.examreview.dto.PreviewSubmitDTO;
import com.ynfy.buss.exam.examreview.entity.ExamReview;
import com.ynfy.buss.exam.examreview.service.IExamReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 阅卷
 * @Author: jeecg-boot
 * @Date: 2023-04-08
 * @Version: V1.0
 */
@Api(tags = "阅卷")
@RestController
@RequestMapping("/examReview")
@Slf4j
public class ExamReviewController extends JeecgController<ExamReview, IExamReviewService> {
    @Autowired
    private IExamReviewService examReviewService;

    /**
     * 分页列表查询
     *
     * @param examReview
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "阅卷-分页列表查询", notes = "阅卷-分页列表查询")
    @RequiresPermissions("examReview:list")
    @GetMapping(value = "/list")
    public Result<IPage<ExamReview>> queryPageList(ExamReview examReview,
                                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                   HttpServletRequest req) {
        Page<ExamReview> page = new Page<ExamReview>(pageNo, pageSize);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        IPage<ExamReview> pageList = examReviewService.selectPageList(page, examReview, user);
        return Result.OK(pageList);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "阅卷-通过id查询", notes = "阅卷-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ExamReview> queryById(@RequestParam(name = "id", required = true) String id) {
        ExamReview examReview = examReviewService.getById(id);
        if (examReview == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(examReview);
    }

    /**
     * 提交阅卷
     *
     * @return
     */
    @ApiOperation(value = "阅卷-提交阅卷", notes = "阅卷-提交阅卷")
    @PostMapping(value = "/submitPreview")
    public Result<?> submitPreview(@RequestBody PreviewSubmitDTO dto) {
        examReviewService.submitPreview(dto);
        return Result.OK("提交阅卷成功！");
    }
}
