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.mapper.CustomerServiceReplyMapper;
import com.ruoyi.system.domain.CustomerServiceReply;
import com.ruoyi.system.service.ICustomerServiceReplyService;

/**
 * 客服回复规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class CustomerServiceReplyServiceImpl implements ICustomerServiceReplyService 
{
    @Autowired
    private CustomerServiceReplyMapper customerServiceReplyMapper;

    /**
     * 查询客服回复规则
     * 
     * @param id 客服回复规则主键
     * @return 客服回复规则
     */
    @Override
    public CustomerServiceReply selectCustomerServiceReplyById(Long id)
    {
        return customerServiceReplyMapper.selectCustomerServiceReplyById(id);
    }

    /**
     * 查询客服回复规则列表
     * 
     * @param customerServiceReply 客服回复规则
     * @return 客服回复规则
     */
    @Override
    public List<CustomerServiceReply> selectCustomerServiceReplyList(CustomerServiceReply customerServiceReply)
    {
        return customerServiceReplyMapper.selectCustomerServiceReplyList(customerServiceReply);
    }

    /**
     * 根据关键字查询回复
     * 
     * @param keyword 关键字
     * @return 客服回复规则集合
     */
    @Override
    public List<CustomerServiceReply> selectCustomerServiceReplyByKeyword(String keyword)
    {
        return customerServiceReplyMapper.selectCustomerServiceReplyByKeyword(keyword);
    }

    /**
     * 获取默认回复
     * 
     * @return 默认回复
     */
    @Override
    public CustomerServiceReply selectDefaultReply()
    {
        return customerServiceReplyMapper.selectDefaultReply();
    }

    /**
     * 根据用户输入获取最佳回复
     * 
     * @param userInput 用户输入
     * @return 最佳回复
     */
    @Override
    public CustomerServiceReply getBestReply(String userInput)
    {
        if (userInput == null || userInput.trim().isEmpty()) {
            return selectDefaultReply();
        }
        
        // 根据关键字查询匹配的回复
        List<CustomerServiceReply> replies = selectCustomerServiceReplyByKeyword(userInput.trim());
        
        // 如果找到匹配的回复，返回优先级最高的
        if (replies != null && !replies.isEmpty()) {
            return replies.get(0); // 已经按优先级排序，取第一个
        }
        
        // 如果没有找到匹配的回复，返回默认回复
        return selectDefaultReply();
    }

    /**
     * 新增客服回复规则
     * 
     * @param customerServiceReply 客服回复规则
     * @return 结果
     */
    @Override
    public int insertCustomerServiceReply(CustomerServiceReply customerServiceReply)
    {
        return customerServiceReplyMapper.insertCustomerServiceReply(customerServiceReply);
    }

    /**
     * 修改客服回复规则
     * 
     * @param customerServiceReply 客服回复规则
     * @return 结果
     */
    @Override
    public int updateCustomerServiceReply(CustomerServiceReply customerServiceReply)
    {
        return customerServiceReplyMapper.updateCustomerServiceReply(customerServiceReply);
    }

    /**
     * 批量删除客服回复规则
     * 
     * @param ids 需要删除的客服回复规则主键
     * @return 结果
     */
    @Override
    public int deleteCustomerServiceReplyByIds(Long[] ids)
    {
        return customerServiceReplyMapper.deleteCustomerServiceReplyByIds(ids);
    }

    /**
     * 删除客服回复规则信息
     * 
     * @param id 客服回复规则主键
     * @return 结果
     */
    @Override
    public int deleteCustomerServiceReplyById(Long id)
    {
        return customerServiceReplyMapper.deleteCustomerServiceReplyById(id);
    }
} 