package com.ruoyi.project.business.controller.train;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.dto.TtQuestionDatabaseDto;
import com.ruoyi.project.business.domain.train.TtQuestionDatabase;
import com.ruoyi.project.business.domain.vo.train.QuestionDatabaseReq;
import com.ruoyi.project.business.domain.vo.train.QuestionDatabaseRes;
import com.ruoyi.project.business.service.train.ITtQuestionDatabaseService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
 * 资料库Controller
 *
 * @author ruoyi
 * @date 2024-05-20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/train/question")
public class TtQuestionDatabaseController extends BaseController {

    private final ITtQuestionDatabaseService ttQuestionDatabaseService;

    @GetMapping("/page")
    public TableDataInfo list(QuestionDatabaseReq req) {
        startPage();
        List<QuestionDatabaseRes> list = ttQuestionDatabaseService.list(req);
        return getDataTable(list);
    }

    /**
     * 查询资料库列表
     */
    @GetMapping("/list")
    public AjaxResult list() {
        return AjaxResult.success(ttQuestionDatabaseService.list());
    }

    /**
     * 获取试题详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ttQuestionDatabaseService.getById(id));
    }

    /**
     * 修改
     */
    @Log(title = "修改试题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TtQuestionDatabase questionDatabase) {
        return toAjax(ttQuestionDatabaseService.updateById(questionDatabase));
    }

    /**
     * 删除试题
     */
    @Log(title = "删除试题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ttQuestionDatabaseService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 批量更新
     */
    @Log(title = "批量更新试题", businessType = BusinessType.UPDATE)
    @PutMapping("/batch")
    public AjaxResult batchEdit(@RequestBody List<TtQuestionDatabase> questionDatabases) {
        return toAjax(ttQuestionDatabaseService.updateBatchById(questionDatabases));
    }

    /**
     * 随机 抽取试题
     */
    // @PreAuthorize("@ss.hasPermi('system:train:randomSelect')")
    @GetMapping("/randomSelect/{num}")
    public TableDataInfo randomSelect(@PathVariable("num") Integer num) {
        List<TtQuestionDatabase> list = ttQuestionDatabaseService.selectRandomQuestionList(num);
        return getDataTable(list);
    }

    /**
     *  抽取试题
     */
    // @PreAuthorize("@ss.hasPermi('system:train:randomSelect')")
    @PostMapping("/randomSelectByNum")
    public TableDataInfo randomSelectByNum(@RequestBody List<TtQuestionDatabaseDto> ttQuestionDatabaseDtos ) {
        List<TtQuestionDatabase> list = ttQuestionDatabaseService.selectRandomQuestionListByNum(ttQuestionDatabaseDtos);

        return getDataTable(list);
    }
}
