From 0078ac6ffa486c481c26b7c0b033ff9664096a7f Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 11 二月 2025 16:38:10 +0800
Subject: [PATCH] 报告生成增加

---
 app/service/v2/initialize_data.py |  105 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 93 insertions(+), 12 deletions(-)

diff --git a/app/service/v2/initialize_data.py b/app/service/v2/initialize_data.py
index ce859dd..512a2d3 100644
--- a/app/service/v2/initialize_data.py
+++ b/app/service/v2/initialize_data.py
@@ -1,7 +1,13 @@
 import json
 import time
+import os
+
+import yaml
+from passlib.context import CryptContext
+
 from Log import logger
-from app.config.const import DIFY
+# from app.api import pwd_context
+from app.config.const import DIFY, ENV_CONF_PATH, RAGFLOW
 from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \
     cipher_suite
 from app.service.auth import UserAppDao
@@ -13,10 +19,12 @@
 from app.config.config import settings
 from app.utils.password_handle import generate_password
 
+pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
+
 
 async def dialog_menu_sync(db):
     menu_list = []
-    with open("env_conf/menu_conf.json", 'r', encoding='utf-8') as file:
+    with open(os.path.join(ENV_CONF_PATH, "menu_conf.json") , 'r', encoding='utf-8') as file:
         # 鍔犺浇JSON鏁版嵁
         data = json.load(file)
         menu_list = data.get("data", [])
@@ -36,6 +44,47 @@
         menu_obj = WebMenuModel(**menu)
         db.add(menu_obj)
     db.commit()
+
+
+async def create_menu_sync(db):
+    # json_file_path = "env_conf/menu_conf.json.template"
+    json_file_path = os.path.join(ENV_CONF_PATH, "menu_conf.json.template")
+    with open(json_file_path, 'r', encoding='utf-8') as file:
+        json_data = json.load(file).get("data", [])
+        # for menu in json_data:
+        #     menu['dialog'].clear()
+    dialogs = db.query(DialogModel).all()
+
+    dialog_dict = {}
+    for dialog in dialogs:
+        if dialog.name not in dialog_dict:
+            dialog_dict[dialog.name] = []
+        dialog_dict[dialog.name].append({
+                    'id': dialog.id,
+                    'chat_id': dialog.id,
+                    'chat_type': '',
+                    'agentType': dialog.dialog_type
+                })
+
+    for menu in json_data:
+        # if menu['title'] in dialog_dict:
+        #     for dialog in dialog_dict[menu['title']]:
+        #         new_dialog_item = {
+        #             'id': dialog.id,
+        #             'chat_id': dialog.id,
+        #             'chat_type': '',
+        #             'agentType': dialog.dialog_type
+        #         }
+        menu['dialog']= dialog_dict.get(menu['title'], [])
+    json_data = {"data": json_data}
+    new_file_name = f"menu_conf.json.template"
+    new_file_path = os.path.join(os.path.dirname(json_file_path), new_file_name)
+    with open(new_file_path, 'w', encoding='utf-8') as new_file:
+        json.dump(json_data, new_file, ensure_ascii=False, indent=4)
+    return {
+        "file_name": new_file_name,
+        "json_data": json_data
+    }
 
 
 async def default_group_sync(db):
@@ -66,7 +115,7 @@
 
 async def app_register_sync(db):
     app_dict = {}
-    with open("env_conf/app_register_conf.json", 'r', encoding='utf-8') as file:
+    with open(os.path.join(ENV_CONF_PATH, "app_register_conf.json"), 'r', encoding='utf-8') as file:
         # 鍔犺浇JSON鏁版嵁
         app_dict = json.load(file)
         try:
@@ -78,7 +127,7 @@
 
 async def basic_agent_sync(db):
     agent_list = []
-    with open("env_conf/default_agent_conf.json", 'r', encoding='utf-8') as file:
+    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", [])
@@ -96,8 +145,7 @@
         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"])
+                                     icon=agent["icon"], tenant_id=user.id if user else "", dialog_type=agent["dialogType"], mode=agent["mode"])
                 db.add(dialog)
                 db.commit()
                 db.refresh(dialog)
