From 8189903d134604c28047eaec7093199d46ff663b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B2=E8=BE=B0=E7=94=9F=E4=BA=A7?= Date: Sun, 12 Apr 2026 01:01:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=86=A0=E5=AD=97=E5=8F=B7?= =?UTF-8?q?=E4=B8=BAJ0+8=E4=BD=8D=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/routers/information.py | 64 +++++++++++++++++------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/backend/app/routers/information.py b/backend/app/routers/information.py index 88863b9..b4d71cd 100644 --- a/backend/app/routers/information.py +++ b/backend/app/routers/information.py @@ -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 # 无法矫正