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.KnowledgeFragmentBo;
import org.dromara.knowledge.domain.vo.KnowledgeFragmentVo;
import org.dromara.knowledge.service.KnowledgeFragmentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 知识库片段控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/fragment")
public class KnowledgeFragmentController extends BaseController {

    private final KnowledgeFragmentService knowledgeFragmentService;

    /**
     * 查询知识库片段列表
     */
    @SaCheckPermission("knowledge:fragment:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgeFragmentVo> list(KnowledgeFragmentBo bo, PageQuery pageQuery) {
        return knowledgeFragmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库片段列表
     */
    @SaCheckPermission("knowledge:fragment:export")
    @Log(title = "知识库片段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgeFragmentBo bo, HttpServletResponse response) {
        List<KnowledgeFragmentVo> list = knowledgeFragmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库片段数据", KnowledgeFragmentVo.class, response);
    }

    /**
     * 获取知识库片段详细信息
     *
     * @param fragmentId 片段ID
     */
    @SaCheckPermission("knowledge:fragment:query")
    @GetMapping("/{fragmentId}")
    public R<KnowledgeFragmentVo> getInfo(@PathVariable Long fragmentId) {
        return R.ok(knowledgeFragmentService.queryById(fragmentId));
    }

    /**
     * 新增知识库片段
     */
    @SaCheckPermission("knowledge:fragment:add")
    @Log(title = "知识库片段", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgeFragmentBo bo) {
        return toAjax(knowledgeFragmentService.insertByBo(bo));
    }

    /**
     * 修改知识库片段
     */
    @SaCheckPermission("knowledge:fragment:edit")
    @Log(title = "知识库片段", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgeFragmentBo bo) {
        return toAjax(knowledgeFragmentService.updateByBo(bo));
    }

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

    /**
     * 根据知识库ID查询片段列表
     *
     * @param knowledgeId 知识库ID
     */
    @SaCheckPermission("knowledge:fragment:query")
    @GetMapping("/knowledge/{knowledgeId}")
    public R<List<KnowledgeFragmentVo>> getByKnowledgeId(@PathVariable Long knowledgeId) {
        return R.ok(knowledgeFragmentService.queryListByKnowledgeId(knowledgeId));
    }

    /**
     * 根据文档ID查询片段列表
     *
     * @param documentId 文档ID
     */
    @SaCheckPermission("knowledge:fragment:query")
    @GetMapping("/document/{documentId}")
    public R<List<KnowledgeFragmentVo>> getByDocumentId(@PathVariable String documentId) {
        return R.ok(knowledgeFragmentService.queryListByDocId(documentId));
    }

    /**
     * 根据片段ID查询片段信息
     *
     * @param fragmentId 片段ID
     */
    @SaCheckPermission("knowledge:fragment:query")
    @GetMapping("/fragment/{fragmentId}")
    public R<KnowledgeFragmentVo> getByFragmentId(@PathVariable String fragmentId) {
        return R.ok(knowledgeFragmentService.queryByFid(fragmentId));
    }
}