package com.ruoyi.abuwx.controller;

import com.ruoyi.abuwx.domain.QuestionCategory;
import com.ruoyi.abuwx.service.IQuestionCategoryService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题目分类管理Controller
 *
 * @author lujie
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/question/category")
public class QuestionCategoryController extends BaseController {
    @Autowired
    private IQuestionCategoryService questionCategoryService;

    /**
     * 查询题目分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('question:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuestionCategory questionCategory) {
        startPage();
        List<QuestionCategory> list = questionCategoryService.selectQuestionCategoryList(questionCategory);
        return getDataTable(list);
    }

    /**
     * 导出题目分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('question:category:export')")
    @Log(title = "题目分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionCategory questionCategory) {
        List<QuestionCategory> list = questionCategoryService.selectQuestionCategoryList(questionCategory);
        ExcelUtil<QuestionCategory> util = new ExcelUtil<QuestionCategory>(QuestionCategory.class);
        util.exportExcel(response, list, "题目分类管理数据");
    }

    /**
     * 获取题目分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('question:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(questionCategoryService.selectQuestionCategoryById(id));
    }

    /**
     * 新增题目分类管理
     */
    @PreAuthorize("@ss.hasPermi('question:category:add')")
    @Log(title = "题目分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionCategory questionCategory) {
        return toAjax(questionCategoryService.insertQuestionCategory(questionCategory));
    }

    /**
     * 修改题目分类管理
     */
    @PreAuthorize("@ss.hasPermi('question:category:edit')")
    @Log(title = "题目分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionCategory questionCategory) {
        return toAjax(questionCategoryService.updateQuestionCategory(questionCategory));
    }

    /**
     * 删除题目分类管理
     */
    @PreAuthorize("@ss.hasPermi('question:category:remove')")
    @Log(title = "题目分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(questionCategoryService.deleteQuestionCategoryByIds(ids));
    }
}
