From 4faa732b1bd55335b142d5454833a8d5432675fe Mon Sep 17 00:00:00 2001
From: xuyonghao <898441624@qq.com>
Date: 星期四, 26 十二月 2024 18:08:42 +0800
Subject: [PATCH] resources_type表同步

---
 app/config/env_conf/resource_type.json |   23 +++++++++++++++++++++++
 app/task/fetch_agent.py                |   28 ++++++++++++++++++++++++++--
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/app/config/env_conf/resource_type.json b/app/config/env_conf/resource_type.json
new file mode 100644
index 0000000..a58d009
--- /dev/null
+++ b/app/config/env_conf/resource_type.json
@@ -0,0 +1,23 @@
+[
+  {
+    "id": "0",
+    "created_at": null,
+    "updated_at": null,
+    "name": "鑿滃崟",
+    "description": "鑿滃崟绫诲瀷浼氭樉绀哄湪绯荤粺棣栭〉宸︿晶鑿滃崟涓�"
+  },
+  {
+    "id": "1",
+    "created_at": null,
+    "updated_at": null,
+    "name": "鍔熻兘",
+    "description": "鍔熻兘绫诲瀷涓嶄細鏄剧ず鍦ㄧ郴缁熼椤靛乏渚ц彍鍗曚腑"
+  },
+  {
+    "id": "3",
+    "created_at": null,
+    "updated_at": null,
+    "name": "鐩綍",
+    "description": null
+  }
+]
\ No newline at end of file
diff --git a/app/task/fetch_agent.py b/app/task/fetch_agent.py
index da17b10..bbb4da9 100644
--- a/app/task/fetch_agent.py
+++ b/app/task/fetch_agent.py
@@ -14,7 +14,7 @@
 from app.models.user_model import UserAppModel
 from app.models.agent_model import AgentModel
 from app.models.base_model import SessionLocal, Base
-from app.models.resource_model import ResourceModel
+from app.models.resource_model import ResourceModel, ResourceTypeModel
 from app.service.v2.app_register import AppRegisterDao
 
 # 鍒涘缓鏁版嵁搴撳紩鎿庡拰浼氳瘽宸ュ巶
@@ -431,6 +431,18 @@
         db.close()
 
 
+def import_type_table(session: Session, node: dict, parent=None):
+    resource_type = ResourceTypeModel(
+        id=node['id'],
+        name=node['name'],
+        description=node.get('description')
+    )
+    if parent:
+        resource_type.parent = parent
+    session.add(resource_type)
+    session.commit()
+
+
 def import_tree(session: Session, node: dict, parent=None):
     resource = ResourceModel(
         id=node['id'],
@@ -460,6 +472,18 @@
 def sync_resources_from_json():
     db = SessionLocal()
     try:
+        if db.query(ResourceTypeModel).count() == 0:
+            with open(os.path.join(ENV_CONF_PATH, "resource_type.json"), 'r', encoding='utf-8') as file:
+                type_json_data = json.load(file)
+
+            db.query(ResourceTypeModel).delete()
+            db.commit()
+
+            for node in type_json_data:
+                import_type_table(db, node)
+            print("add resourceType record successfully")
+        else:
+            print("sync resources successfully")
         if db.query(ResourceModel).count() == 0:
             with open(os.path.join(ENV_CONF_PATH, "resource.json"), 'r', encoding='utf-8') as file:
                 json_data = json.load(file)
@@ -473,7 +497,7 @@
         else:
             print("sync resources successfully")
     except Exception as e:
-        print(f"Failed to sync resources: {str(e)}")
+        print(f"Failed to sync resources or resource type: {str(e)}")
     finally:
         db.close()
 

--
Gitblit v1.8.0