v1.2.53 - 修复编号并发问题+images字段映射

This commit is contained in:
甲辰生产 2026-04-08 08:13:04 +08:00
parent 2510f42b68
commit 404b966ba9
1 changed files with 21 additions and 16 deletions

View File

@ -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,23 +65,27 @@ 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):
if result and result[0]:
try:
num = int(code)
if num > max_num:
max_num = num
max_num = int(result[0])
except (ValueError, TypeError):
pass