fix: 리비전 업로드 시 누적 자재 조회 및 차이분 계산 로직 개선
Some checks failed
SonarQube Analysis / SonarQube Scan (push) Has been cancelled

- 리비전 업로드 시 모든 이전 리비전의 누적 자재를 조회하도록 수정
- 기존 단일 부모 파일 조회 → job_no 기준 누적 조회로 변경
- 차이분 계산 시 디버깅 로그 추가로 매칭 상태 확인 가능
- 자재 그룹핑과 라인 아이템 구분을 명확히 하는 로그 개선
- 기존 자재가 없는 경우 경고 메시지 추가
This commit is contained in:
hyungi
2025-09-16 09:07:06 +09:00
parent 04299542b5
commit af4ad25a54

View File

@@ -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)}개 분류 처리")