Browse Source

Update user_manager: get_staff_profile uses new field

StrayWarrior 1 month ago
parent
commit
177bed9bb0
1 changed files with 48 additions and 1 deletions
  1. 48 1
      pqai_agent/user_manager.py

+ 48 - 1
pqai_agent/user_manager.py

@@ -193,7 +193,10 @@ class MySQLUserManager(UserManager):
     def get_staff_profile(self, staff_id) -> Dict:
         if not self.staff_table:
             raise Exception("staff_table is not set")
-        sql = f"SELECT agent_name, agent_gender, agent_age, agent_region " \
+        return self.get_staff_profile_v3(staff_id)
+
+    def get_staff_profile_v1(self, staff_id) -> Dict:
+        sql = f"SELECT agent_name, agent_gender, agent_age, agent_region, agent_profile " \
               f"FROM {self.staff_table} WHERE third_party_user_id = '{staff_id}'"
         data = self.db.select(sql, pymysql.cursors.DictCursor)
         if not data:
@@ -205,6 +208,49 @@ class MySQLUserManager(UserManager):
         profile['agent_gender'] = gender_map[profile['agent_gender']]
         return profile
 
+    def get_staff_profile_v2(self, staff_id) -> Dict:
+        sql = f"SELECT agent_name as name, agent_gender as gender, agent_age as age, agent_region as region, agent_profile " \
+              f"FROM {self.staff_table} WHERE third_party_user_id = '{staff_id}'"
+        data = self.db.select(sql, pymysql.cursors.DictCursor)
+        if not data:
+            logger.error(f"staff[{staff_id}] not found")
+            return {}
+        profile = data[0]
+        # 转换性别格式
+        gender_map = {0: '未知', 1: '男', 2: '女', None: '未知'}
+        profile['gender'] = gender_map[profile['gender']]
+
+        # 合并JSON字段(新版本)数据
+        if profile['agent_profile']:
+            detail_profile = json.loads(profile['agent_profile'])
+            profile.update(detail_profile)
+
+        # 去除原始字段
+        profile.pop('agent_profile', None)
+        return profile
+
+    def get_staff_profile_v3(self, staff_id) -> Dict:
+        sql = f"SELECT agent_profile " \
+              f"FROM {self.staff_table} WHERE third_party_user_id = '{staff_id}'"
+        data = self.db.select(sql)
+        if not data:
+            logger.error(f"staff[{staff_id}] not found")
+            return {}
+        profile_str = data[0][0]
+        if not profile_str:
+            return {}
+        profile = json.loads(profile_str)
+        return profile
+
+    def save_staff_profile(self, staff_id: str, profile: Dict):
+        # 正常情况下不应该有此操作
+        if not self.staff_table:
+            raise Exception("staff_table is not set")
+        if not staff_id:
+            raise Exception("Invalid staff_id: {}".format(staff_id))
+        sql = f"UPDATE {self.staff_table} SET agent_profile = %s WHERE third_party_user_id = '{staff_id}'"
+        self.db.execute(sql, (json.dumps(profile),))
+
     def list_users(self, **kwargs) -> List[Dict]:
         user_union_id = kwargs.get('user_union_id', None)
         user_name = kwargs.get('user_name', None)
@@ -235,6 +281,7 @@ class LocalUserRelationManager(UserRelationManager):
                     '7881299456216398', '7881299457990953', '7881299461167644', '7881299463002136', '7881299464081604',
                     '7881299465121735', '7881299465998082', '7881299466221881', '7881299467152300', '7881299470051791',
                     '7881299470112816', '7881299471149567', '7881299471168030', '7881299471277650', '7881299473321703']
+        user_ids = user_ids[:5]
         return [
             {"staff_id": "1688855931724582", "user_id": "7881299670930896"},
             *[{"staff_id": "1688855931724582", "user_id": user_id} for user_id in user_ids]