package org.dromara.safety.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletRequest;
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.dto.questionbank.HseQuestionBankCreateReq;
import org.dromara.safety.domain.dto.questionbank.HseQuestionBankQueryReq;
import org.dromara.safety.domain.dto.questionbank.HseQuestionBankUpdateReq;
import org.dromara.safety.domain.vo.questionbank.HseQuestionBankVo;
import org.dromara.safety.service.IHseQuestionBankService;
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/questionBank")
public class HseQuestionBankController extends BaseController {

    private final IHseQuestionBankService questionBankService;

    /**
     * 查询题库列表
     */
    @SaCheckPermission("safety:questionBank:list")
    @GetMapping("/list")
    public TableDataInfo<HseQuestionBankVo> list(HseQuestionBankQueryReq req, PageQuery pageQuery) {
        return questionBankService.queryPageList(req, pageQuery);
    }

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

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

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

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

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