Explorar o código

Merge remote-tracking branch 'origin/dev' into server_test

KCY hai 4 semanas
pai
achega
03bd3e1ede

+ 3 - 2
shudao-chat-py/models/points.py

@@ -1,4 +1,5 @@
-from sqlalchemy import Column, Integer, String, BigInteger, Text, Index
+from sqlalchemy import Column, Integer, String, BigInteger, Text, TIMESTAMP, Index
+from sqlalchemy.sql import func
 from database import Base
 
 
@@ -12,4 +13,4 @@ class PointsConsumptionLog(Base):
     file_url = Column(Text)                                    # 允许为空
     points_consumed = Column(Integer, nullable=False, default=10)  # 默认10积分
     balance_after = Column(Integer, nullable=False)            # 消费后余额,不允许为空
-    created_at = Column(Integer, default=0)                    # Unix时间戳
+    created_at = Column(TIMESTAMP, server_default=func.current_timestamp())  # 数据库timestamp类型

+ 2 - 9
shudao-chat-py/routers/points.py

@@ -5,7 +5,6 @@ from models.user_data import UserData
 from models.total import User
 from models.points import PointsConsumptionLog
 from utils.logger import logger
-import time
 
 router = APIRouter()
 
@@ -92,15 +91,12 @@ async def consume_points(request: Request):
             new_balance = current_points - REQUIRED_POINTS
             user.points = new_balance
 
-            now = int(time.time())
             log = PointsConsumptionLog(
                 user_id=str(user.id),
                 file_name=file_name,
                 file_url=file_url,
                 points_consumed=REQUIRED_POINTS,
-                balance_after=new_balance,
-                created_at=now,
-                updated_at=now,
+                balance_after=new_balance
             )
             db.add(log)
             db.commit()
@@ -136,15 +132,12 @@ async def consume_points(request: Request):
         new_balance = current_points - REQUIRED_POINTS
         user_data.points = new_balance
 
-        now = int(time.time())
         log = PointsConsumptionLog(
             user_id=user_info.account,
             file_name=file_name,
             file_url=file_url,
             points_consumed=REQUIRED_POINTS,
-            balance_after=new_balance,
-            created_at=now,
-            updated_at=now,
+            balance_after=new_balance
         )
         db.add(log)
         db.commit()

+ 8 - 2
shudao-chat-py/routers/scene.py

@@ -93,13 +93,18 @@ def _build_record_view(record: RecognitionRecord, db: Session = None):
     if db and display_labels:
         try:
             from models.scene import SecondScene, ThirdScene
+            from utils.label_translator import translate_scenes_for_query
             
             for label in display_labels:
-                # Find matching secondary scene
+                # Translate sub-secondary scene to secondary scene for database query
+                # e.g., "加油机" -> "加油设施_附属场地"
+                query_label = translate_scenes_for_query([label])[0] if label else label
+                
+                # Find matching secondary scene using translated name
                 second_scene = (
                     db.query(SecondScene)
                     .filter(
-                        SecondScene.second_scene_name == label,
+                        SecondScene.second_scene_name == query_label,
                         SecondScene.is_deleted == 0,
                     )
                     .first()
@@ -117,6 +122,7 @@ def _build_record_view(record: RecognitionRecord, db: Session = None):
                     )
                     
                     if third_scene_records:
+                        # Use original label as key for frontend matching
                         element_hazards[label] = [
                             ts.third_scene_name for ts in third_scene_records
                         ]

+ 32 - 24
shudao-chat-py/routers/total.py

@@ -9,6 +9,7 @@ from models.chat import AIMessage
 from models.user_data import UserData
 from services.oss_service import oss_service
 from utils.crypto import decrypt_url
+from utils.config import get_proxy_url
 import time
 import httpx
 
@@ -28,41 +29,48 @@ async def get_recommend_question(db: Session = Depends(get_db)):
 
 @router.get("/get_policy_file")
 async def get_policy_file(
-    policy_type: Optional[int] = None,
+    policy_type: Optional[str] = None,
     page: int = 1,
-    page_size: int = 20,
+    pageSize: int = 20,
+    search: str = "",
     db: Session = Depends(get_db)
 ):
     """获取策略文件列表"""
     query = db.query(PolicyFile).filter(PolicyFile.is_deleted == 0)
 
-    if policy_type is not None and policy_type != 0:
-        query = query.filter(PolicyFile.policy_type == policy_type)
-
-    total = query.count()
-
-    offset = (page - 1) * page_size
+    # 只有当policy_type有效且不为0或空字符串时才添加类型过滤
+    if policy_type and policy_type != "" and policy_type != "0":
+        try:
+            policy_type_int = int(policy_type)
+            query = query.filter(PolicyFile.policy_type == policy_type_int)
+        except ValueError:
+            pass  # 忽略无效的类型值
+    
+    if search:
+        query = query.filter(PolicyFile.policy_name.like(f"%{search}%"))
+
+    offset = (page - 1) * pageSize
     files = query.order_by(PolicyFile.updated_at.desc()).offset(
-        offset).limit(page_size).all()
+        offset).limit(pageSize).all()
 
     return {
         "statusCode": 200,
         "msg": "success",
-        "data": {
-            "total": total,
-            "items": [
-                {
-                    "id": f.id,
-                    "policy_name": f.policy_name,
-                    "policy_file_url": f.policy_file_url,
-                    "policy_type": f.policy_type,
-                    "file_type": f.file_type,
-                    "view_count": f.view_count,
-                    "created_at": f.created_at
-                }
-                for f in files
-            ]
-        }
+        "data": [
+            {
+                "id": f.id,
+                "policy_name": f.policy_name,
+                "policy_file_url": get_proxy_url(f.policy_file_url),
+                "policy_type": f.policy_type,
+                "file_type": f.file_type,
+                "file_tag": getattr(f, 'file_tag', ''),
+                "publish_time": getattr(f, 'publish_time', f.created_at),
+                "view_count": f.view_count,
+                "created_at": f.created_at,
+                "updated_at": f.updated_at
+            }
+            for f in files
+        ]
     }
 
 

+ 4 - 2
shudao-chat-py/utils/config.py

@@ -204,7 +204,9 @@ def get_base_url() -> str:
 
 
 def get_proxy_url(original_url: str) -> str:
-    """将原始URL转换为代理URL"""
+    """将原始URL转换为代理URL(加密)"""
     if not original_url:
         return ""
-    return f"{settings.base_url}/apiv1/oss/parse?url={original_url}"
+    from .crypto import encrypt_url
+    encrypted = encrypt_url(original_url)
+    return f"{settings.base_url}/apiv1/oss/parse?url={encrypted}"