From 00fe58a94292a3b9921ce134542ee38d74cd9401 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 14 一月 2025 18:35:14 +0800
Subject: [PATCH] tmp

---
 app/service/v2/initialize_data.py |   58 ++++++++++++++++++++++++++++++----------------------------
 1 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/app/service/v2/initialize_data.py b/app/service/v2/initialize_data.py
index 6491b2a..958af75 100644
--- a/app/service/v2/initialize_data.py
+++ b/app/service/v2/initialize_data.py
@@ -1,10 +1,13 @@
 import json
 import time
 import os
+
+import yaml
+
 from Log import logger
 from app.config.const import DIFY, ENV_CONF_PATH
 from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \
-    cipher_suite
+    cipher_suite, UserTokenModel
 from app.service.auth import UserAppDao
 from app.service.bisheng import BishengService
 from app.service.difyService import DifyService
@@ -223,30 +226,29 @@
 
 
 async def admin_account_sync(db):
-    agent_list = []
-    with open(os.path.join(ENV_CONF_PATH, "default_agent_conf.json"), 'r', encoding='utf-8') as file:
-        # 鍔犺浇JSON鏁版嵁
-        agent_dict = json.load(file)
-        agent_list = agent_dict.get("basic", [])
-    user = db.query(UserModel).filter_by(permission="admin").first()
-    for agent in agent_list:
-        dialog = db.query(DialogModel).filter(DialogModel.id == agent["id"]).first()
-        if dialog:
-            try:
-                dialog.name = agent["name"]
-                dialog.description = agent["description"]
-                dialog.icon = agent["icon"]
-                db.commit()
-            except Exception as e:
-                logger.error(e)
-        else:
-            try:
-                dialog = DialogModel(id=agent["id"], name=agent["name"], description=agent["description"],
-                                     icon=agent["icon"], tenant_id=user.id if user else "", dialog_type="3",
-                                     agent_id=agent["id"])
-                db.add(dialog)
-                db.commit()
-                db.refresh(dialog)
-            except Exception as e:
-                print(e)
-                db.rollback()
\ No newline at end of file
+    try:
+        config = {}
+        now_account  =[]
+        with open(os.path.join(ENV_CONF_PATH, "account.yaml"), 'r', encoding='utf-8') as file:
+            # 鍔犺浇JSON鏁版嵁
+            config = yaml.safe_load(file)
+        account_list = db.query(UserTokenModel).all()
+        for account in account_list:
+            if account.id in config:
+
+                if account.account != config[account.id]["account"] or account.password != config[account.id]["password"]:
+                    db.query(UserTokenModel).filter_by(id=account.id).update({"account": config[account.id]["account"],
+                                                                              "password": config[account.id]["password"],
+                                                                              "access_token": ""
+                                                                              })
+                now_account.append(account.id)
+            else:
+                db.query(UserTokenModel).filter_by(id=account.id).delete()
+        for k, v in config.items():
+            if k not in now_account:
+                new_account = UserTokenModel(id=k, account=v["account"], password=v["password"])
+                db.add(new_account)
+        db.commit()
+    except Exception as e:
+        print(e)
+        db.rollback()
\ No newline at end of file

--
Gitblit v1.8.0