12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- from src.core.callcenter.dao import *
- from functools import wraps
- def with_app_context(func):
- @wraps(func)
- def wrapper(self, *args, **kwargs):
- with self.app.app_context():
- return func(self, *args, **kwargs)
- return wrapper
- class DataHandleServer:
- """通话记录服务"""
- def __init__(self,app):
- self.app = app
- @with_app_context
- def create_record(self, call_info):
- call_record = CallRecord()
- # 处理多余的参数
- for key, value in call_info.items():
- if hasattr(call_record, key): # 确保模型有这个属性
- setattr(call_record, key, value)
- try:
- if call_info.get("type") in [0, 2] or call_info.get("category") == 1: # 如果呼入类型是白名单和传统服务或者是呼出 修改用户id和用户名称
- agent_num = call_info.get("agent_num") # 使用 get 方法
- if agent_num: # 确保 agent_num 存在
- agent = self.get_user_name(agent_num)
- call_record.user_id = agent.user_id
- call_record.user_name = agent.agent_name
- db.session.add(call_record)
- db.session.commit()
- print("记录创建成功")
- except Exception as e:
- db.session.rollback()
- raise ValueError(f"创建记录失败: {e}")
- @with_app_context
- def update_record(self, session_id, call_info):
- call_record = CallRecord.query.filter_by(session_id=session_id).first()
- # 动态更新字段
- for key, value in call_info.items():
- if hasattr(call_record, key):
- setattr(call_record, key, value)
- db.session.commit()
- @with_app_context
- def get_user_name(self,agent_num):
- agent = Agent.query.filter(agent_num == agent_num).first()
- return agent
- @with_app_context
- def get_agent_phone(self, saas_id, agent_num):
- return Phone.query.filter(Phone.saas_id == saas_id, Phone.phone_num == agent_num).first()
- @with_app_context
- def update_agent_monitor_service_state(self, agent_num,service_state):
- agent_monitor = AgentMonitor.query.filter(agent_num == agent_num).first()
- agent_monitor.service_state = service_state
- db.session.commit()
|