package com.ruoyi.web.controller.system.exam;

import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TdQuestion;
import com.ruoyi.system.service.ITdQuestionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题库管理Controller
 * 
 * @author admin
 * @date 2024-04-07
 */
@Controller
@RequestMapping("/system/question")
public class TdQuestionController extends BaseController
{
    private String prefix = "system/question";

    @Autowired
    private ITdQuestionService tdQuestionService;

    @RequiresPermissions("system:question:view")
    @GetMapping()
    public String question()
    {
        return prefix + "/question";
    }

    /**
     * 查询题库管理列表
     */
    @RequiresPermissions("system:question:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TdQuestion tdQuestion)
    {
        startPage();
        List<TdQuestion> list = tdQuestionService.selectTdQuestionList(tdQuestion);
        return getDataTable(list);
    }

    /**
     * 导出题库管理列表
     */
    @RequiresPermissions("system:question:export")
    @Log(title = "题库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TdQuestion tdQuestion)
    {
        List<TdQuestion> list = tdQuestionService.selectTdQuestionList(tdQuestion);
        ExcelUtil<TdQuestion> util = new ExcelUtil<TdQuestion>(TdQuestion.class);
        return util.exportExcel(list, "题库管理数据");
    }

    /**
     * 新增题库管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("sysuser",getSysUser());
        return prefix + "/add";
    }

    /**
     * 新增保存题库管理
     */
    @RequiresPermissions("system:question:add")
    @Log(title = "题库管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TdQuestion tdQuestion)
    {
        tdQuestion.setCREATEPERSON(getLoginName());
        tdQuestion.setCREATEDATE(new Date());
        return toAjax(tdQuestionService.insertTdQuestion(tdQuestion));
    }

    /**
     * 修改题库管理
     */
    @RequiresPermissions("system:question:edit")
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        TdQuestion tdQuestion = tdQuestionService.selectTdQuestionByID(ID);
        mmap.put("tdQuestion", tdQuestion);
        return prefix + "/edit";
    }

    /**
     * 修改保存题库管理
     */
    @RequiresPermissions("system:question:edit")
    @Log(title = "题库管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TdQuestion tdQuestion)
    {
        return toAjax(tdQuestionService.updateTdQuestion(tdQuestion));
    }

    /**
     * 删除题库管理
     */
    @RequiresPermissions("system:question:remove")
    @Log(title = "题库管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tdQuestionService.deleteTdQuestionByIDs(ids));
    }
}
