package com.ruoyi.web.llm.controller;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.web.llm.service.ILangChainMilvusService;
import com.ruoyi.web.llm.service.IMilvusService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import dev.langchain4j.model.embedding.onnx.bgesmallzhv15.BgeSmallZhV15EmbeddingModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import io.milvus.client.MilvusServiceClient;
import io.milvus.grpc.MutationResult;
import io.milvus.param.ConnectParam;
import io.milvus.param.R;
import io.milvus.param.RpcStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * cmc知识库Controller
 * 
 * @author cmc
 * @date 2025-04-08
 */
@RestController
@RequestMapping("/llm/knowledge")
public class KnowLedgeController extends BaseController
{
    @Autowired
    private IMilvusService milvusService;

    @Autowired
    private ILangChainMilvusService langChainMilvusService;

    private static final EmbeddingModel embeddingModel = new BgeSmallZhV15EmbeddingModel();

    private static final MilvusServiceClient milvusClient = new MilvusServiceClient(
            ConnectParam.newBuilder()
                    .withHost("192.168.28.188")
                    .withPort(19530)
                    .build());

    /**
     * 新建知识库
     */
    @GetMapping("/list")
    public AjaxResult listKnowLedgeCollection()
    {
        JSONArray collectionNames = milvusService.getCollectionNames(milvusClient);
        return success(collectionNames);
    }

    /**
     * 新建知识库
     */
    @PostMapping("/create")
    public AjaxResult createKnowLedgeCollection(String collectionName, String description)
    {
        R<RpcStatus> status = milvusService.createCollection(milvusClient, collectionName, description, 512);
        return success(status);
    }

    /**
     * 修改知识库
     */
    @PostMapping("/modify")
    public AjaxResult modifyKnowLedgeCollection(String collectionName, String newCollectionName)
    {
        milvusService.collectionRename(milvusClient, collectionName, newCollectionName);
        return success();
    }

    /**
     * 删除知识库集合
     */
    @DeleteMapping("/remove")
    public AjaxResult removeKnowLedgeCollection(String collectionName)
    {
        milvusService.removeAllDocument(milvusClient, collectionName);
        milvusService.deleteCollectionName(milvusClient, collectionName);
        return success();
    }

    /**
     * 查询知识库文件
     */
    @GetMapping("/listDocument")
    public AjaxResult listKnowledgeDocument(String collectionName, String fileType)
    {
        List<String> documentList = milvusService.listDocument(milvusClient, collectionName, fileType);
        return success(documentList);
    }

    /**
     * 导入知识库文件
     */
    @PostMapping("/insertDocument")
    public AjaxResult insertKnowledgeDocument(MultipartFile[] fileList, String collectionName) throws IOException {
        R<MutationResult> insertResult = null;
        for (MultipartFile file : fileList)
            insertResult = langChainMilvusService.insertLangchainEmbeddingDocument(milvusClient, file, collectionName, embeddingModel);
        String message = "文件导入成功";
        if (insertResult != null && insertResult.getStatus() != R.Status.Success.getCode()) {
            message = "文件导入失败：" + insertResult.getMessage();
            return error(message);
        }
        else
            return success(message);
    }

    /**
     * 删除知识库文件
     */
    @DeleteMapping("/removeDocument")
    public AjaxResult deleteKnowledgeDocument(String fileName, String collectionName)
    {
        milvusService.removeDocument(milvusClient, collectionName, fileName);
        return success();
    }

}
