package com.ruoyi.aicall.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aicall.domain.CcLlmAgentAccount;
import com.ruoyi.aicall.service.ICcLlmKbService;
import com.ruoyi.common.utils.CommonUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.aicall.domain.CcLlmKbCat;
import com.ruoyi.aicall.service.ICcLlmKbCatService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 知识库Controller
 * 
 * @author ruoyi
 * @date 2026-01-19
 */
@Controller
@RequestMapping("/aicall/kbcat")
public class CcLlmKbCatController extends BaseController
{
    private String prefix = "aicall/kbcat";

    @Autowired
    private ICcLlmKbCatService ccLlmKbCatService;
    @Autowired
    private ICcLlmKbService ccLlmKbService;

    @RequiresPermissions("aicall:kbcat:view")
    @GetMapping()
    public String kbcat()
    {
        return prefix + "/kbcat";
    }

    /**
     * 查询知识库列表
     */
    @RequiresPermissions("aicall:kbcat:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CcLlmKbCat ccLlmKbCat)
    {
        startPage();
        List<CcLlmKbCat> list = ccLlmKbCatService.selectCcLlmKbCatList(ccLlmKbCat);
        for (CcLlmKbCat data: list) {
            data.setContentCount(ccLlmKbService.selectCountByCatId(data.getId()));
        }
        return getDataTable(list);
    }

    /**
     * 导出知识库列表
     */
    @RequiresPermissions("aicall:kbcat:export")
    @Log(title = "知识库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcLlmKbCat ccLlmKbCat)
    {
        List<CcLlmKbCat> list = ccLlmKbCatService.selectCcLlmKbCatList(ccLlmKbCat);
        ExcelUtil<CcLlmKbCat> util = new ExcelUtil<CcLlmKbCat>(CcLlmKbCat.class);
        return util.exportExcel(list, "知识库数据");
    }

    /**
     * 新增知识库
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("ccLlmKbCat", new CcLlmKbCat());
        return prefix + "/add";
    }

    /**
     * 新增保存知识库
     */
    @RequiresPermissions("aicall:kbcat:add")
    @Log(title = "知识库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CcLlmKbCat ccLlmKbCat)
    {
        CcLlmKbCat checkCcLlmKbCat = ccLlmKbCatService.selectCcLlmKbCatByCat(null, ccLlmKbCat.getCat());
        if (null != checkCcLlmKbCat) {
            return AjaxResult.error("知识库分类不能重复，请修改");
        }
        return toAjax(ccLlmKbCatService.insertCcLlmKbCat(ccLlmKbCat));
    }

    /**
     * 修改知识库
     */
    @RequiresPermissions("aicall:kbcat:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CcLlmKbCat ccLlmKbCat = ccLlmKbCatService.selectCcLlmKbCatById(id);
        mmap.put("ccLlmKbCat", ccLlmKbCat);
        return prefix + "/edit";
    }

    /**
     * 修改保存知识库
     */
    @RequiresPermissions("aicall:kbcat:edit")
    @Log(title = "知识库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CcLlmKbCat ccLlmKbCat)
    {
        CcLlmKbCat checkCcLlmKbCat = ccLlmKbCatService.selectCcLlmKbCatByCat(ccLlmKbCat.getId(), ccLlmKbCat.getCat());
        if (null != checkCcLlmKbCat) {
            return AjaxResult.error("知识库分类不能重复，请修改");
        }
        return toAjax(ccLlmKbCatService.updateCcLlmKbCat(ccLlmKbCat));
    }

    /**
     * 删除知识库
     */
    @RequiresPermissions("aicall:kbcat:remove")
    @Log(title = "知识库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ccLlmKbCatService.deleteCcLlmKbCatByIds(ids));
    }




    @GetMapping("/all")
    @ResponseBody
    public AjaxResult all()
    {
        List<CcLlmKbCat> list = ccLlmKbCatService.selectCcLlmKbCatList(new CcLlmKbCat());
        return AjaxResult.success(list);
    }



}
