From af4ad25a540581e35a283cbecfe29530375a0f97 Mon Sep 17 00:00:00 2001 From: hyungi Date: Tue, 16 Sep 2025 09:07:06 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A6=AC=EB=B9=84=EC=A0=84=20=EC=97=85?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EC=8B=9C=20=EB=88=84=EC=A0=81=20=EC=9E=90?= =?UTF-8?q?=EC=9E=AC=20=EC=A1=B0=ED=9A=8C=20=EB=B0=8F=20=EC=B0=A8=EC=9D=B4?= =?UTF-8?q?=EB=B6=84=20=EA=B3=84=EC=82=B0=20=EB=A1=9C=EC=A7=81=20=EA=B0=9C?= =?UTF-8?q?=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 리비전 업로드 시 모든 이전 리비전의 누적 자재를 조회하도록 수정 - 기존 단일 부모 파일 조회 → job_no 기준 누적 조회로 변경 - 차이분 계산 시 디버깅 로그 추가로 매칭 상태 확인 가능 - 자재 그룹핑과 라인 아이템 구분을 명확히 하는 로그 개선 - 기존 자재가 없는 경우 경고 메시지 추가 --- backend/app/routers/files.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/backend/app/routers/files.py b/backend/app/routers/files.py index 9d16cfc..52163f1 100644 --- a/backend/app/routers/files.py +++ b/backend/app/routers/files.py @@ -211,7 +211,7 @@ async def upload_file( # 리비전 업로드인 경우만 자동 리비전 생성 및 기존 자재 조회 if parent_file_id is not None: - # 로그 제거 + print(f"🔄 리비전 업로드 감지: parent_file_id={parent_file_id}") # 부모 파일의 정보 조회 parent_query = text(""" SELECT original_filename, revision, bom_name FROM files @@ -259,15 +259,19 @@ async def upload_file( revision = "Rev.1" print(f"첫 번째 리비전: {revision}") - # 부모 파일의 자재 목록 조회 (기존 자재 확인용 - 수량 포함) + # 모든 이전 리비전의 누적 자재 목록 조회 (리비전 0부터 현재까지) existing_materials_query = text(""" - SELECT original_description, size_spec, SUM(quantity) as total_quantity - FROM materials - WHERE file_id = :parent_file_id - GROUP BY original_description, size_spec + SELECT m.original_description, m.size_spec, SUM(m.quantity) as total_quantity + FROM materials m + JOIN files f ON m.file_id = f.id + WHERE f.job_no = :job_no + AND f.id <= :parent_file_id + AND f.is_active = TRUE + GROUP BY m.original_description, m.size_spec """) existing_result = db.execute(existing_materials_query, { + "job_no": job_no, "parent_file_id": parent_file_id }) @@ -278,9 +282,15 @@ async def upload_file( existing_materials_descriptions.add(key) existing_materials_with_quantity[key] = float(row.total_quantity or 0) - print(f"기존 자재 수: {len(existing_materials_descriptions)}") + print(f"📊 누적 자재 수 (Rev.0~현재): {len(existing_materials_descriptions)}") + print(f"📊 누적 자재 총 수량: {sum(existing_materials_with_quantity.values())}") if len(existing_materials_descriptions) > 0: - print(f"기존 자재 샘플 (처음 5개): {list(existing_materials_descriptions)[:5]}") + print(f"📝 기존 자재 샘플 (처음 3개): {list(existing_materials_descriptions)[:3]}") + # 수량이 있는 자재들 확인 + quantity_samples = [(k, v) for k, v in list(existing_materials_with_quantity.items())[:3]] + print(f"📊 기존 자재 수량 샘플: {quantity_samples}") + else: + print(f"⚠️ 기존 자재가 없습니다! parent_file_id={parent_file_id}의 materials 테이블을 확인하세요.") # 파일명을 부모와 동일하게 유지 file.filename = parent_file[0] @@ -403,10 +413,16 @@ async def upload_file( } # 차이분 계산 + print(f"🔍 차이분 계산 시작: 신규 {len(new_materials_grouped)}개 vs 기존 {len(existing_materials_with_quantity)}개") + for material_key, new_data in new_materials_grouped.items(): existing_quantity = existing_materials_with_quantity.get(material_key, 0) new_quantity = new_data["quantity"] + # 디버깅: 첫 10개 키 매칭 상태 출력 + if len(materials_diff) < 10: + print(f"🔑 키 매칭: '{material_key}' → 기존:{existing_quantity}, 신규:{new_quantity}") + if new_quantity > existing_quantity: # 증가분이 있는 경우 diff_quantity = new_quantity - existing_quantity @@ -436,7 +452,8 @@ async def upload_file( # 차이분만 처리하도록 materials_to_classify 교체 materials_to_classify = materials_diff print(f"차이분 자재 개수: {len(materials_to_classify)}") - print(f"🔄 리비전 업로드: 차이분 {len(materials_diff)}개만 분류 처리") + print(f"🔄 리비전 업로드: 차이분 {len(materials_diff)}개 라인 아이템 분류 처리") + print(f"📊 차이분 요약: {len(new_materials_grouped)}개 자재 그룹 → {len(materials_diff)}개 라인 아이템") else: print(f"🆕 신규 업로드: 전체 {len(materials_to_classify)}개 분류 처리")