package org.dromara.system.controller.ai;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.bo.AiKnowledgeAttachUploadBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.system.domain.vo.AiKnowledgeAttachVo;
import org.dromara.system.domain.bo.AiKnowledgeAttachBo;
import org.dromara.system.service.IAiKnowledgeAttachService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 知识库附件
 *
 * @author wangkang
 * @date 2025-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/knowledgeAttach")
public class AiKnowledgeAttachController extends BaseController {

    private final IAiKnowledgeAttachService aiKnowledgeAttachService;

    /**
     * 查询知识库附件列表
     */
    @SaCheckPermission("system:knowledgeAttach:list")
    @GetMapping("/list")
    public TableDataInfo<AiKnowledgeAttachVo> list(AiKnowledgeAttachBo bo, PageQuery pageQuery) {
        return aiKnowledgeAttachService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库附件列表
     */
    @SaCheckPermission("system:knowledgeAttach:export")
    @Log(title = "知识库附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiKnowledgeAttachBo bo, HttpServletResponse response) {
        List<AiKnowledgeAttachVo> list = aiKnowledgeAttachService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库附件", AiKnowledgeAttachVo.class, response);
    }

    /**
     * 获取知识库附件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:knowledgeAttach:query")
    @GetMapping("/{id}")
    public R<AiKnowledgeAttachVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable Long id) {
        return R.ok(aiKnowledgeAttachService.queryById(id));
    }

    /**
     * 新增知识库附件
     */
    @SaCheckPermission("system:knowledgeAttach:add")
    @Log(title = "知识库附件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiKnowledgeAttachBo bo) {
        return toAjax(aiKnowledgeAttachService.insertByBo(bo));
    }

    /**
     * 修改知识库附件
     */
    @SaCheckPermission("system:knowledgeAttach:edit")
    @Log(title = "知识库附件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiKnowledgeAttachBo bo) {
        return toAjax(aiKnowledgeAttachService.updateByBo(bo));
    }

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

    /**
     * 上传知识库附件
     */
    @SaCheckPermission("system:knowledgeAttach:upload")
    @Log(title = "知识库附件", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/upload")
    public R<String> upload(AiKnowledgeAttachUploadBo bo) throws Exception {
        aiKnowledgeAttachService.upload(bo);
        return R.ok("上传知识库附件成功!");
    }
}
