package com.ruoyi.base.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.base.domain.RobotOpsFaqSimilar;
import com.ruoyi.base.mapper.RobotOpsFaqMapper;
import com.ruoyi.base.domain.RobotOpsFaq;
import com.ruoyi.base.service.IRobotOpsFaqService;

/**
 * 问答库管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2026-05-07
 */
@Service
public class RobotOpsFaqServiceImpl implements IRobotOpsFaqService 
{
    @Autowired
    private RobotOpsFaqMapper robotOpsFaqMapper;

    /**
     * 查询问答库管理
     * 
     * @param id 问答库管理主键
     * @return 问答库管理
     */
    @Override
    public RobotOpsFaq selectRobotOpsFaqById(Long id)
    {
        return robotOpsFaqMapper.selectRobotOpsFaqById(id);
    }

    /**
     * 查询问答库管理列表
     * 
     * @param robotOpsFaq 问答库管理
     * @return 问答库管理
     */
    @Override
    public List<RobotOpsFaq> selectRobotOpsFaqList(RobotOpsFaq robotOpsFaq)
    {
        return robotOpsFaqMapper.selectRobotOpsFaqList(robotOpsFaq);
    }

    /**
     * 新增问答库管理
     * 
     * @param robotOpsFaq 问答库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRobotOpsFaq(RobotOpsFaq robotOpsFaq)
    {
        robotOpsFaq.setCreateTime(DateUtils.getNowDate());
        int rows = robotOpsFaqMapper.insertRobotOpsFaq(robotOpsFaq);
        insertRobotOpsFaqSimilar(robotOpsFaq);
        return rows;
    }

    /**
     * 修改问答库管理
     * 
     * @param robotOpsFaq 问答库管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRobotOpsFaq(RobotOpsFaq robotOpsFaq)
    {
        robotOpsFaq.setUpdateTime(DateUtils.getNowDate());
        robotOpsFaqMapper.deleteRobotOpsFaqSimilarByFaqId(robotOpsFaq.getId());
        insertRobotOpsFaqSimilar(robotOpsFaq);
        return robotOpsFaqMapper.updateRobotOpsFaq(robotOpsFaq);
    }

    /**
     * 批量删除问答库管理
     * 
     * @param ids 需要删除的问答库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRobotOpsFaqByIds(Long[] ids)
    {
        robotOpsFaqMapper.deleteRobotOpsFaqSimilarByFaqIds(ids);
        return robotOpsFaqMapper.deleteRobotOpsFaqByIds(ids);
    }

    /**
     * 删除问答库管理信息
     * 
     * @param id 问答库管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRobotOpsFaqById(Long id)
    {
        robotOpsFaqMapper.deleteRobotOpsFaqSimilarByFaqId(id);
        return robotOpsFaqMapper.deleteRobotOpsFaqById(id);
    }

    /**
     * 新增问答相似问信息
     * 
     * @param robotOpsFaq 问答库管理对象
     */
    public void insertRobotOpsFaqSimilar(RobotOpsFaq robotOpsFaq)
    {
        List<RobotOpsFaqSimilar> robotOpsFaqSimilarList = robotOpsFaq.getRobotOpsFaqSimilarList();
        Long id = robotOpsFaq.getId();
        if (StringUtils.isNotNull(robotOpsFaqSimilarList))
        {
            List<RobotOpsFaqSimilar> list = new ArrayList<RobotOpsFaqSimilar>();
            for (RobotOpsFaqSimilar robotOpsFaqSimilar : robotOpsFaqSimilarList)
            {
                robotOpsFaqSimilar.setFaqId(id);
                list.add(robotOpsFaqSimilar);
            }
            if (list.size() > 0)
            {
                robotOpsFaqMapper.batchRobotOpsFaqSimilar(list);
            }
        }
    }
}
