统一冠字号为J0+8位数字
This commit is contained in:
parent
7db66d5a7b
commit
8189903d13
|
|
@ -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 # 无法矫正
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue