package com.ruoyi.web.controller.wgz.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.wgz.bo.req.WgzQuestionReq;
import com.ruoyi.wgz.domain.WgzQuestionBank;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wgz.bo.WgzQuestionBankQueryBo;
import com.ruoyi.wgz.service.IWgzQuestionBankService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 题库Controller
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Api(value = "题库控制器", tags = {"题库管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wgz/bank")
public class WgzQuestionBankController extends BaseController {

    private final IWgzQuestionBankService iWgzQuestionBankService;

    /**
     * 查询题库列表
     */
    @ApiOperation("查询题库列表")
    @PreAuthorize("@ss.hasPermi('wgz:bank:list')")
    @GetMapping("/list")
    public TableDataInfo<WgzQuestionBank> list(@Validated WgzQuestionBankQueryBo bo) {
        return iWgzQuestionBankService.queryPageList(bo);
    }

    /**
     * 导出题库列表
     */
    @ApiOperation("导出题库列表")
    @PreAuthorize("@ss.hasPermi('wgz:bank:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<WgzQuestionBank> export(@Validated WgzQuestionBankQueryBo bo) {
        List<WgzQuestionBank> list = iWgzQuestionBankService.queryList(bo);
        ExcelUtil<WgzQuestionBank> util = new ExcelUtil<WgzQuestionBank>(WgzQuestionBank.class);
        return util.exportExcel(list, "题库");
    }

    /**
     * 获取题库详细信息
     */
    @ApiOperation("获取题库详细信息")
    @PreAuthorize("@ss.hasPermi('wgz:bank:query')")
    @GetMapping("/{id}")
    public AjaxResult<WgzQuestionBank> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iWgzQuestionBankService.queryById(id));
    }

    /**
     * 新增题库
     */
    @ApiOperation("新增题库")
    @PreAuthorize("@ss.hasPermi('wgz:bank:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody WgzQuestionReq bo) {
        return toAjax(iWgzQuestionBankService.insert(bo) ? 1 : 0);
    }

    /**
     * 修改题库
     */
    @ApiOperation("修改题库")
    @PreAuthorize("@ss.hasPermi('wgz:bank:edit')")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody WgzQuestionBank bo) {
        return toAjax(iWgzQuestionBankService.update(bo) ? 1 : 0);
    }

    /**
     * 删除题库
     */
    @ApiOperation("删除题库")
    @PreAuthorize("@ss.hasPermi('wgz:bank:remove')")
    @Log(title = "题库" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iWgzQuestionBankService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
