package com.hotwj.platform.config.examPaper.controller;

import com.hotwj.platform.config.examPaper.domain.bo.ExamPaperSaveQuestionsBo;
import com.hotwj.platform.config.examPaper.domain.bo.HotExamPaperBo;
import com.hotwj.platform.config.examPaper.domain.vo.ExamPaperSaveQuestionsResultVo;
import com.hotwj.platform.config.examPaper.domain.vo.HotExamPaperVo;
import com.hotwj.platform.config.examPaper.service.IHotExamPaperService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 试卷库
 *
 * @author shihongwei
 * @date 2025-12-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/config/examPaper")
@Tag(name = "试卷库", description = "试卷库管理")
public class HotExamPaperController extends BaseController {

    private final IHotExamPaperService hotExamPaperService;

    /**
     * 查询试卷库列表
     */
    //@SaCheckPermission("config:examPaper:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询试卷库列表")
    public TableDataInfo<HotExamPaperVo> list(HotExamPaperBo bo, PageQuery pageQuery) {
        return hotExamPaperService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出试卷库列表
     */
    //@SaCheckPermission("config:examPaper:export")
    @Log(title = "试卷库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出试卷库列表")
    public void export(HotExamPaperBo bo, HttpServletResponse response) {
        List<HotExamPaperVo> list = hotExamPaperService.queryList(bo);
        ExcelUtil.exportExcel(list, "试卷库", HotExamPaperVo.class, response);
    }

    /**
     * 获取试卷库详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("config:examPaper:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取试卷库详细信息")
    public R<HotExamPaperVo> getInfo(@NotNull(message = "主键不能为空")
                                     @Parameter(name = "id", description = "主键", required = true, example = "1")
                                     @PathVariable Long id) {
        return R.ok(hotExamPaperService.queryById(id));
    }

    /**
     * 新增试卷库
     */
    //@SaCheckPermission("config:examPaper:add")
    @Log(title = "试卷库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增试卷库")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotExamPaperBo bo) {
        return toAjax(hotExamPaperService.insertByBo(bo));
    }

    /**
     * 修改试卷库
     */
    //@SaCheckPermission("config:examPaper:edit")
    @Log(title = "试卷库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改试卷库")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotExamPaperBo bo) {
        return toAjax(hotExamPaperService.updateByBo(bo));
    }

    @Log(title = "试卷库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/saveQuestions")
    @Operation(summary = "试卷题目批量保存")
    public R<ExamPaperSaveQuestionsResultVo> saveQuestions(@Validated @RequestBody ExamPaperSaveQuestionsBo bo) {
        return R.ok(hotExamPaperService.saveQuestions(bo));
    }

    /**
     * 删除试卷库
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("config:examPaper:remove")
    @Log(title = "试卷库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除试卷库")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @Parameter(name = "ids", description = "主键串", required = true, example = "1,2,3")
                          @PathVariable Long[] ids) {
        return toAjax(hotExamPaperService.deleteWithValidByIds(List.of(ids), true));
    }
}
