data_handler.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from src.core.callcenter.dao import *
  2. from functools import wraps
  3. def with_app_context(func):
  4. @wraps(func)
  5. def wrapper(self, *args, **kwargs):
  6. with self.app.app_context():
  7. return func(self, *args, **kwargs)
  8. return wrapper
  9. class DataHandleServer:
  10. """通话记录服务"""
  11. def __init__(self,app):
  12. self.app = app
  13. @with_app_context
  14. def create_record(self, call_info):
  15. call_record = CallRecord()
  16. # 处理多余的参数
  17. for key, value in call_info.items():
  18. if hasattr(call_record, key): # 确保模型有这个属性
  19. setattr(call_record, key, value)
  20. try:
  21. if call_info.get("type") in [0, 2] or call_info.get("category") == 1: # 如果呼入类型是白名单和传统服务或者是呼出 修改用户id和用户名称
  22. agent_num = call_info.get("agent_num") # 使用 get 方法
  23. if agent_num: # 确保 agent_num 存在
  24. agent = self.get_user_name(agent_num)
  25. call_record.user_id = agent.user_id
  26. call_record.user_name = agent.agent_name
  27. db.session.add(call_record)
  28. db.session.commit()
  29. print("记录创建成功")
  30. except Exception as e:
  31. db.session.rollback()
  32. raise ValueError(f"创建记录失败: {e}")
  33. @with_app_context
  34. def update_record(self, session_id, call_info):
  35. call_record = CallRecord.query.filter_by(session_id=session_id).first()
  36. # 动态更新字段
  37. for key, value in call_info.items():
  38. if hasattr(call_record, key):
  39. setattr(call_record, key, value)
  40. db.session.commit()
  41. @with_app_context
  42. def get_user_name(self,agent_num):
  43. agent = Agent.query.filter(agent_num == agent_num).first()
  44. return agent
  45. @with_app_context
  46. def get_agent_phone(self, saas_id, agent_num):
  47. return Phone.query.filter(Phone.saas_id == saas_id, Phone.phone_num == agent_num).first()
  48. @with_app_context
  49. def update_agent_monitor_service_state(self, agent_num,service_state):
  50. agent_monitor = AgentMonitor.query.filter(agent_num == agent_num).first()
  51. agent_monitor.service_state = service_state
  52. db.session.commit()