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

import com.hotwj.platform.config.questionBank.domain.bo.HotQuestionBankBo;
import com.hotwj.platform.config.questionBank.domain.vo.HotQuestionBankImportVo;
import com.hotwj.platform.config.questionBank.domain.vo.HotQuestionBankVo;
import com.hotwj.platform.config.questionBank.listener.HotQuestionBankImportListener;
import com.hotwj.platform.config.questionBank.service.IHotQuestionBankService;
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.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

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

    private final IHotQuestionBankService hotQuestionBankService;

    /**
     * 查询题库列表
     */
    //@SaCheckPermission("config:questionBank:list")
    @GetMapping("/list")
    @Operation(summary = "分页查询题库列表")
    public TableDataInfo<HotQuestionBankVo> list(HotQuestionBankBo bo, PageQuery pageQuery) {
        return hotQuestionBankService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出题库列表
     */
    //@SaCheckPermission("config:questionBank:export")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出题库列表")
    public void export(HotQuestionBankBo bo, HttpServletResponse response) {
        List<HotQuestionBankVo> list = hotQuestionBankService.queryList(bo);
        ExcelUtil.exportExcel(list, "题库", HotQuestionBankVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file 导入文件
     */
    @Log(title = "题库", businessType = BusinessType.IMPORT)
    //@SaCheckPermission("config:questionBank:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "导入题库数据")
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        var result = ExcelUtil.importExcel(
                file.getInputStream(), HotQuestionBankImportVo.class,
                new HotQuestionBankImportListener()
        );
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    @Operation(summary = "下载题库导入模板")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "题库导入模板", HotQuestionBankImportVo.class, response);
    }

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

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

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

    /**
     * 删除题库
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("config:questionBank: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(hotQuestionBankService.deleteWithValidByIds(List.of(ids), true));
    }
}
