package com.ruoyi.web.llm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SnowFlake;
import com.ruoyi.llm.domain.CmcChat;
import com.ruoyi.llm.domain.CmcDocument;
import com.ruoyi.llm.service.ICmcChatService;
import com.ruoyi.llm.service.ICmcDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.llm.domain.CmcTopic;
import com.ruoyi.llm.service.ICmcTopicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * cmc聊天主题Controller
 * 
 * @author cmc
 * @date 2025-04-08
 */
@RestController
@RequestMapping("/llm/topic")
public class CmcTopicController extends BaseController
{
    @Autowired
    private ICmcTopicService cmcTopicService;

    @Autowired
    private ICmcChatService cmcChatService;

    @Autowired
    private ICmcDocumentService cmcDocumentService;

    /**
     * 查询cmc聊天主题列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmcTopic cmcTopic)
    {
        List<CmcTopic> list = cmcTopicService.selectCmcTopicList(cmcTopic);
        return getDataTable(list);
    }

    /**
     * 导出cmc聊天主题列表
     */
    @Log(title = "cmc聊天主题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmcTopic cmcTopic)
    {
        List<CmcTopic> list = cmcTopicService.selectCmcTopicList(cmcTopic);
        ExcelUtil<CmcTopic> util = new ExcelUtil<CmcTopic>(CmcTopic.class);
        util.exportExcel(response, list, "cmc聊天主题数据");
    }

    /**
     * 获取cmc聊天主题详细信息
     */
    @GetMapping(value = "/{topicId}")
    public AjaxResult getInfo(@PathVariable("topicId") String topicId)
    {
        return success(cmcTopicService.selectCmcTopicByTopicId(topicId));
    }

    /**
     * 新增cmc聊天主题
     */
    @Log(title = "cmc聊天主题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmcTopic cmcTopic)
    {
        cmcTopic.setTopicId(new SnowFlake().generateId());
        cmcTopicService.insertCmcTopic(cmcTopic);
        return success(cmcTopic.getTopicId());
    }

    /**
     * 修改cmc聊天主题
     */
    @Log(title = "cmc聊天主题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmcTopic cmcTopic)
    {
        return toAjax(cmcTopicService.updateCmcTopic(cmcTopic));
    }

    /**
     * 删除cmc聊天主题
     */
    @Log(title = "cmc聊天主题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{topicIds}")
    public AjaxResult remove(@PathVariable String[] topicIds)
    {
        for (String topicId : topicIds) {
            CmcChat cmcChat = new CmcChat();
            cmcChat.setTopicId(topicId);
            List<CmcChat> cmcChatList = cmcChatService.selectCmcChatList(cmcChat);
            String[] chatIds = new String[cmcChatList.size()];
            for (int i = 0; i < cmcChatList.size(); i++)
                chatIds[i] = cmcChatList.get(i).getChatId();
            if (chatIds.length > 0)
                cmcChatService.deleteCmcChatByChatIds(chatIds);
            for (String chatId : chatIds) {
                CmcDocument cmcDocument = new CmcDocument();
                cmcDocument.setChatId(chatId);
                List<CmcDocument> cmcDocumentList = cmcDocumentService.selectCmcDocumentList(cmcDocument);
                String[] documentIds = new String[cmcDocumentList.size()];
                for (int i = 0; i < cmcDocumentList.size(); i++)
                    documentIds[i] = cmcDocumentList.get(i).getDocumentId();
                if (documentIds.length > 0)
                    cmcDocumentService.deleteCmcDocumentByDocumentIds(documentIds);
            }
        }
        return success(cmcTopicService.deleteCmcTopicByTopicIds(topicIds));
    }
}