@@ -114,6 +162,7 @@
     register_dict = {}
     token = ""
     app_password = await generate_password(10)
+    crypt_password = UserAppModel.encrypted_password(app_password)
     for app in app_register:
         if app["id"] == 'ragflow_app':
             user_rag_app = db.query(UserAppModel).filter(UserAppModel.user_id == userid,
@@ -127,7 +176,7 @@
                 app_name = register_info.get("name")
                 app_id = register_info.get("id")
                 app_email = register_info.get("email")
-                await save_db(db, app_name, user.password, app_email, user.id, app_id, "ragflow_app")
+                await save_db(db, app_name, crypt_password, app_email, user.id, app_id, "ragflow_app")
         elif app["id"] == 'bisheng_app':
             user_bs_app = db.query(UserAppModel).filter(UserAppModel.user_id == userid,
                                                         UserAppModel.app_type == 'bisheng_app').all()
@@ -140,7 +189,7 @@
                 app_name = register_info.get("name")
                 app_id = register_info.get("id")
                 app_email = register_info.get("email")
-                await save_db(db, app_name, user.password, app_email, user.id, app_id, "bisheng_app")
+                await save_db(db, app_name, crypt_password, app_email, user.id, app_id, "bisheng_app")
         elif app["id"] == 'dify_app':
             user_df_app = db.query(UserAppModel).filter(UserAppModel.user_id == userid,
                                                         UserAppModel.app_type == 'dify_app').all()
@@ -156,9 +205,7 @@
                 app_name = register_info.get("name")
                 app_id = register_info.get("id")
                 app_email = register_info.get("email")
-                df_password = await generate_password(10)
-                password = cipher_suite.encrypt(df_password.encode("utf-8")).decode("utf-8")
-                await save_db(db, app_name, password, app_email, user.id, app_id, "dify_app")
+                await save_db(db, app_name, crypt_password, app_email, user.id, app_id, "dify_app")
         else:
             raise Exception("鏈煡娉ㄥ唽搴旂敤---")
 
@@ -178,4 +225,38 @@
     user_id = await user_app_dao.insert_user_app_data(username, password, email, user_id, app_id, app_type)
     if not user_id:
         raise Exception("Failed to register with app")
-    print({"msg": "User registered successfully", "userFlag": user_id})
\ No newline at end of file
+    print({"msg": "User registered successfully", "userFlag": user_id})
+
+
+async def admin_user_sync(db):
+    try:
+        config = {}
+        with open(os.path.join(ENV_CONF_PATH, "admin.yaml"), 'r', encoding='utf-8') as file:
+            # 鍔犺浇JSON鏁版嵁
+            config = yaml.safe_load(file)
+        # print(config)
+        db_user = db.query(UserModel).filter(UserModel.username == config["smart_server"]["account"]).first()
+        if db_user:
+            print("admin_user_sync: 鐢ㄦ埛宸茬粡瀛樺湪锛�")
+            return
+        register_dict = {}
+
+        for app in [RAGFLOW, DIFY]:
+            register_dict[app] = {"id": config[app].get("id", "123"), "name": config[app]["account"],
+                                        "pwd":config[app]["password"],
+                                        "email": config[app]["account"]}
+
+        # 瀛樺偍鐢ㄦ埛淇℃伅
+        hashed_password = pwd_context.hash(config["smart_server"]["password"])
+        user_model = UserModel(username=config["smart_server"]["account"], hashed_password=hashed_password, email="",
+                               phone="", login_name="", sync_flag="", creator=0, permission="admin")
+        db.add(user_model)
+        db.commit()
+        db.refresh(user_model)
+        u_id = user_model.id
+        user_app_dao = UserAppDao(db)
+        for k, v in register_dict.items():
+            await user_app_dao.update_and_insert_data(v.get("name"), user_model.encrypted_password(v.get("pwd")), v.get("email"), u_id, str(v.get("id")), k)
+    except Exception as e:
+        print(e)
+        db.rollback()
\ No newline at end of file

--
Gitblit v1.8.0