package com.ruoyi.llm.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.SnowFlake;
import com.ruoyi.llm.domain.CmcDocument;
import com.ruoyi.llm.mapper.CmcDocumentMapper;
import com.ruoyi.llm.service.ICmcDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * cmc聊天附件Service业务层处理
 * 
 * @author cmc
 * @date 2025-04-08
 */
@Service
public class CmcDocumentServiceImpl implements ICmcDocumentService 
{
    @Autowired
    private CmcDocumentMapper cmcDocumentMapper;

    /**
     * 查询cmc聊天附件
     * 
     * @param documentId cmc聊天附件主键
     * @return cmc聊天附件
     */
    @Override
    public CmcDocument selectCmcDocumentByDocumentId(String documentId)
    {
        return cmcDocumentMapper.selectCmcDocumentByDocumentId(documentId);
    }

    /**
     * 查询cmc聊天附件列表
     * 
     * @param cmcDocument cmc聊天附件
     * @return cmc聊天附件
     */
    @Override
    public List<CmcDocument> selectCmcDocumentList(CmcDocument cmcDocument)
    {
        return cmcDocumentMapper.selectCmcDocumentList(cmcDocument);
    }

    /**
     * 新增cmc聊天附件
     * 
     * @param cmcDocument cmc聊天附件
     * @return 结果
     */
    @Override
    public int insertCmcDocument(CmcDocument cmcDocument)
    {
        return cmcDocumentMapper.insertCmcDocument(cmcDocument);
    }

    /**
     * 上传外部文件
     *
     * @param fileList 文件
     * @return 结果
     */
    @Override
    public JSONObject uploadDocument(MultipartFile[] fileList) throws IOException {
        String prefixPath = "";
            prefixPath = "/upload/rag/document";
        File profilePath = new File( RuoYiConfig.getProfile() + prefixPath);
        if (!profilePath.exists())
            profilePath.mkdirs();
        String chatId = new SnowFlake().generateId();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("chatId", chatId);
        for (MultipartFile file : fileList) {
            File transferFile = new File(profilePath + "/" + file.getOriginalFilename());
            if (!transferFile.exists())
                file.transferTo(transferFile);
            CmcDocument cmcDocument = new CmcDocument();
            cmcDocument.setDocumentId(new SnowFlake().generateId());
            cmcDocument.setChatId(chatId);
            cmcDocument.setPath(prefixPath + "/" + file.getOriginalFilename());
            cmcDocumentMapper.insertCmcDocument(cmcDocument);
        }
        return jsonObject;
    }

    /**
     * 修改cmc聊天附件
     * 
     * @param cmcDocument cmc聊天附件
     * @return 结果
     */
    @Override
    public int updateCmcDocument(CmcDocument cmcDocument)
    {
        return cmcDocumentMapper.updateCmcDocument(cmcDocument);
    }

    /**
     * 批量删除cmc聊天附件
     * 
     * @param documentIds 需要删除的cmc聊天附件主键
     * @return 结果
     */
    @Override
    public int deleteCmcDocumentByDocumentIds(String[] documentIds)
    {
        return cmcDocumentMapper.deleteCmcDocumentByDocumentIds(documentIds);
    }

    /**
     * 删除cmc聊天附件信息
     * 
     * @param documentId cmc聊天附件主键
     * @return 结果
     */
    @Override
    public int deleteCmcDocumentByDocumentId(String documentId)
    {
        return cmcDocumentMapper.deleteCmcDocumentByDocumentId(documentId);
    }
}
