from src.core import with_app_context from src.core.callcenter.constant import START_AGENT_NUM from src.core.callcenter.dao import * from sqlalchemy import or_ class DataHandleServer: """通话记录服务""" def __init__(self,app): self.app = app @with_app_context def create_record(self, call_id, caller_number, call_type, service_category=None, destination=None, category=0): call_info = { "session_id": call_id, "time_begin": datetime.now(), "category": category, "phone": caller_number, "type": call_type, "service_category": service_category, # "agent_num":destination, "user_id":destination, "user_name": f"机器人{destination}" if destination else None, } call_record = CallRecord() # 处理多余的参数 for key, value in call_info.items(): if hasattr(call_record, key): # 确保模型有这个属性 setattr(call_record, key, value) try: if call_type in [0, 2] or category == 1: # 如果呼入类型是白名单和传统服务或者是呼出修改坐席id和坐席名称 if destination: # 确保 agent_num 存在 agent = self.get_user_name(destination) 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, **kwargs): call_record = CallRecord.query.filter(CallRecord.session_id == session_id).first() time_end = kwargs.get('time_end') if time_end: bot_record = BotRecords.query.filter(BotRecords.session == session_id).first() call_record.bussiness_type = bot_record.intent # 动态更新字段 for key, value in kwargs.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.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 get_agent(self,saas_id, agent_id=None, agent_number=None, out_id=None): agent = Agent.query.filter( Agent.saas_id == saas_id, or_(Agent.out_id == agent_id, Agent.out_id == out_id, Agent.agent_num == agent_number) ).first() return agent @with_app_context def get_newest_agent_number(self,saas_id): agent = Agent.query.filter(Agent.saas_id == saas_id).order_by(Agent.agent_num.desc()).first() agentNum = START_AGENT_NUM if agent and agent.agent_num: agentNum = str(int(agent.agent_num) + 1) return agentNum @with_app_context def get_agent_monitor(self,saas_id, agent_number): monitor = AgentMonitor.query.filter(AgentMonitor.saas_id == saas_id, AgentMonitor.agent_num == agent_number).first() return monitor @with_app_context def get_phone(self,saas_id, phone_num): phone = Phone.query.filter(Phone.saas_id == saas_id, Phone.phone_num == phone_num).first() return phone @with_app_context def get_human_service_service(self,saas_id, agent_id): human_service_map = HumanServiceMap.query.filter(HumanServiceMap.saas_id == saas_id, HumanServiceMap.agent_id == agent_id).first() return human_service_map @with_app_context def update_agent_monitor_service_state(self, agent_num,service_state): agent_monitor = AgentMonitor.query.filter(AgentMonitor.agent_num == agent_num).first() agent_monitor.service_state = service_state db.session.commit()