feat: 자재 분류 시스템 개선 및 상세 테이블 추가
- 모든 자재 카테고리별 상세 테이블 생성 (fitting, valve, flange, bolt, gasket, instrument) - PIPE, FITTING, VALVE 분류 결과를 각 상세 테이블에 저장하는 로직 구현 - 프론트엔드 라우팅 정리 및 BOM 현황 페이지 기능 개선 - 자재확인 페이지 에러 처리 개선 TODO: FLANGE, BOLT, GASKET, INSTRUMENT 저장 로직 추가 필요
This commit is contained in:
@@ -8,9 +8,18 @@ from datetime import datetime
|
||||
import uuid
|
||||
import pandas as pd
|
||||
import re
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from ..database import get_db
|
||||
from app.services.material_classifier import classify_material
|
||||
from app.services.bolt_classifier import classify_bolt
|
||||
from app.services.flange_classifier import classify_flange
|
||||
from app.services.fitting_classifier import classify_fitting
|
||||
from app.services.gasket_classifier import classify_gasket
|
||||
from app.services.instrument_classifier import classify_instrument
|
||||
from app.services.pipe_classifier import classify_pipe
|
||||
from app.services.valve_classifier import classify_valve
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -59,7 +68,8 @@ def generate_unique_filename(original_filename: str) -> str:
|
||||
|
||||
def parse_dataframe(df):
|
||||
df = df.dropna(how='all')
|
||||
df.columns = df.columns.str.strip().str.lower()
|
||||
# 원본 컬럼명 유지 (소문자 변환하지 않음)
|
||||
df.columns = df.columns.str.strip()
|
||||
|
||||
column_mapping = {
|
||||
'description': ['description', 'item', 'material', '품명', '자재명'],
|
||||
@@ -75,10 +85,16 @@ def parse_dataframe(df):
|
||||
mapped_columns = {}
|
||||
for standard_col, possible_names in column_mapping.items():
|
||||
for possible_name in possible_names:
|
||||
if possible_name in df.columns:
|
||||
mapped_columns[standard_col] = possible_name
|
||||
# 대소문자 구분 없이 매핑
|
||||
for col in df.columns:
|
||||
if possible_name.lower() == col.lower():
|
||||
mapped_columns[standard_col] = col
|
||||
break
|
||||
if standard_col in mapped_columns:
|
||||
break
|
||||
|
||||
print(f"찾은 컬럼 매핑: {mapped_columns}")
|
||||
|
||||
materials = []
|
||||
for index, row in df.iterrows():
|
||||
description = str(row.get(mapped_columns.get('description', ''), ''))
|
||||
@@ -89,6 +105,15 @@ def parse_dataframe(df):
|
||||
except:
|
||||
quantity = 0
|
||||
|
||||
# 길이 정보 파싱
|
||||
length_raw = row.get(mapped_columns.get('length', ''), None)
|
||||
length_value = None
|
||||
if pd.notna(length_raw) and length_raw != '':
|
||||
try:
|
||||
length_value = float(length_raw)
|
||||
except:
|
||||
length_value = None
|
||||
|
||||
material_grade = ""
|
||||
if "ASTM" in description.upper():
|
||||
astm_match = re.search(r'ASTM\s+([A-Z0-9\s]+)', description.upper())
|
||||
@@ -112,6 +137,7 @@ def parse_dataframe(df):
|
||||
'unit': "EA",
|
||||
'size_spec': size_spec,
|
||||
'material_grade': material_grade,
|
||||
'length': length_value,
|
||||
'line_number': index + 1,
|
||||
'row_number': index + 1
|
||||
})
|
||||
@@ -140,7 +166,7 @@ async def upload_file(
|
||||
revision: str = Form("Rev.0"),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
if not validate_file_extension(file.filename):
|
||||
if not validate_file_extension(str(file.filename)):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"지원하지 않는 파일 형식입니다. 허용된 확장자: {', '.join(ALLOWED_EXTENSIONS)}"
|
||||
@@ -149,7 +175,7 @@ async def upload_file(
|
||||
if file.size and file.size > 10 * 1024 * 1024:
|
||||
raise HTTPException(status_code=400, detail="파일 크기는 10MB를 초과할 수 없습니다")
|
||||
|
||||
unique_filename = generate_unique_filename(file.filename)
|
||||
unique_filename = generate_unique_filename(str(file.filename))
|
||||
file_path = UPLOAD_DIR / unique_filename
|
||||
|
||||
try:
|
||||
@@ -183,19 +209,133 @@ async def upload_file(
|
||||
|
||||
file_id = file_result.fetchone()[0]
|
||||
|
||||
# 자재 데이터 저장
|
||||
# 자재 데이터 저장 (분류 포함)
|
||||
materials_inserted = 0
|
||||
for material_data in materials_data:
|
||||
# 자재 타입 분류기 적용 (PIPE, FITTING, VALVE 등)
|
||||
description = material_data["original_description"]
|
||||
size_spec = material_data["size_spec"]
|
||||
|
||||
# 각 분류기로 시도 (올바른 매개변수 사용)
|
||||
print(f"분류 시도: {description}")
|
||||
|
||||
# 분류기 호출 시 타임아웃 및 예외 처리
|
||||
classification_result = None
|
||||
try:
|
||||
# 파이프 분류기 호출 시 length 매개변수 전달
|
||||
length_value = None
|
||||
if 'length' in material_data:
|
||||
try:
|
||||
length_value = float(material_data['length'])
|
||||
except:
|
||||
length_value = None
|
||||
# None이면 0.0으로 대체
|
||||
if length_value is None:
|
||||
length_value = 0.0
|
||||
|
||||
# 타임아웃 설정 (10초)
|
||||
import signal
|
||||
def timeout_handler(signum, frame):
|
||||
raise TimeoutError("분류기 실행 시간 초과")
|
||||
|
||||
signal.signal(signal.SIGALRM, timeout_handler)
|
||||
signal.alarm(10) # 10초 타임아웃
|
||||
|
||||
try:
|
||||
classification_result = classify_pipe("", description, size_spec, length_value)
|
||||
print(f"PIPE 분류 결과: {classification_result.get('category', 'UNKNOWN')} (신뢰도: {classification_result.get('overall_confidence', 0)})")
|
||||
finally:
|
||||
signal.alarm(0) # 타임아웃 해제
|
||||
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
signal.alarm(10)
|
||||
try:
|
||||
classification_result = classify_fitting("", description, size_spec)
|
||||
print(f"FITTING 분류 결과: {classification_result.get('category', 'UNKNOWN')} (신뢰도: {classification_result.get('overall_confidence', 0)})")
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
signal.alarm(10)
|
||||
try:
|
||||
classification_result = classify_valve("", description, size_spec)
|
||||
print(f"VALVE 분류 결과: {classification_result.get('category', 'UNKNOWN')} (신뢰도: {classification_result.get('overall_confidence', 0)})")
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
signal.alarm(10)
|
||||
try:
|
||||
classification_result = classify_flange("", description, size_spec)
|
||||
print(f"FLANGE 분류 결과: {classification_result.get('category', 'UNKNOWN')} (신뢰도: {classification_result.get('overall_confidence', 0)})")
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
signal.alarm(10)
|
||||
try:
|
||||
classification_result = classify_bolt("", description, size_spec)
|
||||
print(f"BOLT 분류 결과: {classification_result.get('category', 'UNKNOWN')} (신뢰도: {classification_result.get('overall_confidence', 0)})")
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
signal.alarm(10)
|
||||
try:
|
||||
classification_result = classify_gasket("", description, size_spec)
|
||||
print(f"GASKET 분류 결과: {classification_result.get('category', 'UNKNOWN')} (신뢰도: {classification_result.get('overall_confidence', 0)})")
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
signal.alarm(10)
|
||||
try:
|
||||
classification_result = classify_instrument("", description, size_spec)
|
||||
print(f"INSTRUMENT 분류 결과: {classification_result.get('category', 'UNKNOWN')} (신뢰도: {classification_result.get('overall_confidence', 0)})")
|
||||
finally:
|
||||
signal.alarm(0)
|
||||
|
||||
except (TimeoutError, Exception) as e:
|
||||
print(f"분류기 실행 중 오류 발생: {e}")
|
||||
# 기본 분류 결과 생성
|
||||
classification_result = {
|
||||
"category": "UNKNOWN",
|
||||
"overall_confidence": 0.0,
|
||||
"reason": f"분류기 오류: {str(e)}"
|
||||
}
|
||||
|
||||
print(f"최종 분류 결과: {classification_result.get('category', 'UNKNOWN')}")
|
||||
|
||||
# 분류 결과에서 상세 정보 추출
|
||||
if classification_result.get('category') == 'PIPE':
|
||||
classification_details = classification_result
|
||||
elif classification_result.get('category') == 'FITTING':
|
||||
classification_details = classification_result
|
||||
elif classification_result.get('category') == 'VALVE':
|
||||
classification_details = classification_result
|
||||
else:
|
||||
classification_details = {}
|
||||
# DB에 저장 시 JSON 직렬화
|
||||
classification_details = json.dumps(classification_details, ensure_ascii=False)
|
||||
|
||||
# 디버깅: 저장 직전 데이터 확인
|
||||
print(f"=== 자재[{materials_inserted + 1}] 저장 직전 ===")
|
||||
print(f"자재명: {material_data['original_description']}")
|
||||
print(f"분류결과: {classification_result.get('category')}")
|
||||
print(f"신뢰도: {classification_result.get('overall_confidence', 0)}")
|
||||
print(f"classification_details 길이: {len(classification_details)}")
|
||||
print(f"classification_details 샘플: {classification_details[:200]}...")
|
||||
print("=" * 50)
|
||||
material_insert_query = text("""
|
||||
INSERT INTO materials (
|
||||
file_id, original_description, quantity, unit, size_spec,
|
||||
material_grade, line_number, row_number, classified_category,
|
||||
classification_confidence, is_verified, created_at
|
||||
classification_confidence, classification_details, is_verified, created_at
|
||||
)
|
||||
VALUES (
|
||||
:file_id, :original_description, :quantity, :unit, :size_spec,
|
||||
:material_grade, :line_number, :row_number, :classified_category,
|
||||
:classification_confidence, :is_verified, :created_at
|
||||
:classification_confidence, :classification_details, :is_verified, :created_at
|
||||
)
|
||||
""")
|
||||
|
||||
@@ -208,11 +348,155 @@ async def upload_file(
|
||||
"material_grade": material_data["material_grade"],
|
||||
"line_number": material_data["line_number"],
|
||||
"row_number": material_data["row_number"],
|
||||
"classified_category": None,
|
||||
"classification_confidence": None,
|
||||
"classified_category": classification_result.get("category", "UNKNOWN"),
|
||||
"classification_confidence": classification_result.get("overall_confidence", 0.0),
|
||||
"classification_details": classification_details,
|
||||
"is_verified": False,
|
||||
"created_at": datetime.now()
|
||||
})
|
||||
|
||||
# 각 카테고리별로 상세 테이블에 저장
|
||||
category = classification_result.get('category')
|
||||
confidence = classification_result.get('overall_confidence', 0)
|
||||
|
||||
if category == 'PIPE' and confidence >= 0.5:
|
||||
try:
|
||||
# 분류 결과에서 파이프 상세 정보 추출
|
||||
pipe_info = classification_result
|
||||
|
||||
# cutting_dimensions에서 length 정보 가져오기
|
||||
cutting_dims = pipe_info.get('cutting_dimensions', {})
|
||||
length_mm = cutting_dims.get('length_mm')
|
||||
|
||||
# length_mm가 없으면 원본 데이터의 length 사용
|
||||
if not length_mm and material_data.get('length'):
|
||||
length_mm = material_data['length']
|
||||
|
||||
pipe_insert_query = text("""
|
||||
INSERT INTO pipe_details (
|
||||
material_id, file_id, size_inches, schedule_type, material_spec,
|
||||
manufacturing_method, length_mm, outer_diameter_mm, wall_thickness_mm,
|
||||
weight_per_meter_kg, classification_confidence, additional_info
|
||||
)
|
||||
VALUES (
|
||||
(SELECT id FROM materials WHERE file_id = :file_id AND original_description = :description AND row_number = :row_number),
|
||||
:file_id, :size_inches, :schedule_type, :material_spec,
|
||||
:manufacturing_method, :length_mm, :outer_diameter_mm, :wall_thickness_mm,
|
||||
:weight_per_meter_kg, :classification_confidence, :additional_info
|
||||
)
|
||||
""")
|
||||
|
||||
db.execute(pipe_insert_query, {
|
||||
"file_id": file_id,
|
||||
"description": material_data["original_description"],
|
||||
"row_number": material_data["row_number"],
|
||||
"size_inches": pipe_info.get('nominal_diameter', ''),
|
||||
"schedule_type": pipe_info.get('schedule', ''),
|
||||
"material_spec": pipe_info.get('material_spec', ''),
|
||||
"manufacturing_method": pipe_info.get('manufacturing_method', ''),
|
||||
"length_mm": length_mm,
|
||||
"outer_diameter_mm": pipe_info.get('outer_diameter_mm'),
|
||||
"wall_thickness_mm": pipe_info.get('wall_thickness_mm'),
|
||||
"weight_per_meter_kg": pipe_info.get('weight_per_meter_kg'),
|
||||
"classification_confidence": classification_result.get('overall_confidence', 0.0),
|
||||
"additional_info": json.dumps(pipe_info, ensure_ascii=False)
|
||||
})
|
||||
|
||||
print(f"PIPE 상세정보 저장 완료: {material_data['original_description']}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"PIPE 상세정보 저장 실패: {e}")
|
||||
# 에러가 발생해도 전체 프로세스는 계속 진행
|
||||
|
||||
elif category == 'FITTING' and confidence >= 0.5:
|
||||
try:
|
||||
fitting_info = classification_result
|
||||
|
||||
fitting_insert_query = text("""
|
||||
INSERT INTO fitting_details (
|
||||
material_id, file_id, fitting_type, fitting_subtype,
|
||||
connection_method, connection_code, pressure_rating, max_pressure,
|
||||
manufacturing_method, material_standard, material_grade, material_type,
|
||||
main_size, reduced_size, classification_confidence, additional_info
|
||||
)
|
||||
VALUES (
|
||||
(SELECT id FROM materials WHERE file_id = :file_id AND original_description = :description AND row_number = :row_number),
|
||||
:file_id, :fitting_type, :fitting_subtype,
|
||||
:connection_method, :connection_code, :pressure_rating, :max_pressure,
|
||||
:manufacturing_method, :material_standard, :material_grade, :material_type,
|
||||
:main_size, :reduced_size, :classification_confidence, :additional_info
|
||||
)
|
||||
""")
|
||||
|
||||
db.execute(fitting_insert_query, {
|
||||
"file_id": file_id,
|
||||
"description": material_data["original_description"],
|
||||
"row_number": material_data["row_number"],
|
||||
"fitting_type": fitting_info.get('fitting_type', {}).get('type', ''),
|
||||
"fitting_subtype": fitting_info.get('fitting_type', {}).get('subtype', ''),
|
||||
"connection_method": fitting_info.get('connection_method', {}).get('method', ''),
|
||||
"connection_code": fitting_info.get('connection_method', {}).get('matched_code', ''),
|
||||
"pressure_rating": fitting_info.get('pressure_rating', {}).get('rating', ''),
|
||||
"max_pressure": fitting_info.get('pressure_rating', {}).get('max_pressure', ''),
|
||||
"manufacturing_method": fitting_info.get('manufacturing', {}).get('method', ''),
|
||||
"material_standard": fitting_info.get('material', {}).get('standard', ''),
|
||||
"material_grade": fitting_info.get('material', {}).get('grade', ''),
|
||||
"material_type": fitting_info.get('material', {}).get('material_type', ''),
|
||||
"main_size": fitting_info.get('size_info', {}).get('main_size', ''),
|
||||
"reduced_size": fitting_info.get('size_info', {}).get('reduced_size', ''),
|
||||
"classification_confidence": confidence,
|
||||
"additional_info": json.dumps(fitting_info, ensure_ascii=False)
|
||||
})
|
||||
|
||||
print(f"FITTING 상세정보 저장 완료: {material_data['original_description']}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"FITTING 상세정보 저장 실패: {e}")
|
||||
|
||||
elif category == 'VALVE' and confidence >= 0.5:
|
||||
try:
|
||||
valve_info = classification_result
|
||||
|
||||
valve_insert_query = text("""
|
||||
INSERT INTO valve_details (
|
||||
material_id, file_id, valve_type, valve_subtype, actuator_type,
|
||||
connection_method, pressure_rating, pressure_class,
|
||||
body_material, trim_material, size_inches,
|
||||
fire_safe, low_temp_service, classification_confidence, additional_info
|
||||
)
|
||||
VALUES (
|
||||
(SELECT id FROM materials WHERE file_id = :file_id AND original_description = :description AND row_number = :row_number),
|
||||
:file_id, :valve_type, :valve_subtype, :actuator_type,
|
||||
:connection_method, :pressure_rating, :pressure_class,
|
||||
:body_material, :trim_material, :size_inches,
|
||||
:fire_safe, :low_temp_service, :classification_confidence, :additional_info
|
||||
)
|
||||
""")
|
||||
|
||||
db.execute(valve_insert_query, {
|
||||
"file_id": file_id,
|
||||
"description": material_data["original_description"],
|
||||
"row_number": material_data["row_number"],
|
||||
"valve_type": valve_info.get('valve_type', ''),
|
||||
"valve_subtype": valve_info.get('valve_subtype', ''),
|
||||
"actuator_type": valve_info.get('actuator_type', ''),
|
||||
"connection_method": valve_info.get('connection_method', ''),
|
||||
"pressure_rating": valve_info.get('pressure_rating', ''),
|
||||
"pressure_class": valve_info.get('pressure_class', ''),
|
||||
"body_material": valve_info.get('body_material', ''),
|
||||
"trim_material": valve_info.get('trim_material', ''),
|
||||
"size_inches": valve_info.get('size', ''),
|
||||
"fire_safe": valve_info.get('fire_safe', False),
|
||||
"low_temp_service": valve_info.get('low_temp_service', False),
|
||||
"classification_confidence": confidence,
|
||||
"additional_info": json.dumps(valve_info, ensure_ascii=False)
|
||||
})
|
||||
|
||||
print(f"VALVE 상세정보 저장 완료: {material_data['original_description']}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"VALVE 상세정보 저장 실패: {e}")
|
||||
|
||||
materials_inserted += 1
|
||||
|
||||
db.commit()
|
||||
@@ -256,6 +540,7 @@ async def get_materials(
|
||||
query = """
|
||||
SELECT m.id, m.file_id, m.original_description, m.quantity, m.unit,
|
||||
m.size_spec, m.material_grade, m.line_number, m.row_number,
|
||||
m.classified_category, m.classification_confidence, m.classification_details,
|
||||
m.created_at,
|
||||
f.original_filename, f.project_id, f.job_no, f.revision,
|
||||
p.official_project_code, p.project_name
|
||||
@@ -383,6 +668,9 @@ async def get_materials(
|
||||
"material_grade": m.material_grade,
|
||||
"line_number": m.line_number,
|
||||
"row_number": m.row_number,
|
||||
"classified_category": m.classified_category,
|
||||
"classification_confidence": float(m.classification_confidence) if m.classification_confidence else 0,
|
||||
"classification_details": json.loads(m.classification_details) if m.classification_details else None,
|
||||
"created_at": m.created_at
|
||||
}
|
||||
for m in materials
|
||||
@@ -444,3 +732,267 @@ async def get_materials_summary(
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"요약 조회 실패: {str(e)}")
|
||||
|
||||
@router.get("/materials/compare-revisions")
|
||||
async def compare_revisions(
|
||||
job_no: str,
|
||||
filename: str,
|
||||
old_revision: str,
|
||||
new_revision: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
리비전 간 자재 비교
|
||||
"""
|
||||
try:
|
||||
# 기존 리비전 자재 조회
|
||||
old_materials_query = text("""
|
||||
SELECT m.original_description, m.quantity, m.unit, m.size_spec,
|
||||
m.material_grade, m.classified_category, m.classification_confidence
|
||||
FROM materials m
|
||||
JOIN files f ON m.file_id = f.id
|
||||
WHERE f.job_no = :job_no
|
||||
AND f.original_filename = :filename
|
||||
AND f.revision = :old_revision
|
||||
""")
|
||||
|
||||
old_result = db.execute(old_materials_query, {
|
||||
"job_no": job_no,
|
||||
"filename": filename,
|
||||
"old_revision": old_revision
|
||||
})
|
||||
old_materials = old_result.fetchall()
|
||||
|
||||
# 새 리비전 자재 조회
|
||||
new_materials_query = text("""
|
||||
SELECT m.original_description, m.quantity, m.unit, m.size_spec,
|
||||
m.material_grade, m.classified_category, m.classification_confidence
|
||||
FROM materials m
|
||||
JOIN files f ON m.file_id = f.id
|
||||
WHERE f.job_no = :job_no
|
||||
AND f.original_filename = :filename
|
||||
AND f.revision = :new_revision
|
||||
""")
|
||||
|
||||
new_result = db.execute(new_materials_query, {
|
||||
"job_no": job_no,
|
||||
"filename": filename,
|
||||
"new_revision": new_revision
|
||||
})
|
||||
new_materials = new_result.fetchall()
|
||||
|
||||
# 자재 키 생성 함수
|
||||
def create_material_key(material):
|
||||
return f"{material.original_description}_{material.size_spec}_{material.material_grade}"
|
||||
|
||||
# 기존 자재를 딕셔너리로 변환
|
||||
old_materials_dict = {}
|
||||
for material in old_materials:
|
||||
key = create_material_key(material)
|
||||
old_materials_dict[key] = {
|
||||
"original_description": material.original_description,
|
||||
"quantity": float(material.quantity) if material.quantity else 0,
|
||||
"unit": material.unit,
|
||||
"size_spec": material.size_spec,
|
||||
"material_grade": material.material_grade,
|
||||
"classified_category": material.classified_category,
|
||||
"classification_confidence": material.classification_confidence
|
||||
}
|
||||
|
||||
# 새 자재를 딕셔너리로 변환
|
||||
new_materials_dict = {}
|
||||
for material in new_materials:
|
||||
key = create_material_key(material)
|
||||
new_materials_dict[key] = {
|
||||
"original_description": material.original_description,
|
||||
"quantity": float(material.quantity) if material.quantity else 0,
|
||||
"unit": material.unit,
|
||||
"size_spec": material.size_spec,
|
||||
"material_grade": material.material_grade,
|
||||
"classified_category": material.classified_category,
|
||||
"classification_confidence": material.classification_confidence
|
||||
}
|
||||
|
||||
# 변경 사항 분석
|
||||
all_keys = set(old_materials_dict.keys()) | set(new_materials_dict.keys())
|
||||
|
||||
added_items = []
|
||||
removed_items = []
|
||||
changed_items = []
|
||||
|
||||
for key in all_keys:
|
||||
old_item = old_materials_dict.get(key)
|
||||
new_item = new_materials_dict.get(key)
|
||||
|
||||
if old_item and not new_item:
|
||||
# 삭제된 항목
|
||||
removed_items.append({
|
||||
"key": key,
|
||||
"item": old_item,
|
||||
"change_type": "removed"
|
||||
})
|
||||
elif not old_item and new_item:
|
||||
# 추가된 항목
|
||||
added_items.append({
|
||||
"key": key,
|
||||
"item": new_item,
|
||||
"change_type": "added"
|
||||
})
|
||||
elif old_item and new_item:
|
||||
# 수량 변경 확인
|
||||
if old_item["quantity"] != new_item["quantity"]:
|
||||
changed_items.append({
|
||||
"key": key,
|
||||
"old_item": old_item,
|
||||
"new_item": new_item,
|
||||
"quantity_change": new_item["quantity"] - old_item["quantity"],
|
||||
"change_type": "quantity_changed"
|
||||
})
|
||||
|
||||
# 분류별 통계
|
||||
def calculate_category_stats(items):
|
||||
stats = {}
|
||||
for item in items:
|
||||
category = item.get("item", {}).get("classified_category", "OTHER")
|
||||
if category not in stats:
|
||||
stats[category] = {"count": 0, "total_quantity": 0}
|
||||
stats[category]["count"] += 1
|
||||
stats[category]["total_quantity"] += item.get("item", {}).get("quantity", 0)
|
||||
return stats
|
||||
|
||||
added_stats = calculate_category_stats(added_items)
|
||||
removed_stats = calculate_category_stats(removed_items)
|
||||
changed_stats = calculate_category_stats(changed_items)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"comparison": {
|
||||
"old_revision": old_revision,
|
||||
"new_revision": new_revision,
|
||||
"filename": filename,
|
||||
"job_no": job_no,
|
||||
"summary": {
|
||||
"added_count": len(added_items),
|
||||
"removed_count": len(removed_items),
|
||||
"changed_count": len(changed_items),
|
||||
"total_changes": len(added_items) + len(removed_items) + len(changed_items)
|
||||
},
|
||||
"changes": {
|
||||
"added": added_items,
|
||||
"removed": removed_items,
|
||||
"changed": changed_items
|
||||
},
|
||||
"category_stats": {
|
||||
"added": added_stats,
|
||||
"removed": removed_stats,
|
||||
"changed": changed_stats
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"리비전 비교 실패: {str(e)}")
|
||||
|
||||
@router.post("/materials/update-classification-details")
|
||||
async def update_classification_details(
|
||||
file_id: Optional[int] = None,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""기존 자재들의 classification_details 업데이트"""
|
||||
try:
|
||||
# 업데이트할 자재들 조회
|
||||
query = """
|
||||
SELECT id, original_description, size_spec, classified_category
|
||||
FROM materials
|
||||
WHERE classification_details IS NULL OR classification_details = '{}'
|
||||
"""
|
||||
params = {}
|
||||
if file_id:
|
||||
query += " AND file_id = :file_id"
|
||||
params["file_id"] = file_id
|
||||
|
||||
query += " ORDER BY id"
|
||||
result = db.execute(text(query), params)
|
||||
materials = result.fetchall()
|
||||
|
||||
if not materials:
|
||||
return {
|
||||
"success": True,
|
||||
"message": "업데이트할 자재가 없습니다.",
|
||||
"updated_count": 0
|
||||
}
|
||||
|
||||
updated_count = 0
|
||||
for material in materials:
|
||||
material_id = material.id
|
||||
description = material.original_description
|
||||
size_spec = material.size_spec
|
||||
category = material.classified_category
|
||||
|
||||
print(f"자재 {material_id} 재분류 중: {description}")
|
||||
|
||||
# 카테고리별로 적절한 분류기 호출
|
||||
classification_result = None
|
||||
|
||||
if category == 'PIPE':
|
||||
classification_result = classify_pipe("", description, size_spec, 0.0)
|
||||
elif category == 'FITTING':
|
||||
classification_result = classify_fitting("", description, size_spec)
|
||||
elif category == 'VALVE':
|
||||
classification_result = classify_valve("", description, size_spec)
|
||||
elif category == 'FLANGE':
|
||||
classification_result = classify_flange("", description, size_spec)
|
||||
elif category == 'BOLT':
|
||||
classification_result = classify_bolt("", description, size_spec)
|
||||
elif category == 'GASKET':
|
||||
classification_result = classify_gasket("", description, size_spec)
|
||||
elif category == 'INSTRUMENT':
|
||||
classification_result = classify_instrument("", description, size_spec)
|
||||
else:
|
||||
# 카테고리가 없으면 모든 분류기 시도
|
||||
classification_result = classify_pipe("", description, size_spec, 0.0)
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
classification_result = classify_fitting("", description, size_spec)
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
classification_result = classify_valve("", description, size_spec)
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
classification_result = classify_flange("", description, size_spec)
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
classification_result = classify_bolt("", description, size_spec)
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
classification_result = classify_gasket("", description, size_spec)
|
||||
if classification_result.get("overall_confidence", 0) < 0.5:
|
||||
classification_result = classify_instrument("", description, size_spec)
|
||||
|
||||
if classification_result:
|
||||
# classification_details를 JSON으로 직렬화
|
||||
classification_details = json.dumps(classification_result, ensure_ascii=False)
|
||||
|
||||
# DB 업데이트
|
||||
update_query = text("""
|
||||
UPDATE materials
|
||||
SET classification_details = :classification_details,
|
||||
updated_at = NOW()
|
||||
WHERE id = :material_id
|
||||
""")
|
||||
|
||||
db.execute(update_query, {
|
||||
"material_id": material_id,
|
||||
"classification_details": classification_details
|
||||
})
|
||||
|
||||
updated_count += 1
|
||||
print(f"자재 {material_id} 업데이트 완료")
|
||||
|
||||
db.commit()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"{updated_count}개 자재의 분류 상세정보가 업데이트되었습니다.",
|
||||
"updated_count": updated_count,
|
||||
"total_materials": len(materials)
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=500, detail=f"분류 상세정보 업데이트 실패: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user