package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.FloodKnowledgeFile;
import com.ruoyi.system.mapper.FloodKnowledgeFileMapper;
import com.ruoyi.system.mapper.FloodKnowledgeBaseMapper;
import com.ruoyi.system.service.IFloodKnowledgeFileService;

/**
 * 知识库文件Service业务层处理
 *
 * @author jackeyhuang
 * @version 1.0.0
 * @date 2025-01-27
 */
@Service
public class FloodKnowledgeFileServiceImpl implements IFloodKnowledgeFileService
{
    @Autowired
    private FloodKnowledgeFileMapper floodKnowledgeFileMapper;

    @Autowired
    private FloodKnowledgeBaseMapper floodKnowledgeBaseMapper;

    /**
     * 查询知识库文件
     *
     * @param fileId 知识库文件主键
     * @return 知识库文件
     */
    @Override
    public FloodKnowledgeFile selectFloodKnowledgeFileByFileId(Long fileId)
    {
        return floodKnowledgeFileMapper.selectFloodKnowledgeFileByFileId(fileId);
    }

    /**
     * 查询知识库文件列表
     *
     * @param floodKnowledgeFile 知识库文件
     * @return 知识库文件
     */
    @Override
    public List<FloodKnowledgeFile> selectFloodKnowledgeFileList(FloodKnowledgeFile floodKnowledgeFile)
    {
        return floodKnowledgeFileMapper.selectFloodKnowledgeFileList(floodKnowledgeFile);
    }

    /**
     * 根据知识库ID查询文件列表
     *
     * @param knowledgeId 知识库ID
     * @return 知识库文件集合
     */
    @Override
    public List<FloodKnowledgeFile> selectFloodKnowledgeFileListByKnowledgeId(Long knowledgeId)
    {
        return floodKnowledgeFileMapper.selectFloodKnowledgeFileListByKnowledgeId(knowledgeId);
    }

    /**
     * 新增知识库文件
     *
     * @param floodKnowledgeFile 知识库文件
     * @return 结果
     */
    @Override
    public int insertFloodKnowledgeFile(FloodKnowledgeFile floodKnowledgeFile)
    {
        // 更新知识库的文件数量
        if (floodKnowledgeFile.getKnowledgeId() != null)
        {
            updateKnowledgeBaseFileCount(floodKnowledgeFile.getKnowledgeId());
        }
        return floodKnowledgeFileMapper.insertFloodKnowledgeFile(floodKnowledgeFile);
    }

    /**
     * 修改知识库文件
     *
     * @param floodKnowledgeFile 知识库文件
     * @return 结果
     */
    @Override
    public int updateFloodKnowledgeFile(FloodKnowledgeFile floodKnowledgeFile)
    {
        int result = floodKnowledgeFileMapper.updateFloodKnowledgeFile(floodKnowledgeFile);
        // 如果更新了knowledgeId，需要更新知识库的文件数量
        if (result > 0 && floodKnowledgeFile.getKnowledgeId() != null)
        {
            updateKnowledgeBaseFileCount(floodKnowledgeFile.getKnowledgeId());
        }
        return result;
    }

    /**
     * 批量删除知识库文件
     *
     * @param fileIds 需要删除的知识库文件主键
     * @return 结果
     */
    @Override
    public int deleteFloodKnowledgeFileByFileIds(Long[] fileIds)
    {
        // 更新知识库的文件数量
        for (Long fileId : fileIds)
        {
            FloodKnowledgeFile file = floodKnowledgeFileMapper.selectFloodKnowledgeFileByFileId(fileId);
            if (file != null && file.getKnowledgeId() != null)
            {
                updateKnowledgeBaseFileCount(file.getKnowledgeId());
            }
        }
        return floodKnowledgeFileMapper.deleteFloodKnowledgeFileByFileIds(fileIds);
    }

    /**
     * 删除知识库文件信息
     *
     * @param fileId 知识库文件主键
     * @return 结果
     */
    @Override
    public int deleteFloodKnowledgeFileByFileId(Long fileId)
    {
        // 更新知识库的文件数量
        FloodKnowledgeFile file = floodKnowledgeFileMapper.selectFloodKnowledgeFileByFileId(fileId);
        if (file != null && file.getKnowledgeId() != null)
        {
            updateKnowledgeBaseFileCount(file.getKnowledgeId());
        }
        return floodKnowledgeFileMapper.deleteFloodKnowledgeFileByFileId(fileId);
    }

    /**
     * 更新知识库的文件数量
     *
     * @param knowledgeId 知识库ID
     */
    private void updateKnowledgeBaseFileCount(Long knowledgeId)
    {
        FloodKnowledgeFile query = new FloodKnowledgeFile();
        query.setKnowledgeId(knowledgeId);
        query.setStatus("0");
        List<FloodKnowledgeFile> files = floodKnowledgeFileMapper.selectFloodKnowledgeFileList(query);
        int fileCount = files != null ? files.size() : 0;
        
        com.ruoyi.system.domain.FloodKnowledgeBase knowledgeBase = floodKnowledgeBaseMapper.selectFloodKnowledgeBaseByKnowledgeId(knowledgeId);
        if (knowledgeBase != null)
        {
            knowledgeBase.setFileCount(fileCount);
            floodKnowledgeBaseMapper.updateFloodKnowledgeBase(knowledgeBase);
        }
    }
}
