From 404b966ba9fffdb90b59bc64f0b511108207839e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B2=E8=BE=B0=E7=94=9F=E4=BA=A7?= Date: Wed, 8 Apr 2026 08:13:04 +0800 Subject: [PATCH] =?UTF-8?q?v1.2.53=20-=20=E4=BF=AE=E5=A4=8D=E7=BC=96?= =?UTF-8?q?=E5=8F=B7=E5=B9=B6=E5=8F=91=E9=97=AE=E9=A2=98+images=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=98=A0=E5=B0=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/routers/collections.py | 37 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/backend/app/routers/collections.py b/backend/app/routers/collections.py index 9c94aeb..51b330e 100644 --- a/backend/app/routers/collections.py +++ b/backend/app/routers/collections.py @@ -57,6 +57,7 @@ def to_camel_case(data: dict) -> dict: 'f05_43_repair_fee': 'repairFee', 'f05_44_grading_fee': 'gradingFee', 'f06_50_purpose': 'purpose', + 'images': 'images', } return {mapping.get(k, k): v for k, v in data.items()} @@ -64,25 +65,29 @@ def to_camel_case(data: dict) -> dict: # 编码生成函数 def generate_code(version: str, user_id: str, db: Session) -> str: - """自动生成藏品编号 - 按用户独立编码""" - import re + """自动生成藏品编号 - 按用户独立编码,使用行锁防止并发冲突""" + from sqlalchemy import text - # 查询当前用户的非空编码(不与其他用户混算) - user_codes = db.query(Collection.f01_02_code).filter( - Collection.f01_02_code.isnot(None), - Collection.f99_91_user_id == user_id - ).all() + # 使用 FOR UPDATE 行锁防止并发冲突 + result = db.execute( + text(""" + SELECT f01_02_code FROM collections + WHERE f99_91_user_id = :user_id + AND f01_02_code IS NOT NULL + AND f01_02_code ~ '^\\d{4,5}$' + ORDER BY f01_02_code::int DESC + LIMIT 1 + FOR UPDATE + """), + {"user_id": user_id} + ).fetchone() max_num = 0 - for (code,) in user_codes: - # 处理纯数字编码(支持4位和5位) - if re.match(r'^\d{4,5}$', code): - try: - num = int(code) - if num > max_num: - max_num = num - except (ValueError, TypeError): - pass + if result and result[0]: + try: + max_num = int(result[0]) + except (ValueError, TypeError): + pass # 当前用户最大号 +1 next_num = max_num + 1