From b274ed1ed13bc4e6e5dee350a35d2f46041cfcf3 Mon Sep 17 00:00:00 2001 From: yueli Date: Fri, 4 Sep 2026 16:12:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20TransBorrow=20=E5=BD=92=E8=BF=98?= =?UTF-8?q?=E4=BA=BA=20to=5Fdict=20=E8=87=AA=E5=8A=A8=E6=8A=8A=E6=95=B0?= =?UTF-8?q?=E5=AD=97=20user=20id=20=E6=98=A0=E5=B0=84=E4=B8=BA=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=90=8D=EF=BC=88=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E6=98=BE=E7=A4=BA=E6=95=B0=E5=AD=97=EF=BC=8C?= =?UTF-8?q?lru=5Fcache=20=E9=98=B2=20N+1=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inventory-backend/app/models/transaction.py | 22 ++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/inventory-backend/app/models/transaction.py b/inventory-backend/app/models/transaction.py index bc4e79f..ab0c614 100644 --- a/inventory-backend/app/models/transaction.py +++ b/inventory-backend/app/models/transaction.py @@ -1,6 +1,26 @@ from app.extensions import db, beijing_time from datetime import datetime from sqlalchemy import func +from functools import lru_cache + + +@lru_cache(maxsize=512) +def _borrow_user_name(uid): + """按用户 ID 取用户名(带缓存,避免列表页 N+1)""" + try: + from app.models.system import SysUser + u = SysUser.query.get(uid) + return u.username if u else None + except Exception: + return None + + +def _display_borrow_operator(op): + """归还人展示映射:历史存的是数字 user id → 映射为用户名;新数据存姓名则原样返回""" + if op and str(op).strip().isdigit(): + mapped = _borrow_user_name(int(op)) + return mapped if mapped else op + return op class TransBorrow(db.Model): @@ -48,7 +68,7 @@ class TransBorrow(db.Model): 'expected_return_time': self.expected_return_time.strftime('%Y-%m-%d %H:%M') if self.expected_return_time else None, 'is_returned': self.is_returned, 'return_time': self.return_time.strftime('%Y-%m-%d %H:%M') if self.return_time else None, - 'return_operator': self.return_operator, + 'return_operator': _display_borrow_operator(self.return_operator), 'return_signature': self.return_signature, 'return_location': self.return_location, 'location': self.location or '',