"""Convert a reviewed CBB list using the application's unchanged BOM writer. This is an offline conversion utility. It never opens CAD or changes task data. M is the proposed replacement; V is retained as history, never a silent fallback. """ from __future__ import annotations import argparse from collections import defaultdict import csv import json from pathlib import Path import re import sys sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'src')) from openpyxl import load_workbook from openpyxl.comments import Comment from dx_productivity.trial_task_center.bom_plan import write_bom_template, read_bom_records from dx_productivity.trial_task_center.project_tasks import scan_project_directory PART = re.compile(r'(? 1: for entry in group: entry['duplicate'] = True entry['record']['enabled'] = False message = '重复工程/旧料号/位号,待核对' if message not in entry['comment']: entry['comment'] += '\n' + message entry['audit'][4] += '; ' + message mapped = [entry for entry in entries if entry['record']['project_path'] and not entry['duplicate']] output_dir.mkdir(parents=True, exist_ok=True) full = output_dir / 'CBB_转换模板_全量待核对.xlsx' ready = output_dir / 'CBB_转换模板_已匹配工程.xlsx' audit_path = output_dir / 'CBB_逐行转换核对.csv' _export(full, entries) _export(ready, mapped) with audit_path.open('w', encoding='utf-8-sig', newline='') as stream: writer = csv.writer(stream) writer.writerow(['来源表', '来源行', '匹配工程路径', '转换状态', '核对说明', *headers]) writer.writerows([_csv_value(value) for value in row] for row in audit) report = {'source_rows': len(audit), 'project_labels': len(names), 'matched_project_labels': len(mapped_names), 'converted_rows': len(entries), 'mapped_rows': len(mapped), 'mapped_enabled_rows': sum(e['record']['enabled'] for e in mapped), 'conflict_rows': conflicts, 'review_only_rows': len(audit) - len(entries), 'duplicate_candidate_rows': sum(e['duplicate'] for e in entries), 'unmapped_projects': sorted(names - mapped_names), 'basis': 'M column; V retained for comparison', 'completion_marker_filtered': False, 'template_headers_changed': False, 'cad_started': False, 'task_database_changed': False, 'full_file': str(full.resolve()), 'mapped_file': str(ready.resolve()), 'audit_file': str(audit_path.resolve())} (output_dir / 'conversion-report.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8') return report if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('source', type=Path) parser.add_argument('--project-root', type=Path, required=True) parser.add_argument('--output-dir', type=Path, required=True) args = parser.parse_args() print(json.dumps(convert(args.source, scan_project_directory(args.project_root), args.output_dir), ensure_ascii=False, indent=2))