From 383c21560fcb7012cd2e5b15d559e46d038c87b0 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 25 二月 2025 09:54:43 +0800
Subject: [PATCH] 增加设备分析

---
 app/api/excel.py |   58 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/app/api/excel.py b/app/api/excel.py
index 2c2fedd..1c0ccf4 100644
--- a/app/api/excel.py
+++ b/app/api/excel.py
@@ -1,12 +1,12 @@
 import random
 import string
 
-from fastapi import APIRouter, File, UploadFile, Form, BackgroundTasks, Depends
+from fastapi import APIRouter, File, UploadFile, Form, BackgroundTasks, Depends, Request, WebSocket
 from fastapi.responses import JSONResponse, FileResponse
 from sqlalchemy.orm import Session
-from starlette.websockets import WebSocket
+# from starlette.websockets import WebSocket
 
-from app.api import get_current_user, get_current_user_websocket
+from app.api import get_current_user, get_current_user_websocket, Response
 from app.models import UserModel, AgentType
 from app.models.base_model import get_db
 from app.service.session import SessionService
@@ -52,25 +52,26 @@
     return prefix + random_part
 
 
-def db_create_session(db: Session, user_id: str):
+def db_create_session(db: Session, user_id: str, message:str, upload_filenames: list):
     db_id = generate_db_id()
     session = SessionService(db).create_session(
         db_id,
-        "鍚堝苟Excel",
+        message,
         "basic_excel_merge",
         AgentType.BASIC,
-        int(user_id)
+        int(user_id),
+        {"role": "user", "content": message, "upload_filenames": upload_filenames}
     )
     return session
 
 
-@router.post('/excel/upload')
+@router.post('/excel/upload', response_model=Response)
 async def upload_file(files: list[UploadFile] = File(...), current_user: UserModel = Depends(get_current_user)):
     user_id = str(current_user.id)
     if not any(file.filename for file in files):
-        return JSONResponse(content={"error": "娌℃湁鏂囦欢閮ㄥ垎"}, status_code=400)
+        return Response(code=400, msg="娌℃湁鏂囦欢閮ㄥ垎", data={})
     if not user_id:
-        return JSONResponse(content={"error": "缂哄皯鍙傛暟user_id"}, status_code=400)
+        return Response(code=400, msg="缂哄皯鍙傛暟user_id", data={})
     user_source = user_file_path(user_id, SOURCE_FILES_PATH)
     user_excel = EXCEL_FILES_PATH
 
@@ -80,16 +81,14 @@
 
     save_path_list = []
     for file in files:
-        if file.filename == '':
-            return JSONResponse(content={"error": "娌℃湁閫夋嫨鏂囦欢"}, status_code=400)
         if file and allowed_file(file.filename):
             save_path = os.path.join(user_source, file.filename)
             with open(save_path, 'wb') as buffer:
                 shutil.copyfileobj(file.file, buffer)
             save_path_list.append(save_path)
         else:
-            return JSONResponse(content={"error": "涓嶅厑璁哥殑鏂囦欢绫诲瀷"}, status_code=400)
-    return JSONResponse(content={"code": 200, "msg": "", "data": {}}, status_code=200)
+            return Response(code=400, msg="涓嶅厑璁哥殑鏂囦欢绫诲瀷", data={})
+    return Response(code=200, msg="涓婁紶鎴愬姛", data={})
 
 
 # ws://localhost:9201/api/document/ws/excel
@@ -104,32 +103,41 @@
     user_excel = EXCEL_FILES_PATH
     create_dir_if_not_exists(user_source)
     create_dir_if_not_exists(user_excel)
-
     while True:
-        data = await websocket.receive_text()
+        # data = await websocket.receive_text()git
+        receive_message = await websocket.receive_json()
         try:
-            if data == "\"鍚堝苟Excel\"":
+            if receive_message.get("message") == "鍚堝苟Excel":
+                upload_filenames = receive_message.get('upload_filenames', [])
                 merge_file = run_conformity(user_source, user_excel)
                 if merge_file is not None:
 
                     await websocket.send_json({
-                        "message": "鏂囨。鍚堝苟鎴愬姛锛�",
                         "type": "stream",
-                        "file_name": f"{merge_file}.xlsx",
-                        "download_url": f"./api/document/download/{merge_file}.xlsx"
+                        "files": [
+                            {
+                                "file_name": "Excel",
+                                "file_url": f"./api/document/download/{merge_file}.xlsx?file_type=excel",
+                            }
+                        ]
                     })
                     await websocket.send_json({
-                        "message": "鏂囨。鍚堝苟鎴愬姛锛�",
+                        "message": "鍚堝苟鎴愬姛",
                         "type": "close",
                     })
                     # 鍒涘缓浼氳瘽璁板綍
-                    session = db_create_session(db, user_id)
+                    session = db_create_session(db, user_id, receive_message.get("message"), upload_filenames)
                     # 鏇存柊浼氳瘽璁板綍
                     if session:
                         session_id = session.id
                         new_message = {
-                            "role": "user",
-                            "download_url": f"./api/document/download/{merge_file}.xlsx"
+                            "role": "assistant",
+                            "content": {
+                                "message": "\u5408\u5e76\u6210\u529f",
+                                "type": "message",
+                                "file_name": "Excel",
+                                "file_url": f"/api/document/download/{merge_file}.xlsx?file_type=excel"
+                            }
                         }
                         session_service = SessionService(db)
                         session_service.update_session(session_id, message=new_message)
@@ -137,8 +145,8 @@
                     await websocket.send_json({"error": "鍚堝苟澶辫触", "type": "stream", "files": []})
                     await websocket.close()
             else:
-                print(f"Received data: {data}")
-                await websocket.send_json({"error": "鏈煡鎸囦护", "data": str(data)})
+                print(f"Received data: {receive_message.get('message')}")
+                await websocket.send_json({"error": "鏈煡鎸囦护", "data": str(receive_message.get('message'))})
                 await websocket.close()
         except Exception as e:
             await websocket.send_json({"error": str(e)})

--
Gitblit v1.8.0