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.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.FloodKnowledgeBase;
import com.ruoyi.system.mapper.FloodKnowledgeBaseMapper;
import com.ruoyi.system.mapper.FloodKnowledgeFileMapper;
import com.ruoyi.system.service.IFloodKnowledgeBaseService;

/**
 * 洪水知识库Service业务层处理
 *
 * @author jackeyhuang
 * @version 1.0.0
 * @date 2025-01-27
 */
@Service
public class FloodKnowledgeBaseServiceImpl implements IFloodKnowledgeBaseService
{
    @Autowired
    private FloodKnowledgeBaseMapper floodKnowledgeBaseMapper;

    @Autowired
    private FloodKnowledgeFileMapper floodKnowledgeFileMapper;

    /**
     * 查询洪水知识库
     *
     * @param knowledgeId 洪水知识库主键
     * @return 洪水知识库
     */
    @Override
    public FloodKnowledgeBase selectFloodKnowledgeBaseByKnowledgeId(Long knowledgeId)
    {
        return floodKnowledgeBaseMapper.selectFloodKnowledgeBaseByKnowledgeId(knowledgeId);
    }

    /**
     * 查询洪水知识库列表
     *
     * @param floodKnowledgeBase 洪水知识库
     * @return 洪水知识库
     */
    @Override
    public List<FloodKnowledgeBase> selectFloodKnowledgeBaseList(FloodKnowledgeBase floodKnowledgeBase)
    {
        return floodKnowledgeBaseMapper.selectFloodKnowledgeBaseList(floodKnowledgeBase);
    }

    /**
     * 新增洪水知识库
     *
     * @param floodKnowledgeBase 洪水知识库
     * @return 结果
     */
    @Override
    public int insertFloodKnowledgeBase(FloodKnowledgeBase floodKnowledgeBase)
    {
        if (floodKnowledgeBase.getFileCount() == null)
        {
            floodKnowledgeBase.setFileCount(0);
        }
        if (StringUtils.isEmpty(floodKnowledgeBase.getStatus()))
        {
            floodKnowledgeBase.setStatus("0");
        }
        return floodKnowledgeBaseMapper.insertFloodKnowledgeBase(floodKnowledgeBase);
    }

    /**
     * 修改洪水知识库
     *
     * @param floodKnowledgeBase 洪水知识库
     * @return 结果
     */
    @Override
    public int updateFloodKnowledgeBase(FloodKnowledgeBase floodKnowledgeBase)
    {
        return floodKnowledgeBaseMapper.updateFloodKnowledgeBase(floodKnowledgeBase);
    }

    /**
     * 批量删除洪水知识库
     *
     * @param knowledgeIds 需要删除的洪水知识库主键
     * @return 结果
     */
    @Override
    public int deleteFloodKnowledgeBaseByKnowledgeIds(Long[] knowledgeIds)
    {
        // 删除知识库下的所有文件
        for (Long knowledgeId : knowledgeIds)
        {
            floodKnowledgeFileMapper.deleteFloodKnowledgeFileByKnowledgeId(knowledgeId);
        }
        return floodKnowledgeBaseMapper.deleteFloodKnowledgeBaseByKnowledgeIds(knowledgeIds);
    }

    /**
     * 删除洪水知识库信息
     *
     * @param knowledgeId 洪水知识库主键
     * @return 结果
     */
    @Override
    public int deleteFloodKnowledgeBaseByKnowledgeId(Long knowledgeId)
    {
        // 删除知识库下的所有文件
        floodKnowledgeFileMapper.deleteFloodKnowledgeFileByKnowledgeId(knowledgeId);
        return floodKnowledgeBaseMapper.deleteFloodKnowledgeBaseByKnowledgeId(knowledgeId);
    }

    /**
     * 校验知识库名称是否唯一
     *
     * @param floodKnowledgeBase 知识库信息
     * @return 结果
     */
    @Override
    public boolean checkKnowledgeNameUnique(FloodKnowledgeBase floodKnowledgeBase)
    {
        Long knowledgeId = StringUtils.isNull(floodKnowledgeBase.getKnowledgeId()) ? -1L : floodKnowledgeBase.getKnowledgeId();
        FloodKnowledgeBase info = floodKnowledgeBaseMapper.selectFloodKnowledgeBaseByKnowledgeName(floodKnowledgeBase.getKnowledgeName());
        if (StringUtils.isNotNull(info) && info.getKnowledgeId().longValue() != knowledgeId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 统计知识库总数
     *
     * @return 知识库总数
     */
    @Override
    public int countFloodKnowledgeBase()
    {
        return floodKnowledgeBaseMapper.countFloodKnowledgeBase();
    }
}
