统一冠字号为J0+8位数字

This commit is contained in:
甲辰生产 2026-04-12 01:01:30 +08:00
parent 7db66d5a7b
commit 8189903d13
1 changed files with 37 additions and 27 deletions

View File

@ -452,21 +452,31 @@ def create_information(
db: Session = Depends(get_db)
):
"""发布资讯"""
# 验证并矫正冠字号:J0 + 8位数字 = 共10位
# 验证并矫正冠字号:必须是J0开头 + 8位数字 = 共10位
if data.title:
import re
# 提取冠字号J0开头后面跟数字
match = re.search(r'J0(\d+)', data.title)
if match:
num = match.group(1)
# 必须是10位J0 + 8位数字
# 必须是8位数字
if len(num) > 8:
# 多于8位取前8位
num = num[:8]
elif len(num) < 8:
# 少于8位前面补0
num = num.zfill(8)
# 重新构建title
# 重新构建title确保是J0开头
original = match.group(0)
data.title = data.title.replace(original, 'J0' + num, 1)
else:
# 如果不是J0开头尝试转换
other_match = re.search(r'J([1-9]\d{0,8})', data.title)
if other_match:
# 非J0开头的尝试补0变成J0开头
num = other_match.group(1).zfill(8)[:8]
original = other_match.group(0)
data.title = data.title.replace(original, 'J0' + num, 1)
# 生成行情编号:日期 + 5位自然数从00001开始
deal_no = None
if data.info_type == 'deal':
@ -1292,30 +1302,30 @@ async def batch_parse_deals(text: str = Body(..., embed=True)):
# 尝试直接解析
data = json.loads(content.strip())
# 对AI返回的数据进行冠字号矫正
# 对AI返回的数据进行冠字号矫正 - 确保J0开头+8位数字
def normalize_serial_ai(num_str):
"""矫正冠字号J0开头9位数字"""
"""矫正冠字号J0开头8位数字共10位"""
if not num_str.startswith('J0'):
return None
num = num_str[2:] # 去掉J0
diff = 9 - len(num)
# 位数正好9位,不需要矫正
diff = 8 - len(num)
# 位数正好8位,不需要矫正
if diff == 0:
return num_str
elif diff == -1:
# 多1位取前5位+最后4位
# 多1位取前4位+最后4位
if len(num) >= 4:
result = num[:5] + num[-4:]
if len(result) == 9:
result = num[:4] + num[-4:]
if len(result) == 8:
return 'J0' + result
elif diff == 1:
# 少1位J0 + 0 + 数字
return 'J0' + '0' + num
elif diff == -2:
# 多2位取前5位+最后4位
if len(num) >= 5:
result = num[:5] + num[-4:]
if len(result) == 9:
# 多2位取前4位+最后4位
if len(num) >= 4:
result = num[:4] + num[-4:]
if len(result) == 8:
return 'J0' + result
elif diff == 2:
# 少2位J0 + 00 + 数字
@ -1395,33 +1405,33 @@ def parse_deals_locally(text: str, default_packaging: str = '', default_date: st
if not serial_match:
continue
# 冠字号矫正函数 - J0开头的情况
# 冠字号矫正函数 - 确保是J0开头+8位数字
def normalize_serial(num_str):
"""矫正冠字号J0开头9位数字"""
"""矫正冠字号J0开头8位数字共10位"""
num = num_str
diff = 9 - len(num)
diff = 8 - len(num)
# 位数正好9位,不需要矫正
# 位数正好8位,不需要矫正
if diff == 0:
return 'J0' + num
# 位数不对才需要矫正
elif diff == -1:
# 多1位10位数字→ 取前5位+最后4位
# 多1位9位数字→ 取前4位+最后4位
if len(num) >= 4:
result = num[:5] + num[-4:]
if len(result) == 9:
result = num[:4] + num[-4:]
if len(result) == 8:
return 'J0' + result
elif diff == 1:
# 少1位8位数字)→ J0 + 0 + 数字
# 少1位7位数字)→ J0 + 0 + 数字
return 'J0' + '0' + num
elif diff == -2:
# 多2位11位数字→ 取前5位+最后4位
if len(num) >= 5:
result = num[:5] + num[-4:]
if len(result) == 9:
# 多2位10位数字→ 取前4位+最后4位
if len(num) >= 4:
result = num[:4] + num[-4:]
if len(result) == 8:
return 'J0' + result
elif diff == 2:
# 少2位7位数字)→ J0 + 00 + 数字
# 少2位6位数字)→ J0 + 00 + 数字
return 'J0' + '00' + num
return None # 无法矫正