feat: TransBorrow 归还人 to_dict 自动把数字 user id 映射为用户名(历史记录不再显示数字,lru_cache 防 N+1)
This commit is contained in:
@ -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 '',
|
||||
|
||||
Reference in New Issue
Block a user