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

import java.util.List;

/**
 * 知识库提示模板控制器
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/knowledge/prompt")
public class KnowledgePromptTemplateController extends BaseController {

    private final KnowledgePromptTemplateService knowledgePromptTemplateService;

    /**
     * 查询知识库提示模板列表
     */
    @SaCheckPermission("knowledge:prompt:list")
    @GetMapping("/list")
    public TableDataInfo<KnowledgePromptTemplateVo> list(KnowledgePromptTemplateBo bo, PageQuery pageQuery) {
        return knowledgePromptTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出知识库提示模板列表
     */
    @SaCheckPermission("knowledge:prompt:export")
    @Log(title = "知识库提示模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(KnowledgePromptTemplateBo bo, HttpServletResponse response) {
        List<KnowledgePromptTemplateVo> list = knowledgePromptTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "知识库提示模板数据", KnowledgePromptTemplateVo.class, response);
    }

    /**
     * 获取知识库提示模板详细信息
     *
     * @param templateId 模板ID
     */
    @SaCheckPermission("knowledge:prompt:query")
    @GetMapping("/{templateId}")
    public R<KnowledgePromptTemplateVo> getInfo(@PathVariable Long templateId) {
        return R.ok(knowledgePromptTemplateService.queryById(templateId));
    }

    /**
     * 新增知识库提示模板
     */
    @SaCheckPermission("knowledge:prompt:add")
    @Log(title = "知识库提示模板", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody KnowledgePromptTemplateBo bo) {
        return toAjax(knowledgePromptTemplateService.insertByBo(bo));
    }

    /**
     * 修改知识库提示模板
     */
    @SaCheckPermission("knowledge:prompt:edit")
    @Log(title = "知识库提示模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody KnowledgePromptTemplateBo bo) {
        return toAjax(knowledgePromptTemplateService.updateByBo(bo));
    }

    /**
     * 删除知识库提示模板
     *
     * @param templateIds 模板ID串
     */
    @SaCheckPermission("knowledge:prompt:remove")
    @Log(title = "知识库提示模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public R<Void> remove(@PathVariable Long[] templateIds) {
        return toAjax(knowledgePromptTemplateService.deleteWithValidByIds(List.of(templateIds), true));
    }

    /**
     * 根据分类查询提示模板列表
     *
     * @param category 分类
     */
    @SaCheckPermission("knowledge:prompt:query")
    @GetMapping("/category/{category}")
    public R<List<KnowledgePromptTemplateVo>> getByCategory(@PathVariable String category) {
        return R.ok(knowledgePromptTemplateService.queryListByCategory(category));
    }

    /**
     * 根据模板名称查询提示模板
     *
     * @param templateName 模板名称
     */
    @SaCheckPermission("knowledge:prompt:query")
    @GetMapping("/name/{templateName}")
    public R<KnowledgePromptTemplateVo> getByTemplateName(@PathVariable String templateName) {
        return R.ok(knowledgePromptTemplateService.queryByTemplateName(templateName));
    }
}