Class AbstractThreadRoutingStrategy

java.lang.Object
com.bytedesk.service.routing_strategy.AbstractThreadRoutingStrategy
Direct Known Subclasses:
AgentThreadRoutingStrategy, RobotThreadRoutingStrategy, UnifiedThreadRoutingStrategy, WorkflowThreadRoutingStrategy, WorkgroupThreadRoutingStrategy

public abstract class AbstractThreadRoutingStrategy extends Object
线程路由策略抽象基类

提供线程路由策略的通用功能和模板方法,减少具体策略实现的重复代码

主要功能: - 线程状态检查和验证 - 通用的线程操作方法 - 消息内容处理 - 异常处理和日志记录

Since:
1.0.0
Author:
jackning 270580156@qq.com
  • Field Details

    • DEFAULT_WELCOME_MESSAGE

      protected static final String DEFAULT_WELCOME_MESSAGE
      默认欢迎消息
      See Also:
    • DEFAULT_OFFLINE_MESSAGE

      protected static final String DEFAULT_OFFLINE_MESSAGE
      默认离线消息
      See Also:
    • QUEUE_NEXT_MESSAGE

      protected static final String QUEUE_NEXT_MESSAGE
      排队等待消息 - 下一个
      See Also:
    • QUEUE_WAITING_MESSAGE_TEMPLATE

      protected static final String QUEUE_WAITING_MESSAGE_TEMPLATE
      排队等待消息模板
      See Also:
    • ESTIMATED_WAIT_TIME_PER_PERSON

      protected static final int ESTIMATED_WAIT_TIME_PER_PERSON
      每人预估等待时间(分钟)
      See Also:
    • MESSAGE_CACHE_MINUTES

      protected static final int MESSAGE_CACHE_MINUTES
      消息缓存时间(分钟) - 用于判断是否复用之前的消息
      See Also:
  • Constructor Details

    • AbstractThreadRoutingStrategy

      public AbstractThreadRoutingStrategy()
  • Method Details

    • createThread

      public abstract MessageProtobuf createThread(VisitorRequest visitorRequest)
      创建线程的核心方法 由具体实现类负责处理不同类型的线程创建逻辑
      Parameters:
      visitorRequest - 访客请求信息
      Returns:
      消息协议对象
    • getThreadRestService

      protected abstract ThreadRestService getThreadRestService()
      获取线程服务实例 由具体实现类提供
      Returns:
      ThreadRestService实例
    • validateThread

      protected void validateThread(ThreadEntity thread, String operation)
      验证线程实体不为空
      Parameters:
      thread - 线程实体
      operation - 操作名称,用于错误日志
      Throws:
      IllegalArgumentException - 如果线程为空
    • validateUid

      protected void validateUid(String uid, String entityType)
      验证UID格式有效性
      Parameters:
      uid - 要验证的UID
      entityType - 实体类型名称,用于错误信息
      Throws:
      IllegalArgumentException - 如果UID无效
    • getThreadByUid

      protected ThreadEntity getThreadByUid(String threadUid)
      根据UID获取线程实体
      Parameters:
      threadUid - 线程UID
      Returns:
      线程实体
      Throws:
      IllegalArgumentException - 如果线程不存在
    • saveThread

      protected ThreadEntity saveThread(ThreadEntity thread)
      保存线程实体并处理异常
      Parameters:
      thread - 要保存的线程实体
      Returns:
      保存后的线程实体
      Throws:
      RuntimeException - 如果保存失败
    • isNewThread

      protected boolean isNewThread(ThreadEntity thread)
      检查线程是否为新创建状态
      Parameters:
      thread - 线程实体
      Returns:
      如果是新状态返回true
    • isActiveThread

      protected boolean isActiveThread(ThreadEntity thread)
      检查线程是否处于活跃状态(聊天中或排队中)
      Parameters:
      thread - 线程实体
      Returns:
      如果处于活跃状态返回true
    • isClosedThread

      protected boolean isClosedThread(ThreadEntity thread)
      检查线程是否已关闭
      Parameters:
      thread - 线程实体
      Returns:
      如果已关闭返回true
    • getValidWelcomeMessage

      protected String getValidWelcomeMessage(String customMessage)
      获取有效的欢迎消息
      Parameters:
      customMessage - 自定义消息
      Returns:
      有效的欢迎消息,如果自定义消息为空则返回默认消息
    • getValidOfflineMessage

      protected String getValidOfflineMessage(String customMessage)
      获取有效的离线消息
      Parameters:
      customMessage - 自定义消息
      Returns:
      有效的离线消息,如果自定义消息为空则返回默认消息
    • generateQueueMessage

      protected String generateQueueMessage(int queueingCount)
      生成排队等待消息
      Parameters:
      queueingCount - 排队人数
      Returns:
      排队等待消息
    • isMessageInCacheTime

      protected boolean isMessageInCacheTime(LocalDateTime messageTime)
      检查消息是否在缓存时间内
      Parameters:
      messageTime - 消息时间
      Returns:
      如果在缓存时间内返回true
    • logThreadStatusChange

      protected void logThreadStatusChange(ThreadEntity thread, String operation, String fromStatus, String toStatus)
      记录线程状态变更
      Parameters:
      thread - 线程实体
      operation - 操作类型
      fromStatus - 原状态
      toStatus - 目标状态
    • handleStrategyException

      protected RuntimeException handleStrategyException(String operation, Throwable error, String threadUid)
      处理策略执行异常
      Parameters:
      operation - 操作名称
      error - 异常信息
      threadUid - 线程UID(可选)
      Returns:
      RuntimeException
    • executeWithExceptionHandling

      protected <T> T executeWithExceptionHandling(String operation, String threadUid, Supplier<T> action)
      创建操作执行模板方法
      Parameters:
      operation - 操作名称
      threadUid - 线程UID
      action - 具体操作
      Returns:
      操作结果