package org.dromara.knowledge.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
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.knowledge.domain.bo.KnowledgeAttachBo;
import org.dromara.knowledge.domain.vo.KnowledgeAttachVo;
import org.dromara.knowledge.service.KnowledgeAttachService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库附件控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/attach")
public class KnowledgeAttachController extends BaseController {

    private final KnowledgeAttachService knowledgeAttachService;

    /**
     * 查询知识库附件列表
     */
    @SaCheckPermission("knowledge:attach:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeAttachVo> list(KnowledgeAttachBo bo, PageQuery pageQuery) {
        return knowledgeAttachService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库附件列表
     */
    @SaCheckPermission("knowledge:attach:export")
    @Log(title = "知识库附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeAttachBo bo, HttpServletResponse response) {
        List<KnowledgeAttachVo> list = knowledgeAttachService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库附件数据", KnowledgeAttachVo.class, response);
    }

    /**
     * 获取知识库附件详细信息
     *
     * @param attachId 附件ID
     */
    @SaCheckPermission("knowledge:attach:query")
    @GetMapping("/{attachId}")
    public R<KnowledgeAttachVo> getInfo(@PathVariable Long attachId) {
        return R.ok(knowledgeAttachService.queryById(attachId));
    }

    /**
     * 新增知识库附件
     */
    @SaCheckPermission("knowledge:attach:add")
    @Log(title = "知识库附件", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeAttachBo bo) {
        return toAjax(knowledgeAttachService.insertByBo(bo));
    }

    /**
     * 修改知识库附件
     */
    @SaCheckPermission("knowledge:attach:edit")
    @Log(title = "知识库附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeAttachBo bo) {
        return toAjax(knowledgeAttachService.updateByBo(bo));
    }

    /**
     * 删除知识库附件
     *
     * @param attachIds 附件ID串
     */
    @SaCheckPermission("knowledge:attach:remove")
    @Log(title = "知识库附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attachIds}")
    public R<Void> remove(@PathVariable Long[] attachIds) {
        return toAjax(knowledgeAttachService.deleteWithValidByIds(List.of(attachIds), true));
    }

    /**
     * 根据知识库ID查询附件列表
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:attach:query")
    @GetMapping("/knowledge/{knowledgeId}")
    public R<List<KnowledgeAttachVo>> getByKnowledgeId(@PathVariable String knowledgeId) {
        return R.ok(knowledgeAttachService.queryListByKnowledgeId(knowledgeId));
    }

    /**
     * 根据文档ID查询附件信息
     *
     * @param documentId 文档ID
     */
    @SaCheckPermission("knowledge:attach:query")
    @GetMapping("/document/{documentId}")
    public R<KnowledgeAttachVo> getByDocumentId(@PathVariable String documentId) {
        return R.ok(knowledgeAttachService.queryByDocId(documentId));
    }
}