fix(no_generator): 编号 MAX 检索改长度优先排序,破解位宽进位后字典序失效

跨位宽(-9999→-10000)后纯字典序反转,MAX 恒取旧值导致当日建单
持续撞唯一键;同前缀下长度大即数值大,同级同长时字典序与数值一致。
新增跨位宽/同宽/空表三个集成用例。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
liuwenxinandClaude Code committed 2026-10-14 14:09:30 +08:00
1 parent d8dba27fb2
commit 64c57e9141
2 files changed
+87 -2

No files matched your search

+5 -2
View File
@@ -8,7 +8,7 @@
from datetime import datetime
from sqlalchemy import select
from sqlalchemy import func, select
from sqlalchemy.orm import Session
@@ -35,10 +35,13 @@ def next_no(
"""
like_prefix = today_prefix(prefix)
column = getattr(model, column_name)
# 排序按「长度优先、同级字典序」:同前缀下序号位数增多(9999→10000)后
# 纯字典序会反转("-10000" < "-9999",'1'<'9'),seq 将恒重算出同一号码、
# 每笔建单都撞唯一键 —— 长度大即数值大,同级同长时字典序与数值序一致
max_no = session.execute(
select(column)
.where(column.like(f"{like_prefix}%"))
.order_by(column.desc())
.order_by(func.length(column).desc(), column.desc())
.limit(1)
.with_for_update()
).scalar()