data_handler.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from src.core import with_app_context
  2. from src.core.callcenter.constant import START_AGENT_NUM
  3. from src.core.callcenter.dao import *
  4. from sqlalchemy import or_
  5. class DataHandleServer:
  6. """通话记录服务"""
  7. def __init__(self,app):
  8. self.app = app
  9. @with_app_context
  10. def create_record(self, call_id, caller_number, call_type, service_category=None, destination=None, category=0):
  11. call_info = {
  12. "session_id": call_id,
  13. "time_begin": datetime.now(),
  14. "category": category,
  15. "phone": caller_number,
  16. "type": call_type,
  17. "service_category": service_category,
  18. # "agent_num":destination,
  19. "user_id":destination,
  20. "user_name": f"机器人{destination}" if destination else None,
  21. }
  22. call_record = CallRecord()
  23. # 处理多余的参数
  24. for key, value in call_info.items():
  25. if hasattr(call_record, key): # 确保模型有这个属性
  26. setattr(call_record, key, value)
  27. try:
  28. if call_type in [0, 2] or category == 1: # 如果呼入类型是白名单和传统服务或者是呼出修改坐席id和坐席名称
  29. if destination: # 确保 agent_num 存在
  30. agent = self.get_user_name(destination)
  31. call_record.user_id = agent.user_id
  32. call_record.user_name = agent.agent_name
  33. db.session.add(call_record)
  34. db.session.commit()
  35. print("记录创建成功")
  36. except Exception as e:
  37. db.session.rollback()
  38. raise ValueError(f"创建记录失败: {e}")
  39. @with_app_context
  40. def update_record(self, session_id, **kwargs):
  41. call_record = CallRecord.query.filter(CallRecord.session_id == session_id).first()
  42. time_end = kwargs.get('time_end')
  43. if time_end:
  44. bot_record = BotRecords.query.filter(BotRecords.session == session_id).first()
  45. call_record.bussiness_type = bot_record.intent
  46. # 动态更新字段
  47. for key, value in kwargs.items():
  48. if hasattr(call_record, key):
  49. setattr(call_record, key, value)
  50. db.session.commit()
  51. @with_app_context
  52. def get_user_name(self,agent_num):
  53. agent = Agent.query.filter(Agent.agent_num == agent_num).first()
  54. return agent
  55. @with_app_context
  56. def get_agent_phone(self, saas_id, agent_num):
  57. return Phone.query.filter(Phone.saas_id == saas_id, Phone.phone_num == agent_num).first()
  58. @with_app_context
  59. def get_agent(self,saas_id, agent_id=None, agent_number=None, out_id=None):
  60. agent = Agent.query.filter(
  61. Agent.saas_id == saas_id,
  62. or_(Agent.out_id == agent_id, Agent.out_id == out_id, Agent.agent_num == agent_number)
  63. ).first()
  64. return agent
  65. @with_app_context
  66. def get_newest_agent_number(self,saas_id):
  67. agent = Agent.query.filter(Agent.saas_id == saas_id).order_by(Agent.agent_num.desc()).first()
  68. agentNum = START_AGENT_NUM
  69. if agent and agent.agent_num:
  70. agentNum = str(int(agent.agent_num) + 1)
  71. return agentNum
  72. @with_app_context
  73. def get_agent_monitor(self,saas_id, agent_number):
  74. monitor = AgentMonitor.query.filter(AgentMonitor.saas_id == saas_id,
  75. AgentMonitor.agent_num == agent_number).first()
  76. return monitor
  77. @with_app_context
  78. def get_phone(self,saas_id, phone_num):
  79. phone = Phone.query.filter(Phone.saas_id == saas_id, Phone.phone_num == phone_num).first()
  80. return phone
  81. @with_app_context
  82. def get_human_service_service(self,saas_id, agent_id):
  83. human_service_map = HumanServiceMap.query.filter(HumanServiceMap.saas_id == saas_id,
  84. HumanServiceMap.agent_id == agent_id).first()
  85. return human_service_map
  86. @with_app_context
  87. def update_agent_monitor_service_state(self, agent_num,service_state):
  88. agent_monitor = AgentMonitor.query.filter(AgentMonitor.agent_num == agent_num).first()
  89. agent_monitor.service_state = service_state
  90. db.session.commit()