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.dto.questionscategory.HseQuestionsCategoryCreateReq;
import org.dromara.safety.domain.dto.questionscategory.HseQuestionsCategoryQueryReq;
import org.dromara.safety.domain.dto.questionscategory.HseQuestionsCategoryUpdateReq;
import org.dromara.safety.domain.vo.questionscategory.HseQuestionsCategoryVo;
import org.dromara.safety.service.IHseQuestionsCategoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题库类别
 *
 * @author lcj
 * @date 2025-04-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/safety/questionsCategory")
public class HseQuestionsCategoryController extends BaseController {

    private final IHseQuestionsCategoryService questionsCategoryService;

    /**
     * 查询题库类别列表
     */
    @SaCheckPermission("safety:questionsCategory:list")
    @GetMapping("/list")
    public TableDataInfo<HseQuestionsCategoryVo> list(HseQuestionsCategoryQueryReq req, PageQuery pageQuery) {
        return questionsCategoryService.queryPageList(req, pageQuery);
    }

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

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

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

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

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