package org.dromara.safety.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.safety.domain.req.questionsconfig.QuestionsConfigCreateReq;
import org.dromara.safety.domain.req.questionsconfig.QuestionsConfigQueryReq;
import org.dromara.safety.domain.req.questionsconfig.QuestionsConfigUpdateReq;
import org.dromara.safety.domain.vo.BusQuestionsConfigVo;
import org.dromara.safety.service.IBusQuestionsConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题库配置
 *
 * @author lcj
 * @date 2025-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/safety/questionsConfig")
public class BusQuestionsConfigController extends BaseController {

    private final IBusQuestionsConfigService busQuestionsConfigService;

    /**
     * 查询题库配置列表
     */
    @SaCheckPermission("safety:questionsConfig:list")
    @GetMapping("/list")
    public TableDataInfo<BusQuestionsConfigVo> list(QuestionsConfigQueryReq req, PageQuery pageQuery) {
        return busQuestionsConfigService.queryPageList(req, pageQuery);
    }

    /**
     * 导出题库配置列表
     */
    @SaCheckPermission("safety:questionsConfig:export")
    @Log(title = "题库配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QuestionsConfigQueryReq req, HttpServletResponse response) {
        List<BusQuestionsConfigVo> list = busQuestionsConfigService.queryList(req);
        ExcelUtil.exportExcel(list, "题库配置", BusQuestionsConfigVo.class, response);
    }

    /**
     * 获取题库配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("safety:questionsConfig:query")
    @GetMapping("/{id}")
    public R<BusQuestionsConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(busQuestionsConfigService.queryById(id));
    }

    /**
     * 新增题库配置
     */
    @SaCheckPermission("safety:questionsConfig:add")
    @Log(title = "题库配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody QuestionsConfigCreateReq req) {
        return R.ok(busQuestionsConfigService.insertByBo(req));
    }

    /**
     * 修改题库配置
     */
    @SaCheckPermission("safety:questionsConfig:edit")
    @Log(title = "题库配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QuestionsConfigUpdateReq req) {
        return toAjax(busQuestionsConfigService.updateByBo(req));
    }

    /**
     * 删除题库配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("safety:questionsConfig:remove")
    @Log(title = "题库配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(busQuestionsConfigService.deleteWithValidByIds(List.of(ids), true));
    }
}
