版本变更V3.31添加识图功能

This commit is contained in:
dxc
2026-05-22 10:59:39 +08:00
parent 465452ef46
commit 8c635d6afe
8 changed files with 154 additions and 143 deletions

View File

@ -1048,14 +1048,15 @@ class MaterialBaseService:
@staticmethod
def get_latest_specs():
"""
获取所有规格型号的最大连号,按连续区间分组返回
获取所有规格型号的分组统计,按规则聚合后返回
- 前缀统一大写处理
- 只有数字完全连续N, N+1, N+2...)才认定为同一组
- 数字不连续时断开,形成新组
- 按每组数量降序排列
- 返回每个连续区间的最大值
- 匹配模式:(前缀)(单数字二级分类位)(纯数字部分),如 OPT12046 -> OPT, 1, 2046
- OPT 系列:使用 前缀+二级分类位 作为分组 Key如 OPT1, OPT2
- 其他前缀:直接使用前缀作为分组 Key
- 返回每个分组的数量、最大号、完整规格名
"""
import re
from collections import defaultdict
# 1. 查询所有不为空的规格型号
specs = MaterialBase.query.filter(
@ -1063,8 +1064,8 @@ class MaterialBaseService:
MaterialBase.spec_model != ''
).all()
# 2. 解析并收集所有有效的 (prefix, num, original_spec)
parsed = []
# 2. 按分组收集所有数字
groups = defaultdict(list)
for material in specs:
spec = material.spec_model
@ -1072,72 +1073,31 @@ class MaterialBaseService:
continue
base_spec = spec.split('/')[0]
match = re.match(r'^([A-Za-z]+)(\d+)$', base_spec)
match = re.match(r'^([A-Za-z]+)(\d)(\d+)$', base_spec)
if not match:
continue
prefix, num_str = match.groups()
prefix, sub_cat, num_str = match.groups()
prefix = prefix.upper()
num = int(num_str)
parsed.append((prefix, num, spec))
# OPT 系列使用 前缀+单数字二级分类 作为 Key
key = f"{prefix}{sub_cat}" if prefix == 'OPT' else prefix
groups[key].append((num, spec))
# 3. 先按 prefix 升序,再按 num 升序排序
parsed.sort(key=lambda x: (x[0], x[1]))
# 4. 遍历切分连续区间
# 核心逻辑:当 current_num != prev_num + 1 时,断开形成新组
intervals = []
current_prefix = None
current_start = None
current_end = None
current_last_spec = None
for prefix, num, spec in parsed:
if current_prefix is None:
current_prefix = prefix
current_start = num
current_end = num
current_last_spec = spec
elif prefix == current_prefix and num == current_end + 1:
current_end = num
current_last_spec = spec
else:
intervals.append({
'prefix': current_prefix,
'start': current_start,
'end': current_end,
'count': current_end - current_start + 1,
'latest': current_last_spec
})
current_prefix = prefix
current_start = num
current_end = num
current_last_spec = spec
if current_prefix is not None:
intervals.append({
'prefix': current_prefix,
'start': current_start,
'end': current_end,
'count': current_end - current_start + 1,
'latest': current_last_spec
})
# 5. 按每组数量降序排列,再按前缀升序
intervals.sort(key=lambda x: (-x['count'], x['prefix']))
# 6. 构建返回结果
# 3. 生成展示用的统计数据
result = []
for item in intervals:
prefix = item['prefix']
start = item['start']
end = item['end']
for key, items in groups.items():
sorted_items = sorted(items, key=lambda x: x[0])
max_num, max_spec = sorted_items[-1]
result.append({
"group": f"{prefix}({start}-{end})",
"count": item['count'],
"latest": item['latest']
'group': key,
'count': len(sorted_items),
'latest': max_spec,
'max_num': max_num
})
# 4. 按数量降序,再按分组名升序排列
result.sort(key=lambda x: (-x['count'], x['group']))
return result