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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynfy.buss.exam.exam.service.IExamService;
import com.ynfy.buss.exam.paper.dto.PaperDTO;
import com.ynfy.buss.exam.paper.entity.Paper;
import com.ynfy.buss.exam.paper.service.IPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: paper
 * @Author: jeecg-boot
 * @Date: 2023-02-22
 * @Version: V1.0
 */
@Api(tags = "试卷")
@RestController
@RequestMapping("/paper")
@Slf4j
public class PaperController extends JeecgController<Paper, IPaperService> {
    @Autowired
    private IPaperService paperService;

    @Autowired
    private IExamService examService;

    /**
     * 分页列表查询
     *
     * @param paper
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "试卷-分页列表查询", notes = "试卷-分页列表查询")
    @RequiresPermissions("paper:list")
    @GetMapping(value = "/list")
    public Result<IPage<Paper>> queryPageList(Paper paper,
                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              HttpServletRequest req) {
        QueryWrapper<Paper> queryWrapper = QueryGenerator.initQueryWrapper(paper, req.getParameterMap());
        Page<Paper> page = new Page<Paper>(pageNo, pageSize);
        IPage<Paper> pageList = paperService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 保存
     *
     * @param paperDTO
     * @return
     */
    @AutoLog(value = "试卷-保存")
    @ApiOperation(value = "试卷-保存", notes = "试卷-保存")
    @RequiresPermissions("paper:save")
    @RequestMapping(value = "/save", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> save(@RequestBody PaperDTO paperDTO) {
        paperService.save(paperDTO);
        return Result.OK("保存成功!");
    }

    /**
     * 生成试卷试题
     *
     * @return
     */
    @AutoLog(value = "试卷-生成试卷试题")
    @ApiOperation(value = "试卷-生成试卷试题", notes = "试卷-生成试卷试题")
    @RequiresPermissions("paper:generateQuestion")
    @RequestMapping(value = "/generateQuestion", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> generateQuestion(String paperId) {
        paperService.generateQuestion(paperId);
        return Result.OK("生成试卷试题成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "试卷-通过id删除")
    @ApiOperation(value = "试卷-通过id删除", notes = "试卷-通过id删除")
    @RequiresPermissions("paper:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        paperService.remove(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "试卷-批量删除")
    @ApiOperation(value = "试卷-批量删除", notes = "试卷-批量删除")
    @RequiresPermissions("paper:delete")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.paperService.removeBatch(ids);
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "试卷-通过id查询", notes = "试卷-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<PaperDTO> queryById(@RequestParam(name = "id", required = true) String id) {
        return Result.ok(paperService.detail(id));
    }

    /**
     * 检查试卷是否已经被用于考试
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "试卷-检查试卷是否已经被用于考试", notes = "试卷-检查试卷是否已经被用于考试")
    @GetMapping(value = "/checkIsUsedInExam")
    public Result<?> checkIsUsedInExam(@RequestParam(name = "id", required = true) String id) {
        return Result.OK(examService.checkIsUsedInExam(id));
    }

    /**
     * 随机组卷抽取部分试题
     *
     * @return
     */
    @AutoLog(value = "随机组卷抽取部分试题")
    @ApiOperation(value = "随机组卷抽取部分试题", notes = "随机组卷抽取部分试题")
    @PostMapping(value = "/extractPartQuestion")
    public Result<?> extractPartQuestion(String ruleDetail, String questionIds) {
        return Result.OK(paperService.extractPartQuestion(ruleDetail, questionIds));
    }

}
