🔧 완전한 스키마 자동화 시스템 구축
Some checks failed
SonarQube Analysis / SonarQube Scan (push) Has been cancelled
Some checks failed
SonarQube Analysis / SonarQube Scan (push) Has been cancelled
✨ 주요 기능: - 완전한 데이터베이스 스키마 분석 및 자동 마이그레이션 시스템 - 44개 테이블 완전 지원 (운영 서버 43개 + 1개 추가) - 누락된 테이블/컬럼 자동 감지 및 생성 🔧 해결된 스키마 문제: - users.status 컬럼 누락 → 자동 추가 - files 테이블 4개 컬럼 누락 → 자동 추가 - materials 테이블 22개 컬럼 누락 → 자동 추가 - support_details, purchase_requests, purchase_request_items 테이블 누락 → 자동 생성 - material_purchase_tracking.description, purchase_status 컬럼 누락 → 자동 추가 🚀 자동화 도구: - schema_analyzer.py: 코드와 DB 스키마 비교 분석 - auto_migrator.py: 자동 마이그레이션 실행 - docker_migrator.py: Docker 환경용 간편 마이그레이션 - schema_monitor.py: 실시간 스키마 모니터링 📋 리비전 관리 시스템: - 8개 카테고리별 리비전 페이지 구현 - PIPE Cutting Plan 관리 시스템 - PIPE Issue Management 시스템 - 완전한 리비전 비교 및 추적 기능 🎯 사용법: docker exec tk-mp-backend python3 scripts/docker_migrator.py 앞으로 스키마 문제가 발생하면 위 명령 하나로 자동 해결!
This commit is contained in:
409
backend/app/routers/enhanced_revision.py
Normal file
409
backend/app/routers/enhanced_revision.py
Normal file
@@ -0,0 +1,409 @@
|
||||
"""
|
||||
강화된 리비전 관리 API 엔드포인트
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from ..database import get_db
|
||||
from ..auth.middleware import get_current_user
|
||||
from ..services.enhanced_revision_service import EnhancedRevisionService
|
||||
from ..auth.models import User
|
||||
from ..models import RevisionComparison, RevisionChangeLog
|
||||
from ..utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/enhanced-revision", tags=["Enhanced Revision Management"])
|
||||
|
||||
|
||||
@router.post("/compare-revisions")
|
||||
async def compare_revisions_enhanced(
|
||||
job_no: str,
|
||||
current_file_id: int,
|
||||
previous_file_id: Optional[int] = None,
|
||||
save_comparison: bool = True,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
강화된 리비전 비교 수행
|
||||
|
||||
Args:
|
||||
job_no: 작업 번호
|
||||
current_file_id: 현재 파일 ID
|
||||
previous_file_id: 이전 파일 ID (None이면 자동 탐지)
|
||||
save_comparison: 비교 결과 저장 여부
|
||||
"""
|
||||
|
||||
try:
|
||||
revision_service = EnhancedRevisionService(db)
|
||||
|
||||
# 리비전 비교 수행
|
||||
comparison_result = revision_service.compare_revisions_with_purchase_status(
|
||||
job_no=job_no,
|
||||
current_file_id=current_file_id,
|
||||
previous_file_id=previous_file_id
|
||||
)
|
||||
|
||||
# 비교 결과 저장 (옵션)
|
||||
if save_comparison:
|
||||
comparison_record = RevisionComparison(
|
||||
job_no=job_no,
|
||||
current_file_id=current_file_id,
|
||||
previous_file_id=previous_file_id,
|
||||
comparison_result=comparison_result,
|
||||
summary_stats=comparison_result.get("summary", {}),
|
||||
created_by=current_user.username,
|
||||
is_applied=False
|
||||
)
|
||||
|
||||
db.add(comparison_record)
|
||||
db.commit()
|
||||
db.refresh(comparison_record)
|
||||
|
||||
comparison_result["comparison_id"] = comparison_record.id
|
||||
|
||||
logger.info(f"Enhanced revision comparison completed for job {job_no}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": comparison_result,
|
||||
"message": "강화된 리비전 비교가 완료되었습니다."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Enhanced revision comparison failed: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 비교 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/apply-revision-changes/{comparison_id}")
|
||||
async def apply_revision_changes(
|
||||
comparison_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 변경사항을 실제 DB에 적용
|
||||
|
||||
Args:
|
||||
comparison_id: 비교 결과 ID
|
||||
"""
|
||||
|
||||
try:
|
||||
# 비교 결과 조회
|
||||
comparison = db.query(RevisionComparison).filter(
|
||||
RevisionComparison.id == comparison_id
|
||||
).first()
|
||||
|
||||
if not comparison:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="비교 결과를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
if comparison.is_applied:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="이미 적용된 비교 결과입니다."
|
||||
)
|
||||
|
||||
revision_service = EnhancedRevisionService(db)
|
||||
|
||||
# 변경사항 적용
|
||||
apply_result = revision_service.apply_revision_changes(
|
||||
comparison_result=comparison.comparison_result,
|
||||
current_file_id=comparison.current_file_id
|
||||
)
|
||||
|
||||
if apply_result["success"]:
|
||||
# 적용 완료 표시
|
||||
comparison.is_applied = True
|
||||
comparison.applied_at = datetime.utcnow()
|
||||
comparison.applied_by = current_user.username
|
||||
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Revision changes applied for comparison {comparison_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": apply_result,
|
||||
"message": "리비전 변경사항이 성공적으로 적용되었습니다."
|
||||
}
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=apply_result.get("message", "변경사항 적용 중 오류가 발생했습니다.")
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to apply revision changes: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"변경사항 적용 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/comparison-history/{job_no}")
|
||||
async def get_comparison_history(
|
||||
job_no: str,
|
||||
limit: int = 10,
|
||||
offset: int = 0,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
작업별 리비전 비교 이력 조회
|
||||
|
||||
Args:
|
||||
job_no: 작업 번호
|
||||
limit: 조회 개수 제한
|
||||
offset: 조회 시작 위치
|
||||
"""
|
||||
|
||||
try:
|
||||
comparisons = db.query(RevisionComparison).filter(
|
||||
RevisionComparison.job_no == job_no
|
||||
).order_by(
|
||||
RevisionComparison.comparison_date.desc()
|
||||
).offset(offset).limit(limit).all()
|
||||
|
||||
result = []
|
||||
for comp in comparisons:
|
||||
result.append({
|
||||
"id": comp.id,
|
||||
"job_no": comp.job_no,
|
||||
"current_file_id": comp.current_file_id,
|
||||
"previous_file_id": comp.previous_file_id,
|
||||
"comparison_date": comp.comparison_date.isoformat(),
|
||||
"summary_stats": comp.summary_stats,
|
||||
"created_by": comp.created_by,
|
||||
"is_applied": comp.is_applied,
|
||||
"applied_at": comp.applied_at.isoformat() if comp.applied_at else None,
|
||||
"applied_by": comp.applied_by
|
||||
})
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": result,
|
||||
"total": len(result),
|
||||
"message": "리비전 비교 이력을 조회했습니다."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get comparison history: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"비교 이력 조회 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/comparison-details/{comparison_id}")
|
||||
async def get_comparison_details(
|
||||
comparison_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
특정 비교 결과의 상세 정보 조회
|
||||
|
||||
Args:
|
||||
comparison_id: 비교 결과 ID
|
||||
"""
|
||||
|
||||
try:
|
||||
comparison = db.query(RevisionComparison).filter(
|
||||
RevisionComparison.id == comparison_id
|
||||
).first()
|
||||
|
||||
if not comparison:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="비교 결과를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 변경 로그도 함께 조회
|
||||
change_logs = db.query(RevisionChangeLog).filter(
|
||||
RevisionChangeLog.comparison_id == comparison_id
|
||||
).all()
|
||||
|
||||
result = {
|
||||
"comparison": {
|
||||
"id": comparison.id,
|
||||
"job_no": comparison.job_no,
|
||||
"current_file_id": comparison.current_file_id,
|
||||
"previous_file_id": comparison.previous_file_id,
|
||||
"comparison_date": comparison.comparison_date.isoformat(),
|
||||
"comparison_result": comparison.comparison_result,
|
||||
"summary_stats": comparison.summary_stats,
|
||||
"created_by": comparison.created_by,
|
||||
"is_applied": comparison.is_applied,
|
||||
"applied_at": comparison.applied_at.isoformat() if comparison.applied_at else None,
|
||||
"applied_by": comparison.applied_by
|
||||
},
|
||||
"change_logs": [
|
||||
{
|
||||
"id": log.id,
|
||||
"material_id": log.material_id,
|
||||
"change_type": log.change_type,
|
||||
"previous_data": log.previous_data,
|
||||
"current_data": log.current_data,
|
||||
"action_taken": log.action_taken,
|
||||
"notes": log.notes,
|
||||
"created_at": log.created_at.isoformat()
|
||||
}
|
||||
for log in change_logs
|
||||
]
|
||||
}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": result,
|
||||
"message": "비교 결과 상세 정보를 조회했습니다."
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get comparison details: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"비교 결과 조회 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/pipe-length-summary/{file_id}")
|
||||
async def get_pipe_length_summary(
|
||||
file_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
PIPE 자재 길이 요약 정보 조회
|
||||
|
||||
Args:
|
||||
file_id: 파일 ID
|
||||
"""
|
||||
|
||||
try:
|
||||
revision_service = EnhancedRevisionService(db)
|
||||
|
||||
# PIPE 자재만 조회하여 도면-라인넘버별 길이 집계
|
||||
pipe_materials = revision_service._get_materials_with_purchase_status(file_id)
|
||||
|
||||
pipe_summary = {}
|
||||
for key, material in pipe_materials.items():
|
||||
if material.get('classified_category') == 'PIPE':
|
||||
drawing_line = f"{material.get('drawing_name', 'Unknown')} - {material.get('line_no', 'Unknown')}"
|
||||
|
||||
if drawing_line not in pipe_summary:
|
||||
pipe_summary[drawing_line] = {
|
||||
"drawing_name": material.get('drawing_name'),
|
||||
"line_no": material.get('line_no'),
|
||||
"material_grade": material.get('material_grade'),
|
||||
"schedule": material.get('schedule'),
|
||||
"nominal_size": material.get('main_nom'),
|
||||
"total_length": 0,
|
||||
"segment_count": 0,
|
||||
"purchase_status": "mixed"
|
||||
}
|
||||
|
||||
pipe_summary[drawing_line]["total_length"] += material.get('total_length', 0)
|
||||
pipe_summary[drawing_line]["segment_count"] += 1
|
||||
|
||||
# 구매 상태 확인
|
||||
if material.get('purchase_confirmed'):
|
||||
if pipe_summary[drawing_line]["purchase_status"] == "mixed":
|
||||
pipe_summary[drawing_line]["purchase_status"] = "purchased"
|
||||
else:
|
||||
if pipe_summary[drawing_line]["purchase_status"] == "purchased":
|
||||
pipe_summary[drawing_line]["purchase_status"] = "mixed"
|
||||
elif pipe_summary[drawing_line]["purchase_status"] != "mixed":
|
||||
pipe_summary[drawing_line]["purchase_status"] = "pending"
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"file_id": file_id,
|
||||
"pipe_lines": list(pipe_summary.values()),
|
||||
"total_lines": len(pipe_summary),
|
||||
"total_length": sum(line["total_length"] for line in pipe_summary.values())
|
||||
},
|
||||
"message": "PIPE 자재 길이 요약을 조회했습니다."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get pipe length summary: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"PIPE 길이 요약 조회 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/recalculate-pipe-lengths/{file_id}")
|
||||
async def recalculate_pipe_lengths(
|
||||
file_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
PIPE 자재 길이 재계산 및 업데이트
|
||||
|
||||
Args:
|
||||
file_id: 파일 ID
|
||||
"""
|
||||
|
||||
try:
|
||||
revision_service = EnhancedRevisionService(db)
|
||||
|
||||
# PIPE 자재 조회 및 길이 재계산
|
||||
pipe_materials = revision_service._get_materials_with_purchase_status(file_id)
|
||||
|
||||
updated_count = 0
|
||||
for key, material in pipe_materials.items():
|
||||
if material.get('classified_category') == 'PIPE':
|
||||
# total_length 업데이트
|
||||
total_length = material.get('total_length', 0)
|
||||
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET total_length = :total_length,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
revision_service.db_service.execute_query(update_query, {
|
||||
"total_length": total_length,
|
||||
"material_id": material["id"]
|
||||
})
|
||||
|
||||
updated_count += 1
|
||||
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Recalculated pipe lengths for {updated_count} materials in file {file_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"file_id": file_id,
|
||||
"updated_count": updated_count
|
||||
},
|
||||
"message": f"PIPE 자재 {updated_count}개의 길이를 재계산했습니다."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to recalculate pipe lengths: {e}")
|
||||
db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"PIPE 길이 재계산 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
224
backend/app/routers/pipe_excel.py
Normal file
224
backend/app/routers/pipe_excel.py
Normal file
@@ -0,0 +1,224 @@
|
||||
"""
|
||||
PIPE 스냅샷 Excel 내보내기 API 라우터
|
||||
|
||||
확정된 Cutting Plan의 고정된 Excel 내보내기 기능
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Dict, Any
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Response
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from io import BytesIO
|
||||
|
||||
from ..database import get_db
|
||||
from ..services.pipe_snapshot_excel_service import get_pipe_snapshot_excel_service, PipeSnapshotExcelService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/pipe-excel", tags=["pipe-excel"])
|
||||
|
||||
|
||||
@router.get("/export-finalized/{job_no}")
|
||||
async def export_finalized_cutting_plan(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
확정된 Cutting Plan Excel 내보내기
|
||||
- 스냅샷 데이터 기준으로 고정된 Excel 생성
|
||||
- 리비전과 무관하게 동일한 데이터 제공
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_snapshot_excel_service(db)
|
||||
result = service.export_finalized_cutting_plan(job_no)
|
||||
|
||||
if not result["success"]:
|
||||
if "확정된 Cutting Plan이 없습니다" in result["message"]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=result["message"]
|
||||
)
|
||||
else:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=result["message"]
|
||||
)
|
||||
|
||||
# Excel 파일 스트리밍 응답
|
||||
excel_buffer = result["excel_buffer"]
|
||||
filename = result["filename"]
|
||||
|
||||
# 파일 다운로드를 위한 헤더 설정
|
||||
headers = {
|
||||
'Content-Disposition': f'attachment; filename="{filename}"',
|
||||
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
||||
}
|
||||
|
||||
return StreamingResponse(
|
||||
BytesIO(excel_buffer.getvalue()),
|
||||
media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
headers=headers
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to export finalized cutting plan: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"확정된 Excel 내보내기 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/check-finalization/{job_no}")
|
||||
async def check_finalization_status(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Cutting Plan 확정 상태 확인
|
||||
- 확정된 Excel 내보내기 가능 여부 확인
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_snapshot_excel_service(db)
|
||||
result = service.check_finalization_status(job_no)
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to check finalization status: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"확정 상태 확인 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/preview-finalized/{job_no}")
|
||||
async def preview_finalized_data(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
확정된 데이터 미리보기
|
||||
- Excel 생성 전 데이터 확인용
|
||||
"""
|
||||
try:
|
||||
from ..services.pipe_issue_snapshot_service import get_pipe_issue_snapshot_service
|
||||
|
||||
# 스냅샷 상태 확인
|
||||
snapshot_service = get_pipe_issue_snapshot_service(db)
|
||||
snapshot_info = snapshot_service.get_snapshot_info(job_no)
|
||||
|
||||
if not snapshot_info["has_snapshot"] or not snapshot_info["is_locked"]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="확정된 Cutting Plan이 없습니다."
|
||||
)
|
||||
|
||||
# 스냅샷 데이터 조회
|
||||
snapshot_id = snapshot_info["snapshot_id"]
|
||||
segments = snapshot_service.get_snapshot_segments(snapshot_id)
|
||||
|
||||
# 구역별/도면별 통계
|
||||
area_stats = {}
|
||||
drawing_stats = {}
|
||||
material_stats = {}
|
||||
|
||||
for segment in segments:
|
||||
# 구역별 통계
|
||||
area = segment.get("area", "미할당")
|
||||
if area not in area_stats:
|
||||
area_stats[area] = {"count": 0, "total_length": 0}
|
||||
area_stats[area]["count"] += 1
|
||||
area_stats[area]["total_length"] += segment.get("length_mm", 0)
|
||||
|
||||
# 도면별 통계
|
||||
drawing = segment.get("drawing_name", "UNKNOWN")
|
||||
if drawing not in drawing_stats:
|
||||
drawing_stats[drawing] = {"count": 0, "total_length": 0}
|
||||
drawing_stats[drawing]["count"] += 1
|
||||
drawing_stats[drawing]["total_length"] += segment.get("length_mm", 0)
|
||||
|
||||
# 재질별 통계
|
||||
material = segment.get("material_grade", "UNKNOWN")
|
||||
if material not in material_stats:
|
||||
material_stats[material] = {"count": 0, "total_length": 0}
|
||||
material_stats[material]["count"] += 1
|
||||
material_stats[material]["total_length"] += segment.get("length_mm", 0)
|
||||
|
||||
return {
|
||||
"job_no": job_no,
|
||||
"snapshot_info": snapshot_info,
|
||||
"preview_data": {
|
||||
"total_segments": len(segments),
|
||||
"area_stats": area_stats,
|
||||
"drawing_stats": drawing_stats,
|
||||
"material_stats": material_stats
|
||||
},
|
||||
"sample_segments": segments[:10] if segments else [], # 처음 10개만 미리보기
|
||||
"can_export": True,
|
||||
"message": "확정된 데이터 미리보기가 준비되었습니다."
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to preview finalized data: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"데이터 미리보기 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/export-temp/{job_no}")
|
||||
async def export_temp_cutting_plan(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
임시 Cutting Plan Excel 내보내기 (구현 예정)
|
||||
- 현재 작업 중인 데이터 기준
|
||||
- 리비전 시 변경될 수 있는 데이터
|
||||
"""
|
||||
try:
|
||||
# TODO: 임시 Excel 내보내기 구현
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_501_NOT_IMPLEMENTED,
|
||||
detail="임시 Excel 내보내기 기능은 구현 예정입니다."
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to export temp cutting plan: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"임시 Excel 내보내기 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/download-history/{job_no}")
|
||||
async def get_download_history(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Excel 다운로드 이력 조회 (구현 예정)
|
||||
- 확정된 Excel 다운로드 기록
|
||||
- 다운로드 시간 및 사용자 추적
|
||||
"""
|
||||
try:
|
||||
# TODO: 다운로드 이력 추적 구현
|
||||
return {
|
||||
"job_no": job_no,
|
||||
"download_history": [],
|
||||
"message": "다운로드 이력 추적 기능은 구현 예정입니다."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get download history: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"다운로드 이력 조회 실패: {str(e)}"
|
||||
)
|
||||
535
backend/app/routers/pipe_issue.py
Normal file
535
backend/app/routers/pipe_issue.py
Normal file
@@ -0,0 +1,535 @@
|
||||
"""
|
||||
PIPE 이슈 관리 API 라우터
|
||||
|
||||
스냅샷 기반 도면별/단관별 이슈 관리 기능
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Dict, List, Optional, Any
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic import BaseModel
|
||||
from datetime import datetime
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import (
|
||||
PipeIssueSnapshot, PipeIssueSegment,
|
||||
PipeDrawingIssue, PipeSegmentIssue
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/pipe-issue", tags=["pipe-issue"])
|
||||
|
||||
|
||||
# Pydantic 모델들
|
||||
class CreateDrawingIssueRequest(BaseModel):
|
||||
snapshot_id: int
|
||||
area: str
|
||||
drawing_name: str
|
||||
issue_description: str
|
||||
severity: str = 'medium' # low, medium, high, critical
|
||||
reported_by: Optional[str] = 'user'
|
||||
|
||||
|
||||
class CreateSegmentIssueRequest(BaseModel):
|
||||
snapshot_id: int
|
||||
segment_id: int
|
||||
issue_description: str
|
||||
issue_type: Optional[str] = 'other' # cutting, installation, material, routing, other
|
||||
length_change: Optional[float] = None
|
||||
new_length: Optional[float] = None
|
||||
material_change: Optional[str] = None
|
||||
severity: str = 'medium'
|
||||
reported_by: Optional[str] = 'user'
|
||||
|
||||
|
||||
class UpdateIssueStatusRequest(BaseModel):
|
||||
status: str # open, in_progress, resolved
|
||||
resolution_notes: Optional[str] = None
|
||||
resolved_by: Optional[str] = None
|
||||
|
||||
|
||||
class DrawingIssueResponse(BaseModel):
|
||||
id: int
|
||||
snapshot_id: int
|
||||
area: str
|
||||
drawing_name: str
|
||||
issue_description: str
|
||||
severity: str
|
||||
status: str
|
||||
resolution_notes: Optional[str] = None
|
||||
resolved_by: Optional[str] = None
|
||||
resolved_at: Optional[str] = None
|
||||
reported_by: Optional[str] = None
|
||||
reported_at: str
|
||||
updated_at: str
|
||||
|
||||
|
||||
class SegmentIssueResponse(BaseModel):
|
||||
id: int
|
||||
snapshot_id: int
|
||||
segment_id: int
|
||||
issue_description: str
|
||||
issue_type: Optional[str] = None
|
||||
length_change: Optional[float] = None
|
||||
new_length: Optional[float] = None
|
||||
material_change: Optional[str] = None
|
||||
severity: str
|
||||
status: str
|
||||
resolution_notes: Optional[str] = None
|
||||
resolved_by: Optional[str] = None
|
||||
resolved_at: Optional[str] = None
|
||||
reported_by: Optional[str] = None
|
||||
reported_at: str
|
||||
updated_at: str
|
||||
|
||||
|
||||
@router.get("/snapshots/{job_no}")
|
||||
async def get_job_snapshots(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""작업의 활성 스냅샷 조회"""
|
||||
try:
|
||||
snapshots = db.query(PipeIssueSnapshot).filter(
|
||||
PipeIssueSnapshot.job_no == job_no,
|
||||
PipeIssueSnapshot.is_active == True
|
||||
).all()
|
||||
|
||||
result = []
|
||||
for snapshot in snapshots:
|
||||
result.append({
|
||||
"snapshot_id": snapshot.id,
|
||||
"snapshot_name": snapshot.snapshot_name,
|
||||
"is_locked": snapshot.is_locked,
|
||||
"total_segments": snapshot.total_segments,
|
||||
"total_drawings": snapshot.total_drawings,
|
||||
"created_at": snapshot.created_at.isoformat() if snapshot.created_at else None,
|
||||
"locked_at": snapshot.locked_at.isoformat() if snapshot.locked_at else None
|
||||
})
|
||||
|
||||
return {
|
||||
"job_no": job_no,
|
||||
"snapshots": result,
|
||||
"total_count": len(result)
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get job snapshots: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"스냅샷 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/drawing-issues/{snapshot_id}")
|
||||
async def get_drawing_issues(
|
||||
snapshot_id: int,
|
||||
area: Optional[str] = None,
|
||||
drawing_name: Optional[str] = None,
|
||||
status_filter: Optional[str] = None,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""도면 이슈 목록 조회"""
|
||||
try:
|
||||
query = db.query(PipeDrawingIssue).filter(
|
||||
PipeDrawingIssue.snapshot_id == snapshot_id
|
||||
)
|
||||
|
||||
if area:
|
||||
query = query.filter(PipeDrawingIssue.area == area)
|
||||
|
||||
if drawing_name:
|
||||
query = query.filter(PipeDrawingIssue.drawing_name == drawing_name)
|
||||
|
||||
if status_filter:
|
||||
query = query.filter(PipeDrawingIssue.status == status_filter)
|
||||
|
||||
issues = query.order_by(PipeDrawingIssue.reported_at.desc()).all()
|
||||
|
||||
result = []
|
||||
for issue in issues:
|
||||
result.append(DrawingIssueResponse(
|
||||
id=issue.id,
|
||||
snapshot_id=issue.snapshot_id,
|
||||
area=issue.area,
|
||||
drawing_name=issue.drawing_name,
|
||||
issue_description=issue.issue_description,
|
||||
severity=issue.severity,
|
||||
status=issue.status,
|
||||
resolution_notes=issue.resolution_notes,
|
||||
resolved_by=issue.resolved_by,
|
||||
resolved_at=issue.resolved_at.isoformat() if issue.resolved_at else None,
|
||||
reported_by=issue.reported_by,
|
||||
reported_at=issue.reported_at.isoformat() if issue.reported_at else '',
|
||||
updated_at=issue.updated_at.isoformat() if issue.updated_at else ''
|
||||
))
|
||||
|
||||
return {
|
||||
"snapshot_id": snapshot_id,
|
||||
"issues": result,
|
||||
"total_count": len(result)
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get drawing issues: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"도면 이슈 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/drawing-issues", response_model=DrawingIssueResponse)
|
||||
async def create_drawing_issue(
|
||||
request: CreateDrawingIssueRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""도면 이슈 생성"""
|
||||
try:
|
||||
# 스냅샷 존재 확인
|
||||
snapshot = db.query(PipeIssueSnapshot).filter(
|
||||
PipeIssueSnapshot.id == request.snapshot_id
|
||||
).first()
|
||||
|
||||
if not snapshot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="스냅샷을 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 이슈 생성
|
||||
issue = PipeDrawingIssue(
|
||||
snapshot_id=request.snapshot_id,
|
||||
area=request.area,
|
||||
drawing_name=request.drawing_name,
|
||||
issue_description=request.issue_description,
|
||||
severity=request.severity,
|
||||
reported_by=request.reported_by
|
||||
)
|
||||
|
||||
db.add(issue)
|
||||
db.commit()
|
||||
db.refresh(issue)
|
||||
|
||||
return DrawingIssueResponse(
|
||||
id=issue.id,
|
||||
snapshot_id=issue.snapshot_id,
|
||||
area=issue.area,
|
||||
drawing_name=issue.drawing_name,
|
||||
issue_description=issue.issue_description,
|
||||
severity=issue.severity,
|
||||
status=issue.status,
|
||||
resolution_notes=issue.resolution_notes,
|
||||
resolved_by=issue.resolved_by,
|
||||
resolved_at=issue.resolved_at.isoformat() if issue.resolved_at else None,
|
||||
reported_by=issue.reported_by,
|
||||
reported_at=issue.reported_at.isoformat() if issue.reported_at else '',
|
||||
updated_at=issue.updated_at.isoformat() if issue.updated_at else ''
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to create drawing issue: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"도면 이슈 생성 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/segment-issues/{snapshot_id}")
|
||||
async def get_segment_issues(
|
||||
snapshot_id: int,
|
||||
segment_id: Optional[int] = None,
|
||||
status_filter: Optional[str] = None,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""단관 이슈 목록 조회"""
|
||||
try:
|
||||
query = db.query(PipeSegmentIssue).filter(
|
||||
PipeSegmentIssue.snapshot_id == snapshot_id
|
||||
)
|
||||
|
||||
if segment_id:
|
||||
query = query.filter(PipeSegmentIssue.segment_id == segment_id)
|
||||
|
||||
if status_filter:
|
||||
query = query.filter(PipeSegmentIssue.status == status_filter)
|
||||
|
||||
issues = query.order_by(PipeSegmentIssue.reported_at.desc()).all()
|
||||
|
||||
result = []
|
||||
for issue in issues:
|
||||
result.append(SegmentIssueResponse(
|
||||
id=issue.id,
|
||||
snapshot_id=issue.snapshot_id,
|
||||
segment_id=issue.segment_id,
|
||||
issue_description=issue.issue_description,
|
||||
issue_type=issue.issue_type,
|
||||
length_change=float(issue.length_change) if issue.length_change else None,
|
||||
new_length=float(issue.new_length) if issue.new_length else None,
|
||||
material_change=issue.material_change,
|
||||
severity=issue.severity,
|
||||
status=issue.status,
|
||||
resolution_notes=issue.resolution_notes,
|
||||
resolved_by=issue.resolved_by,
|
||||
resolved_at=issue.resolved_at.isoformat() if issue.resolved_at else None,
|
||||
reported_by=issue.reported_by,
|
||||
reported_at=issue.reported_at.isoformat() if issue.reported_at else '',
|
||||
updated_at=issue.updated_at.isoformat() if issue.updated_at else ''
|
||||
))
|
||||
|
||||
return {
|
||||
"snapshot_id": snapshot_id,
|
||||
"issues": result,
|
||||
"total_count": len(result)
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get segment issues: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"단관 이슈 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/segment-issues", response_model=SegmentIssueResponse)
|
||||
async def create_segment_issue(
|
||||
request: CreateSegmentIssueRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""단관 이슈 생성"""
|
||||
try:
|
||||
# 단관 존재 확인
|
||||
segment = db.query(PipeIssueSegment).filter(
|
||||
PipeIssueSegment.id == request.segment_id
|
||||
).first()
|
||||
|
||||
if not segment:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="단관을 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 이슈 생성
|
||||
issue = PipeSegmentIssue(
|
||||
snapshot_id=request.snapshot_id,
|
||||
segment_id=request.segment_id,
|
||||
issue_description=request.issue_description,
|
||||
issue_type=request.issue_type,
|
||||
length_change=request.length_change,
|
||||
new_length=request.new_length,
|
||||
material_change=request.material_change,
|
||||
severity=request.severity,
|
||||
reported_by=request.reported_by
|
||||
)
|
||||
|
||||
db.add(issue)
|
||||
db.commit()
|
||||
db.refresh(issue)
|
||||
|
||||
return SegmentIssueResponse(
|
||||
id=issue.id,
|
||||
snapshot_id=issue.snapshot_id,
|
||||
segment_id=issue.segment_id,
|
||||
issue_description=issue.issue_description,
|
||||
issue_type=issue.issue_type,
|
||||
length_change=float(issue.length_change) if issue.length_change else None,
|
||||
new_length=float(issue.new_length) if issue.new_length else None,
|
||||
material_change=issue.material_change,
|
||||
severity=issue.severity,
|
||||
status=issue.status,
|
||||
resolution_notes=issue.resolution_notes,
|
||||
resolved_by=issue.resolved_by,
|
||||
resolved_at=issue.resolved_at.isoformat() if issue.resolved_at else None,
|
||||
reported_by=issue.reported_by,
|
||||
reported_at=issue.reported_at.isoformat() if issue.reported_at else '',
|
||||
updated_at=issue.updated_at.isoformat() if issue.updated_at else ''
|
||||
)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to create segment issue: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"단관 이슈 생성 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.put("/drawing-issues/{issue_id}/status")
|
||||
async def update_drawing_issue_status(
|
||||
issue_id: int,
|
||||
request: UpdateIssueStatusRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""도면 이슈 상태 업데이트"""
|
||||
try:
|
||||
issue = db.query(PipeDrawingIssue).filter(
|
||||
PipeDrawingIssue.id == issue_id
|
||||
).first()
|
||||
|
||||
if not issue:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="이슈를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 상태 업데이트
|
||||
issue.status = request.status
|
||||
if request.resolution_notes:
|
||||
issue.resolution_notes = request.resolution_notes
|
||||
if request.resolved_by:
|
||||
issue.resolved_by = request.resolved_by
|
||||
|
||||
if request.status == 'resolved':
|
||||
issue.resolved_at = datetime.utcnow()
|
||||
|
||||
db.commit()
|
||||
|
||||
return {"message": "이슈 상태가 업데이트되었습니다.", "issue_id": issue_id}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to update drawing issue status: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"이슈 상태 업데이트 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.put("/segment-issues/{issue_id}/status")
|
||||
async def update_segment_issue_status(
|
||||
issue_id: int,
|
||||
request: UpdateIssueStatusRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""단관 이슈 상태 업데이트"""
|
||||
try:
|
||||
issue = db.query(PipeSegmentIssue).filter(
|
||||
PipeSegmentIssue.id == issue_id
|
||||
).first()
|
||||
|
||||
if not issue:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="이슈를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 상태 업데이트
|
||||
issue.status = request.status
|
||||
if request.resolution_notes:
|
||||
issue.resolution_notes = request.resolution_notes
|
||||
if request.resolved_by:
|
||||
issue.resolved_by = request.resolved_by
|
||||
|
||||
if request.status == 'resolved':
|
||||
issue.resolved_at = datetime.utcnow()
|
||||
|
||||
db.commit()
|
||||
|
||||
return {"message": "이슈 상태가 업데이트되었습니다.", "issue_id": issue_id}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to update segment issue status: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"이슈 상태 업데이트 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/report/{snapshot_id}")
|
||||
async def generate_issue_report(
|
||||
snapshot_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""이슈 리포트 생성"""
|
||||
try:
|
||||
# 스냅샷 정보
|
||||
snapshot = db.query(PipeIssueSnapshot).filter(
|
||||
PipeIssueSnapshot.id == snapshot_id
|
||||
).first()
|
||||
|
||||
if not snapshot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="스냅샷을 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 도면 이슈 통계
|
||||
drawing_issues = db.query(PipeDrawingIssue).filter(
|
||||
PipeDrawingIssue.snapshot_id == snapshot_id
|
||||
).all()
|
||||
|
||||
# 단관 이슈 통계
|
||||
segment_issues = db.query(PipeSegmentIssue).filter(
|
||||
PipeSegmentIssue.snapshot_id == snapshot_id
|
||||
).all()
|
||||
|
||||
# 통계 계산
|
||||
drawing_stats = {
|
||||
"total": len(drawing_issues),
|
||||
"by_status": {},
|
||||
"by_severity": {},
|
||||
"by_area": {}
|
||||
}
|
||||
|
||||
for issue in drawing_issues:
|
||||
# 상태별
|
||||
status = issue.status
|
||||
drawing_stats["by_status"][status] = drawing_stats["by_status"].get(status, 0) + 1
|
||||
|
||||
# 심각도별
|
||||
severity = issue.severity
|
||||
drawing_stats["by_severity"][severity] = drawing_stats["by_severity"].get(severity, 0) + 1
|
||||
|
||||
# 구역별
|
||||
area = issue.area
|
||||
drawing_stats["by_area"][area] = drawing_stats["by_area"].get(area, 0) + 1
|
||||
|
||||
segment_stats = {
|
||||
"total": len(segment_issues),
|
||||
"by_status": {},
|
||||
"by_severity": {},
|
||||
"by_type": {}
|
||||
}
|
||||
|
||||
for issue in segment_issues:
|
||||
# 상태별
|
||||
status = issue.status
|
||||
segment_stats["by_status"][status] = segment_stats["by_status"].get(status, 0) + 1
|
||||
|
||||
# 심각도별
|
||||
severity = issue.severity
|
||||
segment_stats["by_severity"][severity] = segment_stats["by_severity"].get(severity, 0) + 1
|
||||
|
||||
# 유형별
|
||||
issue_type = issue.issue_type or 'other'
|
||||
segment_stats["by_type"][issue_type] = segment_stats["by_type"].get(issue_type, 0) + 1
|
||||
|
||||
return {
|
||||
"snapshot_id": snapshot_id,
|
||||
"snapshot_name": snapshot.snapshot_name,
|
||||
"job_no": snapshot.job_no,
|
||||
"report_generated_at": datetime.utcnow().isoformat(),
|
||||
"drawing_issues": drawing_stats,
|
||||
"segment_issues": segment_stats,
|
||||
"total_issues": len(drawing_issues) + len(segment_issues)
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to generate issue report: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"이슈 리포트 생성 실패: {str(e)}"
|
||||
)
|
||||
435
backend/app/routers/pipe_revision.py
Normal file
435
backend/app/routers/pipe_revision.py
Normal file
@@ -0,0 +1,435 @@
|
||||
"""
|
||||
PIPE 리비전 관리 API 라우터
|
||||
|
||||
Cutting Plan 전/후 리비전 처리 및 비교 기능
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Dict, List, Optional, Any
|
||||
from datetime import datetime
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..database import get_db
|
||||
from ..services.pipe_revision_service import get_pipe_revision_service, PipeRevisionService
|
||||
from ..models import PipeRevisionComparison, PipeRevisionChange
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/pipe-revision", tags=["pipe-revision"])
|
||||
|
||||
|
||||
# Pydantic 모델들
|
||||
class RevisionStatusRequest(BaseModel):
|
||||
job_no: str
|
||||
new_file_id: int
|
||||
|
||||
|
||||
class PreRevisionRequest(BaseModel):
|
||||
job_no: str
|
||||
new_file_id: int
|
||||
|
||||
|
||||
class PostRevisionRequest(BaseModel):
|
||||
job_no: str
|
||||
new_file_id: int
|
||||
|
||||
|
||||
class RevisionStatusResponse(BaseModel):
|
||||
revision_type: str
|
||||
requires_action: bool
|
||||
message: str
|
||||
previous_file_id: Optional[int] = None
|
||||
|
||||
|
||||
class PreRevisionResponse(BaseModel):
|
||||
status: str
|
||||
revision_type: str
|
||||
deleted_items: int
|
||||
new_pipe_materials: int
|
||||
message: str
|
||||
next_action: str
|
||||
pipe_materials: List[Dict[str, Any]]
|
||||
|
||||
|
||||
class PostRevisionResponse(BaseModel):
|
||||
status: str
|
||||
revision_type: str
|
||||
comparison_id: int
|
||||
summary: Dict[str, Any]
|
||||
changed_drawings: List[Dict[str, Any]]
|
||||
unchanged_drawings: List[Dict[str, Any]]
|
||||
message: str
|
||||
next_action: str
|
||||
|
||||
|
||||
@router.post("/check-status", response_model=RevisionStatusResponse)
|
||||
async def check_revision_status(
|
||||
request: RevisionStatusRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
PIPE 리비전 상태 확인
|
||||
- 리비전 유형 판단 (pre/post cutting plan)
|
||||
- 필요한 처리 방식 결정
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_revision_service(db)
|
||||
result = service.check_revision_status(request.job_no, request.new_file_id)
|
||||
|
||||
return RevisionStatusResponse(**result)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to check pipe revision status: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 상태 확인 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/handle-pre-cutting-plan", response_model=PreRevisionResponse)
|
||||
async def handle_pre_cutting_plan_revision(
|
||||
request: PreRevisionRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Cutting Plan 작성 전 리비전 처리
|
||||
- 기존 PIPE 데이터 삭제
|
||||
- 새 BOM 데이터로 초기화
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_revision_service(db)
|
||||
result = service.handle_pre_cutting_plan_revision(request.job_no, request.new_file_id)
|
||||
|
||||
if result["status"] != "success":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=result["message"]
|
||||
)
|
||||
|
||||
return PreRevisionResponse(**result)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to handle pre-cutting-plan revision: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Cutting Plan 작성 전 리비전 처리 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/handle-post-cutting-plan", response_model=PostRevisionResponse)
|
||||
async def handle_post_cutting_plan_revision(
|
||||
request: PostRevisionRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Cutting Plan 작성 후 리비전 처리
|
||||
- 기존 Cutting Plan과 신규 BOM 비교
|
||||
- 변경사항 상세 분석
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_revision_service(db)
|
||||
result = service.handle_post_cutting_plan_revision(request.job_no, request.new_file_id)
|
||||
|
||||
if result["status"] != "success":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=result["message"]
|
||||
)
|
||||
|
||||
return PostRevisionResponse(**result)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to handle post-cutting-plan revision: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Cutting Plan 작성 후 리비전 처리 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/comparison/{comparison_id}")
|
||||
async def get_revision_comparison(
|
||||
comparison_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
리비전 비교 결과 상세 조회
|
||||
"""
|
||||
try:
|
||||
# 비교 결과 조회
|
||||
comparison = db.query(PipeRevisionComparison).filter(
|
||||
PipeRevisionComparison.id == comparison_id
|
||||
).first()
|
||||
|
||||
if not comparison:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="비교 결과를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 상세 변경사항 조회
|
||||
changes = db.query(PipeRevisionChange).filter(
|
||||
PipeRevisionChange.comparison_id == comparison_id
|
||||
).all()
|
||||
|
||||
# 도면별로 변경사항 그룹화
|
||||
changes_by_drawing = {}
|
||||
for change in changes:
|
||||
drawing = change.drawing_name
|
||||
if drawing not in changes_by_drawing:
|
||||
changes_by_drawing[drawing] = []
|
||||
|
||||
changes_by_drawing[drawing].append({
|
||||
"id": change.id,
|
||||
"change_type": change.change_type,
|
||||
"old_data": {
|
||||
"line_no": change.old_line_no,
|
||||
"material_grade": change.old_material_grade,
|
||||
"schedule_spec": change.old_schedule_spec,
|
||||
"nominal_size": change.old_nominal_size,
|
||||
"length_mm": change.old_length_mm,
|
||||
"end_preparation": change.old_end_preparation
|
||||
} if change.old_line_no else None,
|
||||
"new_data": {
|
||||
"line_no": change.new_line_no,
|
||||
"material_grade": change.new_material_grade,
|
||||
"schedule_spec": change.new_schedule_spec,
|
||||
"nominal_size": change.new_nominal_size,
|
||||
"length_mm": change.new_length_mm,
|
||||
"end_preparation": change.new_end_preparation
|
||||
} if change.new_line_no else None,
|
||||
"change_reason": change.change_reason
|
||||
})
|
||||
|
||||
return {
|
||||
"comparison_id": comparison.id,
|
||||
"job_no": comparison.job_no,
|
||||
"comparison_date": comparison.comparison_date,
|
||||
"summary": {
|
||||
"total_drawings": comparison.total_drawings,
|
||||
"changed_drawings": comparison.changed_drawings,
|
||||
"unchanged_drawings": comparison.unchanged_drawings,
|
||||
"total_segments": comparison.total_segments,
|
||||
"added_segments": comparison.added_segments,
|
||||
"removed_segments": comparison.removed_segments,
|
||||
"modified_segments": comparison.modified_segments,
|
||||
"unchanged_segments": comparison.unchanged_segments
|
||||
},
|
||||
"changes_by_drawing": changes_by_drawing,
|
||||
"is_applied": comparison.is_applied,
|
||||
"applied_at": comparison.applied_at,
|
||||
"applied_by": comparison.applied_by
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get revision comparison: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 비교 결과 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/comparison/{comparison_id}/apply")
|
||||
async def apply_revision_changes(
|
||||
comparison_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
리비전 변경사항 적용
|
||||
"""
|
||||
try:
|
||||
# 비교 결과 조회
|
||||
comparison = db.query(PipeRevisionComparison).filter(
|
||||
PipeRevisionComparison.id == comparison_id
|
||||
).first()
|
||||
|
||||
if not comparison:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="비교 결과를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
if comparison.is_applied:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="이미 적용된 리비전입니다."
|
||||
)
|
||||
|
||||
# 적용 상태 업데이트
|
||||
comparison.is_applied = True
|
||||
comparison.applied_at = datetime.utcnow()
|
||||
comparison.applied_by = "system" # 추후 사용자 정보로 변경
|
||||
|
||||
db.commit()
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": "리비전 변경사항이 적용되었습니다.",
|
||||
"comparison_id": comparison_id,
|
||||
"applied_at": comparison.applied_at
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to apply revision changes: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 변경사항 적용 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/job/{job_no}/history")
|
||||
async def get_revision_history(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
작업별 PIPE 리비전 이력 조회
|
||||
"""
|
||||
try:
|
||||
comparisons = db.query(PipeRevisionComparison).filter(
|
||||
PipeRevisionComparison.job_no == job_no
|
||||
).order_by(PipeRevisionComparison.comparison_date.desc()).all()
|
||||
|
||||
history = []
|
||||
for comp in comparisons:
|
||||
history.append({
|
||||
"comparison_id": comp.id,
|
||||
"comparison_date": comp.comparison_date,
|
||||
"summary": {
|
||||
"total_drawings": comp.total_drawings,
|
||||
"changed_drawings": comp.changed_drawings,
|
||||
"total_segments": comp.total_segments,
|
||||
"added_segments": comp.added_segments,
|
||||
"removed_segments": comp.removed_segments,
|
||||
"modified_segments": comp.modified_segments
|
||||
},
|
||||
"is_applied": comp.is_applied,
|
||||
"applied_at": comp.applied_at,
|
||||
"applied_by": comp.applied_by,
|
||||
"created_by": comp.created_by
|
||||
})
|
||||
|
||||
return {
|
||||
"job_no": job_no,
|
||||
"total_revisions": len(history),
|
||||
"history": history
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get revision history: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 이력 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/comparison/{comparison_id}/purchase-impact")
|
||||
async def get_purchase_impact(
|
||||
comparison_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
리비전이 구매량에 미치는 영향 분석
|
||||
"""
|
||||
try:
|
||||
# 비교 결과 조회
|
||||
comparison = db.query(PipeRevisionComparison).filter(
|
||||
PipeRevisionComparison.id == comparison_id
|
||||
).first()
|
||||
|
||||
if not comparison:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="비교 결과를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 변경사항 조회
|
||||
changes = db.query(PipeRevisionChange).filter(
|
||||
PipeRevisionChange.comparison_id == comparison_id
|
||||
).all()
|
||||
|
||||
# 재질별 길이 변화 계산
|
||||
material_impacts = {}
|
||||
|
||||
for change in changes:
|
||||
# 재질 정보 추출
|
||||
material = None
|
||||
length_change = 0
|
||||
|
||||
if change.change_type == "added" and change.new_material_grade:
|
||||
material = change.new_material_grade
|
||||
length_change = change.new_length_mm or 0
|
||||
elif change.change_type == "removed" and change.old_material_grade:
|
||||
material = change.old_material_grade
|
||||
length_change = -(change.old_length_mm or 0)
|
||||
elif change.change_type == "modified":
|
||||
# 재질이 같은 경우만 길이 변화 계산
|
||||
if (change.old_material_grade == change.new_material_grade and
|
||||
change.old_material_grade):
|
||||
material = change.old_material_grade
|
||||
old_length = change.old_length_mm or 0
|
||||
new_length = change.new_length_mm or 0
|
||||
length_change = new_length - old_length
|
||||
|
||||
if material and length_change != 0:
|
||||
if material not in material_impacts:
|
||||
material_impacts[material] = {
|
||||
"material": material,
|
||||
"total_length_change": 0,
|
||||
"change_count": 0
|
||||
}
|
||||
|
||||
material_impacts[material]["total_length_change"] += length_change
|
||||
material_impacts[material]["change_count"] += 1
|
||||
|
||||
# 결과 정리
|
||||
impact_summary = []
|
||||
for material, impact in material_impacts.items():
|
||||
length_change_m = impact["total_length_change"] / 1000 # mm to m
|
||||
impact_summary.append({
|
||||
"material": material,
|
||||
"length_change_mm": impact["total_length_change"],
|
||||
"length_change_m": round(length_change_m, 3),
|
||||
"change_count": impact["change_count"],
|
||||
"impact_type": "increase" if length_change_m > 0 else "decrease",
|
||||
"requires_additional_purchase": length_change_m > 0
|
||||
})
|
||||
|
||||
# 전체 영향 요약
|
||||
total_additional_purchase = sum(
|
||||
impact["length_change_m"] for impact in impact_summary
|
||||
if impact["requires_additional_purchase"]
|
||||
)
|
||||
|
||||
return {
|
||||
"comparison_id": comparison_id,
|
||||
"job_no": comparison.job_no,
|
||||
"material_impacts": impact_summary,
|
||||
"summary": {
|
||||
"total_materials_affected": len(impact_summary),
|
||||
"materials_requiring_additional_purchase": len([
|
||||
i for i in impact_summary if i["requires_additional_purchase"]
|
||||
]),
|
||||
"total_additional_purchase_needed": total_additional_purchase > 0,
|
||||
"total_additional_length_m": round(total_additional_purchase, 3)
|
||||
}
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get purchase impact: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"구매 영향 분석 실패: {str(e)}"
|
||||
)
|
||||
338
backend/app/routers/pipe_snapshot.py
Normal file
338
backend/app/routers/pipe_snapshot.py
Normal file
@@ -0,0 +1,338 @@
|
||||
"""
|
||||
PIPE 스냅샷 관리 API 라우터
|
||||
|
||||
Cutting Plan 확정, 스냅샷 생성, 이슈 관리 준비 등의 기능 제공
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Dict, List, Optional, Any
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..database import get_db
|
||||
from ..services.pipe_issue_snapshot_service import get_pipe_issue_snapshot_service, PipeIssueSnapshotService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/pipe-snapshot", tags=["pipe-snapshot"])
|
||||
|
||||
|
||||
# Pydantic 모델들
|
||||
class FinalizeCuttingPlanRequest(BaseModel):
|
||||
job_no: str
|
||||
created_by: Optional[str] = "system"
|
||||
|
||||
|
||||
class SnapshotStatusResponse(BaseModel):
|
||||
has_snapshot: bool
|
||||
snapshot_id: Optional[int] = None
|
||||
snapshot_name: Optional[str] = None
|
||||
is_locked: bool = False
|
||||
created_at: Optional[str] = None
|
||||
created_by: Optional[str] = None
|
||||
locked_at: Optional[str] = None
|
||||
locked_by: Optional[str] = None
|
||||
total_segments: int = 0
|
||||
total_drawings: int = 0
|
||||
drawing_issues_count: int = 0
|
||||
segment_issues_count: int = 0
|
||||
can_start_issue_management: bool = False
|
||||
message: str
|
||||
|
||||
|
||||
class SnapshotSegmentsResponse(BaseModel):
|
||||
snapshot_id: int
|
||||
segments: List[Dict[str, Any]]
|
||||
total_count: int
|
||||
areas: List[str]
|
||||
drawings: List[str]
|
||||
|
||||
|
||||
class FinalizeCuttingPlanResponse(BaseModel):
|
||||
success: bool
|
||||
snapshot_id: Optional[int] = None
|
||||
snapshot_name: Optional[str] = None
|
||||
total_segments: int = 0
|
||||
total_drawings: int = 0
|
||||
is_locked: bool = False
|
||||
locked_at: Optional[str] = None
|
||||
message: str
|
||||
next_action: Optional[str] = None
|
||||
|
||||
|
||||
@router.post("/finalize-cutting-plan", response_model=FinalizeCuttingPlanResponse)
|
||||
async def finalize_cutting_plan(
|
||||
request: FinalizeCuttingPlanRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Cutting Plan 확정 및 스냅샷 생성
|
||||
- 현재 단관 데이터를 스냅샷으로 고정
|
||||
- 이슈 관리 시작 가능 상태로 변경
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_issue_snapshot_service(db)
|
||||
result = service.create_and_lock_snapshot_on_finalize(
|
||||
request.job_no,
|
||||
request.created_by
|
||||
)
|
||||
|
||||
if not result["success"]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=result["message"]
|
||||
)
|
||||
|
||||
return FinalizeCuttingPlanResponse(**result)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to finalize cutting plan: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Cutting Plan 확정 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/status/{job_no}", response_model=SnapshotStatusResponse)
|
||||
async def get_snapshot_status(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
작업의 스냅샷 상태 조회
|
||||
- 스냅샷 존재 여부
|
||||
- 잠금 상태
|
||||
- 이슈 관리 가능 여부
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_issue_snapshot_service(db)
|
||||
result = service.get_snapshot_info(job_no)
|
||||
|
||||
return SnapshotStatusResponse(**result)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get snapshot status: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"스냅샷 상태 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/segments/{snapshot_id}", response_model=SnapshotSegmentsResponse)
|
||||
async def get_snapshot_segments(
|
||||
snapshot_id: int,
|
||||
area: Optional[str] = None,
|
||||
drawing_name: Optional[str] = None,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
스냅샷된 단관 정보 조회
|
||||
- 구역별/도면별 필터링 가능
|
||||
- 이슈 관리 페이지에서 사용
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_issue_snapshot_service(db)
|
||||
|
||||
# 단관 데이터 조회
|
||||
segments = service.get_snapshot_segments(snapshot_id, area, drawing_name)
|
||||
|
||||
# 사용 가능한 구역/도면 목록
|
||||
areas = service.get_available_areas(snapshot_id)
|
||||
drawings = service.get_available_drawings(snapshot_id, area)
|
||||
|
||||
return SnapshotSegmentsResponse(
|
||||
snapshot_id=snapshot_id,
|
||||
segments=segments,
|
||||
total_count=len(segments),
|
||||
areas=areas,
|
||||
drawings=drawings
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get snapshot segments: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"스냅샷 단관 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/areas/{snapshot_id}")
|
||||
async def get_available_areas(
|
||||
snapshot_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""스냅샷의 사용 가능한 구역 목록 조회"""
|
||||
try:
|
||||
service = get_pipe_issue_snapshot_service(db)
|
||||
areas = service.get_available_areas(snapshot_id)
|
||||
|
||||
return {
|
||||
"snapshot_id": snapshot_id,
|
||||
"areas": areas,
|
||||
"total_count": len(areas)
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get available areas: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"구역 목록 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/drawings/{snapshot_id}")
|
||||
async def get_available_drawings(
|
||||
snapshot_id: int,
|
||||
area: Optional[str] = None,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""스냅샷의 사용 가능한 도면 목록 조회"""
|
||||
try:
|
||||
service = get_pipe_issue_snapshot_service(db)
|
||||
drawings = service.get_available_drawings(snapshot_id, area)
|
||||
|
||||
return {
|
||||
"snapshot_id": snapshot_id,
|
||||
"area": area,
|
||||
"drawings": drawings,
|
||||
"total_count": len(drawings)
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get available drawings: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"도면 목록 조회 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/revision-protection/{job_no}")
|
||||
async def check_revision_protection(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
리비전 보호 상태 확인
|
||||
- 잠긴 스냅샷이 있으면 리비전 영향 받지 않음
|
||||
"""
|
||||
try:
|
||||
service = get_pipe_issue_snapshot_service(db)
|
||||
result = service.check_revision_protection(job_no)
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to check revision protection: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 보호 상태 확인 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/snapshot/{snapshot_id}")
|
||||
async def delete_snapshot(
|
||||
snapshot_id: int,
|
||||
force: bool = False,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
스냅샷 삭제 (개발/테스트용)
|
||||
- force=True: 강제 삭제 (이슈 데이터 포함)
|
||||
- force=False: 이슈가 없는 경우만 삭제
|
||||
"""
|
||||
try:
|
||||
from ..models import PipeIssueSnapshot, PipeDrawingIssue, PipeSegmentIssue
|
||||
|
||||
# 스냅샷 존재 확인
|
||||
snapshot = db.query(PipeIssueSnapshot).filter(
|
||||
PipeIssueSnapshot.id == snapshot_id
|
||||
).first()
|
||||
|
||||
if not snapshot:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="스냅샷을 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
# 이슈 데이터 확인
|
||||
drawing_issues_count = db.query(PipeDrawingIssue).filter(
|
||||
PipeDrawingIssue.snapshot_id == snapshot_id
|
||||
).count()
|
||||
|
||||
segment_issues_count = db.query(PipeSegmentIssue).filter(
|
||||
PipeSegmentIssue.snapshot_id == snapshot_id
|
||||
).count()
|
||||
|
||||
total_issues = drawing_issues_count + segment_issues_count
|
||||
|
||||
if total_issues > 0 and not force:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"이슈 데이터가 {total_issues}개 있습니다. force=true로 강제 삭제하거나 이슈를 먼저 정리해주세요."
|
||||
)
|
||||
|
||||
# 스냅샷 삭제 (CASCADE로 관련 데이터 자동 삭제)
|
||||
db.delete(snapshot)
|
||||
db.commit()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"스냅샷 '{snapshot.snapshot_name}'이 삭제되었습니다.",
|
||||
"deleted_snapshot_id": snapshot_id,
|
||||
"deleted_issues_count": total_issues
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Failed to delete snapshot: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"스냅샷 삭제 실패: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/job/{job_no}/history")
|
||||
async def get_snapshot_history(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""작업별 스냅샷 이력 조회"""
|
||||
try:
|
||||
from ..models import PipeIssueSnapshot
|
||||
|
||||
snapshots = db.query(PipeIssueSnapshot).filter(
|
||||
PipeIssueSnapshot.job_no == job_no
|
||||
).order_by(PipeIssueSnapshot.created_at.desc()).all()
|
||||
|
||||
history = []
|
||||
for snapshot in snapshots:
|
||||
history.append({
|
||||
"snapshot_id": snapshot.id,
|
||||
"snapshot_name": snapshot.snapshot_name,
|
||||
"is_active": snapshot.is_active,
|
||||
"is_locked": snapshot.is_locked,
|
||||
"created_at": snapshot.created_at.isoformat() if snapshot.created_at else None,
|
||||
"created_by": snapshot.created_by,
|
||||
"locked_at": snapshot.locked_at.isoformat() if snapshot.locked_at else None,
|
||||
"locked_by": snapshot.locked_by,
|
||||
"total_segments": snapshot.total_segments,
|
||||
"total_drawings": snapshot.total_drawings
|
||||
})
|
||||
|
||||
return {
|
||||
"job_no": job_no,
|
||||
"total_snapshots": len(history),
|
||||
"history": history
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get snapshot history: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"스냅샷 이력 조회 실패: {str(e)}"
|
||||
)
|
||||
97
backend/app/routers/revision_comparison.py
Normal file
97
backend/app/routers/revision_comparison.py
Normal file
@@ -0,0 +1,97 @@
|
||||
"""
|
||||
리비전 비교 API 엔드포인트
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
|
||||
from ..database import get_db
|
||||
from ..auth.middleware import get_current_user
|
||||
from ..services.revision_comparison_service import RevisionComparisonService
|
||||
from ..auth.models import User
|
||||
from ..utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/revision-comparison", tags=["Revision Comparison"])
|
||||
|
||||
|
||||
@router.post("/compare")
|
||||
async def compare_revisions(
|
||||
current_file_id: int = Query(..., description="현재 파일 ID"),
|
||||
previous_file_id: int = Query(..., description="이전 파일 ID"),
|
||||
category_filter: Optional[str] = Query(None, description="카테고리 필터"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
두 리비전 간 자재 비교
|
||||
"""
|
||||
|
||||
try:
|
||||
comparison_service = RevisionComparisonService(db)
|
||||
|
||||
comparison_result = comparison_service.compare_revisions(
|
||||
current_file_id=current_file_id,
|
||||
previous_file_id=previous_file_id,
|
||||
category_filter=category_filter
|
||||
)
|
||||
|
||||
logger.info(f"Revision comparison completed: {current_file_id} vs {previous_file_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": comparison_result,
|
||||
"message": "리비전 비교 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to compare revisions: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 비교 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/category/{current_file_id}/{previous_file_id}/{category}")
|
||||
async def get_category_comparison(
|
||||
current_file_id: int,
|
||||
previous_file_id: int,
|
||||
category: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
특정 카테고리의 리비전 비교
|
||||
"""
|
||||
|
||||
if category == 'PIPE':
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="PIPE 카테고리는 별도 처리가 필요합니다."
|
||||
)
|
||||
|
||||
try:
|
||||
comparison_service = RevisionComparisonService(db)
|
||||
|
||||
comparison_result = comparison_service.get_category_comparison(
|
||||
current_file_id=current_file_id,
|
||||
previous_file_id=previous_file_id,
|
||||
category=category
|
||||
)
|
||||
|
||||
logger.info(f"Category comparison completed: {category} ({current_file_id} vs {previous_file_id})")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": comparison_result,
|
||||
"message": f"{category} 카테고리 비교 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to compare category {category}: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"카테고리 비교 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
199
backend/app/routers/revision_material.py
Normal file
199
backend/app/routers/revision_material.py
Normal file
@@ -0,0 +1,199 @@
|
||||
"""
|
||||
리비전 자재 처리 API 엔드포인트
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List, Dict, Any
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..database import get_db
|
||||
from ..auth.middleware import get_current_user
|
||||
from ..services.revision_material_service import RevisionMaterialService
|
||||
from ..auth.models import User
|
||||
from ..utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/revision-material", tags=["Revision Material"])
|
||||
|
||||
|
||||
class ProcessingResultRequest(BaseModel):
|
||||
processing_results: List[Dict[str, Any]]
|
||||
|
||||
|
||||
class MaterialProcessRequest(BaseModel):
|
||||
action: str
|
||||
additional_data: Dict[str, Any] = {}
|
||||
|
||||
|
||||
@router.get("/category/{file_id}/{category}")
|
||||
async def get_category_materials(
|
||||
file_id: int,
|
||||
category: str,
|
||||
include_processing_info: bool = True,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 페이지용 카테고리별 자재 조회
|
||||
"""
|
||||
|
||||
if category == 'PIPE':
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="PIPE 카테고리는 별도 처리가 필요합니다."
|
||||
)
|
||||
|
||||
try:
|
||||
material_service = RevisionMaterialService(db)
|
||||
|
||||
materials = material_service.get_category_materials_for_revision(
|
||||
file_id=file_id,
|
||||
category=category,
|
||||
include_processing_info=include_processing_info
|
||||
)
|
||||
|
||||
# 처리 정보 요약
|
||||
processing_info = {
|
||||
"total_materials": len(materials),
|
||||
"by_status": {},
|
||||
"by_priority": {"high": 0, "medium": 0, "low": 0}
|
||||
}
|
||||
|
||||
for material in materials:
|
||||
proc_info = material.get('processing_info', {})
|
||||
status = proc_info.get('display_status', 'UNKNOWN')
|
||||
priority = proc_info.get('priority', 'medium')
|
||||
|
||||
processing_info["by_status"][status] = processing_info["by_status"].get(status, 0) + 1
|
||||
processing_info["by_priority"][priority] += 1
|
||||
|
||||
logger.info(f"Retrieved {len(materials)} materials for category {category} in file {file_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"materials": materials,
|
||||
"processing_info": processing_info
|
||||
},
|
||||
"message": f"{category} 카테고리 자재 조회 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get category materials: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"카테고리 자재 조회 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/process/{material_id}")
|
||||
async def process_material(
|
||||
material_id: int,
|
||||
request: MaterialProcessRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
개별 자재 처리
|
||||
"""
|
||||
|
||||
try:
|
||||
material_service = RevisionMaterialService(db)
|
||||
|
||||
# 자재 정보 조회
|
||||
material_query = """
|
||||
SELECT * FROM materials WHERE id = :material_id
|
||||
"""
|
||||
|
||||
result = material_service.db_service.execute_query(
|
||||
material_query, {"material_id": material_id}
|
||||
)
|
||||
material = result.fetchone()
|
||||
|
||||
if not material:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="자재를 찾을 수 없습니다."
|
||||
)
|
||||
|
||||
material_dict = dict(material._mapping)
|
||||
|
||||
# 액션에 따른 처리
|
||||
if request.action == "new_material":
|
||||
processing_result = material_service.process_new_material(
|
||||
material_dict,
|
||||
material_dict.get('classified_category', 'UNKNOWN')
|
||||
)
|
||||
elif request.action == "remove_material":
|
||||
processing_result = material_service.process_removed_material(
|
||||
material_dict,
|
||||
material_dict.get('classified_category', 'UNKNOWN')
|
||||
)
|
||||
else:
|
||||
# 기본 처리 (구매 상태별)
|
||||
# 이전 자재 정보가 필요한 경우 additional_data에서 가져옴
|
||||
prev_material = request.additional_data.get('previous_material', material_dict)
|
||||
|
||||
processing_result = material_service.process_material_by_purchase_status(
|
||||
prev_material,
|
||||
material_dict,
|
||||
material_dict.get('classified_category', 'UNKNOWN')
|
||||
)
|
||||
|
||||
# 처리 결과 적용
|
||||
apply_result = material_service.apply_material_processing_results([processing_result])
|
||||
|
||||
logger.info(f"Processed material {material_id} with action {request.action}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {
|
||||
"processing_result": processing_result,
|
||||
"apply_result": apply_result
|
||||
},
|
||||
"message": "자재 처리 완료"
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to process material {material_id}: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"자재 처리 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/apply-results")
|
||||
async def apply_processing_results(
|
||||
request: ProcessingResultRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
자재 처리 결과를 일괄 적용
|
||||
"""
|
||||
|
||||
try:
|
||||
material_service = RevisionMaterialService(db)
|
||||
|
||||
apply_result = material_service.apply_material_processing_results(
|
||||
request.processing_results
|
||||
)
|
||||
|
||||
logger.info(f"Applied {len(request.processing_results)} processing results")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": apply_result,
|
||||
"message": "처리 결과 적용 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to apply processing results: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"처리 결과 적용 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
130
backend/app/routers/revision_redirect.py
Normal file
130
backend/app/routers/revision_redirect.py
Normal file
@@ -0,0 +1,130 @@
|
||||
"""
|
||||
리비전 리다이렉트 API 엔드포인트
|
||||
BOM 페이지 접근 시 리비전 페이지로 리다이렉트 필요성 판단
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
|
||||
from ..database import get_db
|
||||
from ..auth.middleware import get_current_user
|
||||
from ..services.revision_logic_service import RevisionLogicService
|
||||
from ..auth.models import User
|
||||
from ..utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/revision-redirect", tags=["Revision Redirect"])
|
||||
|
||||
|
||||
@router.get("/check/{job_no}/{file_id}")
|
||||
async def check_revision_redirect(
|
||||
job_no: str,
|
||||
file_id: int,
|
||||
previous_file_id: Optional[int] = None,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 페이지 리다이렉트 필요성 확인
|
||||
|
||||
Args:
|
||||
job_no: 작업 번호
|
||||
file_id: 현재 파일 ID
|
||||
previous_file_id: 이전 파일 ID (선택사항)
|
||||
|
||||
Returns:
|
||||
{
|
||||
"should_redirect": bool,
|
||||
"reason": str,
|
||||
"redirect_url": str,
|
||||
"processing_summary": dict
|
||||
}
|
||||
"""
|
||||
|
||||
try:
|
||||
revision_service = RevisionLogicService(db)
|
||||
|
||||
# 리다이렉트 필요성 판단
|
||||
should_redirect, reason = revision_service.should_redirect_to_revision_page(
|
||||
job_no=job_no,
|
||||
current_file_id=file_id,
|
||||
previous_file_id=previous_file_id
|
||||
)
|
||||
|
||||
result = {
|
||||
"should_redirect": should_redirect,
|
||||
"reason": reason,
|
||||
"redirect_url": f"/enhanced-revision?job_no={job_no}¤t_file_id={file_id}",
|
||||
"processing_summary": None
|
||||
}
|
||||
|
||||
# 상세 처리 결과도 함께 반환 (필요시)
|
||||
if should_redirect:
|
||||
processing_result = revision_service.process_revision_by_purchase_status(
|
||||
job_no=job_no,
|
||||
current_file_id=file_id,
|
||||
previous_file_id=previous_file_id
|
||||
)
|
||||
result["processing_summary"] = processing_result["summary"]
|
||||
|
||||
logger.info(f"Revision redirect check for {job_no}/{file_id}: {should_redirect}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": result,
|
||||
"message": "리비전 리다이렉트 확인 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to check revision redirect: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 리다이렉트 확인 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/process-revision/{job_no}/{file_id}")
|
||||
async def process_revision_logic(
|
||||
job_no: str,
|
||||
file_id: int,
|
||||
previous_file_id: Optional[int] = None,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 로직 처리 및 결과 반환
|
||||
|
||||
Args:
|
||||
job_no: 작업 번호
|
||||
file_id: 현재 파일 ID
|
||||
previous_file_id: 이전 파일 ID (선택사항)
|
||||
|
||||
Returns:
|
||||
전체 리비전 처리 결과
|
||||
"""
|
||||
|
||||
try:
|
||||
revision_service = RevisionLogicService(db)
|
||||
|
||||
processing_result = revision_service.process_revision_by_purchase_status(
|
||||
job_no=job_no,
|
||||
current_file_id=file_id,
|
||||
previous_file_id=previous_file_id
|
||||
)
|
||||
|
||||
logger.info(f"Revision processing completed for {job_no}/{file_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": processing_result,
|
||||
"message": "리비전 처리 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to process revision logic: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 처리 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
258
backend/app/routers/revision_status.py
Normal file
258
backend/app/routers/revision_status.py
Normal file
@@ -0,0 +1,258 @@
|
||||
"""
|
||||
리비전 상태 관리 API 엔드포인트
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..database import get_db
|
||||
from ..auth.middleware import get_current_user
|
||||
from ..services.revision_status_service import RevisionStatusService
|
||||
from ..auth.models import User
|
||||
from ..utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/revision-status", tags=["Revision Status"])
|
||||
|
||||
|
||||
class CreateComparisonRequest(BaseModel):
|
||||
job_no: str
|
||||
current_file_id: int
|
||||
previous_file_id: int
|
||||
comparison_result: dict
|
||||
|
||||
|
||||
class RejectComparisonRequest(BaseModel):
|
||||
reason: str = ""
|
||||
|
||||
|
||||
@router.get("/{job_no}/{file_id}")
|
||||
async def get_revision_status(
|
||||
job_no: str,
|
||||
file_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 상태 조회
|
||||
"""
|
||||
|
||||
try:
|
||||
status_service = RevisionStatusService(db)
|
||||
|
||||
revision_status = status_service.get_revision_status(job_no, file_id)
|
||||
|
||||
if "error" in revision_status:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail=revision_status["error"]
|
||||
)
|
||||
|
||||
logger.info(f"Retrieved revision status for {job_no}/{file_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": revision_status,
|
||||
"message": "리비전 상태 조회 완료"
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get revision status: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 상태 조회 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/history/{job_no}")
|
||||
async def get_revision_history(
|
||||
job_no: str,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
작업의 전체 리비전 히스토리 조회
|
||||
"""
|
||||
|
||||
try:
|
||||
status_service = RevisionStatusService(db)
|
||||
|
||||
history = status_service.get_revision_history(job_no)
|
||||
|
||||
logger.info(f"Retrieved revision history for {job_no}: {len(history)} revisions")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": history,
|
||||
"message": "리비전 히스토리 조회 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get revision history: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"리비전 히스토리 조회 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/create-comparison")
|
||||
async def create_comparison_record(
|
||||
request: CreateComparisonRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 비교 기록 생성
|
||||
"""
|
||||
|
||||
try:
|
||||
status_service = RevisionStatusService(db)
|
||||
|
||||
comparison_id = status_service.create_revision_comparison_record(
|
||||
job_no=request.job_no,
|
||||
current_file_id=request.current_file_id,
|
||||
previous_file_id=request.previous_file_id,
|
||||
comparison_result=request.comparison_result,
|
||||
created_by=current_user.username
|
||||
)
|
||||
|
||||
logger.info(f"Created revision comparison record: {comparison_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {"comparison_id": comparison_id},
|
||||
"message": "리비전 비교 기록 생성 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to create comparison record: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"비교 기록 생성 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/apply-comparison/{comparison_id}")
|
||||
async def apply_comparison(
|
||||
comparison_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 비교 결과 적용
|
||||
"""
|
||||
|
||||
try:
|
||||
status_service = RevisionStatusService(db)
|
||||
|
||||
apply_result = status_service.apply_revision_comparison(
|
||||
comparison_id=comparison_id,
|
||||
applied_by=current_user.username
|
||||
)
|
||||
|
||||
if not apply_result["success"]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=apply_result["error"]
|
||||
)
|
||||
|
||||
logger.info(f"Applied revision comparison: {comparison_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": apply_result,
|
||||
"message": "리비전 비교 결과 적용 완료"
|
||||
}
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to apply comparison {comparison_id}: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"비교 결과 적용 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.get("/pending")
|
||||
async def get_pending_revisions(
|
||||
job_no: Optional[str] = Query(None, description="특정 작업의 대기 중인 리비전만 조회"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
대기 중인 리비전 목록 조회
|
||||
"""
|
||||
|
||||
try:
|
||||
status_service = RevisionStatusService(db)
|
||||
|
||||
pending_revisions = status_service.get_pending_revisions(job_no)
|
||||
|
||||
logger.info(f"Retrieved {len(pending_revisions)} pending revisions")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": pending_revisions,
|
||||
"message": "대기 중인 리비전 조회 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get pending revisions: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"대기 중인 리비전 조회 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@router.post("/reject-comparison/{comparison_id}")
|
||||
async def reject_comparison(
|
||||
comparison_id: int,
|
||||
request: RejectComparisonRequest,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
리비전 비교 결과 거부
|
||||
"""
|
||||
|
||||
try:
|
||||
# 비교 기록을 거부 상태로 업데이트
|
||||
update_query = """
|
||||
UPDATE revision_comparisons
|
||||
SET is_applied = false,
|
||||
notes = CONCAT(COALESCE(notes, ''), '\n거부됨: ', :reason, ' (by ', :rejected_by, ')')
|
||||
WHERE id = :comparison_id
|
||||
"""
|
||||
|
||||
from ..services.database_service import DatabaseService
|
||||
db_service = DatabaseService(db)
|
||||
|
||||
db_service.execute_query(update_query, {
|
||||
"comparison_id": comparison_id,
|
||||
"reason": request.reason or "사유 없음",
|
||||
"rejected_by": current_user.username
|
||||
})
|
||||
|
||||
db.commit()
|
||||
|
||||
logger.info(f"Rejected revision comparison: {comparison_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": {"comparison_id": comparison_id, "reason": request.reason},
|
||||
"message": "리비전 비교 결과 거부 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to reject comparison {comparison_id}: {e}")
|
||||
db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"비교 결과 거부 중 오류가 발생했습니다: {str(e)}"
|
||||
)
|
||||
488
backend/app/services/enhanced_revision_service.py
Normal file
488
backend/app/services/enhanced_revision_service.py
Normal file
@@ -0,0 +1,488 @@
|
||||
"""
|
||||
강화된 리비전 관리 서비스
|
||||
구매 상태 기반 리비전 비교 및 처리
|
||||
"""
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text, and_, or_
|
||||
from typing import List, Dict, Any, Optional, Tuple
|
||||
from decimal import Decimal
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
|
||||
from ..models import Material, File
|
||||
from ..utils.logger import get_logger
|
||||
from .database_service import DatabaseService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class EnhancedRevisionService:
|
||||
"""강화된 리비전 관리 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.db_service = DatabaseService(db)
|
||||
|
||||
def compare_revisions_with_purchase_status(
|
||||
self,
|
||||
job_no: str,
|
||||
current_file_id: int,
|
||||
previous_file_id: Optional[int] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
구매 상태를 고려한 리비전 비교
|
||||
|
||||
Args:
|
||||
job_no: 작업 번호
|
||||
current_file_id: 현재 파일 ID
|
||||
previous_file_id: 이전 파일 ID (None이면 자동 탐지)
|
||||
|
||||
Returns:
|
||||
비교 결과 딕셔너리
|
||||
"""
|
||||
|
||||
if not previous_file_id:
|
||||
previous_file_id = self._get_previous_file_id(job_no, current_file_id)
|
||||
|
||||
if not previous_file_id:
|
||||
return self._handle_first_revision(current_file_id)
|
||||
|
||||
# 이전 리비전 자재 조회 (구매 상태 포함)
|
||||
previous_materials = self._get_materials_with_purchase_status(previous_file_id)
|
||||
|
||||
# 현재 리비전 자재 조회
|
||||
current_materials = self._get_materials_with_purchase_status(current_file_id)
|
||||
|
||||
# 자재별 비교 수행
|
||||
comparison_result = self._perform_detailed_comparison(
|
||||
previous_materials, current_materials, job_no
|
||||
)
|
||||
|
||||
return comparison_result
|
||||
|
||||
def _get_materials_with_purchase_status(self, file_id: int) -> Dict[str, Dict]:
|
||||
"""파일의 자재를 구매 상태와 함께 조회"""
|
||||
|
||||
query = """
|
||||
SELECT
|
||||
m.id, m.original_description, m.classified_category,
|
||||
m.material_grade, m.schedule, m.size_spec, m.main_nom, m.red_nom,
|
||||
m.quantity, m.unit, m.length, m.drawing_name, m.line_no,
|
||||
m.purchase_confirmed, m.confirmed_quantity, m.purchase_status,
|
||||
m.material_hash, m.revision_status,
|
||||
-- PIPE 자재 특별 키 생성
|
||||
CASE
|
||||
WHEN m.classified_category = 'PIPE' THEN
|
||||
CONCAT(m.drawing_name, '|', m.line_no, '|', COALESCE(m.length, 0))
|
||||
ELSE
|
||||
m.material_hash
|
||||
END as comparison_key
|
||||
FROM materials m
|
||||
WHERE m.file_id = :file_id
|
||||
ORDER BY m.line_number
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {"file_id": file_id})
|
||||
materials = {}
|
||||
|
||||
for row in result.fetchall():
|
||||
row_dict = dict(row._mapping)
|
||||
comparison_key = row_dict['comparison_key']
|
||||
|
||||
# PIPE 자재의 경우 도면-라인넘버별로 길이 합산
|
||||
if row_dict['classified_category'] == 'PIPE':
|
||||
if comparison_key in materials:
|
||||
# 기존 자재에 길이 합산
|
||||
materials[comparison_key]['quantity'] += row_dict['quantity']
|
||||
materials[comparison_key]['total_length'] = (
|
||||
materials[comparison_key].get('total_length', 0) +
|
||||
(row_dict['length'] or 0) * row_dict['quantity']
|
||||
)
|
||||
else:
|
||||
row_dict['total_length'] = (row_dict['length'] or 0) * row_dict['quantity']
|
||||
materials[comparison_key] = row_dict
|
||||
else:
|
||||
materials[comparison_key] = row_dict
|
||||
|
||||
return materials
|
||||
|
||||
def _perform_detailed_comparison(
|
||||
self,
|
||||
previous_materials: Dict[str, Dict],
|
||||
current_materials: Dict[str, Dict],
|
||||
job_no: str
|
||||
) -> Dict[str, Any]:
|
||||
"""상세 비교 수행"""
|
||||
|
||||
result = {
|
||||
"job_no": job_no,
|
||||
"comparison_date": datetime.now().isoformat(),
|
||||
"summary": {
|
||||
"total_previous": len(previous_materials),
|
||||
"total_current": len(current_materials),
|
||||
"purchased_maintained": 0,
|
||||
"purchased_increased": 0,
|
||||
"purchased_decreased": 0,
|
||||
"unpurchased_maintained": 0,
|
||||
"unpurchased_increased": 0,
|
||||
"unpurchased_decreased": 0,
|
||||
"new_materials": 0,
|
||||
"deleted_materials": 0
|
||||
},
|
||||
"changes": {
|
||||
"purchased_materials": {
|
||||
"maintained": [],
|
||||
"additional_purchase_needed": [],
|
||||
"excess_inventory": []
|
||||
},
|
||||
"unpurchased_materials": {
|
||||
"maintained": [],
|
||||
"quantity_updated": [],
|
||||
"quantity_reduced": []
|
||||
},
|
||||
"new_materials": [],
|
||||
"deleted_materials": []
|
||||
}
|
||||
}
|
||||
|
||||
# 이전 자재 기준으로 비교
|
||||
for key, prev_material in previous_materials.items():
|
||||
if key in current_materials:
|
||||
curr_material = current_materials[key]
|
||||
change_info = self._analyze_material_change(prev_material, curr_material)
|
||||
|
||||
if prev_material.get('purchase_confirmed', False):
|
||||
# 구매 완료된 자재 처리
|
||||
self._process_purchased_material_change(result, change_info, prev_material, curr_material)
|
||||
else:
|
||||
# 구매 미완료 자재 처리
|
||||
self._process_unpurchased_material_change(result, change_info, prev_material, curr_material)
|
||||
else:
|
||||
# 삭제된 자재
|
||||
result["changes"]["deleted_materials"].append({
|
||||
"material": prev_material,
|
||||
"reason": "removed_from_new_revision"
|
||||
})
|
||||
result["summary"]["deleted_materials"] += 1
|
||||
|
||||
# 신규 자재 처리
|
||||
for key, curr_material in current_materials.items():
|
||||
if key not in previous_materials:
|
||||
result["changes"]["new_materials"].append({
|
||||
"material": curr_material,
|
||||
"action": "new_material_added"
|
||||
})
|
||||
result["summary"]["new_materials"] += 1
|
||||
|
||||
return result
|
||||
|
||||
def _analyze_material_change(self, prev_material: Dict, curr_material: Dict) -> Dict:
|
||||
"""자재 변경 사항 분석"""
|
||||
|
||||
prev_qty = float(prev_material.get('quantity', 0))
|
||||
curr_qty = float(curr_material.get('quantity', 0))
|
||||
|
||||
# PIPE 자재의 경우 총 길이로 비교
|
||||
if prev_material.get('classified_category') == 'PIPE':
|
||||
prev_total = prev_material.get('total_length', 0)
|
||||
curr_total = curr_material.get('total_length', 0)
|
||||
|
||||
return {
|
||||
"quantity_change": curr_qty - prev_qty,
|
||||
"length_change": curr_total - prev_total,
|
||||
"change_type": "length_based" if abs(curr_total - prev_total) > 0.01 else "no_change"
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"quantity_change": curr_qty - prev_qty,
|
||||
"change_type": "increased" if curr_qty > prev_qty else "decreased" if curr_qty < prev_qty else "no_change"
|
||||
}
|
||||
|
||||
def _process_purchased_material_change(
|
||||
self,
|
||||
result: Dict,
|
||||
change_info: Dict,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict
|
||||
):
|
||||
"""구매 완료 자재 변경 처리"""
|
||||
|
||||
if change_info["change_type"] == "no_change":
|
||||
result["changes"]["purchased_materials"]["maintained"].append({
|
||||
"material": curr_material,
|
||||
"action": "maintain_inventory"
|
||||
})
|
||||
result["summary"]["purchased_maintained"] += 1
|
||||
|
||||
elif change_info["change_type"] == "increased":
|
||||
additional_qty = change_info["quantity_change"]
|
||||
result["changes"]["purchased_materials"]["additional_purchase_needed"].append({
|
||||
"material": curr_material,
|
||||
"previous_quantity": prev_material.get('quantity'),
|
||||
"current_quantity": curr_material.get('quantity'),
|
||||
"additional_needed": additional_qty,
|
||||
"action": "additional_purchase_required"
|
||||
})
|
||||
result["summary"]["purchased_increased"] += 1
|
||||
|
||||
else: # decreased
|
||||
excess_qty = abs(change_info["quantity_change"])
|
||||
result["changes"]["purchased_materials"]["excess_inventory"].append({
|
||||
"material": curr_material,
|
||||
"previous_quantity": prev_material.get('quantity'),
|
||||
"current_quantity": curr_material.get('quantity'),
|
||||
"excess_quantity": excess_qty,
|
||||
"action": "mark_as_excess_inventory"
|
||||
})
|
||||
result["summary"]["purchased_decreased"] += 1
|
||||
|
||||
def _process_unpurchased_material_change(
|
||||
self,
|
||||
result: Dict,
|
||||
change_info: Dict,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict
|
||||
):
|
||||
"""구매 미완료 자재 변경 처리"""
|
||||
|
||||
if change_info["change_type"] == "no_change":
|
||||
result["changes"]["unpurchased_materials"]["maintained"].append({
|
||||
"material": curr_material,
|
||||
"action": "maintain_purchase_pending"
|
||||
})
|
||||
result["summary"]["unpurchased_maintained"] += 1
|
||||
|
||||
elif change_info["change_type"] == "increased":
|
||||
result["changes"]["unpurchased_materials"]["quantity_updated"].append({
|
||||
"material": curr_material,
|
||||
"previous_quantity": prev_material.get('quantity'),
|
||||
"current_quantity": curr_material.get('quantity'),
|
||||
"quantity_change": change_info["quantity_change"],
|
||||
"action": "update_purchase_quantity"
|
||||
})
|
||||
result["summary"]["unpurchased_increased"] += 1
|
||||
|
||||
else: # decreased
|
||||
result["changes"]["unpurchased_materials"]["quantity_reduced"].append({
|
||||
"material": curr_material,
|
||||
"previous_quantity": prev_material.get('quantity'),
|
||||
"current_quantity": curr_material.get('quantity'),
|
||||
"quantity_change": change_info["quantity_change"],
|
||||
"action": "reduce_purchase_quantity"
|
||||
})
|
||||
result["summary"]["unpurchased_decreased"] += 1
|
||||
|
||||
def _get_previous_file_id(self, job_no: str, current_file_id: int) -> Optional[int]:
|
||||
"""이전 파일 ID 자동 탐지"""
|
||||
|
||||
query = """
|
||||
SELECT id, revision
|
||||
FROM files
|
||||
WHERE job_no = :job_no AND id != :current_file_id AND is_active = true
|
||||
ORDER BY upload_date DESC
|
||||
LIMIT 1
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {
|
||||
"job_no": job_no,
|
||||
"current_file_id": current_file_id
|
||||
})
|
||||
|
||||
row = result.fetchone()
|
||||
return row.id if row else None
|
||||
|
||||
def _handle_first_revision(self, current_file_id: int) -> Dict[str, Any]:
|
||||
"""첫 번째 리비전 처리"""
|
||||
|
||||
materials = self._get_materials_with_purchase_status(current_file_id)
|
||||
|
||||
return {
|
||||
"job_no": None,
|
||||
"comparison_date": datetime.now().isoformat(),
|
||||
"is_first_revision": True,
|
||||
"summary": {
|
||||
"total_materials": len(materials),
|
||||
"all_new": True
|
||||
},
|
||||
"changes": {
|
||||
"new_materials": [{"material": mat, "action": "first_revision"} for mat in materials.values()]
|
||||
}
|
||||
}
|
||||
|
||||
def apply_revision_changes(self, comparison_result: Dict, current_file_id: int) -> Dict[str, Any]:
|
||||
"""리비전 변경사항을 DB에 적용"""
|
||||
|
||||
try:
|
||||
# 각 변경사항별로 DB 업데이트
|
||||
updates_applied = {
|
||||
"purchased_materials": 0,
|
||||
"unpurchased_materials": 0,
|
||||
"new_materials": 0,
|
||||
"deleted_materials": 0
|
||||
}
|
||||
|
||||
changes = comparison_result.get("changes", {})
|
||||
|
||||
# 구매 완료 자재 처리
|
||||
purchased = changes.get("purchased_materials", {})
|
||||
for category, materials in purchased.items():
|
||||
for item in materials:
|
||||
material = item["material"]
|
||||
action = item["action"]
|
||||
|
||||
if action == "additional_purchase_required":
|
||||
self._mark_additional_purchase_needed(material, item)
|
||||
elif action == "mark_as_excess_inventory":
|
||||
self._mark_excess_inventory(material, item)
|
||||
|
||||
updates_applied["purchased_materials"] += 1
|
||||
|
||||
# 구매 미완료 자재 처리
|
||||
unpurchased = changes.get("unpurchased_materials", {})
|
||||
for category, materials in unpurchased.items():
|
||||
for item in materials:
|
||||
material = item["material"]
|
||||
action = item["action"]
|
||||
|
||||
if action == "update_purchase_quantity":
|
||||
self._update_purchase_quantity(material, item)
|
||||
elif action == "reduce_purchase_quantity":
|
||||
self._reduce_purchase_quantity(material, item)
|
||||
|
||||
updates_applied["unpurchased_materials"] += 1
|
||||
|
||||
# 신규 자재 처리
|
||||
for item in changes.get("new_materials", []):
|
||||
self._mark_new_material(item["material"])
|
||||
updates_applied["new_materials"] += 1
|
||||
|
||||
# 삭제된 자재 처리
|
||||
for item in changes.get("deleted_materials", []):
|
||||
self._mark_deleted_material(item["material"])
|
||||
updates_applied["deleted_materials"] += 1
|
||||
|
||||
self.db.commit()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"updates_applied": updates_applied,
|
||||
"message": "리비전 변경사항이 성공적으로 적용되었습니다."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to apply revision changes: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"message": "리비전 변경사항 적용 중 오류가 발생했습니다."
|
||||
}
|
||||
|
||||
def _mark_additional_purchase_needed(self, material: Dict, change_info: Dict):
|
||||
"""추가 구매 필요 표시"""
|
||||
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET revision_status = 'additional_purchase_needed',
|
||||
notes = CONCAT(COALESCE(notes, ''),
|
||||
'\n추가 구매 필요: ', :additional_qty, ' ', unit)
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
self.db_service.execute_query(update_query, {
|
||||
"material_id": material["id"],
|
||||
"additional_qty": change_info["additional_needed"]
|
||||
})
|
||||
|
||||
def _mark_excess_inventory(self, material: Dict, change_info: Dict):
|
||||
"""잉여 재고 표시"""
|
||||
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET revision_status = 'excess_inventory',
|
||||
notes = CONCAT(COALESCE(notes, ''),
|
||||
'\n잉여 재고: ', :excess_qty, ' ', unit)
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
self.db_service.execute_query(update_query, {
|
||||
"material_id": material["id"],
|
||||
"excess_qty": change_info["excess_quantity"]
|
||||
})
|
||||
|
||||
def _update_purchase_quantity(self, material: Dict, change_info: Dict):
|
||||
"""구매 수량 업데이트"""
|
||||
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET quantity = :new_quantity,
|
||||
revision_status = 'quantity_updated',
|
||||
notes = CONCAT(COALESCE(notes, ''),
|
||||
'\n수량 변경: ', :prev_qty, ' → ', :new_qty)
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
self.db_service.execute_query(update_query, {
|
||||
"material_id": material["id"],
|
||||
"new_quantity": change_info["current_quantity"],
|
||||
"prev_qty": change_info["previous_quantity"],
|
||||
"new_qty": change_info["current_quantity"]
|
||||
})
|
||||
|
||||
def _reduce_purchase_quantity(self, material: Dict, change_info: Dict):
|
||||
"""구매 수량 감소"""
|
||||
|
||||
if change_info["current_quantity"] <= 0:
|
||||
# 수량이 0 이하면 삭제 표시
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET revision_status = 'deleted',
|
||||
is_active = false,
|
||||
notes = CONCAT(COALESCE(notes, ''), '\n리비전에서 삭제됨')
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
else:
|
||||
# 수량만 감소
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET quantity = :new_quantity,
|
||||
revision_status = 'quantity_reduced',
|
||||
notes = CONCAT(COALESCE(notes, ''),
|
||||
'\n수량 감소: ', :prev_qty, ' → ', :new_qty)
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
self.db_service.execute_query(update_query, {
|
||||
"material_id": material["id"],
|
||||
"new_quantity": change_info.get("current_quantity", 0),
|
||||
"prev_qty": change_info.get("previous_quantity", 0),
|
||||
"new_qty": change_info.get("current_quantity", 0)
|
||||
})
|
||||
|
||||
def _mark_new_material(self, material: Dict):
|
||||
"""신규 자재 표시"""
|
||||
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET revision_status = 'new_in_revision'
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
self.db_service.execute_query(update_query, {
|
||||
"material_id": material["id"]
|
||||
})
|
||||
|
||||
def _mark_deleted_material(self, material: Dict):
|
||||
"""삭제된 자재 표시 (이전 리비전에서)"""
|
||||
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET revision_status = 'removed_in_new_revision',
|
||||
notes = CONCAT(COALESCE(notes, ''), '\n신규 리비전에서 제거됨')
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
self.db_service.execute_query(update_query, {
|
||||
"material_id": material["id"]
|
||||
})
|
||||
396
backend/app/services/pipe_data_extraction_service.py
Normal file
396
backend/app/services/pipe_data_extraction_service.py
Normal file
@@ -0,0 +1,396 @@
|
||||
"""
|
||||
PIPE 데이터 추출 서비스
|
||||
|
||||
BOM 파일에서 PIPE 자재의 도면-라인번호-길이 정보를 추출하고 처리
|
||||
"""
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import Dict, List, Optional, Any, Tuple
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import Material, File
|
||||
from ..utils.pipe_utils import (
|
||||
PipeConstants, PipeDataExtractor, PipeValidator,
|
||||
PipeFormatter, PipeLogger
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PipeDataExtractionService:
|
||||
"""PIPE 데이터 추출 및 처리 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def extract_pipe_data_from_file(self, file_id: int) -> Dict[str, Any]:
|
||||
"""
|
||||
파일에서 PIPE 데이터 추출
|
||||
|
||||
Args:
|
||||
file_id: 파일 ID
|
||||
|
||||
Returns:
|
||||
추출된 PIPE 데이터 정보
|
||||
"""
|
||||
try:
|
||||
# 1. 파일 정보 확인
|
||||
file_info = self.db.query(File).filter(File.id == file_id).first()
|
||||
if not file_info:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "파일을 찾을 수 없습니다."
|
||||
}
|
||||
|
||||
# 2. PIPE 자재 조회
|
||||
pipe_materials = self._get_pipe_materials_from_file(file_id)
|
||||
if not pipe_materials:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "PIPE 자재가 없습니다."
|
||||
}
|
||||
|
||||
# 3. 데이터 추출 및 정제
|
||||
extracted_data = []
|
||||
extraction_stats = {
|
||||
"total_materials": len(pipe_materials),
|
||||
"successful_extractions": 0,
|
||||
"failed_extractions": 0,
|
||||
"unique_drawings": set(),
|
||||
"unique_line_numbers": set(),
|
||||
"total_length": 0
|
||||
}
|
||||
|
||||
for material in pipe_materials:
|
||||
extracted_item = self._extract_pipe_item_data(material)
|
||||
if extracted_item["success"]:
|
||||
extracted_data.append(extracted_item["data"])
|
||||
extraction_stats["successful_extractions"] += 1
|
||||
extraction_stats["unique_drawings"].add(extracted_item["data"]["drawing_name"])
|
||||
if extracted_item["data"]["line_no"]:
|
||||
extraction_stats["unique_line_numbers"].add(extracted_item["data"]["line_no"])
|
||||
extraction_stats["total_length"] += extracted_item["data"]["length_mm"]
|
||||
else:
|
||||
extraction_stats["failed_extractions"] += 1
|
||||
logger.warning(f"Failed to extract data from material {material.id}: {extracted_item['message']}")
|
||||
|
||||
# 4. 통계 정리
|
||||
extraction_stats["unique_drawings"] = len(extraction_stats["unique_drawings"])
|
||||
extraction_stats["unique_line_numbers"] = len(extraction_stats["unique_line_numbers"])
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"file_id": file_id,
|
||||
"file_name": file_info.original_filename,
|
||||
"job_no": file_info.job_no,
|
||||
"extracted_data": extracted_data,
|
||||
"extraction_stats": extraction_stats,
|
||||
"message": f"PIPE 데이터 추출 완료: {extraction_stats['successful_extractions']}개 성공, {extraction_stats['failed_extractions']}개 실패"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to extract pipe data from file {file_id}: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"PIPE 데이터 추출 실패: {str(e)}"
|
||||
}
|
||||
|
||||
def _get_pipe_materials_from_file(self, file_id: int) -> List[Material]:
|
||||
"""파일에서 PIPE 자재 조회"""
|
||||
return self.db.query(Material).filter(
|
||||
Material.file_id == file_id,
|
||||
Material.classified_category == 'PIPE',
|
||||
Material.is_active == True
|
||||
).all()
|
||||
|
||||
def _extract_pipe_item_data(self, material: Material) -> Dict[str, Any]:
|
||||
"""개별 PIPE 자재에서 데이터 추출"""
|
||||
try:
|
||||
# 기본 정보
|
||||
data = {
|
||||
"material_id": material.id,
|
||||
"drawing_name": self._extract_drawing_name(material),
|
||||
"line_no": self._extract_line_number(material),
|
||||
"material_grade": self._extract_material_grade(material),
|
||||
"schedule_spec": self._extract_schedule_spec(material),
|
||||
"nominal_size": self._extract_nominal_size(material),
|
||||
"length_mm": self._extract_length(material),
|
||||
"end_preparation": self._extract_end_preparation(material),
|
||||
"quantity": int(material.quantity or 1),
|
||||
"description": material.description or "",
|
||||
"original_description": material.description or ""
|
||||
}
|
||||
|
||||
# 데이터 검증
|
||||
validation_result = self._validate_extracted_data(data)
|
||||
if not validation_result["valid"]:
|
||||
return {
|
||||
"success": False,
|
||||
"message": validation_result["message"],
|
||||
"data": data
|
||||
}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"data": data,
|
||||
"message": "데이터 추출 성공"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to extract data from material {material.id}: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"데이터 추출 실패: {str(e)}",
|
||||
"data": {}
|
||||
}
|
||||
|
||||
def _extract_drawing_name(self, material: Material) -> str:
|
||||
"""도면명 추출"""
|
||||
# 1. drawing_name 필드 우선
|
||||
if material.drawing_name:
|
||||
return material.drawing_name.strip()
|
||||
|
||||
# 2. description에서 추출 시도
|
||||
if material.description:
|
||||
# 일반적인 도면명 패턴 (P&ID-001, DWG-A-001 등)
|
||||
drawing_patterns = [
|
||||
r'(P&ID[-_]\w+)',
|
||||
r'(DWG[-_]\w+[-_]\w+)',
|
||||
r'(DRAWING[-_]\w+)',
|
||||
r'([A-Z]+[-_]\d+[-_]\w+)',
|
||||
r'([A-Z]+\d+[A-Z]*)'
|
||||
]
|
||||
|
||||
for pattern in drawing_patterns:
|
||||
match = re.search(pattern, material.description.upper())
|
||||
if match:
|
||||
return match.group(1)
|
||||
|
||||
return "UNKNOWN_DRAWING"
|
||||
|
||||
def _extract_line_number(self, material: Material) -> str:
|
||||
"""라인번호 추출"""
|
||||
# 1. line_no 필드 우선
|
||||
if material.line_no:
|
||||
return material.line_no.strip()
|
||||
|
||||
# 2. description에서 추출 시도
|
||||
if material.description:
|
||||
# 라인번호 패턴 (LINE-001, L-001, 1001 등)
|
||||
line_patterns = [
|
||||
r'LINE[-_]?(\w+)',
|
||||
r'L[-_]?(\d+[A-Z]*)',
|
||||
r'(\d{3,4}[A-Z]*)', # 3-4자리 숫자 + 선택적 문자
|
||||
r'([A-Z]\d+[A-Z]*)' # 문자+숫자+선택적문자
|
||||
]
|
||||
|
||||
for pattern in line_patterns:
|
||||
match = re.search(pattern, material.description.upper())
|
||||
if match:
|
||||
return f"LINE-{match.group(1)}"
|
||||
|
||||
return "" # 라인번호는 필수가 아님
|
||||
|
||||
def _extract_material_grade(self, material: Material) -> str:
|
||||
"""재질 추출"""
|
||||
# 1. full_material_grade 필드 우선
|
||||
if material.full_material_grade:
|
||||
return material.full_material_grade.strip()
|
||||
|
||||
# 2. description에서 추출 시도
|
||||
if material.description:
|
||||
# 일반적인 재질 패턴
|
||||
material_patterns = [
|
||||
r'(A\d+\s*GR\.?\s*[A-Z])', # A106 GR.B
|
||||
r'(A\d+)', # A106
|
||||
r'(SS\d+[A-Z]*)', # SS316L
|
||||
r'(CS|CARBON\s*STEEL)', # Carbon Steel
|
||||
r'(SS|STAINLESS\s*STEEL)' # Stainless Steel
|
||||
]
|
||||
|
||||
for pattern in material_patterns:
|
||||
match = re.search(pattern, material.description.upper())
|
||||
if match:
|
||||
return match.group(1).strip()
|
||||
|
||||
return "UNKNOWN"
|
||||
|
||||
def _extract_schedule_spec(self, material: Material) -> str:
|
||||
"""스케줄/규격 추출"""
|
||||
if material.description:
|
||||
# 스케줄 패턴 (SCH40, SCH80, STD, XS 등)
|
||||
schedule_patterns = [
|
||||
r'(SCH\s*\d+[A-Z]*)',
|
||||
r'(STD|STANDARD)',
|
||||
r'(XS|EXTRA\s*STRONG)',
|
||||
r'(XXS|DOUBLE\s*EXTRA\s*STRONG)',
|
||||
r'(\d+\.?\d*\s*MM)', # 두께 (mm)
|
||||
r'(\d+\.?\d*"?\s*THK)' # 두께 (THK)
|
||||
]
|
||||
|
||||
for pattern in schedule_patterns:
|
||||
match = re.search(pattern, material.description.upper())
|
||||
if match:
|
||||
return match.group(1).strip()
|
||||
|
||||
return ""
|
||||
|
||||
def _extract_nominal_size(self, material: Material) -> str:
|
||||
"""호칭 크기 추출"""
|
||||
# 1. main_nom 필드 우선
|
||||
if material.main_nom:
|
||||
return material.main_nom.strip()
|
||||
|
||||
# 2. description에서 추출 시도
|
||||
if material.description:
|
||||
# 호칭 크기 패턴 (4", 6", 100A 등)
|
||||
size_patterns = [
|
||||
r'(\d+\.?\d*")', # 4", 6.5"
|
||||
r'(\d+\.?\d*\s*INCH)', # 4 INCH
|
||||
r'(\d+A)', # 100A
|
||||
r'(DN\s*\d+)', # DN100
|
||||
r'(\d+\.?\d*\s*MM)' # 100MM (직경)
|
||||
]
|
||||
|
||||
for pattern in size_patterns:
|
||||
match = re.search(pattern, material.description.upper())
|
||||
if match:
|
||||
return match.group(1).strip()
|
||||
|
||||
return ""
|
||||
|
||||
def _extract_length(self, material: Material) -> float:
|
||||
"""길이 추출 (mm 단위)"""
|
||||
# 1. length 필드 우선
|
||||
if material.length and material.length > 0:
|
||||
return float(material.length)
|
||||
|
||||
# 2. total_length 필드
|
||||
if material.total_length and material.total_length > 0:
|
||||
return float(material.total_length)
|
||||
|
||||
# 3. description에서 추출 시도
|
||||
if material.description:
|
||||
# 길이 패턴
|
||||
length_patterns = [
|
||||
r'(\d+\.?\d*)\s*MM', # 1500MM
|
||||
r'(\d+\.?\d*)\s*M(?!\w)', # 1.5M (단, MM이 아닌)
|
||||
r'(\d+\.?\d*)\s*METER', # 1.5 METER
|
||||
r'L\s*=?\s*(\d+\.?\d*)', # L=1500
|
||||
r'LENGTH\s*:?\s*(\d+\.?\d*)' # LENGTH: 1500
|
||||
]
|
||||
|
||||
for pattern in length_patterns:
|
||||
match = re.search(pattern, material.description.upper())
|
||||
if match:
|
||||
length_value = float(match.group(1))
|
||||
# 단위 변환 (M -> MM)
|
||||
if 'M' in pattern and 'MM' not in pattern:
|
||||
length_value *= 1000
|
||||
return length_value
|
||||
|
||||
# 기본값: 6000mm (6m)
|
||||
return 6000.0
|
||||
|
||||
def _extract_end_preparation(self, material: Material) -> str:
|
||||
"""끝단 가공 정보 추출"""
|
||||
if material.description:
|
||||
desc_upper = material.description.upper()
|
||||
|
||||
# 끝단 가공 패턴
|
||||
if any(keyword in desc_upper for keyword in ['DOUBLE BEVEL', '양개선', 'DBE']):
|
||||
return '양개선'
|
||||
elif any(keyword in desc_upper for keyword in ['SINGLE BEVEL', '한개선', 'SBE']):
|
||||
return '한개선'
|
||||
elif any(keyword in desc_upper for keyword in ['PLAIN', '무개선', 'PE']):
|
||||
return '무개선'
|
||||
|
||||
return '무개선' # 기본값
|
||||
|
||||
def _validate_extracted_data(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""추출된 데이터 검증"""
|
||||
errors = []
|
||||
|
||||
# 필수 필드 검증
|
||||
if not data.get("drawing_name") or data["drawing_name"] == "UNKNOWN_DRAWING":
|
||||
errors.append("도면명을 추출할 수 없습니다")
|
||||
|
||||
if data.get("length_mm", 0) <= 0:
|
||||
errors.append("유효한 길이 정보가 없습니다")
|
||||
|
||||
if not data.get("material_grade") or data["material_grade"] == "UNKNOWN":
|
||||
errors.append("재질 정보를 추출할 수 없습니다")
|
||||
|
||||
# 경고 (오류는 아님)
|
||||
warnings = []
|
||||
if not data.get("line_no"):
|
||||
warnings.append("라인번호가 없습니다")
|
||||
|
||||
if not data.get("nominal_size"):
|
||||
warnings.append("호칭 크기가 없습니다")
|
||||
|
||||
return {
|
||||
"valid": len(errors) == 0,
|
||||
"errors": errors,
|
||||
"warnings": warnings,
|
||||
"message": "; ".join(errors) if errors else "검증 통과"
|
||||
}
|
||||
|
||||
def get_extraction_summary(self, file_id: int) -> Dict[str, Any]:
|
||||
"""파일의 PIPE 데이터 추출 요약 정보"""
|
||||
try:
|
||||
extraction_result = self.extract_pipe_data_from_file(file_id)
|
||||
|
||||
if not extraction_result["success"]:
|
||||
return extraction_result
|
||||
|
||||
# 요약 통계 생성
|
||||
extracted_data = extraction_result["extracted_data"]
|
||||
|
||||
# 도면별 통계
|
||||
drawing_stats = {}
|
||||
for item in extracted_data:
|
||||
drawing = item["drawing_name"]
|
||||
if drawing not in drawing_stats:
|
||||
drawing_stats[drawing] = {
|
||||
"count": 0,
|
||||
"total_length": 0,
|
||||
"line_numbers": set(),
|
||||
"materials": set()
|
||||
}
|
||||
|
||||
drawing_stats[drawing]["count"] += 1
|
||||
drawing_stats[drawing]["total_length"] += item["length_mm"]
|
||||
if item["line_no"]:
|
||||
drawing_stats[drawing]["line_numbers"].add(item["line_no"])
|
||||
drawing_stats[drawing]["materials"].add(item["material_grade"])
|
||||
|
||||
# set을 list로 변환
|
||||
for drawing in drawing_stats:
|
||||
drawing_stats[drawing]["line_numbers"] = list(drawing_stats[drawing]["line_numbers"])
|
||||
drawing_stats[drawing]["materials"] = list(drawing_stats[drawing]["materials"])
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"file_id": file_id,
|
||||
"extraction_stats": extraction_result["extraction_stats"],
|
||||
"drawing_stats": drawing_stats,
|
||||
"ready_for_cutting_plan": extraction_result["extraction_stats"]["successful_extractions"] > 0
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get extraction summary: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"추출 요약 생성 실패: {str(e)}"
|
||||
}
|
||||
|
||||
|
||||
def get_pipe_data_extraction_service(db: Session = None) -> PipeDataExtractionService:
|
||||
"""PipeDataExtractionService 인스턴스 생성"""
|
||||
if db is None:
|
||||
db = next(get_db())
|
||||
return PipeDataExtractionService(db)
|
||||
362
backend/app/services/pipe_issue_snapshot_service.py
Normal file
362
backend/app/services/pipe_issue_snapshot_service.py
Normal file
@@ -0,0 +1,362 @@
|
||||
"""
|
||||
PIPE 이슈 관리용 스냅샷 시스템
|
||||
|
||||
단관 관리 DB의 특정 시점 데이터를 고정하여
|
||||
이후 리비전이 발생해도 이슈 관리에 영향을 주지 않도록 함
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Dict, List, Optional, Any
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text, and_, or_
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import (
|
||||
PipeCuttingPlan, PipeIssueSnapshot, PipeIssueSegment,
|
||||
PipeDrawingIssue, PipeSegmentIssue
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PipeIssueSnapshotService:
|
||||
"""PIPE 이슈 관리용 스냅샷 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def create_and_lock_snapshot_on_finalize(self, job_no: str, created_by: str = "system") -> Dict[str, Any]:
|
||||
"""
|
||||
Cutting Plan 확정 시 스냅샷 생성 및 즉시 잠금
|
||||
|
||||
Args:
|
||||
job_no: 작업 번호
|
||||
created_by: 생성자
|
||||
|
||||
Returns:
|
||||
생성된 스냅샷 정보
|
||||
"""
|
||||
try:
|
||||
# 1. 기존 활성 스냅샷 확인
|
||||
existing_snapshot = self.db.query(PipeIssueSnapshot).filter(
|
||||
and_(
|
||||
PipeIssueSnapshot.job_no == job_no,
|
||||
PipeIssueSnapshot.is_active == True
|
||||
)
|
||||
).first()
|
||||
|
||||
if existing_snapshot:
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"이미 확정된 Cutting Plan이 존재합니다: {existing_snapshot.snapshot_name}",
|
||||
"existing_snapshot_id": existing_snapshot.id,
|
||||
"can_manage_issues": True
|
||||
}
|
||||
|
||||
# 2. 현재 단관 데이터 조회
|
||||
current_segments = self._get_current_cutting_plan_data(job_no)
|
||||
if not current_segments:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "확정할 Cutting Plan 데이터가 없습니다."
|
||||
}
|
||||
|
||||
# 3. 자동 스냅샷 이름 생성
|
||||
snapshot_name = f"Cutting Plan 확정 - {datetime.now().strftime('%Y%m%d_%H%M%S')}"
|
||||
|
||||
# 4. 스냅샷 레코드 생성 (즉시 잠금 상태)
|
||||
snapshot = PipeIssueSnapshot(
|
||||
job_no=job_no,
|
||||
snapshot_name=snapshot_name,
|
||||
created_by=created_by,
|
||||
is_locked=True, # 확정과 동시에 잠금
|
||||
locked_at=datetime.utcnow(),
|
||||
locked_by=created_by,
|
||||
total_segments=len(current_segments),
|
||||
total_drawings=len(set(seg["drawing_name"] for seg in current_segments))
|
||||
)
|
||||
|
||||
self.db.add(snapshot)
|
||||
self.db.flush() # ID 생성을 위해
|
||||
|
||||
# 4. 단관 데이터 스냅샷 저장
|
||||
snapshot_segments = []
|
||||
for segment_data in current_segments:
|
||||
segment = PipeIssueSegment(
|
||||
snapshot_id=snapshot.id,
|
||||
area=segment_data.get("area"),
|
||||
drawing_name=segment_data["drawing_name"],
|
||||
line_no=segment_data["line_no"],
|
||||
material_grade=segment_data.get("material_grade"),
|
||||
schedule_spec=segment_data.get("schedule_spec"),
|
||||
nominal_size=segment_data.get("nominal_size"),
|
||||
length_mm=segment_data["length_mm"],
|
||||
end_preparation=segment_data.get("end_preparation", "무개선"),
|
||||
original_cutting_plan_id=segment_data.get("original_id")
|
||||
)
|
||||
snapshot_segments.append(segment)
|
||||
|
||||
self.db.add_all(snapshot_segments)
|
||||
self.db.commit()
|
||||
|
||||
logger.info(f"Created snapshot {snapshot.id} for job {job_no} with {len(snapshot_segments)} segments")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"snapshot_id": snapshot.id,
|
||||
"snapshot_name": snapshot_name,
|
||||
"total_segments": len(snapshot_segments),
|
||||
"total_drawings": snapshot.total_drawings,
|
||||
"is_locked": True,
|
||||
"locked_at": snapshot.locked_at,
|
||||
"message": f"Cutting Plan이 확정되었습니다! 이제 이슈 관리를 시작할 수 있습니다.",
|
||||
"next_action": "start_issue_management"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to create snapshot: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"스냅샷 생성 실패: {str(e)}"
|
||||
}
|
||||
|
||||
def lock_snapshot(self, snapshot_id: int, locked_by: str = "system") -> Dict[str, Any]:
|
||||
"""
|
||||
스냅샷 잠금 (이슈 등록 시작)
|
||||
잠금 후에는 더 이상 리비전 영향을 받지 않음
|
||||
"""
|
||||
try:
|
||||
snapshot = self.db.query(PipeIssueSnapshot).filter(
|
||||
PipeIssueSnapshot.id == snapshot_id
|
||||
).first()
|
||||
|
||||
if not snapshot:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "스냅샷을 찾을 수 없습니다."
|
||||
}
|
||||
|
||||
if snapshot.is_locked:
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"이미 잠긴 스냅샷입니다. (잠금자: {snapshot.locked_by})"
|
||||
}
|
||||
|
||||
# 스냅샷 잠금
|
||||
snapshot.is_locked = True
|
||||
snapshot.locked_at = datetime.utcnow()
|
||||
snapshot.locked_by = locked_by
|
||||
|
||||
self.db.commit()
|
||||
|
||||
logger.info(f"Locked snapshot {snapshot_id} by {locked_by}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"스냅샷 '{snapshot.snapshot_name}'이 잠금되었습니다. 이제 이슈 관리를 시작할 수 있습니다.",
|
||||
"locked_at": snapshot.locked_at
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to lock snapshot: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"스냅샷 잠금 실패: {str(e)}"
|
||||
}
|
||||
|
||||
def get_snapshot_info(self, job_no: str) -> Dict[str, Any]:
|
||||
"""작업의 스냅샷 정보 조회"""
|
||||
try:
|
||||
snapshot = self.db.query(PipeIssueSnapshot).filter(
|
||||
and_(
|
||||
PipeIssueSnapshot.job_no == job_no,
|
||||
PipeIssueSnapshot.is_active == True
|
||||
)
|
||||
).first()
|
||||
|
||||
if not snapshot:
|
||||
return {
|
||||
"has_snapshot": False,
|
||||
"message": "생성된 스냅샷이 없습니다."
|
||||
}
|
||||
|
||||
# 이슈 통계 조회
|
||||
drawing_issues_count = self.db.query(PipeDrawingIssue).filter(
|
||||
PipeDrawingIssue.snapshot_id == snapshot.id
|
||||
).count()
|
||||
|
||||
segment_issues_count = self.db.query(PipeSegmentIssue).filter(
|
||||
PipeSegmentIssue.snapshot_id == snapshot.id
|
||||
).count()
|
||||
|
||||
return {
|
||||
"has_snapshot": True,
|
||||
"snapshot_id": snapshot.id,
|
||||
"snapshot_name": snapshot.snapshot_name,
|
||||
"is_locked": snapshot.is_locked,
|
||||
"created_at": snapshot.created_at,
|
||||
"created_by": snapshot.created_by,
|
||||
"locked_at": snapshot.locked_at,
|
||||
"locked_by": snapshot.locked_by,
|
||||
"total_segments": snapshot.total_segments,
|
||||
"total_drawings": snapshot.total_drawings,
|
||||
"drawing_issues_count": drawing_issues_count,
|
||||
"segment_issues_count": segment_issues_count,
|
||||
"can_start_issue_management": not snapshot.is_locked,
|
||||
"message": "잠긴 스냅샷 - 이슈 관리 진행 중" if snapshot.is_locked else "스냅샷 준비 완료"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get snapshot info: {e}")
|
||||
return {
|
||||
"has_snapshot": False,
|
||||
"message": f"스냅샷 정보 조회 실패: {str(e)}"
|
||||
}
|
||||
|
||||
def get_snapshot_segments(self, snapshot_id: int, area: str = None, drawing_name: str = None) -> List[Dict[str, Any]]:
|
||||
"""스냅샷된 단관 데이터 조회"""
|
||||
try:
|
||||
query = self.db.query(PipeIssueSegment).filter(
|
||||
PipeIssueSegment.snapshot_id == snapshot_id
|
||||
)
|
||||
|
||||
if area:
|
||||
query = query.filter(PipeIssueSegment.area == area)
|
||||
|
||||
if drawing_name:
|
||||
query = query.filter(PipeIssueSegment.drawing_name == drawing_name)
|
||||
|
||||
segments = query.order_by(
|
||||
PipeIssueSegment.area,
|
||||
PipeIssueSegment.drawing_name,
|
||||
PipeIssueSegment.line_no
|
||||
).all()
|
||||
|
||||
result = []
|
||||
for segment in segments:
|
||||
result.append({
|
||||
"id": segment.id,
|
||||
"area": segment.area,
|
||||
"drawing_name": segment.drawing_name,
|
||||
"line_no": segment.line_no,
|
||||
"material_grade": segment.material_grade,
|
||||
"schedule_spec": segment.schedule_spec,
|
||||
"nominal_size": segment.nominal_size,
|
||||
"length_mm": float(segment.length_mm) if segment.length_mm else 0,
|
||||
"end_preparation": segment.end_preparation,
|
||||
"material_info": f"{segment.material_grade or ''} {segment.schedule_spec or ''} {segment.nominal_size or ''}".strip()
|
||||
})
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get snapshot segments: {e}")
|
||||
return []
|
||||
|
||||
def get_available_areas(self, snapshot_id: int) -> List[str]:
|
||||
"""스냅샷의 사용 가능한 구역 목록"""
|
||||
try:
|
||||
result = self.db.query(PipeIssueSegment.area).filter(
|
||||
and_(
|
||||
PipeIssueSegment.snapshot_id == snapshot_id,
|
||||
PipeIssueSegment.area.isnot(None)
|
||||
)
|
||||
).distinct().all()
|
||||
|
||||
areas = [row.area for row in result if row.area]
|
||||
return sorted(areas)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get available areas: {e}")
|
||||
return []
|
||||
|
||||
def get_available_drawings(self, snapshot_id: int, area: str = None) -> List[str]:
|
||||
"""스냅샷의 사용 가능한 도면 목록"""
|
||||
try:
|
||||
query = self.db.query(PipeIssueSegment.drawing_name).filter(
|
||||
PipeIssueSegment.snapshot_id == snapshot_id
|
||||
)
|
||||
|
||||
if area:
|
||||
query = query.filter(PipeIssueSegment.area == area)
|
||||
|
||||
result = query.distinct().all()
|
||||
drawings = [row.drawing_name for row in result if row.drawing_name]
|
||||
return sorted(drawings)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get available drawings: {e}")
|
||||
return []
|
||||
|
||||
def _get_current_cutting_plan_data(self, job_no: str) -> List[Dict[str, Any]]:
|
||||
"""현재 단관 관리 DB에서 데이터 조회"""
|
||||
try:
|
||||
cutting_plans = self.db.query(PipeCuttingPlan).filter(
|
||||
PipeCuttingPlan.job_no == job_no
|
||||
).all()
|
||||
|
||||
segments = []
|
||||
for plan in cutting_plans:
|
||||
segments.append({
|
||||
"original_id": plan.id,
|
||||
"area": plan.area,
|
||||
"drawing_name": plan.drawing_name,
|
||||
"line_no": plan.line_no,
|
||||
"material_grade": plan.material_grade,
|
||||
"schedule_spec": plan.schedule_spec,
|
||||
"nominal_size": plan.nominal_size,
|
||||
"length_mm": float(plan.length_mm) if plan.length_mm else 0,
|
||||
"end_preparation": plan.end_preparation or "무개선"
|
||||
})
|
||||
|
||||
return segments
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get current cutting plan data: {e}")
|
||||
return []
|
||||
|
||||
def check_revision_protection(self, job_no: str) -> Dict[str, Any]:
|
||||
"""
|
||||
리비전 보호 상태 확인
|
||||
잠긴 스냅샷이 있으면 더 이상 리비전 영향을 받지 않음
|
||||
"""
|
||||
try:
|
||||
snapshot = self.db.query(PipeIssueSnapshot).filter(
|
||||
and_(
|
||||
PipeIssueSnapshot.job_no == job_no,
|
||||
PipeIssueSnapshot.is_active == True,
|
||||
PipeIssueSnapshot.is_locked == True
|
||||
)
|
||||
).first()
|
||||
|
||||
if snapshot:
|
||||
return {
|
||||
"is_protected": True,
|
||||
"snapshot_id": snapshot.id,
|
||||
"snapshot_name": snapshot.snapshot_name,
|
||||
"locked_at": snapshot.locked_at,
|
||||
"locked_by": snapshot.locked_by,
|
||||
"message": f"이슈 관리가 진행 중입니다. 스냅샷 '{snapshot.snapshot_name}'이 보호되고 있습니다."
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"is_protected": False,
|
||||
"message": "리비전 보호가 활성화되지 않았습니다."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to check revision protection: {e}")
|
||||
return {
|
||||
"is_protected": False,
|
||||
"message": f"리비전 보호 상태 확인 실패: {str(e)}"
|
||||
}
|
||||
|
||||
|
||||
def get_pipe_issue_snapshot_service(db: Session = None) -> PipeIssueSnapshotService:
|
||||
"""PipeIssueSnapshotService 인스턴스 생성"""
|
||||
if db is None:
|
||||
db = next(get_db())
|
||||
return PipeIssueSnapshotService(db)
|
||||
541
backend/app/services/pipe_revision_service.py
Normal file
541
backend/app/services/pipe_revision_service.py
Normal file
@@ -0,0 +1,541 @@
|
||||
"""
|
||||
PIPE 전용 리비전 관리 서비스
|
||||
|
||||
Cutting Plan 작성 전/후에 따른 차별화된 리비전 처리 로직
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Dict, List, Optional, Tuple, Any
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text, and_, or_
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import (
|
||||
File, Material, PipeCuttingPlan, PipeRevisionComparison,
|
||||
PipeRevisionChange, PipeLengthCalculation
|
||||
)
|
||||
from ..utils.pipe_utils import (
|
||||
PipeConstants, PipeDataExtractor, PipeCalculator,
|
||||
PipeComparator, PipeValidator, PipeLogger
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PipeRevisionService:
|
||||
"""PIPE 전용 리비전 관리 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def check_revision_status(self, job_no: str, new_file_id: int) -> Dict[str, Any]:
|
||||
"""
|
||||
리비전 상태 확인 및 처리 방식 결정
|
||||
|
||||
Returns:
|
||||
- revision_type: 'no_revision', 'pre_cutting_plan', 'post_cutting_plan'
|
||||
- requires_action: 처리가 필요한지 여부
|
||||
- message: 사용자에게 표시할 메시지
|
||||
"""
|
||||
try:
|
||||
# 기존 파일 확인
|
||||
previous_file = self._get_previous_file(job_no, new_file_id)
|
||||
if not previous_file:
|
||||
return {
|
||||
"revision_type": "no_revision",
|
||||
"requires_action": False,
|
||||
"message": "첫 번째 BOM 파일입니다. 새로운 Cutting Plan을 작성해주세요."
|
||||
}
|
||||
|
||||
# Cutting Plan 존재 여부 확인
|
||||
has_cutting_plan = self._has_existing_cutting_plan(job_no)
|
||||
|
||||
if not has_cutting_plan:
|
||||
# Cutting Plan 작성 전 리비전
|
||||
return {
|
||||
"revision_type": "pre_cutting_plan",
|
||||
"requires_action": True,
|
||||
"previous_file_id": previous_file.id,
|
||||
"message": "Cutting Plan 작성 전 리비전이 감지되었습니다. 새로운 BOM으로 Cutting Plan을 작성해주세요."
|
||||
}
|
||||
else:
|
||||
# Cutting Plan 작성 후 리비전
|
||||
return {
|
||||
"revision_type": "post_cutting_plan",
|
||||
"requires_action": True,
|
||||
"previous_file_id": previous_file.id,
|
||||
"message": "기존 Cutting Plan이 있는 상태에서 리비전이 감지되었습니다. 변경사항을 비교 검토해주세요."
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to check pipe revision status: {e}")
|
||||
return {
|
||||
"revision_type": "error",
|
||||
"requires_action": False,
|
||||
"message": f"리비전 상태 확인 중 오류가 발생했습니다: {str(e)}"
|
||||
}
|
||||
|
||||
def handle_pre_cutting_plan_revision(self, job_no: str, new_file_id: int) -> Dict[str, Any]:
|
||||
"""
|
||||
Cutting Plan 작성 전 리비전 처리
|
||||
- 기존 PIPE 관련 데이터 전체 삭제
|
||||
- 새 BOM 파일로 초기화
|
||||
"""
|
||||
try:
|
||||
logger.info(f"Processing pre-cutting-plan revision for job {job_no}")
|
||||
|
||||
# 1. 기존 PIPE 관련 데이터 삭제
|
||||
deleted_count = self._delete_existing_pipe_data(job_no)
|
||||
|
||||
# 2. 새 BOM에서 PIPE 데이터 추출
|
||||
pipe_materials = self._extract_pipe_materials_from_bom(new_file_id)
|
||||
|
||||
# 3. 처리 결과 반환
|
||||
return {
|
||||
"status": "success",
|
||||
"revision_type": "pre_cutting_plan",
|
||||
"deleted_items": deleted_count,
|
||||
"new_pipe_materials": len(pipe_materials),
|
||||
"message": f"기존 PIPE 데이터 {deleted_count}건이 삭제되었습니다. 새로운 Cutting Plan을 작성해주세요.",
|
||||
"next_action": "create_new_cutting_plan",
|
||||
"pipe_materials": pipe_materials
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to handle pre-cutting-plan revision: {e}")
|
||||
return {
|
||||
"status": "error",
|
||||
"message": f"Cutting Plan 작성 전 리비전 처리 실패: {str(e)}"
|
||||
}
|
||||
|
||||
def handle_post_cutting_plan_revision(self, job_no: str, new_file_id: int) -> Dict[str, Any]:
|
||||
"""
|
||||
Cutting Plan 작성 후 리비전 처리
|
||||
- 기존 Cutting Plan과 신규 BOM 비교
|
||||
- 변경사항 상세 분석
|
||||
"""
|
||||
try:
|
||||
logger.info(f"Processing post-cutting-plan revision for job {job_no}")
|
||||
|
||||
# 1. 기존 Cutting Plan 조회
|
||||
existing_plan = self._get_existing_cutting_plan_data(job_no)
|
||||
if not existing_plan:
|
||||
return {
|
||||
"status": "error",
|
||||
"message": "기존 Cutting Plan을 찾을 수 없습니다."
|
||||
}
|
||||
|
||||
# 2. 새 BOM에서 PIPE 데이터 추출
|
||||
new_pipe_data = self._extract_pipe_materials_from_bom(new_file_id)
|
||||
|
||||
# 3. 도면별 비교 수행
|
||||
comparison_result = self._compare_pipe_data_by_drawing(existing_plan, new_pipe_data)
|
||||
|
||||
# 4. 비교 결과 저장
|
||||
comparison_id = self._save_comparison_result(job_no, new_file_id, comparison_result)
|
||||
|
||||
# 5. 변경사항 요약
|
||||
summary = self._generate_comparison_summary(comparison_result)
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"revision_type": "post_cutting_plan",
|
||||
"comparison_id": comparison_id,
|
||||
"summary": summary,
|
||||
"changed_drawings": [d for d in comparison_result if d["has_changes"]],
|
||||
"unchanged_drawings": [d for d in comparison_result if not d["has_changes"]],
|
||||
"message": f"리비전 비교가 완료되었습니다. {summary['changed_drawings_count']}개 도면에서 변경사항이 발견되었습니다.",
|
||||
"next_action": "review_changes"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to handle post-cutting-plan revision: {e}")
|
||||
return {
|
||||
"status": "error",
|
||||
"message": f"Cutting Plan 작성 후 리비전 처리 실패: {str(e)}"
|
||||
}
|
||||
|
||||
def _get_previous_file(self, job_no: str, current_file_id: int) -> Optional[File]:
|
||||
"""이전 파일 조회"""
|
||||
return self.db.query(File).filter(
|
||||
and_(
|
||||
File.job_no == job_no,
|
||||
File.id < current_file_id,
|
||||
File.is_active == True
|
||||
)
|
||||
).order_by(File.id.desc()).first()
|
||||
|
||||
def _has_existing_cutting_plan(self, job_no: str) -> bool:
|
||||
"""기존 Cutting Plan 존재 여부 확인"""
|
||||
count = self.db.query(PipeCuttingPlan).filter(
|
||||
PipeCuttingPlan.job_no == job_no
|
||||
).count()
|
||||
return count > 0
|
||||
|
||||
def _delete_existing_pipe_data(self, job_no: str) -> int:
|
||||
"""기존 PIPE 관련 데이터 삭제"""
|
||||
try:
|
||||
# Cutting Plan 데이터 삭제
|
||||
cutting_plan_count = self.db.query(PipeCuttingPlan).filter(
|
||||
PipeCuttingPlan.job_no == job_no
|
||||
).count()
|
||||
|
||||
self.db.query(PipeCuttingPlan).filter(
|
||||
PipeCuttingPlan.job_no == job_no
|
||||
).delete()
|
||||
|
||||
# Length Calculation 데이터 삭제
|
||||
self.db.query(PipeLengthCalculation).filter(
|
||||
PipeLengthCalculation.file_id.in_(
|
||||
self.db.query(File.id).filter(File.job_no == job_no)
|
||||
)
|
||||
).delete()
|
||||
|
||||
self.db.commit()
|
||||
logger.info(f"Deleted {cutting_plan_count} cutting plan records for job {job_no}")
|
||||
|
||||
return cutting_plan_count
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to delete existing pipe data: {e}")
|
||||
raise
|
||||
|
||||
def _extract_pipe_materials_from_bom(self, file_id: int) -> List[Dict[str, Any]]:
|
||||
"""BOM 파일에서 PIPE 자재 추출 (리팩토링된 유틸리티 사용)"""
|
||||
return PipeDataExtractor.extract_pipe_materials_from_file(self.db, file_id)
|
||||
|
||||
def _get_existing_cutting_plan_data(self, job_no: str) -> List[Dict[str, Any]]:
|
||||
"""기존 Cutting Plan 데이터 조회"""
|
||||
try:
|
||||
cutting_plans = self.db.query(PipeCuttingPlan).filter(
|
||||
PipeCuttingPlan.job_no == job_no
|
||||
).all()
|
||||
|
||||
plan_data = []
|
||||
for plan in cutting_plans:
|
||||
plan_data.append({
|
||||
"id": plan.id,
|
||||
"area": plan.area or "",
|
||||
"drawing_name": plan.drawing_name,
|
||||
"line_no": plan.line_no,
|
||||
"material_grade": plan.material_grade or "",
|
||||
"schedule_spec": plan.schedule_spec or "",
|
||||
"nominal_size": plan.nominal_size or "",
|
||||
"length_mm": float(plan.length_mm or 0),
|
||||
"end_preparation": plan.end_preparation or "무개선"
|
||||
})
|
||||
|
||||
logger.info(f"Retrieved {len(plan_data)} cutting plan records for job {job_no}")
|
||||
return plan_data
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get existing cutting plan data: {e}")
|
||||
raise
|
||||
|
||||
def _compare_pipe_data_by_drawing(self, existing_plan: List[Dict], new_pipe_data: List[Dict]) -> List[Dict[str, Any]]:
|
||||
"""도면별 PIPE 데이터 비교"""
|
||||
try:
|
||||
# 도면별로 데이터 그룹화
|
||||
existing_by_drawing = self._group_by_drawing(existing_plan)
|
||||
new_by_drawing = self._group_by_drawing(new_pipe_data)
|
||||
|
||||
# 모든 도면 목록
|
||||
all_drawings = set(existing_by_drawing.keys()) | set(new_by_drawing.keys())
|
||||
|
||||
comparison_results = []
|
||||
|
||||
for drawing_name in sorted(all_drawings):
|
||||
existing_segments = existing_by_drawing.get(drawing_name, [])
|
||||
new_segments = new_by_drawing.get(drawing_name, [])
|
||||
|
||||
# 도면별 비교 수행
|
||||
drawing_comparison = self._compare_drawing_segments(
|
||||
drawing_name, existing_segments, new_segments
|
||||
)
|
||||
|
||||
comparison_results.append(drawing_comparison)
|
||||
|
||||
return comparison_results
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to compare pipe data by drawing: {e}")
|
||||
raise
|
||||
|
||||
def _group_by_drawing(self, data: List[Dict]) -> Dict[str, List[Dict]]:
|
||||
"""데이터를 도면별로 그룹화"""
|
||||
grouped = {}
|
||||
for item in data:
|
||||
drawing = item.get("drawing_name", "UNKNOWN")
|
||||
if drawing not in grouped:
|
||||
grouped[drawing] = []
|
||||
grouped[drawing].append(item)
|
||||
return grouped
|
||||
|
||||
def _compare_drawing_segments(self, drawing_name: str, existing: List[Dict], new: List[Dict]) -> Dict[str, Any]:
|
||||
"""단일 도면의 세그먼트 비교"""
|
||||
try:
|
||||
# 세그먼트 매칭 (재질, 길이, 끝단가공 기준)
|
||||
matched_pairs, added_segments, removed_segments = self._match_segments(existing, new)
|
||||
|
||||
# 변경사항 분석
|
||||
unchanged_segments = []
|
||||
modified_segments = []
|
||||
|
||||
for existing_seg, new_seg in matched_pairs:
|
||||
if self._segments_are_identical(existing_seg, new_seg):
|
||||
unchanged_segments.append({
|
||||
"change_type": "unchanged",
|
||||
"segment_data": new_seg,
|
||||
"existing_data": existing_seg
|
||||
})
|
||||
else:
|
||||
changes = self._get_segment_changes(existing_seg, new_seg)
|
||||
modified_segments.append({
|
||||
"change_type": "modified",
|
||||
"segment_data": new_seg,
|
||||
"existing_data": existing_seg,
|
||||
"changes": changes
|
||||
})
|
||||
|
||||
# 추가된 세그먼트
|
||||
added_segment_data = [
|
||||
{
|
||||
"change_type": "added",
|
||||
"segment_data": seg,
|
||||
"existing_data": None
|
||||
}
|
||||
for seg in added_segments
|
||||
]
|
||||
|
||||
# 삭제된 세그먼트
|
||||
removed_segment_data = [
|
||||
{
|
||||
"change_type": "removed",
|
||||
"segment_data": None,
|
||||
"existing_data": seg
|
||||
}
|
||||
for seg in removed_segments
|
||||
]
|
||||
|
||||
# 전체 세그먼트 목록
|
||||
all_segments = unchanged_segments + modified_segments + added_segment_data + removed_segment_data
|
||||
|
||||
# 변경사항 여부 판단
|
||||
has_changes = len(modified_segments) > 0 or len(added_segments) > 0 or len(removed_segments) > 0
|
||||
|
||||
return {
|
||||
"drawing_name": drawing_name,
|
||||
"has_changes": has_changes,
|
||||
"segments": all_segments,
|
||||
"summary": {
|
||||
"total_segments": len(all_segments),
|
||||
"unchanged_count": len(unchanged_segments),
|
||||
"modified_count": len(modified_segments),
|
||||
"added_count": len(added_segments),
|
||||
"removed_count": len(removed_segments)
|
||||
}
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to compare segments for drawing {drawing_name}: {e}")
|
||||
raise
|
||||
|
||||
def _match_segments(self, existing: List[Dict], new: List[Dict]) -> Tuple[List[Tuple], List[Dict], List[Dict]]:
|
||||
"""세그먼트 매칭 (재질, 길이 기준)"""
|
||||
matched_pairs = []
|
||||
remaining_new = new.copy()
|
||||
remaining_existing = existing.copy()
|
||||
|
||||
# 정확히 일치하는 세그먼트 찾기
|
||||
for existing_seg in existing.copy():
|
||||
for new_seg in remaining_new.copy():
|
||||
if self._segments_match_for_pairing(existing_seg, new_seg):
|
||||
matched_pairs.append((existing_seg, new_seg))
|
||||
remaining_existing.remove(existing_seg)
|
||||
remaining_new.remove(new_seg)
|
||||
break
|
||||
|
||||
# 남은 것들은 추가/삭제로 분류
|
||||
added_segments = remaining_new
|
||||
removed_segments = remaining_existing
|
||||
|
||||
return matched_pairs, added_segments, removed_segments
|
||||
|
||||
def _segments_match_for_pairing(self, seg1: Dict, seg2: Dict) -> bool:
|
||||
"""세그먼트 매칭 기준 (재질과 길이가 유사한지 확인)"""
|
||||
# 재질 비교
|
||||
material1 = seg1.get("material_grade", "").strip()
|
||||
material2 = seg2.get("material_grade", "").strip()
|
||||
|
||||
# 길이 비교 (허용 오차 1mm)
|
||||
length1 = seg1.get("length_mm", seg1.get("length", 0))
|
||||
length2 = seg2.get("length_mm", seg2.get("length", 0))
|
||||
|
||||
material_match = material1.lower() == material2.lower()
|
||||
length_match = abs(float(length1) - float(length2)) <= 1.0
|
||||
|
||||
return material_match and length_match
|
||||
|
||||
def _segments_are_identical(self, seg1: Dict, seg2: Dict) -> bool:
|
||||
"""세그먼트 완전 동일성 검사"""
|
||||
# 주요 속성들 비교
|
||||
material_match = seg1.get("material_grade", "").strip().lower() == seg2.get("material_grade", "").strip().lower()
|
||||
|
||||
length1 = seg1.get("length_mm", seg1.get("length", 0))
|
||||
length2 = seg2.get("length_mm", seg2.get("length", 0))
|
||||
length_match = abs(float(length1) - float(length2)) <= 0.1
|
||||
|
||||
end_prep1 = seg1.get("end_preparation", "무개선")
|
||||
end_prep2 = seg2.get("end_preparation", "무개선")
|
||||
end_prep_match = end_prep1 == end_prep2
|
||||
|
||||
return material_match and length_match and end_prep_match
|
||||
|
||||
def _get_segment_changes(self, existing: Dict, new: Dict) -> List[Dict[str, Any]]:
|
||||
"""세그먼트 변경사항 상세 분석"""
|
||||
changes = []
|
||||
|
||||
# 재질 변경
|
||||
old_material = existing.get("material_grade", "").strip()
|
||||
new_material = new.get("material_grade", "").strip()
|
||||
if old_material.lower() != new_material.lower():
|
||||
changes.append({
|
||||
"field": "material_grade",
|
||||
"old_value": old_material,
|
||||
"new_value": new_material
|
||||
})
|
||||
|
||||
# 길이 변경
|
||||
old_length = existing.get("length_mm", existing.get("length", 0))
|
||||
new_length = new.get("length_mm", new.get("length", 0))
|
||||
if abs(float(old_length) - float(new_length)) > 0.1:
|
||||
changes.append({
|
||||
"field": "length",
|
||||
"old_value": f"{old_length}mm",
|
||||
"new_value": f"{new_length}mm"
|
||||
})
|
||||
|
||||
# 끝단가공 변경
|
||||
old_end_prep = existing.get("end_preparation", "무개선")
|
||||
new_end_prep = new.get("end_preparation", "무개선")
|
||||
if old_end_prep != new_end_prep:
|
||||
changes.append({
|
||||
"field": "end_preparation",
|
||||
"old_value": old_end_prep,
|
||||
"new_value": new_end_prep
|
||||
})
|
||||
|
||||
return changes
|
||||
|
||||
def _save_comparison_result(self, job_no: str, new_file_id: int, comparison_result: List[Dict]) -> int:
|
||||
"""비교 결과를 데이터베이스에 저장"""
|
||||
try:
|
||||
# 이전 파일 ID 조회
|
||||
previous_file = self._get_previous_file(job_no, new_file_id)
|
||||
previous_file_id = previous_file.id if previous_file else None
|
||||
|
||||
# 통계 계산
|
||||
total_drawings = len(comparison_result)
|
||||
changed_drawings = len([d for d in comparison_result if d["has_changes"]])
|
||||
unchanged_drawings = total_drawings - changed_drawings
|
||||
|
||||
total_segments = sum(d["summary"]["total_segments"] for d in comparison_result)
|
||||
added_segments = sum(d["summary"]["added_count"] for d in comparison_result)
|
||||
removed_segments = sum(d["summary"]["removed_count"] for d in comparison_result)
|
||||
modified_segments = sum(d["summary"]["modified_count"] for d in comparison_result)
|
||||
unchanged_segments = sum(d["summary"]["unchanged_count"] for d in comparison_result)
|
||||
|
||||
# 비교 결과 저장
|
||||
comparison = PipeRevisionComparison(
|
||||
job_no=job_no,
|
||||
current_file_id=new_file_id,
|
||||
previous_cutting_plan_id=None, # 추후 구현
|
||||
total_drawings=total_drawings,
|
||||
changed_drawings=changed_drawings,
|
||||
unchanged_drawings=unchanged_drawings,
|
||||
total_segments=total_segments,
|
||||
added_segments=added_segments,
|
||||
removed_segments=removed_segments,
|
||||
modified_segments=modified_segments,
|
||||
unchanged_segments=unchanged_segments,
|
||||
created_by="system"
|
||||
)
|
||||
|
||||
self.db.add(comparison)
|
||||
self.db.flush() # ID 생성을 위해
|
||||
|
||||
# 상세 변경사항 저장
|
||||
for drawing_data in comparison_result:
|
||||
if drawing_data["has_changes"]:
|
||||
for segment in drawing_data["segments"]:
|
||||
if segment["change_type"] != "unchanged":
|
||||
change = PipeRevisionChange(
|
||||
comparison_id=comparison.id,
|
||||
drawing_name=drawing_data["drawing_name"],
|
||||
change_type=segment["change_type"]
|
||||
)
|
||||
|
||||
# 기존 데이터
|
||||
if segment["existing_data"]:
|
||||
existing = segment["existing_data"]
|
||||
change.old_line_no = existing.get("line_no")
|
||||
change.old_material_grade = existing.get("material_grade")
|
||||
change.old_schedule_spec = existing.get("schedule_spec")
|
||||
change.old_nominal_size = existing.get("nominal_size")
|
||||
change.old_length_mm = existing.get("length_mm", existing.get("length"))
|
||||
change.old_end_preparation = existing.get("end_preparation")
|
||||
|
||||
# 새 데이터
|
||||
if segment["segment_data"]:
|
||||
new_data = segment["segment_data"]
|
||||
change.new_line_no = new_data.get("line_no")
|
||||
change.new_material_grade = new_data.get("material_grade")
|
||||
change.new_schedule_spec = new_data.get("schedule_spec")
|
||||
change.new_nominal_size = new_data.get("nominal_size")
|
||||
change.new_length_mm = new_data.get("length_mm", new_data.get("length"))
|
||||
change.new_end_preparation = new_data.get("end_preparation")
|
||||
|
||||
self.db.add(change)
|
||||
|
||||
self.db.commit()
|
||||
logger.info(f"Saved comparison result with ID {comparison.id}")
|
||||
|
||||
return comparison.id
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to save comparison result: {e}")
|
||||
raise
|
||||
|
||||
def _generate_comparison_summary(self, comparison_result: List[Dict]) -> Dict[str, Any]:
|
||||
"""비교 결과 요약 생성"""
|
||||
total_drawings = len(comparison_result)
|
||||
changed_drawings = [d for d in comparison_result if d["has_changes"]]
|
||||
changed_drawings_count = len(changed_drawings)
|
||||
|
||||
total_segments = sum(d["summary"]["total_segments"] for d in comparison_result)
|
||||
added_segments = sum(d["summary"]["added_count"] for d in comparison_result)
|
||||
removed_segments = sum(d["summary"]["removed_count"] for d in comparison_result)
|
||||
modified_segments = sum(d["summary"]["modified_count"] for d in comparison_result)
|
||||
unchanged_segments = sum(d["summary"]["unchanged_count"] for d in comparison_result)
|
||||
|
||||
return {
|
||||
"total_drawings": total_drawings,
|
||||
"changed_drawings_count": changed_drawings_count,
|
||||
"unchanged_drawings_count": total_drawings - changed_drawings_count,
|
||||
"total_segments": total_segments,
|
||||
"added_segments": added_segments,
|
||||
"removed_segments": removed_segments,
|
||||
"modified_segments": modified_segments,
|
||||
"unchanged_segments": unchanged_segments,
|
||||
"change_percentage": round((changed_drawings_count / total_drawings * 100) if total_drawings > 0 else 0, 1)
|
||||
}
|
||||
|
||||
|
||||
def get_pipe_revision_service(db: Session = None) -> PipeRevisionService:
|
||||
"""PipeRevisionService 인스턴스 생성"""
|
||||
if db is None:
|
||||
db = next(get_db())
|
||||
return PipeRevisionService(db)
|
||||
220
backend/app/services/pipe_snapshot_excel_service.py
Normal file
220
backend/app/services/pipe_snapshot_excel_service.py
Normal file
@@ -0,0 +1,220 @@
|
||||
"""
|
||||
PIPE 스냅샷 기반 Excel 내보내기 서비스
|
||||
|
||||
확정된 Cutting Plan의 스냅샷 데이터를 기준으로 Excel 생성
|
||||
이후 리비전이 발생해도 Excel 내용은 변경되지 않음
|
||||
"""
|
||||
|
||||
import logging
|
||||
import pandas as pd
|
||||
from typing import Dict, List, Optional, Any
|
||||
from datetime import datetime
|
||||
from io import BytesIO
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..models import PipeIssueSnapshot, PipeIssueSegment
|
||||
from ..services.pipe_issue_snapshot_service import get_pipe_issue_snapshot_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PipeSnapshotExcelService:
|
||||
"""PIPE 스냅샷 기반 Excel 내보내기 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.snapshot_service = get_pipe_issue_snapshot_service(db)
|
||||
|
||||
def export_finalized_cutting_plan(self, job_no: str) -> Dict[str, Any]:
|
||||
"""
|
||||
확정된 Cutting Plan Excel 내보내기
|
||||
스냅샷 데이터 기준으로 고정된 Excel 생성
|
||||
"""
|
||||
try:
|
||||
# 1. 활성 스냅샷 확인
|
||||
snapshot_info = self.snapshot_service.get_snapshot_info(job_no)
|
||||
if not snapshot_info["has_snapshot"]:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "확정된 Cutting Plan이 없습니다. 먼저 Cutting Plan을 확정해주세요."
|
||||
}
|
||||
|
||||
if not snapshot_info["is_locked"]:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "Cutting Plan이 아직 확정되지 않았습니다."
|
||||
}
|
||||
|
||||
snapshot_id = snapshot_info["snapshot_id"]
|
||||
|
||||
# 2. 스냅샷 데이터 조회
|
||||
segments_data = self.snapshot_service.get_snapshot_segments(snapshot_id)
|
||||
if not segments_data:
|
||||
return {
|
||||
"success": False,
|
||||
"message": "내보낼 단관 데이터가 없습니다."
|
||||
}
|
||||
|
||||
# 3. Excel 파일 생성
|
||||
excel_buffer = self._create_cutting_plan_excel(
|
||||
segments_data,
|
||||
snapshot_info["snapshot_name"],
|
||||
job_no
|
||||
)
|
||||
|
||||
# 4. 파일명 생성
|
||||
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"PIPE_Cutting_Plan_{job_no}_{timestamp}_FINALIZED.xlsx"
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"excel_buffer": excel_buffer,
|
||||
"filename": filename,
|
||||
"snapshot_id": snapshot_id,
|
||||
"snapshot_name": snapshot_info["snapshot_name"],
|
||||
"total_segments": len(segments_data),
|
||||
"message": f"확정된 Cutting Plan Excel이 생성되었습니다. (스냅샷 기준)"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to export finalized cutting plan: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"message": f"Excel 내보내기 실패: {str(e)}"
|
||||
}
|
||||
|
||||
def _create_cutting_plan_excel(self, segments_data: List[Dict], snapshot_name: str, job_no: str) -> BytesIO:
|
||||
"""스냅샷 데이터로 Excel 파일 생성"""
|
||||
|
||||
# DataFrame 생성
|
||||
df_data = []
|
||||
for segment in segments_data:
|
||||
df_data.append({
|
||||
'구역': segment.get('area', ''),
|
||||
'도면명': segment.get('drawing_name', ''),
|
||||
'라인번호': segment.get('line_no', ''),
|
||||
'재질': segment.get('material_grade', ''),
|
||||
'규격': segment.get('schedule_spec', ''),
|
||||
'호칭': segment.get('nominal_size', ''),
|
||||
'길이(mm)': segment.get('length_mm', 0),
|
||||
'끝단가공': segment.get('end_preparation', '무개선'),
|
||||
'파이프정보': segment.get('material_info', '')
|
||||
})
|
||||
|
||||
df = pd.DataFrame(df_data)
|
||||
|
||||
# Excel 버퍼 생성
|
||||
excel_buffer = BytesIO()
|
||||
|
||||
with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:
|
||||
# 메인 시트 - 단관 목록
|
||||
df.to_excel(writer, sheet_name='단관 목록', index=False)
|
||||
|
||||
# 요약 시트
|
||||
summary_data = self._create_summary_data(segments_data, snapshot_name, job_no)
|
||||
summary_df = pd.DataFrame(summary_data)
|
||||
summary_df.to_excel(writer, sheet_name='요약', index=False)
|
||||
|
||||
# 구역별 시트
|
||||
areas = sorted(set(segment.get('area', '') for segment in segments_data if segment.get('area')))
|
||||
for area in areas:
|
||||
if area: # 빈 구역 제외
|
||||
area_segments = [s for s in segments_data if s.get('area') == area]
|
||||
area_df_data = []
|
||||
for segment in area_segments:
|
||||
area_df_data.append({
|
||||
'도면명': segment.get('drawing_name', ''),
|
||||
'라인번호': segment.get('line_no', ''),
|
||||
'재질': segment.get('material_grade', ''),
|
||||
'규격': segment.get('schedule_spec', ''),
|
||||
'호칭': segment.get('nominal_size', ''),
|
||||
'길이(mm)': segment.get('length_mm', 0),
|
||||
'끝단가공': segment.get('end_preparation', '무개선')
|
||||
})
|
||||
|
||||
area_df = pd.DataFrame(area_df_data)
|
||||
sheet_name = f'{area} 구역'
|
||||
area_df.to_excel(writer, sheet_name=sheet_name, index=False)
|
||||
|
||||
excel_buffer.seek(0)
|
||||
return excel_buffer
|
||||
|
||||
def _create_summary_data(self, segments_data: List[Dict], snapshot_name: str, job_no: str) -> List[Dict]:
|
||||
"""요약 정보 생성"""
|
||||
|
||||
# 기본 통계
|
||||
total_segments = len(segments_data)
|
||||
total_drawings = len(set(segment.get('drawing_name', '') for segment in segments_data))
|
||||
areas = sorted(set(segment.get('area', '') for segment in segments_data if segment.get('area')))
|
||||
|
||||
# 재질별 통계
|
||||
material_stats = {}
|
||||
for segment in segments_data:
|
||||
material = segment.get('material_grade', 'UNKNOWN')
|
||||
if material not in material_stats:
|
||||
material_stats[material] = {
|
||||
'count': 0,
|
||||
'total_length': 0
|
||||
}
|
||||
material_stats[material]['count'] += 1
|
||||
material_stats[material]['total_length'] += segment.get('length_mm', 0)
|
||||
|
||||
# 요약 데이터 구성
|
||||
summary_data = [
|
||||
{'항목': '작업번호', '값': job_no},
|
||||
{'항목': '스냅샷명', '값': snapshot_name},
|
||||
{'항목': '확정일시', '값': datetime.now().strftime('%Y-%m-%d %H:%M:%S')},
|
||||
{'항목': '총 단관 수', '값': total_segments},
|
||||
{'항목': '총 도면 수', '값': total_drawings},
|
||||
{'항목': '구역 수', '값': len(areas)},
|
||||
{'항목': '구역 목록', '값': ', '.join(areas)},
|
||||
{'항목': '', '값': ''}, # 빈 줄
|
||||
{'항목': '=== 재질별 통계 ===', '값': ''},
|
||||
]
|
||||
|
||||
for material, stats in material_stats.items():
|
||||
summary_data.extend([
|
||||
{'항목': f'{material} - 개수', '값': stats['count']},
|
||||
{'항목': f'{material} - 총길이(mm)', '값': f"{stats['total_length']:,.1f}"},
|
||||
{'항목': f'{material} - 총길이(m)', '값': f"{stats['total_length']/1000:,.3f}"}
|
||||
])
|
||||
|
||||
# 주의사항 추가
|
||||
summary_data.extend([
|
||||
{'항목': '', '값': ''}, # 빈 줄
|
||||
{'항목': '=== 주의사항 ===', '값': ''},
|
||||
{'항목': '⚠️ 확정된 데이터', '값': '이 Excel은 Cutting Plan 확정 시점의 데이터입니다.'},
|
||||
{'항목': '⚠️ 리비전 보호', '값': '이후 BOM 리비전이 발생해도 이 데이터는 변경되지 않습니다.'},
|
||||
{'항목': '⚠️ 수정 방법', '값': '변경이 필요한 경우 수동으로 편집하거나 새로운 Cutting Plan을 작성하세요.'},
|
||||
{'항목': '⚠️ 이슈 관리', '값': '현장 이슈는 별도 이슈 관리 시스템을 사용하세요.'}
|
||||
])
|
||||
|
||||
return summary_data
|
||||
|
||||
def check_finalization_status(self, job_no: str) -> Dict[str, Any]:
|
||||
"""Cutting Plan 확정 상태 확인"""
|
||||
try:
|
||||
snapshot_info = self.snapshot_service.get_snapshot_info(job_no)
|
||||
|
||||
return {
|
||||
"is_finalized": snapshot_info["has_snapshot"] and snapshot_info["is_locked"],
|
||||
"can_export_finalized": snapshot_info["has_snapshot"] and snapshot_info["is_locked"],
|
||||
"snapshot_info": snapshot_info if snapshot_info["has_snapshot"] else None,
|
||||
"message": "확정된 Cutting Plan Excel 내보내기 가능" if snapshot_info["has_snapshot"] and snapshot_info["is_locked"] else "Cutting Plan을 먼저 확정해주세요"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to check finalization status: {e}")
|
||||
return {
|
||||
"is_finalized": False,
|
||||
"can_export_finalized": False,
|
||||
"message": f"확정 상태 확인 실패: {str(e)}"
|
||||
}
|
||||
|
||||
|
||||
def get_pipe_snapshot_excel_service(db: Session = None) -> PipeSnapshotExcelService:
|
||||
"""PipeSnapshotExcelService 인스턴스 생성"""
|
||||
if db is None:
|
||||
db = next(get_db())
|
||||
return PipeSnapshotExcelService(db)
|
||||
224
backend/app/services/revision_comparison_service.py
Normal file
224
backend/app/services/revision_comparison_service.py
Normal file
@@ -0,0 +1,224 @@
|
||||
"""
|
||||
리비전 비교 전용 서비스
|
||||
두 리비전 간의 자재 비교 및 차이점 분석
|
||||
"""
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text
|
||||
from typing import List, Dict, Any, Optional, Tuple
|
||||
from decimal import Decimal
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
|
||||
from ..models import Material, File
|
||||
from ..utils.logger import get_logger
|
||||
from .database_service import DatabaseService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class RevisionComparisonService:
|
||||
"""리비전 비교 전용 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.db_service = DatabaseService(db)
|
||||
|
||||
def compare_revisions(
|
||||
self,
|
||||
current_file_id: int,
|
||||
previous_file_id: int,
|
||||
category_filter: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
두 리비전 간 자재 비교
|
||||
|
||||
Args:
|
||||
current_file_id: 현재 리비전 파일 ID
|
||||
previous_file_id: 이전 리비전 파일 ID
|
||||
category_filter: 특정 카테고리만 비교 (선택사항)
|
||||
|
||||
Returns:
|
||||
비교 결과 딕셔너리
|
||||
"""
|
||||
|
||||
# 이전/현재 자재 조회
|
||||
previous_materials = self._get_materials_for_comparison(previous_file_id, category_filter)
|
||||
current_materials = self._get_materials_for_comparison(current_file_id, category_filter)
|
||||
|
||||
# 비교 수행
|
||||
comparison_result = {
|
||||
"comparison_date": datetime.now().isoformat(),
|
||||
"current_file_id": current_file_id,
|
||||
"previous_file_id": previous_file_id,
|
||||
"category_filter": category_filter,
|
||||
"summary": {
|
||||
"previous_count": len(previous_materials),
|
||||
"current_count": len(current_materials),
|
||||
"unchanged": 0,
|
||||
"modified": 0,
|
||||
"added": 0,
|
||||
"removed": 0
|
||||
},
|
||||
"changes": {
|
||||
"unchanged": [],
|
||||
"modified": [],
|
||||
"added": [],
|
||||
"removed": []
|
||||
}
|
||||
}
|
||||
|
||||
# 이전 자재 기준으로 비교
|
||||
for key, prev_material in previous_materials.items():
|
||||
if key in current_materials:
|
||||
curr_material = current_materials[key]
|
||||
|
||||
# 자재 변경 여부 확인
|
||||
if self._is_material_changed(prev_material, curr_material):
|
||||
comparison_result["changes"]["modified"].append({
|
||||
"key": key,
|
||||
"previous": prev_material,
|
||||
"current": curr_material,
|
||||
"changes": self._get_material_changes(prev_material, curr_material)
|
||||
})
|
||||
comparison_result["summary"]["modified"] += 1
|
||||
else:
|
||||
comparison_result["changes"]["unchanged"].append({
|
||||
"key": key,
|
||||
"material": curr_material
|
||||
})
|
||||
comparison_result["summary"]["unchanged"] += 1
|
||||
else:
|
||||
# 제거된 자재
|
||||
comparison_result["changes"]["removed"].append({
|
||||
"key": key,
|
||||
"material": prev_material
|
||||
})
|
||||
comparison_result["summary"]["removed"] += 1
|
||||
|
||||
# 신규 자재
|
||||
for key, curr_material in current_materials.items():
|
||||
if key not in previous_materials:
|
||||
comparison_result["changes"]["added"].append({
|
||||
"key": key,
|
||||
"material": curr_material
|
||||
})
|
||||
comparison_result["summary"]["added"] += 1
|
||||
|
||||
return comparison_result
|
||||
|
||||
def get_category_comparison(
|
||||
self,
|
||||
current_file_id: int,
|
||||
previous_file_id: int,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""특정 카테고리의 리비전 비교"""
|
||||
|
||||
return self.compare_revisions(current_file_id, previous_file_id, category)
|
||||
|
||||
# PIPE 관련 메서드는 별도 처리 예정
|
||||
|
||||
def _get_materials_for_comparison(
|
||||
self,
|
||||
file_id: int,
|
||||
category_filter: Optional[str] = None
|
||||
) -> Dict[str, Dict]:
|
||||
"""비교용 자재 데이터 조회"""
|
||||
|
||||
query = """
|
||||
SELECT
|
||||
m.id, m.original_description, m.classified_category,
|
||||
m.material_grade, m.schedule, m.size_spec, m.main_nom, m.red_nom,
|
||||
m.quantity, m.unit, m.length, m.drawing_name, m.line_no,
|
||||
m.purchase_confirmed, m.confirmed_quantity, m.purchase_status,
|
||||
m.material_hash, m.revision_status, m.brand, m.user_requirement,
|
||||
m.line_number, m.is_active,
|
||||
-- 비교 키 생성 (PIPE 제외)
|
||||
COALESCE(m.material_hash,
|
||||
CONCAT(m.original_description, '|',
|
||||
COALESCE(m.material_grade, ''), '|',
|
||||
COALESCE(m.size_spec, ''))) as comparison_key
|
||||
FROM materials m
|
||||
WHERE m.file_id = :file_id AND m.is_active = true
|
||||
"""
|
||||
|
||||
params = {"file_id": file_id}
|
||||
|
||||
if category_filter:
|
||||
query += " AND m.classified_category = :category"
|
||||
params["category"] = category_filter
|
||||
|
||||
# PIPE 카테고리는 제외
|
||||
query += " AND m.classified_category != 'PIPE'"
|
||||
|
||||
query += " ORDER BY m.line_number"
|
||||
|
||||
result = self.db_service.execute_query(query, params)
|
||||
materials = {}
|
||||
|
||||
for row in result.fetchall():
|
||||
row_dict = dict(row._mapping)
|
||||
comparison_key = row_dict['comparison_key']
|
||||
|
||||
# PIPE 제외한 일반 자재 처리
|
||||
materials[comparison_key] = row_dict
|
||||
|
||||
return materials
|
||||
|
||||
def _is_material_changed(self, prev_material: Dict, curr_material: Dict) -> bool:
|
||||
"""자재 변경 여부 확인"""
|
||||
|
||||
# 주요 필드 비교
|
||||
compare_fields = ['quantity', 'material_grade', 'schedule', 'size_spec',
|
||||
'main_nom', 'red_nom', 'unit', 'length']
|
||||
|
||||
for field in compare_fields:
|
||||
prev_val = prev_material.get(field)
|
||||
curr_val = curr_material.get(field)
|
||||
|
||||
# 수치 필드는 부동소수점 오차 고려
|
||||
if field in ['quantity', 'length']:
|
||||
if prev_val is not None and curr_val is not None:
|
||||
if abs(float(prev_val) - float(curr_val)) > 0.001:
|
||||
return True
|
||||
elif prev_val != curr_val:
|
||||
return True
|
||||
else:
|
||||
if prev_val != curr_val:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def _get_material_changes(self, prev_material: Dict, curr_material: Dict) -> Dict[str, Any]:
|
||||
"""자재 변경 내용 상세 분석"""
|
||||
|
||||
changes = {}
|
||||
compare_fields = ['quantity', 'material_grade', 'schedule', 'size_spec',
|
||||
'main_nom', 'red_nom', 'unit', 'length']
|
||||
|
||||
for field in compare_fields:
|
||||
prev_val = prev_material.get(field)
|
||||
curr_val = curr_material.get(field)
|
||||
|
||||
if field in ['quantity', 'length']:
|
||||
if prev_val is not None and curr_val is not None:
|
||||
if abs(float(prev_val) - float(curr_val)) > 0.001:
|
||||
changes[field] = {
|
||||
"previous": float(prev_val),
|
||||
"current": float(curr_val),
|
||||
"change": float(curr_val) - float(prev_val)
|
||||
}
|
||||
elif prev_val != curr_val:
|
||||
changes[field] = {
|
||||
"previous": prev_val,
|
||||
"current": curr_val
|
||||
}
|
||||
else:
|
||||
if prev_val != curr_val:
|
||||
changes[field] = {
|
||||
"previous": prev_val,
|
||||
"current": curr_val
|
||||
}
|
||||
|
||||
return changes
|
||||
478
backend/app/services/revision_logic_service.py
Normal file
478
backend/app/services/revision_logic_service.py
Normal file
@@ -0,0 +1,478 @@
|
||||
"""
|
||||
리비전 처리 로직 서비스
|
||||
구매 상태와 카테고리별 특성을 고려한 스마트 리비전 관리
|
||||
"""
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text, and_, or_
|
||||
from typing import List, Dict, Any, Optional, Tuple
|
||||
from decimal import Decimal
|
||||
import hashlib
|
||||
from datetime import datetime
|
||||
|
||||
from ..models import Material, File
|
||||
from ..utils.logger import get_logger
|
||||
from .database_service import DatabaseService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class RevisionLogicService:
|
||||
"""리비전 처리 로직 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.db_service = DatabaseService(db)
|
||||
|
||||
def process_revision_by_purchase_status(
|
||||
self,
|
||||
job_no: str,
|
||||
current_file_id: int,
|
||||
previous_file_id: Optional[int] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
구매 상태별 리비전 처리
|
||||
|
||||
Returns:
|
||||
{
|
||||
"needs_revision_page": bool, # 리비전 페이지 필요 여부
|
||||
"can_use_bom_page": bool, # 기존 BOM 페이지 사용 가능 여부
|
||||
"processing_results": dict, # 처리 결과
|
||||
"revision_materials": list, # 리비전 페이지에서 관리할 자재
|
||||
"inventory_materials": list, # 재고로 분류할 자재
|
||||
"deleted_materials": list # 삭제할 자재
|
||||
}
|
||||
"""
|
||||
|
||||
if not previous_file_id:
|
||||
previous_file_id = self._get_previous_file_id(job_no, current_file_id)
|
||||
|
||||
if not previous_file_id:
|
||||
return self._handle_first_revision(current_file_id)
|
||||
|
||||
# 이전/현재 자재 조회
|
||||
previous_materials = self._get_materials_with_details(previous_file_id)
|
||||
current_materials = self._get_materials_with_details(current_file_id)
|
||||
|
||||
# 카테고리별 처리
|
||||
processing_results = {}
|
||||
revision_materials = []
|
||||
inventory_materials = []
|
||||
deleted_materials = []
|
||||
|
||||
# 각 카테고리별로 처리
|
||||
categories = ['PIPE', 'FITTING', 'FLANGE', 'VALVE', 'GASKET', 'BOLT', 'SUPPORT', 'SPECIAL']
|
||||
|
||||
for category in categories:
|
||||
category_result = self._process_category_revision(
|
||||
category, previous_materials, current_materials
|
||||
)
|
||||
|
||||
processing_results[category] = category_result
|
||||
|
||||
# 자재 분류
|
||||
revision_materials.extend(category_result['revision_materials'])
|
||||
inventory_materials.extend(category_result['inventory_materials'])
|
||||
deleted_materials.extend(category_result['deleted_materials'])
|
||||
|
||||
return {
|
||||
"needs_revision_page": True, # 리비전이면 항상 리비전 페이지 필요
|
||||
"can_use_bom_page": False, # 리비전이면 기존 BOM 페이지 사용 불가
|
||||
"processing_results": processing_results,
|
||||
"revision_materials": revision_materials,
|
||||
"inventory_materials": inventory_materials,
|
||||
"deleted_materials": deleted_materials,
|
||||
"summary": self._generate_revision_summary(processing_results)
|
||||
}
|
||||
|
||||
def _process_category_revision(
|
||||
self,
|
||||
category: str,
|
||||
previous_materials: Dict[str, Dict],
|
||||
current_materials: Dict[str, Dict]
|
||||
) -> Dict[str, Any]:
|
||||
"""카테고리별 리비전 처리"""
|
||||
|
||||
# 카테고리별 자재 필터링
|
||||
prev_category_materials = {
|
||||
k: v for k, v in previous_materials.items()
|
||||
if v.get('classified_category') == category
|
||||
}
|
||||
|
||||
curr_category_materials = {
|
||||
k: v for k, v in current_materials.items()
|
||||
if v.get('classified_category') == category
|
||||
}
|
||||
|
||||
result = {
|
||||
"category": category,
|
||||
"revision_materials": [],
|
||||
"inventory_materials": [],
|
||||
"deleted_materials": [],
|
||||
"unchanged_materials": [],
|
||||
"processing_summary": {
|
||||
"purchased_unchanged": 0,
|
||||
"purchased_excess": 0,
|
||||
"purchased_insufficient": 0,
|
||||
"unpurchased_deleted": 0,
|
||||
"unpurchased_unchanged": 0,
|
||||
"unpurchased_updated": 0,
|
||||
"new_materials": 0
|
||||
}
|
||||
}
|
||||
|
||||
# 이전 자재 기준으로 비교
|
||||
for key, prev_material in prev_category_materials.items():
|
||||
if key in curr_category_materials:
|
||||
curr_material = curr_category_materials[key]
|
||||
|
||||
# GASKET, BOLT는 규칙 적용 전 수량으로 비교
|
||||
if category in ['GASKET', 'BOLT']:
|
||||
comparison = self._compare_materials_pre_calculation(prev_material, curr_material, category)
|
||||
else:
|
||||
comparison = self._compare_materials_standard(prev_material, curr_material, category)
|
||||
|
||||
# 구매 완료 자재 처리
|
||||
if prev_material.get('purchase_confirmed', False):
|
||||
processed = self._process_purchased_material(prev_material, curr_material, comparison, category)
|
||||
else:
|
||||
# 구매 미완료 자재 처리
|
||||
processed = self._process_unpurchased_material(prev_material, curr_material, comparison, category)
|
||||
|
||||
# 결과 분류
|
||||
if processed['action'] == 'revision_management':
|
||||
result['revision_materials'].append(processed)
|
||||
elif processed['action'] == 'inventory':
|
||||
result['inventory_materials'].append(processed)
|
||||
elif processed['action'] == 'unchanged':
|
||||
result['unchanged_materials'].append(processed)
|
||||
|
||||
# 통계 업데이트
|
||||
result['processing_summary'][processed['summary_key']] += 1
|
||||
|
||||
else:
|
||||
# 삭제된 자재 (현재 리비전에 없음)
|
||||
if prev_material.get('purchase_confirmed', False):
|
||||
# 구매 완료된 자재가 삭제됨 → 재고로 분류
|
||||
result['inventory_materials'].append({
|
||||
'material': prev_material,
|
||||
'action': 'inventory',
|
||||
'reason': 'purchased_but_removed_in_revision',
|
||||
'category': category
|
||||
})
|
||||
else:
|
||||
# 구매 미완료 자재가 삭제됨 → 완전 삭제
|
||||
result['deleted_materials'].append({
|
||||
'material': prev_material,
|
||||
'action': 'delete',
|
||||
'reason': 'no_longer_needed',
|
||||
'category': category
|
||||
})
|
||||
result['processing_summary']['unpurchased_deleted'] += 1
|
||||
|
||||
# 신규 자재 처리
|
||||
for key, curr_material in curr_category_materials.items():
|
||||
if key not in prev_category_materials:
|
||||
result['revision_materials'].append({
|
||||
'material': curr_material,
|
||||
'action': 'revision_management',
|
||||
'reason': 'new_material',
|
||||
'category': category,
|
||||
'summary_key': 'new_materials'
|
||||
})
|
||||
result['processing_summary']['new_materials'] += 1
|
||||
|
||||
return result
|
||||
|
||||
def _process_purchased_material(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
comparison: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""구매 완료 자재 처리"""
|
||||
|
||||
if comparison['change_type'] == 'no_change':
|
||||
# 변동 없음 → 구매 완료 상태 유지, 더 이상 관리 불필요
|
||||
return {
|
||||
'material': curr_material,
|
||||
'action': 'unchanged',
|
||||
'reason': 'purchased_no_change',
|
||||
'category': category,
|
||||
'summary_key': 'purchased_unchanged'
|
||||
}
|
||||
|
||||
elif comparison['change_type'] == 'decreased':
|
||||
# 수량 감소/불필요 → 재고 자재로 분류
|
||||
excess_quantity = abs(comparison['quantity_change'])
|
||||
return {
|
||||
'material': prev_material, # 이전 자재 정보 사용
|
||||
'action': 'inventory',
|
||||
'reason': 'purchased_excess',
|
||||
'category': category,
|
||||
'excess_quantity': excess_quantity,
|
||||
'current_needed': curr_material.get('quantity', 0),
|
||||
'summary_key': 'purchased_excess'
|
||||
}
|
||||
|
||||
else: # increased
|
||||
# 수량 부족 → 리비전 페이지에서 추가 구매 관리
|
||||
additional_needed = comparison['quantity_change']
|
||||
return {
|
||||
'material': curr_material,
|
||||
'action': 'revision_management',
|
||||
'reason': 'purchased_insufficient',
|
||||
'category': category,
|
||||
'additional_needed': additional_needed,
|
||||
'already_purchased': prev_material.get('quantity', 0),
|
||||
'summary_key': 'purchased_insufficient'
|
||||
}
|
||||
|
||||
def _process_unpurchased_material(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
comparison: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""구매 미완료 자재 처리"""
|
||||
|
||||
if comparison['change_type'] == 'no_change':
|
||||
# 수량 동일 → 리비전 페이지에서 구매 관리 계속
|
||||
return {
|
||||
'material': curr_material,
|
||||
'action': 'revision_management',
|
||||
'reason': 'unpurchased_unchanged',
|
||||
'category': category,
|
||||
'summary_key': 'unpurchased_unchanged'
|
||||
}
|
||||
|
||||
else:
|
||||
# 수량 변경 → 필요 수량만큼 리비전 페이지에서 관리
|
||||
return {
|
||||
'material': curr_material,
|
||||
'action': 'revision_management',
|
||||
'reason': 'unpurchased_quantity_changed',
|
||||
'category': category,
|
||||
'quantity_change': comparison['quantity_change'],
|
||||
'previous_quantity': prev_material.get('quantity', 0),
|
||||
'summary_key': 'unpurchased_updated'
|
||||
}
|
||||
|
||||
def _compare_materials_standard(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""표준 자재 비교 (PIPE 제외)"""
|
||||
|
||||
prev_qty = float(prev_material.get('quantity', 0))
|
||||
curr_qty = float(curr_material.get('quantity', 0))
|
||||
|
||||
quantity_change = curr_qty - prev_qty
|
||||
|
||||
if abs(quantity_change) < 0.001: # 부동소수점 오차 고려
|
||||
change_type = 'no_change'
|
||||
elif quantity_change > 0:
|
||||
change_type = 'increased'
|
||||
else:
|
||||
change_type = 'decreased'
|
||||
|
||||
return {
|
||||
'quantity_change': quantity_change,
|
||||
'change_type': change_type,
|
||||
'previous_quantity': prev_qty,
|
||||
'current_quantity': curr_qty
|
||||
}
|
||||
|
||||
def _compare_materials_pre_calculation(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""규칙 적용 전 수량으로 비교 (GASKET, BOLT)"""
|
||||
|
||||
# 원본 수량 (규칙 적용 전)으로 비교
|
||||
prev_original_qty = float(prev_material.get('original_quantity', prev_material.get('quantity', 0)))
|
||||
curr_original_qty = float(curr_material.get('original_quantity', curr_material.get('quantity', 0)))
|
||||
|
||||
quantity_change = curr_original_qty - prev_original_qty
|
||||
|
||||
if abs(quantity_change) < 0.001:
|
||||
change_type = 'no_change'
|
||||
elif quantity_change > 0:
|
||||
change_type = 'increased'
|
||||
else:
|
||||
change_type = 'decreased'
|
||||
|
||||
# 최종 계산된 수량도 포함
|
||||
final_prev_qty = float(prev_material.get('quantity', 0))
|
||||
final_curr_qty = float(curr_material.get('quantity', 0))
|
||||
|
||||
return {
|
||||
'quantity_change': quantity_change,
|
||||
'change_type': change_type,
|
||||
'previous_quantity': prev_original_qty,
|
||||
'current_quantity': curr_original_qty,
|
||||
'final_previous_quantity': final_prev_qty,
|
||||
'final_current_quantity': final_curr_qty,
|
||||
'calculation_rule_applied': True
|
||||
}
|
||||
|
||||
def _get_materials_with_details(self, file_id: int) -> Dict[str, Dict]:
|
||||
"""파일의 자재를 상세 정보와 함께 조회"""
|
||||
|
||||
query = """
|
||||
SELECT
|
||||
m.id, m.original_description, m.classified_category,
|
||||
m.material_grade, m.schedule, m.size_spec, m.main_nom, m.red_nom,
|
||||
m.quantity, m.unit, m.length, m.drawing_name, m.line_no,
|
||||
m.purchase_confirmed, m.confirmed_quantity, m.purchase_status,
|
||||
m.material_hash, m.revision_status, m.brand, m.user_requirement,
|
||||
-- PIPE 자재 특별 키 생성
|
||||
CASE
|
||||
WHEN m.classified_category = 'PIPE' THEN
|
||||
CONCAT(m.drawing_name, '|', m.line_no, '|', COALESCE(m.length, 0))
|
||||
ELSE
|
||||
m.material_hash
|
||||
END as comparison_key
|
||||
FROM materials m
|
||||
WHERE m.file_id = :file_id AND m.is_active = true
|
||||
ORDER BY m.line_number
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {"file_id": file_id})
|
||||
materials = {}
|
||||
|
||||
for row in result.fetchall():
|
||||
row_dict = dict(row._mapping)
|
||||
comparison_key = row_dict['comparison_key']
|
||||
|
||||
# PIPE 자재의 경우 도면-라인넘버별로 길이 합산
|
||||
if row_dict['classified_category'] == 'PIPE':
|
||||
if comparison_key in materials:
|
||||
# 기존 자재에 길이 합산
|
||||
materials[comparison_key]['quantity'] += row_dict['quantity']
|
||||
materials[comparison_key]['total_length'] = (
|
||||
materials[comparison_key].get('total_length', 0) +
|
||||
(row_dict['length'] or 0) * row_dict['quantity']
|
||||
)
|
||||
else:
|
||||
row_dict['total_length'] = (row_dict['length'] or 0) * row_dict['quantity']
|
||||
materials[comparison_key] = row_dict
|
||||
else:
|
||||
materials[comparison_key] = row_dict
|
||||
|
||||
return materials
|
||||
|
||||
def _get_previous_file_id(self, job_no: str, current_file_id: int) -> Optional[int]:
|
||||
"""이전 파일 ID 자동 탐지"""
|
||||
|
||||
query = """
|
||||
SELECT id, revision
|
||||
FROM files
|
||||
WHERE job_no = :job_no AND id != :current_file_id AND is_active = true
|
||||
ORDER BY upload_date DESC
|
||||
LIMIT 1
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {
|
||||
"job_no": job_no,
|
||||
"current_file_id": current_file_id
|
||||
})
|
||||
|
||||
row = result.fetchone()
|
||||
return row.id if row else None
|
||||
|
||||
def _handle_first_revision(self, current_file_id: int) -> Dict[str, Any]:
|
||||
"""첫 번째 리비전 처리"""
|
||||
|
||||
materials = self._get_materials_with_details(current_file_id)
|
||||
|
||||
return {
|
||||
"needs_revision_page": True, # 첫 리비전은 항상 리비전 페이지 필요
|
||||
"can_use_bom_page": False,
|
||||
"processing_results": {},
|
||||
"revision_materials": [{"material": mat, "action": "revision_management", "reason": "first_revision"} for mat in materials.values()],
|
||||
"inventory_materials": [],
|
||||
"deleted_materials": [],
|
||||
"summary": {
|
||||
"is_first_revision": True,
|
||||
"total_materials": len(materials)
|
||||
}
|
||||
}
|
||||
|
||||
def _generate_revision_summary(self, processing_results: Dict) -> Dict[str, Any]:
|
||||
"""리비전 처리 요약 생성"""
|
||||
|
||||
summary = {
|
||||
"total_categories": len(processing_results),
|
||||
"total_revision_materials": 0,
|
||||
"total_inventory_materials": 0,
|
||||
"total_deleted_materials": 0,
|
||||
"by_category": {}
|
||||
}
|
||||
|
||||
for category, result in processing_results.items():
|
||||
summary["total_revision_materials"] += len(result['revision_materials'])
|
||||
summary["total_inventory_materials"] += len(result['inventory_materials'])
|
||||
summary["total_deleted_materials"] += len(result['deleted_materials'])
|
||||
|
||||
summary["by_category"][category] = {
|
||||
"revision_count": len(result['revision_materials']),
|
||||
"inventory_count": len(result['inventory_materials']),
|
||||
"deleted_count": len(result['deleted_materials']),
|
||||
"processing_summary": result['processing_summary']
|
||||
}
|
||||
|
||||
return summary
|
||||
|
||||
def should_redirect_to_revision_page(
|
||||
self,
|
||||
job_no: str,
|
||||
current_file_id: int,
|
||||
previous_file_id: Optional[int] = None
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
리비전 페이지로 리다이렉트해야 하는지 판단
|
||||
실제 변경사항이 있을 때만 리비전 페이지로 이동
|
||||
|
||||
Returns:
|
||||
(should_redirect: bool, reason: str)
|
||||
"""
|
||||
|
||||
try:
|
||||
# 이전 파일이 있는지 확인 (리비전 여부 판단)
|
||||
if not previous_file_id:
|
||||
previous_file_id = self._get_previous_file_id(job_no, current_file_id)
|
||||
|
||||
if not previous_file_id:
|
||||
# 첫 번째 파일 (리비전 아님) → 기존 BOM 페이지 사용
|
||||
return False, "첫 번째 BOM 파일이므로 기존 페이지에서 관리합니다."
|
||||
|
||||
# 실제 변경사항이 있는지 확인
|
||||
processing_results = self.process_revision_by_purchase_status(
|
||||
job_no, current_file_id, previous_file_id
|
||||
)
|
||||
|
||||
# 변경사항 통계 확인
|
||||
summary = processing_results.get('summary', {})
|
||||
total_changes = (
|
||||
summary.get('revision_materials', 0) +
|
||||
summary.get('inventory_materials', 0) +
|
||||
summary.get('deleted_materials', 0)
|
||||
)
|
||||
|
||||
if total_changes > 0:
|
||||
# 실제 변경사항이 있으면 리비전 페이지로
|
||||
return True, f"리비전 변경사항이 감지되었습니다 (변경: {total_changes}개). 리비전 페이지에서 관리해야 합니다."
|
||||
else:
|
||||
# 변경사항이 없으면 기존 BOM 페이지 사용
|
||||
return False, "리비전 파일이지만 변경사항이 없어 기존 페이지에서 관리합니다."
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to determine revision redirect: {e}")
|
||||
return False, "리비전 상태 확인 실패 - 기존 페이지 사용"
|
||||
425
backend/app/services/revision_material_service.py
Normal file
425
backend/app/services/revision_material_service.py
Normal file
@@ -0,0 +1,425 @@
|
||||
"""
|
||||
리비전 자재 처리 전용 서비스
|
||||
구매 상태별 자재 처리 로직
|
||||
"""
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text
|
||||
from typing import List, Dict, Any, Optional
|
||||
from decimal import Decimal
|
||||
from datetime import datetime
|
||||
|
||||
from ..models import Material, File
|
||||
from ..utils.logger import get_logger
|
||||
from .database_service import DatabaseService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class RevisionMaterialService:
|
||||
"""리비전 자재 처리 전용 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.db_service = DatabaseService(db)
|
||||
|
||||
def process_material_by_purchase_status(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
구매 상태별 자재 처리
|
||||
|
||||
Args:
|
||||
prev_material: 이전 리비전 자재
|
||||
curr_material: 현재 리비전 자재
|
||||
category: 자재 카테고리
|
||||
|
||||
Returns:
|
||||
처리 결과
|
||||
"""
|
||||
|
||||
# 수량 변화 계산
|
||||
quantity_change = self._calculate_quantity_change(prev_material, curr_material, category)
|
||||
|
||||
# 구매 완료 자재 처리
|
||||
if prev_material.get('purchase_confirmed', False):
|
||||
return self._process_purchased_material(prev_material, curr_material, quantity_change, category)
|
||||
else:
|
||||
# 구매 미완료 자재 처리
|
||||
return self._process_unpurchased_material(prev_material, curr_material, quantity_change, category)
|
||||
|
||||
def process_new_material(self, material: Dict, category: str) -> Dict[str, Any]:
|
||||
"""신규 자재 처리"""
|
||||
|
||||
return {
|
||||
'material_id': material['id'],
|
||||
'category': category,
|
||||
'action': 'new_material',
|
||||
'status': 'needs_purchase',
|
||||
'quantity': material.get('quantity', 0),
|
||||
'description': material.get('original_description', ''),
|
||||
'processing_note': '신규 자재 - 구매 필요',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': True,
|
||||
'highlight_color': 'green',
|
||||
'action_required': '구매 신청',
|
||||
'badge': 'NEW'
|
||||
}
|
||||
}
|
||||
|
||||
def process_removed_material(self, material: Dict, category: str) -> Dict[str, Any]:
|
||||
"""제거된 자재 처리"""
|
||||
|
||||
if material.get('purchase_confirmed', False):
|
||||
# 구매 완료된 자재가 제거됨 → 재고로 분류
|
||||
return {
|
||||
'material_id': material['id'],
|
||||
'category': category,
|
||||
'action': 'move_to_inventory',
|
||||
'status': 'excess_inventory',
|
||||
'quantity': material.get('quantity', 0),
|
||||
'description': material.get('original_description', ''),
|
||||
'processing_note': '구매 완료 후 리비전에서 제거됨 - 재고 보관',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': True,
|
||||
'highlight_color': 'orange',
|
||||
'action_required': '재고 관리',
|
||||
'badge': 'INVENTORY'
|
||||
}
|
||||
}
|
||||
else:
|
||||
# 구매 미완료 자재가 제거됨 → 완전 삭제
|
||||
return {
|
||||
'material_id': material['id'],
|
||||
'category': category,
|
||||
'action': 'delete',
|
||||
'status': 'deleted',
|
||||
'quantity': material.get('quantity', 0),
|
||||
'description': material.get('original_description', ''),
|
||||
'processing_note': '리비전에서 제거됨 - 구매 불필요',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': False, # 삭제된 자재는 표시 안함
|
||||
'highlight_color': 'red',
|
||||
'action_required': '삭제 완료',
|
||||
'badge': 'DELETED'
|
||||
}
|
||||
}
|
||||
|
||||
def _process_purchased_material(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
quantity_change: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""구매 완료 자재 처리"""
|
||||
|
||||
if quantity_change['change_type'] == 'no_change':
|
||||
# 변동 없음 → 구매 완료 상태 유지
|
||||
return {
|
||||
'material_id': curr_material['id'],
|
||||
'category': category,
|
||||
'action': 'maintain_status',
|
||||
'status': 'purchased_completed',
|
||||
'quantity': curr_material.get('quantity', 0),
|
||||
'description': curr_material.get('original_description', ''),
|
||||
'processing_note': '구매 완료 - 변동 없음',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': False, # 변동 없는 구매완료 자재는 숨김
|
||||
'highlight_color': 'gray',
|
||||
'action_required': '관리 불필요',
|
||||
'badge': 'COMPLETED'
|
||||
}
|
||||
}
|
||||
|
||||
elif quantity_change['change_type'] == 'decreased':
|
||||
# 수량 감소 → 재고 자재로 분류
|
||||
excess_quantity = abs(quantity_change['quantity_change'])
|
||||
return {
|
||||
'material_id': curr_material['id'],
|
||||
'category': category,
|
||||
'action': 'partial_inventory',
|
||||
'status': 'excess_inventory',
|
||||
'quantity': curr_material.get('quantity', 0),
|
||||
'excess_quantity': excess_quantity,
|
||||
'purchased_quantity': prev_material.get('quantity', 0),
|
||||
'description': curr_material.get('original_description', ''),
|
||||
'processing_note': f'구매 완료 후 수량 감소 - 잉여 {excess_quantity}개 재고 보관',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': True,
|
||||
'highlight_color': 'orange',
|
||||
'action_required': '잉여 재고 관리',
|
||||
'badge': 'EXCESS'
|
||||
}
|
||||
}
|
||||
|
||||
else: # increased
|
||||
# 수량 부족 → 추가 구매 필요
|
||||
additional_needed = quantity_change['quantity_change']
|
||||
return {
|
||||
'material_id': curr_material['id'],
|
||||
'category': category,
|
||||
'action': 'additional_purchase',
|
||||
'status': 'needs_additional_purchase',
|
||||
'quantity': curr_material.get('quantity', 0),
|
||||
'additional_needed': additional_needed,
|
||||
'already_purchased': prev_material.get('quantity', 0),
|
||||
'description': curr_material.get('original_description', ''),
|
||||
'processing_note': f'구매 완료 후 수량 부족 - 추가 {additional_needed}개 구매 필요',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': True,
|
||||
'highlight_color': 'red',
|
||||
'action_required': '추가 구매 신청',
|
||||
'badge': 'ADDITIONAL'
|
||||
}
|
||||
}
|
||||
|
||||
def _process_unpurchased_material(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
quantity_change: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""구매 미완료 자재 처리"""
|
||||
|
||||
if quantity_change['change_type'] == 'no_change':
|
||||
# 수량 동일 → 구매 관리 계속
|
||||
return {
|
||||
'material_id': curr_material['id'],
|
||||
'category': category,
|
||||
'action': 'continue_purchase',
|
||||
'status': 'pending_purchase',
|
||||
'quantity': curr_material.get('quantity', 0),
|
||||
'description': curr_material.get('original_description', ''),
|
||||
'processing_note': '수량 변동 없음 - 구매 진행',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': True,
|
||||
'highlight_color': 'blue',
|
||||
'action_required': '구매 신청',
|
||||
'badge': 'PENDING'
|
||||
}
|
||||
}
|
||||
|
||||
else:
|
||||
# 수량 변경 → 수량 업데이트 후 구매 관리
|
||||
return {
|
||||
'material_id': curr_material['id'],
|
||||
'category': category,
|
||||
'action': 'update_quantity',
|
||||
'status': 'quantity_updated',
|
||||
'quantity': curr_material.get('quantity', 0),
|
||||
'previous_quantity': prev_material.get('quantity', 0),
|
||||
'quantity_change': quantity_change['quantity_change'],
|
||||
'description': curr_material.get('original_description', ''),
|
||||
'processing_note': f'수량 변경: {prev_material.get("quantity", 0)} → {curr_material.get("quantity", 0)}',
|
||||
'ui_display': {
|
||||
'show_in_revision_page': True,
|
||||
'highlight_color': 'yellow',
|
||||
'action_required': '수량 확인 후 구매 신청',
|
||||
'badge': 'UPDATED'
|
||||
}
|
||||
}
|
||||
|
||||
def _calculate_quantity_change(
|
||||
self,
|
||||
prev_material: Dict,
|
||||
curr_material: Dict,
|
||||
category: str
|
||||
) -> Dict[str, Any]:
|
||||
"""수량 변화 계산"""
|
||||
|
||||
# GASKET, BOLT는 규칙 적용 전 수량으로 비교
|
||||
if category in ['GASKET', 'BOLT']:
|
||||
prev_qty = float(prev_material.get('original_quantity', prev_material.get('quantity', 0)))
|
||||
curr_qty = float(curr_material.get('original_quantity', curr_material.get('quantity', 0)))
|
||||
else:
|
||||
prev_qty = float(prev_material.get('quantity', 0))
|
||||
curr_qty = float(curr_material.get('quantity', 0))
|
||||
|
||||
quantity_change = curr_qty - prev_qty
|
||||
|
||||
if abs(quantity_change) < 0.001: # 부동소수점 오차 고려
|
||||
change_type = 'no_change'
|
||||
elif quantity_change > 0:
|
||||
change_type = 'increased'
|
||||
else:
|
||||
change_type = 'decreased'
|
||||
|
||||
return {
|
||||
'previous_quantity': prev_qty,
|
||||
'current_quantity': curr_qty,
|
||||
'quantity_change': quantity_change,
|
||||
'change_type': change_type,
|
||||
'is_gasket_bolt': category in ['GASKET', 'BOLT']
|
||||
}
|
||||
|
||||
def get_category_materials_for_revision(
|
||||
self,
|
||||
file_id: int,
|
||||
category: str,
|
||||
include_processing_info: bool = True
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
리비전 페이지용 카테고리별 자재 조회
|
||||
|
||||
Args:
|
||||
file_id: 파일 ID
|
||||
category: 카테고리
|
||||
include_processing_info: 처리 정보 포함 여부
|
||||
|
||||
Returns:
|
||||
자재 목록 (처리 정보 포함)
|
||||
"""
|
||||
|
||||
query = """
|
||||
SELECT
|
||||
m.id, m.original_description, m.classified_category,
|
||||
m.material_grade, m.schedule, m.size_spec, m.main_nom, m.red_nom,
|
||||
m.quantity, m.unit, m.length, m.drawing_name, m.line_no,
|
||||
m.purchase_confirmed, m.confirmed_quantity, m.purchase_status,
|
||||
m.material_hash, m.revision_status, m.brand, m.user_requirement,
|
||||
m.line_number, m.is_active, m.notes,
|
||||
-- 추가 정보
|
||||
COALESCE(m.purchase_confirmed_at, m.created_at) as status_date,
|
||||
COALESCE(m.purchase_confirmed_by, 'system') as status_by
|
||||
FROM materials m
|
||||
WHERE m.file_id = :file_id
|
||||
AND m.classified_category = :category
|
||||
AND m.classified_category != 'PIPE'
|
||||
AND m.is_active = true
|
||||
ORDER BY m.line_number
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {
|
||||
"file_id": file_id,
|
||||
"category": category
|
||||
})
|
||||
|
||||
materials = []
|
||||
for row in result.fetchall():
|
||||
material_dict = dict(row._mapping)
|
||||
|
||||
if include_processing_info:
|
||||
# 처리 정보 추가
|
||||
material_dict['processing_info'] = self._get_material_processing_info(material_dict)
|
||||
|
||||
materials.append(material_dict)
|
||||
|
||||
return materials
|
||||
|
||||
def _get_material_processing_info(self, material: Dict) -> Dict[str, Any]:
|
||||
"""자재 처리 정보 생성"""
|
||||
|
||||
revision_status = material.get('revision_status', '')
|
||||
purchase_confirmed = material.get('purchase_confirmed', False)
|
||||
|
||||
if revision_status == 'new_in_revision':
|
||||
return {
|
||||
'display_status': 'NEW',
|
||||
'color': 'green',
|
||||
'action': '신규 구매 필요',
|
||||
'priority': 'high'
|
||||
}
|
||||
elif revision_status == 'additional_purchase_needed':
|
||||
return {
|
||||
'display_status': 'ADDITIONAL',
|
||||
'color': 'red',
|
||||
'action': '추가 구매 필요',
|
||||
'priority': 'high'
|
||||
}
|
||||
elif revision_status == 'excess_inventory':
|
||||
return {
|
||||
'display_status': 'EXCESS',
|
||||
'color': 'orange',
|
||||
'action': '재고 관리',
|
||||
'priority': 'medium'
|
||||
}
|
||||
elif revision_status == 'quantity_updated':
|
||||
return {
|
||||
'display_status': 'UPDATED',
|
||||
'color': 'yellow',
|
||||
'action': '수량 확인',
|
||||
'priority': 'medium'
|
||||
}
|
||||
elif purchase_confirmed:
|
||||
return {
|
||||
'display_status': 'COMPLETED',
|
||||
'color': 'gray',
|
||||
'action': '완료',
|
||||
'priority': 'low'
|
||||
}
|
||||
else:
|
||||
return {
|
||||
'display_status': 'PENDING',
|
||||
'color': 'blue',
|
||||
'action': '구매 대기',
|
||||
'priority': 'medium'
|
||||
}
|
||||
|
||||
def apply_material_processing_results(
|
||||
self,
|
||||
processing_results: List[Dict[str, Any]]
|
||||
) -> Dict[str, Any]:
|
||||
"""자재 처리 결과를 DB에 적용"""
|
||||
|
||||
try:
|
||||
applied_count = 0
|
||||
error_count = 0
|
||||
|
||||
for result in processing_results:
|
||||
try:
|
||||
material_id = result['material_id']
|
||||
action = result['action']
|
||||
status = result['status']
|
||||
|
||||
if action == 'delete':
|
||||
# 자재 비활성화
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET is_active = false,
|
||||
revision_status = 'deleted',
|
||||
notes = CONCAT(COALESCE(notes, ''), '\n', :note)
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
else:
|
||||
# 자재 상태 업데이트
|
||||
update_query = """
|
||||
UPDATE materials
|
||||
SET revision_status = :status,
|
||||
notes = CONCAT(COALESCE(notes, ''), '\n', :note)
|
||||
WHERE id = :material_id
|
||||
"""
|
||||
|
||||
self.db_service.execute_query(update_query, {
|
||||
"material_id": material_id,
|
||||
"status": status,
|
||||
"note": result.get('processing_note', '')
|
||||
})
|
||||
|
||||
applied_count += 1
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to apply processing result for material {result.get('material_id')}: {e}")
|
||||
error_count += 1
|
||||
|
||||
self.db.commit()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"applied_count": applied_count,
|
||||
"error_count": error_count,
|
||||
"message": f"자재 처리 완료: {applied_count}개 적용, {error_count}개 오류"
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to apply material processing results: {e}")
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
"message": "자재 처리 적용 중 오류 발생"
|
||||
}
|
||||
421
backend/app/services/revision_status_service.py
Normal file
421
backend/app/services/revision_status_service.py
Normal file
@@ -0,0 +1,421 @@
|
||||
"""
|
||||
리비전 상태 관리 서비스
|
||||
리비전 진행 상태, 히스토리, 확정 등 관리
|
||||
"""
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text, desc
|
||||
from typing import List, Dict, Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from ..models import File, RevisionComparison, RevisionChangeLog
|
||||
from ..utils.logger import get_logger
|
||||
from .database_service import DatabaseService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class RevisionStatusService:
|
||||
"""리비전 상태 관리 서비스"""
|
||||
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.db_service = DatabaseService(db)
|
||||
|
||||
def get_revision_status(self, job_no: str, file_id: int) -> Dict[str, Any]:
|
||||
"""
|
||||
리비전 상태 조회
|
||||
|
||||
Args:
|
||||
job_no: 작업 번호
|
||||
file_id: 파일 ID
|
||||
|
||||
Returns:
|
||||
리비전 상태 정보
|
||||
"""
|
||||
|
||||
# 파일 정보 조회
|
||||
current_file = self._get_file_info(file_id)
|
||||
if not current_file:
|
||||
return {"error": "파일을 찾을 수 없습니다."}
|
||||
|
||||
# 같은 작업의 모든 파일 조회
|
||||
all_files = self._get_job_files(job_no)
|
||||
|
||||
# 리비전 히스토리 구성
|
||||
revision_history = self._build_revision_history(all_files, file_id)
|
||||
|
||||
# 현재 리비전의 처리 상태
|
||||
processing_status = self._get_processing_status(file_id)
|
||||
|
||||
return {
|
||||
"job_no": job_no,
|
||||
"current_file": current_file,
|
||||
"revision_history": revision_history,
|
||||
"processing_status": processing_status,
|
||||
"is_latest": revision_history.get("is_latest", False),
|
||||
"can_upload_new_revision": revision_history.get("can_upload_new", True),
|
||||
"status_summary": self._generate_status_summary(processing_status)
|
||||
}
|
||||
|
||||
def get_revision_history(self, job_no: str) -> List[Dict[str, Any]]:
|
||||
"""작업의 전체 리비전 히스토리 조회"""
|
||||
|
||||
files = self._get_job_files(job_no)
|
||||
|
||||
history = []
|
||||
for i, file_info in enumerate(files):
|
||||
# 이전 파일과의 비교 정보
|
||||
comparison_info = None
|
||||
if i > 0:
|
||||
prev_file = files[i-1]
|
||||
comparison_info = self._get_comparison_summary(file_info['id'], prev_file['id'])
|
||||
|
||||
history.append({
|
||||
"file_id": file_info['id'],
|
||||
"revision": file_info['revision'],
|
||||
"filename": file_info['original_filename'],
|
||||
"upload_date": file_info['upload_date'],
|
||||
"uploaded_by": file_info['uploaded_by'],
|
||||
"file_size": file_info['file_size'],
|
||||
"material_count": self._get_material_count(file_info['id']),
|
||||
"comparison_with_previous": comparison_info,
|
||||
"is_latest": i == 0, # 최신순 정렬이므로 첫 번째가 최신
|
||||
"processing_status": self._get_processing_status(file_info['id'])
|
||||
})
|
||||
|
||||
return history
|
||||
|
||||
def create_revision_comparison_record(
|
||||
self,
|
||||
job_no: str,
|
||||
current_file_id: int,
|
||||
previous_file_id: int,
|
||||
comparison_result: Dict[str, Any],
|
||||
created_by: str
|
||||
) -> int:
|
||||
"""리비전 비교 기록 생성"""
|
||||
|
||||
try:
|
||||
comparison_record = RevisionComparison(
|
||||
job_no=job_no,
|
||||
current_file_id=current_file_id,
|
||||
previous_file_id=previous_file_id,
|
||||
comparison_result=comparison_result,
|
||||
summary_stats=comparison_result.get("summary", {}),
|
||||
created_by=created_by,
|
||||
is_applied=False
|
||||
)
|
||||
|
||||
self.db.add(comparison_record)
|
||||
self.db.commit()
|
||||
self.db.refresh(comparison_record)
|
||||
|
||||
logger.info(f"Created revision comparison record: {comparison_record.id}")
|
||||
return comparison_record.id
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to create revision comparison record: {e}")
|
||||
raise
|
||||
|
||||
def apply_revision_comparison(
|
||||
self,
|
||||
comparison_id: int,
|
||||
applied_by: str
|
||||
) -> Dict[str, Any]:
|
||||
"""리비전 비교 결과 적용"""
|
||||
|
||||
try:
|
||||
# 비교 기록 조회
|
||||
comparison = self.db.query(RevisionComparison).filter(
|
||||
RevisionComparison.id == comparison_id
|
||||
).first()
|
||||
|
||||
if not comparison:
|
||||
return {"success": False, "error": "비교 기록을 찾을 수 없습니다."}
|
||||
|
||||
if comparison.is_applied:
|
||||
return {"success": False, "error": "이미 적용된 비교 결과입니다."}
|
||||
|
||||
# 적용 처리
|
||||
comparison.is_applied = True
|
||||
comparison.applied_at = datetime.utcnow()
|
||||
comparison.applied_by = applied_by
|
||||
|
||||
# 변경 로그 생성
|
||||
self._create_change_logs(comparison)
|
||||
|
||||
self.db.commit()
|
||||
|
||||
logger.info(f"Applied revision comparison: {comparison_id}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"comparison_id": comparison_id,
|
||||
"applied_at": comparison.applied_at.isoformat(),
|
||||
"applied_by": applied_by
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
self.db.rollback()
|
||||
logger.error(f"Failed to apply revision comparison: {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
def get_pending_revisions(self, job_no: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
"""대기 중인 리비전 목록 조회"""
|
||||
|
||||
query = """
|
||||
SELECT
|
||||
rc.id, rc.job_no, rc.current_file_id, rc.previous_file_id,
|
||||
rc.comparison_date, rc.created_by, rc.summary_stats,
|
||||
cf.original_filename as current_filename,
|
||||
cf.revision as current_revision,
|
||||
pf.original_filename as previous_filename,
|
||||
pf.revision as previous_revision
|
||||
FROM revision_comparisons rc
|
||||
JOIN files cf ON rc.current_file_id = cf.id
|
||||
LEFT JOIN files pf ON rc.previous_file_id = pf.id
|
||||
WHERE rc.is_applied = false
|
||||
"""
|
||||
|
||||
params = {}
|
||||
if job_no:
|
||||
query += " AND rc.job_no = :job_no"
|
||||
params["job_no"] = job_no
|
||||
|
||||
query += " ORDER BY rc.comparison_date DESC"
|
||||
|
||||
result = self.db_service.execute_query(query, params)
|
||||
|
||||
pending_revisions = []
|
||||
for row in result.fetchall():
|
||||
row_dict = dict(row._mapping)
|
||||
pending_revisions.append({
|
||||
"comparison_id": row_dict['id'],
|
||||
"job_no": row_dict['job_no'],
|
||||
"current_file": {
|
||||
"id": row_dict['current_file_id'],
|
||||
"filename": row_dict['current_filename'],
|
||||
"revision": row_dict['current_revision']
|
||||
},
|
||||
"previous_file": {
|
||||
"id": row_dict['previous_file_id'],
|
||||
"filename": row_dict['previous_filename'],
|
||||
"revision": row_dict['previous_revision']
|
||||
} if row_dict['previous_file_id'] else None,
|
||||
"comparison_date": row_dict['comparison_date'],
|
||||
"created_by": row_dict['created_by'],
|
||||
"summary_stats": row_dict['summary_stats']
|
||||
})
|
||||
|
||||
return pending_revisions
|
||||
|
||||
def _get_file_info(self, file_id: int) -> Optional[Dict[str, Any]]:
|
||||
"""파일 정보 조회"""
|
||||
|
||||
query = """
|
||||
SELECT
|
||||
id, filename, original_filename, file_path, job_no, revision,
|
||||
bom_name, description, file_size, parsed_count,
|
||||
upload_date, uploaded_by, is_active
|
||||
FROM files
|
||||
WHERE id = :file_id
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {"file_id": file_id})
|
||||
row = result.fetchone()
|
||||
|
||||
return dict(row._mapping) if row else None
|
||||
|
||||
def _get_job_files(self, job_no: str) -> List[Dict[str, Any]]:
|
||||
"""작업의 모든 파일 조회 (최신순)"""
|
||||
|
||||
query = """
|
||||
SELECT
|
||||
id, filename, original_filename, file_path, job_no, revision,
|
||||
bom_name, description, file_size, parsed_count,
|
||||
upload_date, uploaded_by, is_active
|
||||
FROM files
|
||||
WHERE job_no = :job_no AND is_active = true
|
||||
ORDER BY upload_date DESC, id DESC
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {"job_no": job_no})
|
||||
|
||||
return [dict(row._mapping) for row in result.fetchall()]
|
||||
|
||||
def _build_revision_history(self, all_files: List[Dict], current_file_id: int) -> Dict[str, Any]:
|
||||
"""리비전 히스토리 구성"""
|
||||
|
||||
current_index = None
|
||||
for i, file_info in enumerate(all_files):
|
||||
if file_info['id'] == current_file_id:
|
||||
current_index = i
|
||||
break
|
||||
|
||||
if current_index is None:
|
||||
return {"error": "현재 파일을 찾을 수 없습니다."}
|
||||
|
||||
return {
|
||||
"total_revisions": len(all_files),
|
||||
"current_position": current_index + 1, # 1-based
|
||||
"is_latest": current_index == 0,
|
||||
"is_first": current_index == len(all_files) - 1,
|
||||
"can_upload_new": current_index == 0, # 최신 리비전에서만 새 리비전 업로드 가능
|
||||
"previous_file_id": all_files[current_index + 1]['id'] if current_index < len(all_files) - 1 else None,
|
||||
"next_file_id": all_files[current_index - 1]['id'] if current_index > 0 else None
|
||||
}
|
||||
|
||||
def _get_processing_status(self, file_id: int) -> Dict[str, Any]:
|
||||
"""파일의 처리 상태 조회"""
|
||||
|
||||
# 자재별 처리 상태 통계
|
||||
query = """
|
||||
SELECT
|
||||
classified_category,
|
||||
COUNT(*) as total_count,
|
||||
SUM(CASE WHEN purchase_confirmed = true THEN 1 ELSE 0 END) as purchased_count,
|
||||
SUM(CASE WHEN revision_status IS NOT NULL THEN 1 ELSE 0 END) as processed_count,
|
||||
COUNT(DISTINCT COALESCE(revision_status, 'pending')) as status_types
|
||||
FROM materials
|
||||
WHERE file_id = :file_id AND is_active = true AND classified_category != 'PIPE'
|
||||
GROUP BY classified_category
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {"file_id": file_id})
|
||||
|
||||
category_status = {}
|
||||
total_materials = 0
|
||||
total_purchased = 0
|
||||
total_processed = 0
|
||||
|
||||
for row in result.fetchall():
|
||||
row_dict = dict(row._mapping)
|
||||
category = row_dict['classified_category']
|
||||
|
||||
category_status[category] = {
|
||||
"total": row_dict['total_count'],
|
||||
"purchased": row_dict['purchased_count'],
|
||||
"processed": row_dict['processed_count'],
|
||||
"pending": row_dict['total_count'] - row_dict['processed_count']
|
||||
}
|
||||
|
||||
total_materials += row_dict['total_count']
|
||||
total_purchased += row_dict['purchased_count']
|
||||
total_processed += row_dict['processed_count']
|
||||
|
||||
return {
|
||||
"file_id": file_id,
|
||||
"total_materials": total_materials,
|
||||
"total_purchased": total_purchased,
|
||||
"total_processed": total_processed,
|
||||
"pending_processing": total_materials - total_processed,
|
||||
"category_breakdown": category_status,
|
||||
"completion_percentage": (total_processed / total_materials * 100) if total_materials > 0 else 0
|
||||
}
|
||||
|
||||
def _get_comparison_summary(self, current_file_id: int, previous_file_id: int) -> Optional[Dict[str, Any]]:
|
||||
"""비교 요약 정보 조회"""
|
||||
|
||||
query = """
|
||||
SELECT summary_stats, comparison_date, is_applied
|
||||
FROM revision_comparisons
|
||||
WHERE current_file_id = :current_file_id AND previous_file_id = :previous_file_id
|
||||
ORDER BY comparison_date DESC
|
||||
LIMIT 1
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {
|
||||
"current_file_id": current_file_id,
|
||||
"previous_file_id": previous_file_id
|
||||
})
|
||||
|
||||
row = result.fetchone()
|
||||
if row:
|
||||
row_dict = dict(row._mapping)
|
||||
return {
|
||||
"summary_stats": row_dict['summary_stats'],
|
||||
"comparison_date": row_dict['comparison_date'],
|
||||
"is_applied": row_dict['is_applied']
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
def _get_material_count(self, file_id: int) -> int:
|
||||
"""파일의 자재 개수 조회"""
|
||||
|
||||
query = """
|
||||
SELECT COUNT(*) as count
|
||||
FROM materials
|
||||
WHERE file_id = :file_id AND is_active = true AND classified_category != 'PIPE'
|
||||
"""
|
||||
|
||||
result = self.db_service.execute_query(query, {"file_id": file_id})
|
||||
row = result.fetchone()
|
||||
|
||||
return row.count if row else 0
|
||||
|
||||
def _create_change_logs(self, comparison: RevisionComparison):
|
||||
"""변경 로그 생성"""
|
||||
|
||||
try:
|
||||
changes = comparison.comparison_result.get("changes", {})
|
||||
|
||||
# 각 변경사항에 대해 로그 생성
|
||||
for change_type, change_list in changes.items():
|
||||
for change_item in change_list:
|
||||
change_log = RevisionChangeLog(
|
||||
comparison_id=comparison.id,
|
||||
material_id=change_item.get("material", {}).get("id"),
|
||||
change_type=change_type,
|
||||
previous_data=change_item.get("previous"),
|
||||
current_data=change_item.get("current") or change_item.get("material"),
|
||||
action_taken=change_item.get("action", change_type),
|
||||
notes=change_item.get("reason", "")
|
||||
)
|
||||
|
||||
self.db.add(change_log)
|
||||
|
||||
logger.info(f"Created change logs for comparison {comparison.id}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to create change logs: {e}")
|
||||
raise
|
||||
|
||||
def _generate_status_summary(self, processing_status: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""상태 요약 생성"""
|
||||
|
||||
total = processing_status.get("total_materials", 0)
|
||||
processed = processing_status.get("total_processed", 0)
|
||||
purchased = processing_status.get("total_purchased", 0)
|
||||
|
||||
if total == 0:
|
||||
return {"status": "empty", "message": "자료가 없습니다."}
|
||||
|
||||
completion_rate = processed / total
|
||||
|
||||
if completion_rate >= 1.0:
|
||||
status = "completed"
|
||||
message = "모든 자재 처리 완료"
|
||||
elif completion_rate >= 0.8:
|
||||
status = "nearly_complete"
|
||||
message = f"처리 진행 중 ({processed}/{total})"
|
||||
elif completion_rate >= 0.5:
|
||||
status = "in_progress"
|
||||
message = f"처리 진행 중 ({processed}/{total})"
|
||||
else:
|
||||
status = "started"
|
||||
message = f"처리 시작됨 ({processed}/{total})"
|
||||
|
||||
return {
|
||||
"status": status,
|
||||
"message": message,
|
||||
"completion_rate": completion_rate,
|
||||
"stats": {
|
||||
"total": total,
|
||||
"processed": processed,
|
||||
"purchased": purchased,
|
||||
"pending": total - processed
|
||||
}
|
||||
}
|
||||
583
backend/app/utils/pipe_utils.py
Normal file
583
backend/app/utils/pipe_utils.py
Normal file
@@ -0,0 +1,583 @@
|
||||
"""
|
||||
PIPE 시스템 공통 유틸리티
|
||||
|
||||
모든 PIPE 관련 서비스에서 공통으로 사용되는 함수들을 모아놓은 유틸리티 모듈
|
||||
"""
|
||||
|
||||
import logging
|
||||
import math
|
||||
from typing import Dict, List, Optional, Any, Tuple
|
||||
from decimal import Decimal
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import text
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ========== PIPE 상수 정의 ==========
|
||||
|
||||
class PipeConstants:
|
||||
"""PIPE 시스템에서 사용되는 상수들"""
|
||||
|
||||
# 길이 관련
|
||||
STANDARD_PIPE_LENGTH_MM = 6000 # 표준 파이프 길이 (6M)
|
||||
CUTTING_LOSS_PER_CUT_MM = 2 # 절단당 손실 (2mm)
|
||||
|
||||
# 분류 관련
|
||||
PIPE_CATEGORY = "PIPE"
|
||||
|
||||
# 끝단 처리 타입
|
||||
END_PREPARATION_TYPES = {
|
||||
"무개선": "PLAIN",
|
||||
"한개선": "SINGLE_BEVEL",
|
||||
"양개선": "DOUBLE_BEVEL"
|
||||
}
|
||||
|
||||
# 상태 관련
|
||||
REVISION_TYPES = {
|
||||
"NO_REVISION": "no_revision",
|
||||
"PRE_CUTTING_PLAN": "pre_cutting_plan",
|
||||
"POST_CUTTING_PLAN": "post_cutting_plan"
|
||||
}
|
||||
|
||||
CHANGE_TYPES = {
|
||||
"ADDED": "added",
|
||||
"REMOVED": "removed",
|
||||
"MODIFIED": "modified",
|
||||
"UNCHANGED": "unchanged"
|
||||
}
|
||||
|
||||
|
||||
# ========== 데이터 추출 유틸리티 ==========
|
||||
|
||||
class PipeDataExtractor:
|
||||
"""PIPE 데이터 추출 관련 유틸리티"""
|
||||
|
||||
@staticmethod
|
||||
def extract_pipe_materials_from_file(db: Session, file_id: int) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
파일에서 PIPE 자재 데이터 추출
|
||||
|
||||
Args:
|
||||
db: 데이터베이스 세션
|
||||
file_id: 파일 ID
|
||||
|
||||
Returns:
|
||||
PIPE 자재 리스트
|
||||
"""
|
||||
try:
|
||||
query = text("""
|
||||
SELECT
|
||||
m.id,
|
||||
m.drawing_name,
|
||||
m.line_no,
|
||||
m.description,
|
||||
m.classified_category,
|
||||
m.full_material_grade,
|
||||
m.main_nom,
|
||||
m.red_nom,
|
||||
m.length,
|
||||
m.total_length,
|
||||
m.quantity,
|
||||
m.row_number,
|
||||
m.original_description
|
||||
FROM materials m
|
||||
WHERE m.file_id = :file_id
|
||||
AND m.classified_category = 'PIPE'
|
||||
AND m.is_active = true
|
||||
ORDER BY m.drawing_name, m.line_no, m.row_number
|
||||
""")
|
||||
|
||||
result = db.execute(query, {"file_id": file_id})
|
||||
materials = []
|
||||
|
||||
for row in result:
|
||||
materials.append({
|
||||
"id": row.id,
|
||||
"drawing_name": row.drawing_name or "UNKNOWN",
|
||||
"line_no": row.line_no or "",
|
||||
"description": row.description or "",
|
||||
"original_description": row.original_description or "",
|
||||
"material_grade": row.full_material_grade or "UNKNOWN",
|
||||
"main_nom": row.main_nom or "",
|
||||
"red_nom": row.red_nom or "",
|
||||
"length": float(row.length or 0),
|
||||
"total_length": float(row.total_length or 0),
|
||||
"quantity": int(row.quantity or 1),
|
||||
"row_number": row.row_number or 0
|
||||
})
|
||||
|
||||
logger.info(f"✅ {len(materials)}개 PIPE 자재 추출 완료 (파일 ID: {file_id})")
|
||||
return materials
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"❌ PIPE 자재 추출 실패: {e}")
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def parse_pipe_description(description: str) -> Dict[str, Any]:
|
||||
"""
|
||||
PIPE 설명에서 정보 추출
|
||||
|
||||
Args:
|
||||
description: 자재 설명
|
||||
|
||||
Returns:
|
||||
추출된 정보 딕셔너리
|
||||
"""
|
||||
# 기본값 설정
|
||||
result = {
|
||||
"material_grade": "UNKNOWN",
|
||||
"schedule": "UNKNOWN",
|
||||
"nominal_size": "UNKNOWN",
|
||||
"length_info": None,
|
||||
"end_preparation": "무개선"
|
||||
}
|
||||
|
||||
if not description:
|
||||
return result
|
||||
|
||||
# 간단한 파싱 로직 (실제로는 더 복잡할 수 있음)
|
||||
description_upper = description.upper()
|
||||
|
||||
# 재질 추출 (A106, A53 등)
|
||||
if "A106" in description_upper:
|
||||
result["material_grade"] = "A106 GR.B"
|
||||
elif "A53" in description_upper:
|
||||
result["material_grade"] = "A53 GR.B"
|
||||
|
||||
# 스케줄 추출 (SCH40, SCH80 등)
|
||||
if "SCH40" in description_upper:
|
||||
result["schedule"] = "SCH40"
|
||||
elif "SCH80" in description_upper:
|
||||
result["schedule"] = "SCH80"
|
||||
|
||||
# 끝단 처리 추출
|
||||
if "양개선" in description or "DOUBLE" in description_upper:
|
||||
result["end_preparation"] = "양개선"
|
||||
elif "한개선" in description or "SINGLE" in description_upper:
|
||||
result["end_preparation"] = "한개선"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# ========== 계산 유틸리티 ==========
|
||||
|
||||
class PipeCalculator:
|
||||
"""PIPE 관련 계산 유틸리티"""
|
||||
|
||||
@staticmethod
|
||||
def calculate_pipe_purchase_quantity(materials: List[Dict]) -> Dict[str, Any]:
|
||||
"""
|
||||
PIPE 구매 수량 계산
|
||||
|
||||
Args:
|
||||
materials: PIPE 자재 리스트
|
||||
|
||||
Returns:
|
||||
계산 결과
|
||||
"""
|
||||
total_bom_length = 0
|
||||
cutting_count = 0
|
||||
pipe_details = []
|
||||
|
||||
for material in materials:
|
||||
# 길이 정보 추출 (Decimal 타입 처리)
|
||||
length_mm = float(material.get('length', 0) or 0)
|
||||
if not length_mm:
|
||||
length_mm = float(material.get('length_mm', 0) or 0)
|
||||
|
||||
quantity = float(material.get('quantity', 1) or 1)
|
||||
|
||||
if length_mm > 0:
|
||||
total_length = length_mm * quantity
|
||||
total_bom_length += total_length
|
||||
cutting_count += quantity
|
||||
|
||||
pipe_details.append({
|
||||
'description': material.get('description', ''),
|
||||
'original_description': material.get('original_description', ''),
|
||||
'drawing_name': material.get('drawing_name', ''),
|
||||
'line_no': material.get('line_no', ''),
|
||||
'length_mm': length_mm,
|
||||
'quantity': quantity,
|
||||
'total_length': total_length
|
||||
})
|
||||
|
||||
# 절단 손실 계산
|
||||
cutting_loss = cutting_count * PipeConstants.CUTTING_LOSS_PER_CUT_MM
|
||||
|
||||
# 총 필요 길이
|
||||
required_length = total_bom_length + cutting_loss
|
||||
|
||||
# 6M 단위로 올림 계산
|
||||
pipes_needed = math.ceil(required_length / PipeConstants.STANDARD_PIPE_LENGTH_MM) if required_length > 0 else 0
|
||||
total_purchase_length = pipes_needed * PipeConstants.STANDARD_PIPE_LENGTH_MM
|
||||
waste_length = total_purchase_length - required_length if pipes_needed > 0 else 0
|
||||
|
||||
return {
|
||||
'bom_quantity': total_bom_length,
|
||||
'cutting_count': cutting_count,
|
||||
'cutting_loss': cutting_loss,
|
||||
'required_length': required_length,
|
||||
'pipes_count': pipes_needed,
|
||||
'calculated_qty': total_purchase_length,
|
||||
'waste_length': waste_length,
|
||||
'utilization_rate': (required_length / total_purchase_length * 100) if total_purchase_length > 0 else 0,
|
||||
'unit': 'mm',
|
||||
'pipe_details': pipe_details,
|
||||
'summary': {
|
||||
'total_materials': len(materials),
|
||||
'total_drawings': len(set(m.get('drawing_name', '') for m in materials if m.get('drawing_name'))),
|
||||
'average_length': total_bom_length / len(materials) if materials else 0
|
||||
}
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def calculate_length_difference(old_length: float, new_length: float) -> Dict[str, Any]:
|
||||
"""
|
||||
길이 변화량 계산
|
||||
|
||||
Args:
|
||||
old_length: 이전 길이
|
||||
new_length: 새로운 길이
|
||||
|
||||
Returns:
|
||||
변화량 정보
|
||||
"""
|
||||
difference = new_length - old_length
|
||||
percentage = (difference / old_length * 100) if old_length > 0 else 0
|
||||
|
||||
return {
|
||||
'old_length': old_length,
|
||||
'new_length': new_length,
|
||||
'difference': difference,
|
||||
'percentage': percentage,
|
||||
'change_type': 'increased' if difference > 0 else 'decreased' if difference < 0 else 'unchanged'
|
||||
}
|
||||
|
||||
|
||||
# ========== 비교 유틸리티 ==========
|
||||
|
||||
class PipeComparator:
|
||||
"""PIPE 데이터 비교 유틸리티"""
|
||||
|
||||
@staticmethod
|
||||
def compare_pipe_segments(old_segments: List[Dict], new_segments: List[Dict]) -> Dict[str, Any]:
|
||||
"""
|
||||
단관 데이터 비교
|
||||
|
||||
Args:
|
||||
old_segments: 이전 단관 데이터
|
||||
new_segments: 새로운 단관 데이터
|
||||
|
||||
Returns:
|
||||
비교 결과
|
||||
"""
|
||||
# 키 생성 함수
|
||||
def create_segment_key(segment):
|
||||
return (
|
||||
segment.get('drawing_name', ''),
|
||||
segment.get('material_grade', ''),
|
||||
segment.get('length', 0),
|
||||
segment.get('end_preparation', '무개선')
|
||||
)
|
||||
|
||||
# 기존 데이터를 키로 매핑
|
||||
old_map = {}
|
||||
for segment in old_segments:
|
||||
key = create_segment_key(segment)
|
||||
if key not in old_map:
|
||||
old_map[key] = []
|
||||
old_map[key].append(segment)
|
||||
|
||||
# 새로운 데이터를 키로 매핑
|
||||
new_map = {}
|
||||
for segment in new_segments:
|
||||
key = create_segment_key(segment)
|
||||
if key not in new_map:
|
||||
new_map[key] = []
|
||||
new_map[key].append(segment)
|
||||
|
||||
# 비교 결과 생성
|
||||
changes = {
|
||||
'added': [],
|
||||
'removed': [],
|
||||
'modified': [],
|
||||
'unchanged': []
|
||||
}
|
||||
|
||||
all_keys = set(old_map.keys()) | set(new_map.keys())
|
||||
|
||||
for key in all_keys:
|
||||
old_count = len(old_map.get(key, []))
|
||||
new_count = len(new_map.get(key, []))
|
||||
|
||||
if old_count == 0:
|
||||
# 새로 추가된 항목
|
||||
for segment in new_map[key]:
|
||||
changes['added'].append({
|
||||
**segment,
|
||||
'change_type': 'added',
|
||||
'quantity_change': new_count
|
||||
})
|
||||
elif new_count == 0:
|
||||
# 삭제된 항목
|
||||
for segment in old_map[key]:
|
||||
changes['removed'].append({
|
||||
**segment,
|
||||
'change_type': 'removed',
|
||||
'quantity_change': -old_count
|
||||
})
|
||||
elif old_count != new_count:
|
||||
# 수량이 변경된 항목
|
||||
base_segment = new_map[key][0] if new_map[key] else old_map[key][0]
|
||||
changes['modified'].append({
|
||||
**base_segment,
|
||||
'change_type': 'modified',
|
||||
'old_quantity': old_count,
|
||||
'new_quantity': new_count,
|
||||
'quantity_change': new_count - old_count
|
||||
})
|
||||
else:
|
||||
# 변경되지 않은 항목
|
||||
base_segment = new_map[key][0]
|
||||
changes['unchanged'].append({
|
||||
**base_segment,
|
||||
'change_type': 'unchanged',
|
||||
'quantity': old_count
|
||||
})
|
||||
|
||||
# 통계 생성
|
||||
stats = {
|
||||
'total_old': len(old_segments),
|
||||
'total_new': len(new_segments),
|
||||
'added_count': len(changes['added']),
|
||||
'removed_count': len(changes['removed']),
|
||||
'modified_count': len(changes['modified']),
|
||||
'unchanged_count': len(changes['unchanged']),
|
||||
'changed_drawings': len(set(
|
||||
item.get('drawing_name', '') for change_list in [changes['added'], changes['removed'], changes['modified']]
|
||||
for item in change_list if item.get('drawing_name')
|
||||
))
|
||||
}
|
||||
|
||||
return {
|
||||
'changes': changes,
|
||||
'statistics': stats,
|
||||
'has_changes': stats['added_count'] + stats['removed_count'] + stats['modified_count'] > 0
|
||||
}
|
||||
|
||||
|
||||
# ========== 검증 유틸리티 ==========
|
||||
|
||||
class PipeValidator:
|
||||
"""PIPE 데이터 검증 유틸리티"""
|
||||
|
||||
@staticmethod
|
||||
def validate_pipe_data(pipe_data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
PIPE 데이터 유효성 검증
|
||||
|
||||
Args:
|
||||
pipe_data: 검증할 PIPE 데이터
|
||||
|
||||
Returns:
|
||||
검증 결과
|
||||
"""
|
||||
errors = []
|
||||
warnings = []
|
||||
|
||||
# 필수 필드 검증
|
||||
required_fields = ['drawing_name', 'material_grade', 'length']
|
||||
for field in required_fields:
|
||||
if not pipe_data.get(field):
|
||||
errors.append(f"필수 필드 누락: {field}")
|
||||
|
||||
# 길이 검증
|
||||
length = pipe_data.get('length', 0)
|
||||
if length <= 0:
|
||||
errors.append("길이는 0보다 커야 합니다")
|
||||
elif length > 20000: # 20m 초과시 경고
|
||||
warnings.append(f"길이가 비정상적으로 큽니다: {length}mm")
|
||||
|
||||
# 수량 검증
|
||||
quantity = pipe_data.get('quantity', 1)
|
||||
if quantity <= 0:
|
||||
errors.append("수량은 0보다 커야 합니다")
|
||||
|
||||
# 도면명 검증
|
||||
drawing_name = pipe_data.get('drawing_name', '')
|
||||
if drawing_name == 'UNKNOWN':
|
||||
warnings.append("도면명이 지정되지 않았습니다")
|
||||
|
||||
return {
|
||||
'is_valid': len(errors) == 0,
|
||||
'errors': errors,
|
||||
'warnings': warnings,
|
||||
'error_count': len(errors),
|
||||
'warning_count': len(warnings)
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def validate_cutting_plan_data(cutting_plan: List[Dict]) -> Dict[str, Any]:
|
||||
"""
|
||||
Cutting Plan 데이터 전체 검증
|
||||
|
||||
Args:
|
||||
cutting_plan: Cutting Plan 데이터 리스트
|
||||
|
||||
Returns:
|
||||
검증 결과
|
||||
"""
|
||||
total_errors = []
|
||||
total_warnings = []
|
||||
valid_items = 0
|
||||
|
||||
for i, item in enumerate(cutting_plan):
|
||||
validation = PipeValidator.validate_pipe_data(item)
|
||||
|
||||
if validation['is_valid']:
|
||||
valid_items += 1
|
||||
else:
|
||||
for error in validation['errors']:
|
||||
total_errors.append(f"항목 {i+1}: {error}")
|
||||
|
||||
for warning in validation['warnings']:
|
||||
total_warnings.append(f"항목 {i+1}: {warning}")
|
||||
|
||||
return {
|
||||
'is_valid': len(total_errors) == 0,
|
||||
'total_items': len(cutting_plan),
|
||||
'valid_items': valid_items,
|
||||
'invalid_items': len(cutting_plan) - valid_items,
|
||||
'errors': total_errors,
|
||||
'warnings': total_warnings,
|
||||
'validation_rate': (valid_items / len(cutting_plan) * 100) if cutting_plan else 0
|
||||
}
|
||||
|
||||
|
||||
# ========== 포맷팅 유틸리티 ==========
|
||||
|
||||
class PipeFormatter:
|
||||
"""PIPE 데이터 포맷팅 유틸리티"""
|
||||
|
||||
@staticmethod
|
||||
def format_length(length_mm: float, unit: str = 'mm') -> str:
|
||||
"""
|
||||
길이 포맷팅
|
||||
|
||||
Args:
|
||||
length_mm: 길이 (mm)
|
||||
unit: 표시 단위
|
||||
|
||||
Returns:
|
||||
포맷된 길이 문자열
|
||||
"""
|
||||
if unit == 'm':
|
||||
return f"{length_mm / 1000:.3f}m"
|
||||
elif unit == 'mm':
|
||||
return f"{length_mm:.0f}mm"
|
||||
else:
|
||||
return f"{length_mm}"
|
||||
|
||||
@staticmethod
|
||||
def format_pipe_description(pipe_data: Dict[str, Any]) -> str:
|
||||
"""
|
||||
PIPE 설명 포맷팅
|
||||
|
||||
Args:
|
||||
pipe_data: PIPE 데이터
|
||||
|
||||
Returns:
|
||||
포맷된 설명
|
||||
"""
|
||||
parts = []
|
||||
|
||||
if pipe_data.get('material_grade'):
|
||||
parts.append(pipe_data['material_grade'])
|
||||
|
||||
if pipe_data.get('main_nom'):
|
||||
parts.append(f"{pipe_data['main_nom']}")
|
||||
|
||||
if pipe_data.get('schedule'):
|
||||
parts.append(pipe_data['schedule'])
|
||||
|
||||
if pipe_data.get('length'):
|
||||
parts.append(PipeFormatter.format_length(pipe_data['length']))
|
||||
|
||||
return " ".join(parts) if parts else "PIPE"
|
||||
|
||||
@staticmethod
|
||||
def format_change_summary(changes: Dict[str, List]) -> str:
|
||||
"""
|
||||
변경사항 요약 포맷팅
|
||||
|
||||
Args:
|
||||
changes: 변경사항 딕셔너리
|
||||
|
||||
Returns:
|
||||
포맷된 요약 문자열
|
||||
"""
|
||||
summary_parts = []
|
||||
|
||||
if changes.get('added'):
|
||||
summary_parts.append(f"추가 {len(changes['added'])}개")
|
||||
|
||||
if changes.get('removed'):
|
||||
summary_parts.append(f"삭제 {len(changes['removed'])}개")
|
||||
|
||||
if changes.get('modified'):
|
||||
summary_parts.append(f"수정 {len(changes['modified'])}개")
|
||||
|
||||
if not summary_parts:
|
||||
return "변경사항 없음"
|
||||
|
||||
return ", ".join(summary_parts)
|
||||
|
||||
|
||||
# ========== 로깅 유틸리티 ==========
|
||||
|
||||
class PipeLogger:
|
||||
"""PIPE 시스템 전용 로거 유틸리티"""
|
||||
|
||||
@staticmethod
|
||||
def log_pipe_operation(operation: str, job_no: str, details: Dict[str, Any] = None):
|
||||
"""
|
||||
PIPE 작업 로깅
|
||||
|
||||
Args:
|
||||
operation: 작업 유형
|
||||
job_no: 작업 번호
|
||||
details: 상세 정보
|
||||
"""
|
||||
message = f"🔧 PIPE {operation} | Job: {job_no}"
|
||||
|
||||
if details:
|
||||
detail_parts = []
|
||||
for key, value in details.items():
|
||||
detail_parts.append(f"{key}: {value}")
|
||||
message += f" | {', '.join(detail_parts)}"
|
||||
|
||||
logger.info(message)
|
||||
|
||||
@staticmethod
|
||||
def log_pipe_error(operation: str, job_no: str, error: Exception, context: Dict[str, Any] = None):
|
||||
"""
|
||||
PIPE 오류 로깅
|
||||
|
||||
Args:
|
||||
operation: 작업 유형
|
||||
job_no: 작업 번호
|
||||
error: 오류 객체
|
||||
context: 컨텍스트 정보
|
||||
"""
|
||||
message = f"❌ PIPE {operation} 실패 | Job: {job_no} | Error: {str(error)}"
|
||||
|
||||
if context:
|
||||
context_parts = []
|
||||
for key, value in context.items():
|
||||
context_parts.append(f"{key}: {value}")
|
||||
message += f" | Context: {', '.join(context_parts)}"
|
||||
|
||||
logger.error(message)
|
||||
199
backend/scripts/auto_migrator.py
Normal file
199
backend/scripts/auto_migrator.py
Normal file
@@ -0,0 +1,199 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
자동 마이그레이션 실행기 - 스키마 분석 결과를 바탕으로 자동으로 DB를 업데이트
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import psycopg2
|
||||
from psycopg2.extras import RealDictCursor
|
||||
from datetime import datetime
|
||||
from schema_analyzer import SchemaAnalyzer
|
||||
|
||||
class AutoMigrator:
|
||||
def __init__(self):
|
||||
self.db_config = {
|
||||
'host': 'localhost',
|
||||
'port': 5432,
|
||||
'database': 'tk_mp_bom',
|
||||
'user': 'tkmp_user',
|
||||
'password': 'tkmp_password'
|
||||
}
|
||||
self.analyzer = SchemaAnalyzer()
|
||||
self.migration_log = []
|
||||
|
||||
def run_full_analysis_and_migration(self):
|
||||
"""전체 분석 및 마이그레이션 실행"""
|
||||
print("🚀 자동 마이그레이션 시작")
|
||||
|
||||
# 1. 스키마 분석
|
||||
print("\n1️⃣ 스키마 분석 중...")
|
||||
self.analyzer.analyze_code_models()
|
||||
self.analyzer.analyze_db_schema()
|
||||
analysis_result = self.analyzer.compare_schemas()
|
||||
|
||||
# 2. 분석 결과 확인
|
||||
if not analysis_result['missing_tables'] and not analysis_result['missing_columns']:
|
||||
print("✅ 스키마가 이미 최신 상태입니다!")
|
||||
return True
|
||||
|
||||
# 3. 마이그레이션 실행
|
||||
print("\n2️⃣ 마이그레이션 실행 중...")
|
||||
success = self._execute_migrations(analysis_result)
|
||||
|
||||
# 4. 결과 로깅
|
||||
self._log_migration_result(success, analysis_result)
|
||||
|
||||
return success
|
||||
|
||||
def _execute_migrations(self, analysis_result) -> bool:
|
||||
"""마이그레이션 실행"""
|
||||
try:
|
||||
conn = psycopg2.connect(**self.db_config)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# 트랜잭션 시작
|
||||
conn.autocommit = False
|
||||
|
||||
# 1. 누락된 테이블 생성
|
||||
for table_name in analysis_result['missing_tables']:
|
||||
success = self._create_missing_table(cursor, table_name)
|
||||
if not success:
|
||||
conn.rollback()
|
||||
return False
|
||||
|
||||
# 2. 누락된 컬럼 추가
|
||||
for missing_col in analysis_result['missing_columns']:
|
||||
success = self._add_missing_column(cursor, missing_col)
|
||||
if not success:
|
||||
conn.rollback()
|
||||
return False
|
||||
|
||||
# 커밋
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
print("✅ 마이그레이션 성공!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 마이그레이션 실패: {e}")
|
||||
if 'conn' in locals():
|
||||
conn.rollback()
|
||||
return False
|
||||
|
||||
def _create_missing_table(self, cursor, table_name: str) -> bool:
|
||||
"""누락된 테이블 생성"""
|
||||
try:
|
||||
create_sql = self.analyzer._generate_create_table_sql(table_name)
|
||||
print(f" 📋 테이블 생성: {table_name}")
|
||||
cursor.execute(create_sql)
|
||||
|
||||
self.migration_log.append({
|
||||
'type': 'CREATE_TABLE',
|
||||
'table': table_name,
|
||||
'sql': create_sql,
|
||||
'timestamp': datetime.now().isoformat()
|
||||
})
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ 테이블 생성 실패 {table_name}: {e}")
|
||||
return False
|
||||
|
||||
def _add_missing_column(self, cursor, missing_col: dict) -> bool:
|
||||
"""누락된 컬럼 추가"""
|
||||
try:
|
||||
alter_sql = self.analyzer._generate_add_column_sql(missing_col)
|
||||
print(f" 🔧 컬럼 추가: {missing_col['table']}.{missing_col['column']}")
|
||||
cursor.execute(alter_sql)
|
||||
|
||||
self.migration_log.append({
|
||||
'type': 'ADD_COLUMN',
|
||||
'table': missing_col['table'],
|
||||
'column': missing_col['column'],
|
||||
'sql': alter_sql,
|
||||
'timestamp': datetime.now().isoformat()
|
||||
})
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ 컬럼 추가 실패 {missing_col['table']}.{missing_col['column']}: {e}")
|
||||
return False
|
||||
|
||||
def _log_migration_result(self, success: bool, analysis_result: dict):
|
||||
"""마이그레이션 결과 로깅"""
|
||||
log_entry = {
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'success': success,
|
||||
'analysis_result': analysis_result,
|
||||
'migration_log': self.migration_log
|
||||
}
|
||||
|
||||
# 로그 파일에 저장
|
||||
import json
|
||||
log_filename = f"migration_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
||||
|
||||
with open(log_filename, 'w', encoding='utf-8') as f:
|
||||
json.dump(log_entry, f, indent=2, ensure_ascii=False)
|
||||
|
||||
print(f"📄 마이그레이션 로그 저장: {log_filename}")
|
||||
|
||||
def fix_immediate_issues(self):
|
||||
"""즉시 해결이 필요한 문제들 수정"""
|
||||
print("🔧 즉시 해결 필요한 문제들 수정 중...")
|
||||
|
||||
immediate_fixes = [
|
||||
{
|
||||
'description': 'users 테이블에 status 컬럼 추가',
|
||||
'sql': "ALTER TABLE users ADD COLUMN status VARCHAR(20) DEFAULT 'active'",
|
||||
'check_sql': "SELECT column_name FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'status'"
|
||||
}
|
||||
]
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect(**self.db_config)
|
||||
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
||||
|
||||
for fix in immediate_fixes:
|
||||
# 이미 존재하는지 확인
|
||||
cursor.execute(fix['check_sql'])
|
||||
if cursor.fetchone():
|
||||
print(f" ✅ 이미 존재: {fix['description']}")
|
||||
continue
|
||||
|
||||
# 수정 실행
|
||||
cursor.execute(fix['sql'])
|
||||
conn.commit()
|
||||
print(f" 🔧 수정 완료: {fix['description']}")
|
||||
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 즉시 수정 실패: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
migrator = AutoMigrator()
|
||||
|
||||
# 1. 즉시 해결 필요한 문제들 수정
|
||||
migrator.fix_immediate_issues()
|
||||
|
||||
# 2. 전체 분석 및 마이그레이션
|
||||
success = migrator.run_full_analysis_and_migration()
|
||||
|
||||
if success:
|
||||
print("\n🎉 모든 마이그레이션이 성공적으로 완료되었습니다!")
|
||||
else:
|
||||
print("\n💥 마이그레이션 중 오류가 발생했습니다. 로그를 확인해주세요.")
|
||||
|
||||
return success
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
405
backend/scripts/docker_migrator.py
Normal file
405
backend/scripts/docker_migrator.py
Normal file
@@ -0,0 +1,405 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Docker 환경용 마이그레이션 스크립트
|
||||
컨테이너 내부에서 실행되는 간단한 마이그레이션 도구
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import psycopg2
|
||||
from psycopg2.extras import RealDictCursor
|
||||
from datetime import datetime
|
||||
|
||||
class DockerMigrator:
|
||||
def __init__(self):
|
||||
# Docker 환경의 DB 연결 설정
|
||||
self.db_config = {
|
||||
'host': os.getenv('DB_HOST', 'tk-mp-postgres'),
|
||||
'port': int(os.getenv('DB_PORT', 5432)),
|
||||
'database': os.getenv('DB_NAME', 'tk_mp_bom'),
|
||||
'user': os.getenv('DB_USER', 'tkmp_user'),
|
||||
'password': os.getenv('DB_PASSWORD', 'tkmp_password_2025')
|
||||
}
|
||||
|
||||
def check_and_fix_schema(self):
|
||||
"""스키마 체크 및 수정"""
|
||||
print("🔍 스키마 체크 시작...")
|
||||
|
||||
fixes_applied = []
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect(**self.db_config)
|
||||
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
||||
|
||||
# 1. users.status 컬럼 체크 및 추가
|
||||
if self._check_and_add_users_status(cursor):
|
||||
fixes_applied.append("users.status 컬럼 추가")
|
||||
|
||||
# 2. files 테이블 누락 컬럼들 체크 및 추가
|
||||
if self._check_and_add_files_columns(cursor):
|
||||
fixes_applied.append("files 테이블 누락 컬럼들 추가")
|
||||
|
||||
# 3. materials 테이블 누락 컬럼들 체크 및 추가
|
||||
if self._check_and_add_materials_columns(cursor):
|
||||
fixes_applied.append("materials 테이블 누락 컬럼들 추가")
|
||||
|
||||
# 3.5. material_purchase_tracking 테이블 누락 컬럼들 체크 및 추가
|
||||
if self._check_and_add_mpt_columns(cursor):
|
||||
fixes_applied.append("material_purchase_tracking 테이블 누락 컬럼들 추가")
|
||||
|
||||
# 4. 누락된 상세 테이블들 체크 및 생성
|
||||
if self._check_and_create_detail_tables(cursor):
|
||||
fixes_applied.append("누락된 상세 테이블들 생성")
|
||||
|
||||
# 5. 기타 필요한 수정사항들...
|
||||
# 향후 추가될 수 있는 다른 스키마 수정사항들
|
||||
|
||||
conn.commit()
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
if fixes_applied:
|
||||
print("✅ 스키마 수정 완료:")
|
||||
for fix in fixes_applied:
|
||||
print(f" - {fix}")
|
||||
else:
|
||||
print("✅ 스키마가 이미 최신 상태입니다.")
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 스키마 체크 실패: {e}")
|
||||
return False
|
||||
|
||||
def _check_and_add_users_status(self, cursor) -> bool:
|
||||
"""users.status 컬럼 체크 및 추가"""
|
||||
try:
|
||||
# status 컬럼이 존재하는지 확인
|
||||
cursor.execute("""
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'users' AND column_name = 'status'
|
||||
""")
|
||||
|
||||
if cursor.fetchone():
|
||||
return False # 이미 존재함
|
||||
|
||||
# status 컬럼 추가
|
||||
cursor.execute("ALTER TABLE users ADD COLUMN status VARCHAR(20) DEFAULT 'active'")
|
||||
|
||||
# 기존 사용자들의 status를 'active'로 설정
|
||||
cursor.execute("UPDATE users SET status = 'active' WHERE status IS NULL")
|
||||
|
||||
print(" 🔧 users.status 컬럼 추가됨")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ users.status 컬럼 추가 실패: {e}")
|
||||
return False
|
||||
|
||||
def _check_and_add_files_columns(self, cursor) -> bool:
|
||||
"""files 테이블 누락 컬럼들 체크 및 추가"""
|
||||
try:
|
||||
# 필요한 컬럼들과 그 정의
|
||||
required_columns = {
|
||||
'job_no': 'VARCHAR(100)',
|
||||
'bom_name': 'VARCHAR(255)',
|
||||
'description': 'TEXT',
|
||||
'parsed_count': 'INTEGER'
|
||||
}
|
||||
|
||||
added_columns = []
|
||||
|
||||
for column_name, column_type in required_columns.items():
|
||||
# 컬럼이 존재하는지 확인
|
||||
cursor.execute("""
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'files' AND column_name = %s
|
||||
""", (column_name,))
|
||||
|
||||
if not cursor.fetchone():
|
||||
# 컬럼 추가
|
||||
cursor.execute(f"ALTER TABLE files ADD COLUMN {column_name} {column_type}")
|
||||
added_columns.append(column_name)
|
||||
print(f" 🔧 files.{column_name} 컬럼 추가됨")
|
||||
|
||||
return len(added_columns) > 0
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ files 테이블 컬럼 추가 실패: {e}")
|
||||
return False
|
||||
|
||||
def _check_and_add_materials_columns(self, cursor) -> bool:
|
||||
"""materials 테이블 누락 컬럼들 체크 및 추가 (테스팅 서버 기준)"""
|
||||
try:
|
||||
# 테스팅 서버 기준 필요한 컬럼들
|
||||
required_columns = {
|
||||
# 사이즈 정보
|
||||
'main_nom': 'VARCHAR(50)',
|
||||
'red_nom': 'VARCHAR(50)',
|
||||
'row_number': 'INTEGER',
|
||||
|
||||
# 재질 정보
|
||||
'full_material_grade': 'VARCHAR(100)',
|
||||
'standard': 'VARCHAR(100)',
|
||||
'grade': 'VARCHAR(100)',
|
||||
'subcategory': 'VARCHAR(100)',
|
||||
|
||||
# 사용자 입력 정보
|
||||
'brand': 'VARCHAR(100)',
|
||||
'user_requirement': 'TEXT',
|
||||
|
||||
# 메타데이터
|
||||
'material_hash': 'VARCHAR(64)',
|
||||
'classified_by': 'VARCHAR(100)',
|
||||
'updated_by': 'VARCHAR(100)',
|
||||
'revision_status': 'VARCHAR(20) DEFAULT \'current\'',
|
||||
|
||||
# 추가 필드들
|
||||
'length': 'NUMERIC(10,3)',
|
||||
'total_length': 'NUMERIC(10,3)',
|
||||
'is_active': 'BOOLEAN DEFAULT true',
|
||||
'purchase_confirmed': 'BOOLEAN DEFAULT false',
|
||||
'confirmed_quantity': 'NUMERIC(10,3)',
|
||||
'purchase_status': 'VARCHAR(20)',
|
||||
'purchase_confirmed_by': 'VARCHAR(100)',
|
||||
'purchase_confirmed_at': 'TIMESTAMP',
|
||||
'normalized_description': 'TEXT'
|
||||
}
|
||||
|
||||
added_columns = []
|
||||
|
||||
for column_name, column_type in required_columns.items():
|
||||
# 컬럼이 존재하는지 확인
|
||||
cursor.execute("""
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'materials' AND column_name = %s
|
||||
""", (column_name,))
|
||||
|
||||
if not cursor.fetchone():
|
||||
# 컬럼 추가
|
||||
cursor.execute(f"ALTER TABLE materials ADD COLUMN {column_name} {column_type}")
|
||||
added_columns.append(column_name)
|
||||
print(f" 🔧 materials.{column_name} 컬럼 추가됨")
|
||||
|
||||
if added_columns:
|
||||
print(f" ✅ materials 테이블에 {len(added_columns)}개 컬럼 추가 완료")
|
||||
|
||||
return len(added_columns) > 0
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ materials 테이블 컬럼 추가 실패: {e}")
|
||||
return False
|
||||
|
||||
def _check_and_add_mpt_columns(self, cursor) -> bool:
|
||||
"""material_purchase_tracking 테이블 누락 컬럼들 체크 및 추가"""
|
||||
try:
|
||||
# 필요한 컬럼들
|
||||
required_columns = {
|
||||
'description': 'TEXT',
|
||||
'purchase_status': 'VARCHAR(20)'
|
||||
}
|
||||
|
||||
added_columns = []
|
||||
|
||||
for column_name, column_type in required_columns.items():
|
||||
# 컬럼이 존재하는지 확인
|
||||
cursor.execute("""
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'material_purchase_tracking' AND column_name = %s
|
||||
""", (column_name,))
|
||||
|
||||
if not cursor.fetchone():
|
||||
# 컬럼 추가
|
||||
cursor.execute(f"ALTER TABLE material_purchase_tracking ADD COLUMN {column_name} {column_type}")
|
||||
added_columns.append(column_name)
|
||||
print(f" 🔧 material_purchase_tracking.{column_name} 컬럼 추가됨")
|
||||
|
||||
if added_columns:
|
||||
print(f" ✅ material_purchase_tracking 테이블에 {len(added_columns)}개 컬럼 추가 완료")
|
||||
|
||||
return len(added_columns) > 0
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ material_purchase_tracking 테이블 컬럼 추가 실패: {e}")
|
||||
return False
|
||||
|
||||
def _check_and_create_detail_tables(self, cursor) -> bool:
|
||||
"""누락된 상세 테이블들 체크 및 생성"""
|
||||
try:
|
||||
# 필요한 상세 테이블들과 그 구조
|
||||
required_tables = {
|
||||
'support_details': """
|
||||
CREATE TABLE support_details (
|
||||
id SERIAL PRIMARY KEY,
|
||||
material_id INTEGER NOT NULL,
|
||||
file_id INTEGER NOT NULL,
|
||||
support_type VARCHAR(50),
|
||||
support_subtype VARCHAR(50),
|
||||
load_rating VARCHAR(50),
|
||||
load_capacity VARCHAR(50),
|
||||
material_standard VARCHAR(100),
|
||||
material_grade VARCHAR(100),
|
||||
pipe_size VARCHAR(50),
|
||||
length_mm NUMERIC(10,3),
|
||||
width_mm NUMERIC(10,3),
|
||||
height_mm NUMERIC(10,3),
|
||||
classification_confidence DOUBLE PRECISION,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (material_id) REFERENCES materials(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (file_id) REFERENCES files(id) ON DELETE CASCADE
|
||||
)
|
||||
""",
|
||||
'special_material_details': """
|
||||
CREATE TABLE special_material_details (
|
||||
id SERIAL PRIMARY KEY,
|
||||
material_id INTEGER NOT NULL,
|
||||
file_id INTEGER NOT NULL,
|
||||
special_type VARCHAR(50),
|
||||
special_subtype VARCHAR(50),
|
||||
material_standard VARCHAR(100),
|
||||
material_grade VARCHAR(100),
|
||||
size_spec VARCHAR(50),
|
||||
classification_confidence DOUBLE PRECISION,
|
||||
additional_info JSONB,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (material_id) REFERENCES materials(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (file_id) REFERENCES files(id) ON DELETE CASCADE
|
||||
)
|
||||
""",
|
||||
'purchase_requests': """
|
||||
CREATE TABLE purchase_requests (
|
||||
request_id SERIAL PRIMARY KEY,
|
||||
request_no VARCHAR(50) UNIQUE,
|
||||
file_id INTEGER,
|
||||
job_no VARCHAR(50),
|
||||
category VARCHAR(50),
|
||||
material_count INTEGER,
|
||||
excel_file_path VARCHAR(500),
|
||||
requested_by INTEGER,
|
||||
requested_at TIMESTAMP,
|
||||
status VARCHAR(20) DEFAULT 'requested',
|
||||
notes TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (file_id) REFERENCES files(id),
|
||||
FOREIGN KEY (requested_by) REFERENCES users(user_id)
|
||||
)
|
||||
""",
|
||||
'purchase_request_items': """
|
||||
CREATE TABLE purchase_request_items (
|
||||
item_id SERIAL PRIMARY KEY,
|
||||
request_id INTEGER NOT NULL,
|
||||
material_id INTEGER NOT NULL,
|
||||
quantity INTEGER,
|
||||
unit VARCHAR(20),
|
||||
description TEXT,
|
||||
user_requirement TEXT,
|
||||
is_ordered BOOLEAN DEFAULT false,
|
||||
is_received BOOLEAN DEFAULT false,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (request_id) REFERENCES purchase_requests(request_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (material_id) REFERENCES materials(id) ON DELETE CASCADE
|
||||
)
|
||||
"""
|
||||
}
|
||||
|
||||
created_tables = []
|
||||
|
||||
for table_name, create_sql in required_tables.items():
|
||||
# 테이블이 존재하는지 확인
|
||||
cursor.execute("""
|
||||
SELECT EXISTS (
|
||||
SELECT FROM information_schema.tables
|
||||
WHERE table_name = %s
|
||||
)
|
||||
""", (table_name,))
|
||||
|
||||
result = cursor.fetchone()
|
||||
if isinstance(result, dict):
|
||||
table_exists = result.get('exists', False)
|
||||
else:
|
||||
table_exists = result[0] if result else False
|
||||
if not table_exists:
|
||||
# 테이블 생성
|
||||
cursor.execute(create_sql)
|
||||
created_tables.append(table_name)
|
||||
print(f" 🏗️ {table_name} 테이블 생성됨")
|
||||
|
||||
if created_tables:
|
||||
print(f" ✅ {len(created_tables)}개 상세 테이블 생성 완료")
|
||||
|
||||
return len(created_tables) > 0
|
||||
|
||||
except Exception as e:
|
||||
print(f" ❌ 상세 테이블 생성 실패: {e}")
|
||||
import traceback
|
||||
print(f" 상세 오류: {traceback.format_exc()}")
|
||||
return False
|
||||
|
||||
def verify_critical_tables(self):
|
||||
"""중요 테이블들이 존재하는지 확인"""
|
||||
print("🔍 중요 테이블 존재 여부 확인...")
|
||||
|
||||
critical_tables = [
|
||||
'users', 'projects', 'files', 'materials',
|
||||
'pipe_details', 'fitting_details', 'flange_details',
|
||||
'valve_details', 'gasket_details', 'bolt_details'
|
||||
]
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect(**self.db_config)
|
||||
cursor = conn.cursor()
|
||||
|
||||
missing_tables = []
|
||||
|
||||
for table in critical_tables:
|
||||
cursor.execute("""
|
||||
SELECT EXISTS (
|
||||
SELECT FROM information_schema.tables
|
||||
WHERE table_name = %s
|
||||
)
|
||||
""", (table,))
|
||||
|
||||
if not cursor.fetchone()[0]:
|
||||
missing_tables.append(table)
|
||||
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
if missing_tables:
|
||||
print("❌ 누락된 중요 테이블들:")
|
||||
for table in missing_tables:
|
||||
print(f" - {table}")
|
||||
return False
|
||||
else:
|
||||
print("✅ 모든 중요 테이블이 존재합니다.")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 테이블 확인 실패: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
print("🚀 Docker 환경 마이그레이션 시작")
|
||||
|
||||
migrator = DockerMigrator()
|
||||
|
||||
# 1. 중요 테이블 존재 여부 확인
|
||||
if not migrator.verify_critical_tables():
|
||||
print("💥 중요 테이블이 누락되어 있습니다!")
|
||||
return False
|
||||
|
||||
# 2. 스키마 체크 및 수정
|
||||
if not migrator.check_and_fix_schema():
|
||||
print("💥 스키마 수정 실패!")
|
||||
return False
|
||||
|
||||
print("🎉 마이그레이션 완료!")
|
||||
return True
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = main()
|
||||
sys.exit(0 if success else 1)
|
||||
361
backend/scripts/schema_analyzer.py
Normal file
361
backend/scripts/schema_analyzer.py
Normal file
@@ -0,0 +1,361 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
스키마 분석기 - 코드와 DB 스키마를 비교하여 누락된 테이블/컬럼을 찾는 도구
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
import json
|
||||
from typing import Dict, List, Set, Tuple
|
||||
from datetime import datetime
|
||||
import psycopg2
|
||||
from psycopg2.extras import RealDictCursor
|
||||
|
||||
# 프로젝트 루트를 Python 경로에 추가
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
class SchemaAnalyzer:
|
||||
def __init__(self):
|
||||
self.db_config = {
|
||||
'host': 'localhost',
|
||||
'port': 5432,
|
||||
'database': 'tk_mp_bom',
|
||||
'user': 'tkmp_user',
|
||||
'password': 'tkmp_password'
|
||||
}
|
||||
self.code_tables = {}
|
||||
self.db_tables = {}
|
||||
self.analysis_result = {
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'missing_tables': [],
|
||||
'missing_columns': [],
|
||||
'extra_tables': [],
|
||||
'schema_issues': []
|
||||
}
|
||||
|
||||
def analyze_code_models(self) -> Dict[str, Dict]:
|
||||
"""코드에서 SQLAlchemy 모델을 분석하여 테이블 구조 추출"""
|
||||
print("📋 코드 모델 분석 중...")
|
||||
|
||||
model_files = [
|
||||
'backend/app/models.py',
|
||||
'backend/app/auth/models.py'
|
||||
]
|
||||
|
||||
for file_path in model_files:
|
||||
if os.path.exists(file_path):
|
||||
self._parse_model_file(file_path)
|
||||
|
||||
print(f"✅ 코드에서 {len(self.code_tables)}개 테이블 발견")
|
||||
return self.code_tables
|
||||
|
||||
def _parse_model_file(self, file_path: str):
|
||||
"""모델 파일을 파싱하여 테이블 정보 추출"""
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# 클래스 정의 찾기
|
||||
class_pattern = r'class\s+(\w+)\(Base\):(.*?)(?=class\s+\w+\(Base\):|$)'
|
||||
classes = re.findall(class_pattern, content, re.DOTALL)
|
||||
|
||||
for class_name, class_content in classes:
|
||||
table_info = self._parse_class_content(class_name, class_content)
|
||||
if table_info:
|
||||
table_name = table_info['table_name']
|
||||
self.code_tables[table_name] = table_info
|
||||
|
||||
def _parse_class_content(self, class_name: str, content: str) -> Dict:
|
||||
"""클래스 내용을 파싱하여 테이블 정보 추출"""
|
||||
# __tablename__ 찾기
|
||||
tablename_match = re.search(r'__tablename__\s*=\s*["\']([^"\']+)["\']', content)
|
||||
if not tablename_match:
|
||||
return None
|
||||
|
||||
table_name = tablename_match.group(1)
|
||||
|
||||
# 컬럼 정의 찾기
|
||||
column_pattern = r'(\w+)\s*=\s*Column\((.*?)\)'
|
||||
columns = {}
|
||||
|
||||
for match in re.finditer(column_pattern, content, re.DOTALL):
|
||||
column_name = match.group(1)
|
||||
column_def = match.group(2)
|
||||
|
||||
# 컬럼 타입과 속성 파싱
|
||||
column_info = self._parse_column_definition(column_def)
|
||||
columns[column_name] = column_info
|
||||
|
||||
return {
|
||||
'class_name': class_name,
|
||||
'table_name': table_name,
|
||||
'columns': columns,
|
||||
'file_path': None # 나중에 설정
|
||||
}
|
||||
|
||||
def _parse_column_definition(self, column_def: str) -> Dict:
|
||||
"""컬럼 정의를 파싱하여 타입과 속성 추출"""
|
||||
# 기본 타입 매핑
|
||||
type_mapping = {
|
||||
'Integer': 'integer',
|
||||
'String': 'character varying',
|
||||
'Text': 'text',
|
||||
'Boolean': 'boolean',
|
||||
'DateTime': 'timestamp without time zone',
|
||||
'Numeric': 'numeric',
|
||||
'JSON': 'json'
|
||||
}
|
||||
|
||||
# 타입 추출
|
||||
type_match = re.search(r'(Integer|String|Text|Boolean|DateTime|Numeric|JSON)', column_def)
|
||||
column_type = 'unknown'
|
||||
if type_match:
|
||||
sqlalchemy_type = type_match.group(1)
|
||||
column_type = type_mapping.get(sqlalchemy_type, sqlalchemy_type.lower())
|
||||
|
||||
# 속성 추출
|
||||
nullable = 'nullable=False' not in column_def
|
||||
primary_key = 'primary_key=True' in column_def
|
||||
unique = 'unique=True' in column_def
|
||||
default = None
|
||||
|
||||
# default 값 추출
|
||||
default_match = re.search(r'default=([^,)]+)', column_def)
|
||||
if default_match:
|
||||
default = default_match.group(1).strip()
|
||||
|
||||
return {
|
||||
'type': column_type,
|
||||
'nullable': nullable,
|
||||
'primary_key': primary_key,
|
||||
'unique': unique,
|
||||
'default': default
|
||||
}
|
||||
|
||||
def analyze_db_schema(self) -> Dict[str, Dict]:
|
||||
"""실제 DB 스키마 분석"""
|
||||
print("🗄️ DB 스키마 분석 중...")
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect(**self.db_config)
|
||||
cursor = conn.cursor(cursor_factory=RealDictCursor)
|
||||
|
||||
# 테이블 목록 조회
|
||||
cursor.execute("""
|
||||
SELECT tablename
|
||||
FROM pg_tables
|
||||
WHERE schemaname = 'public'
|
||||
ORDER BY tablename
|
||||
""")
|
||||
|
||||
tables = [row['tablename'] for row in cursor.fetchall()]
|
||||
|
||||
# 각 테이블의 컬럼 정보 조회
|
||||
for table_name in tables:
|
||||
cursor.execute("""
|
||||
SELECT
|
||||
column_name,
|
||||
data_type,
|
||||
is_nullable,
|
||||
column_default,
|
||||
character_maximum_length
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = %s
|
||||
AND table_schema = 'public'
|
||||
ORDER BY ordinal_position
|
||||
""", (table_name,))
|
||||
|
||||
columns = {}
|
||||
for row in cursor.fetchall():
|
||||
columns[row['column_name']] = {
|
||||
'type': row['data_type'],
|
||||
'nullable': row['is_nullable'] == 'YES',
|
||||
'default': row['column_default'],
|
||||
'max_length': row['character_maximum_length']
|
||||
}
|
||||
|
||||
self.db_tables[table_name] = {
|
||||
'table_name': table_name,
|
||||
'columns': columns
|
||||
}
|
||||
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
print(f"✅ DB에서 {len(self.db_tables)}개 테이블 발견")
|
||||
return self.db_tables
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ DB 연결 실패: {e}")
|
||||
return {}
|
||||
|
||||
def compare_schemas(self) -> Dict:
|
||||
"""코드와 DB 스키마 비교"""
|
||||
print("🔍 스키마 비교 중...")
|
||||
|
||||
code_table_names = set(self.code_tables.keys())
|
||||
db_table_names = set(self.db_tables.keys())
|
||||
|
||||
# 누락된 테이블 (코드에는 있지만 DB에는 없음)
|
||||
missing_tables = code_table_names - db_table_names
|
||||
self.analysis_result['missing_tables'] = list(missing_tables)
|
||||
|
||||
# 추가 테이블 (DB에는 있지만 코드에는 없음)
|
||||
extra_tables = db_table_names - code_table_names
|
||||
self.analysis_result['extra_tables'] = list(extra_tables)
|
||||
|
||||
# 공통 테이블의 컬럼 비교
|
||||
common_tables = code_table_names & db_table_names
|
||||
|
||||
for table_name in common_tables:
|
||||
missing_columns = self._compare_table_columns(table_name)
|
||||
if missing_columns:
|
||||
self.analysis_result['missing_columns'].extend(missing_columns)
|
||||
|
||||
return self.analysis_result
|
||||
|
||||
def _compare_table_columns(self, table_name: str) -> List[Dict]:
|
||||
"""특정 테이블의 컬럼 비교"""
|
||||
code_columns = set(self.code_tables[table_name]['columns'].keys())
|
||||
db_columns = set(self.db_tables[table_name]['columns'].keys())
|
||||
|
||||
missing_columns = []
|
||||
|
||||
# 누락된 컬럼들
|
||||
for column_name in code_columns - db_columns:
|
||||
column_info = self.code_tables[table_name]['columns'][column_name]
|
||||
missing_columns.append({
|
||||
'table': table_name,
|
||||
'column': column_name,
|
||||
'type': column_info['type'],
|
||||
'nullable': column_info['nullable'],
|
||||
'default': column_info['default']
|
||||
})
|
||||
|
||||
return missing_columns
|
||||
|
||||
def generate_migration_sql(self) -> str:
|
||||
"""누락된 스키마에 대한 마이그레이션 SQL 생성"""
|
||||
sql_statements = []
|
||||
sql_statements.append("-- 자동 생성된 마이그레이션 SQL")
|
||||
sql_statements.append(f"-- 생성 시간: {datetime.now()}")
|
||||
sql_statements.append("")
|
||||
|
||||
# 누락된 테이블 생성
|
||||
for table_name in self.analysis_result['missing_tables']:
|
||||
if table_name in self.code_tables:
|
||||
create_sql = self._generate_create_table_sql(table_name)
|
||||
sql_statements.append(create_sql)
|
||||
sql_statements.append("")
|
||||
|
||||
# 누락된 컬럼 추가
|
||||
for missing_col in self.analysis_result['missing_columns']:
|
||||
alter_sql = self._generate_add_column_sql(missing_col)
|
||||
sql_statements.append(alter_sql)
|
||||
|
||||
return "\n".join(sql_statements)
|
||||
|
||||
def _generate_create_table_sql(self, table_name: str) -> str:
|
||||
"""테이블 생성 SQL 생성"""
|
||||
table_info = self.code_tables[table_name]
|
||||
columns = []
|
||||
|
||||
for col_name, col_info in table_info['columns'].items():
|
||||
col_def = f" {col_name} {col_info['type']}"
|
||||
|
||||
if not col_info['nullable']:
|
||||
col_def += " NOT NULL"
|
||||
|
||||
if col_info['default']:
|
||||
col_def += f" DEFAULT {col_info['default']}"
|
||||
|
||||
if col_info['primary_key']:
|
||||
col_def += " PRIMARY KEY"
|
||||
|
||||
columns.append(col_def)
|
||||
|
||||
return f"CREATE TABLE {table_name} (\n" + ",\n".join(columns) + "\n);"
|
||||
|
||||
def _generate_add_column_sql(self, missing_col: Dict) -> str:
|
||||
"""컬럼 추가 SQL 생성"""
|
||||
sql = f"ALTER TABLE {missing_col['table']} ADD COLUMN {missing_col['column']} {missing_col['type']}"
|
||||
|
||||
if missing_col['default']:
|
||||
sql += f" DEFAULT {missing_col['default']}"
|
||||
|
||||
if not missing_col['nullable']:
|
||||
sql += " NOT NULL"
|
||||
|
||||
return sql + ";"
|
||||
|
||||
def save_analysis_report(self, filename: str = "schema_analysis_report.json"):
|
||||
"""분석 결과를 JSON 파일로 저장"""
|
||||
with open(filename, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.analysis_result, f, indent=2, ensure_ascii=False)
|
||||
|
||||
print(f"📄 분석 보고서 저장: {filename}")
|
||||
|
||||
def print_summary(self):
|
||||
"""분석 결과 요약 출력"""
|
||||
print("\n" + "="*60)
|
||||
print("📊 스키마 분석 결과 요약")
|
||||
print("="*60)
|
||||
|
||||
print(f"🔍 분석 시간: {self.analysis_result['timestamp']}")
|
||||
print(f"📋 코드 테이블: {len(self.code_tables)}개")
|
||||
print(f"🗄️ DB 테이블: {len(self.db_tables)}개")
|
||||
|
||||
if self.analysis_result['missing_tables']:
|
||||
print(f"\n❌ 누락된 테이블 ({len(self.analysis_result['missing_tables'])}개):")
|
||||
for table in self.analysis_result['missing_tables']:
|
||||
print(f" - {table}")
|
||||
|
||||
if self.analysis_result['missing_columns']:
|
||||
print(f"\n❌ 누락된 컬럼 ({len(self.analysis_result['missing_columns'])}개):")
|
||||
for col in self.analysis_result['missing_columns']:
|
||||
print(f" - {col['table']}.{col['column']} ({col['type']})")
|
||||
|
||||
if self.analysis_result['extra_tables']:
|
||||
print(f"\n➕ 추가 테이블 ({len(self.analysis_result['extra_tables'])}개):")
|
||||
for table in self.analysis_result['extra_tables']:
|
||||
print(f" - {table}")
|
||||
|
||||
if not any([self.analysis_result['missing_tables'],
|
||||
self.analysis_result['missing_columns']]):
|
||||
print("\n✅ 스키마가 완전히 동기화되어 있습니다!")
|
||||
|
||||
def main():
|
||||
print("🚀 TK-MP-Project 스키마 분석기 시작")
|
||||
|
||||
analyzer = SchemaAnalyzer()
|
||||
|
||||
# 1. 코드 모델 분석
|
||||
analyzer.analyze_code_models()
|
||||
|
||||
# 2. DB 스키마 분석
|
||||
analyzer.analyze_db_schema()
|
||||
|
||||
# 3. 스키마 비교
|
||||
analyzer.compare_schemas()
|
||||
|
||||
# 4. 결과 출력
|
||||
analyzer.print_summary()
|
||||
|
||||
# 5. 마이그레이션 SQL 생성
|
||||
if analyzer.analysis_result['missing_tables'] or analyzer.analysis_result['missing_columns']:
|
||||
migration_sql = analyzer.generate_migration_sql()
|
||||
|
||||
with open('migration.sql', 'w', encoding='utf-8') as f:
|
||||
f.write(migration_sql)
|
||||
|
||||
print(f"\n📝 마이그레이션 SQL 생성: migration.sql")
|
||||
print("다음 명령으로 실행 가능:")
|
||||
print("docker exec tk-mp-postgres psql -U tkmp_user -d tk_mp_bom -f /path/to/migration.sql")
|
||||
|
||||
# 6. 분석 보고서 저장
|
||||
analyzer.save_analysis_report()
|
||||
|
||||
return analyzer.analysis_result
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
278
backend/scripts/schema_monitor.py
Normal file
278
backend/scripts/schema_monitor.py
Normal file
@@ -0,0 +1,278 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
스키마 모니터링 시스템 - 코드 변경사항을 감지하고 스키마 분석을 자동으로 실행
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import json
|
||||
from datetime import datetime
|
||||
from typing import Dict, List
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from schema_analyzer import SchemaAnalyzer
|
||||
from auto_migrator import AutoMigrator
|
||||
|
||||
class SchemaMonitor(FileSystemEventHandler):
|
||||
def __init__(self):
|
||||
self.analyzer = SchemaAnalyzer()
|
||||
self.migrator = AutoMigrator()
|
||||
self.last_check = datetime.now()
|
||||
self.monitored_files = [
|
||||
'backend/app/models.py',
|
||||
'backend/app/auth/models.py'
|
||||
]
|
||||
self.change_log = []
|
||||
|
||||
def on_modified(self, event):
|
||||
"""파일 변경 감지"""
|
||||
if event.is_directory:
|
||||
return
|
||||
|
||||
# 모니터링 대상 파일인지 확인
|
||||
file_path = event.src_path.replace('\\', '/')
|
||||
if not any(monitored in file_path for monitored in self.monitored_files):
|
||||
return
|
||||
|
||||
print(f"📝 파일 변경 감지: {file_path}")
|
||||
|
||||
# 변경 로그 기록
|
||||
self.change_log.append({
|
||||
'file': file_path,
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'event_type': 'modified'
|
||||
})
|
||||
|
||||
# 스키마 분석 실행 (디바운싱 적용)
|
||||
self._schedule_schema_check()
|
||||
|
||||
def _schedule_schema_check(self):
|
||||
"""스키마 체크 스케줄링 (디바운싱)"""
|
||||
# 마지막 체크로부터 5초 후에 실행
|
||||
time.sleep(5)
|
||||
|
||||
current_time = datetime.now()
|
||||
if (current_time - self.last_check).seconds >= 5:
|
||||
self.last_check = current_time
|
||||
self._run_schema_check()
|
||||
|
||||
def _run_schema_check(self):
|
||||
"""스키마 체크 실행"""
|
||||
print("\n🔍 스키마 변경사항 체크 중...")
|
||||
|
||||
# 분석 실행
|
||||
self.analyzer.analyze_code_models()
|
||||
self.analyzer.analyze_db_schema()
|
||||
analysis_result = self.analyzer.compare_schemas()
|
||||
|
||||
# 변경사항이 있는지 확인
|
||||
has_changes = (
|
||||
analysis_result['missing_tables'] or
|
||||
analysis_result['missing_columns']
|
||||
)
|
||||
|
||||
if has_changes:
|
||||
print("⚠️ 스키마 불일치 발견!")
|
||||
self.analyzer.print_summary()
|
||||
|
||||
# 자동 마이그레이션 실행 여부 확인
|
||||
self._handle_schema_changes(analysis_result)
|
||||
else:
|
||||
print("✅ 스키마가 동기화되어 있습니다.")
|
||||
|
||||
def _handle_schema_changes(self, analysis_result: Dict):
|
||||
"""스키마 변경사항 처리"""
|
||||
print("\n🤖 자동 마이그레이션을 실행하시겠습니까?")
|
||||
print("1. 자동 실행")
|
||||
print("2. SQL 파일만 생성")
|
||||
print("3. 무시")
|
||||
|
||||
# 개발 환경에서는 자동으로 실행
|
||||
choice = "1" # 자동 실행
|
||||
|
||||
if choice == "1":
|
||||
print("🚀 자동 마이그레이션 실행 중...")
|
||||
success = self.migrator._execute_migrations(analysis_result)
|
||||
|
||||
if success:
|
||||
print("✅ 마이그레이션 완료!")
|
||||
self._notify_migration_success(analysis_result)
|
||||
else:
|
||||
print("❌ 마이그레이션 실패!")
|
||||
self._notify_migration_failure(analysis_result)
|
||||
|
||||
elif choice == "2":
|
||||
migration_sql = self.analyzer.generate_migration_sql()
|
||||
filename = f"migration_{datetime.now().strftime('%Y%m%d_%H%M%S')}.sql"
|
||||
|
||||
with open(filename, 'w', encoding='utf-8') as f:
|
||||
f.write(migration_sql)
|
||||
|
||||
print(f"📝 마이그레이션 SQL 생성: {filename}")
|
||||
|
||||
def _notify_migration_success(self, analysis_result: Dict):
|
||||
"""마이그레이션 성공 알림"""
|
||||
notification = {
|
||||
'type': 'MIGRATION_SUCCESS',
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'changes': {
|
||||
'tables_created': len(analysis_result['missing_tables']),
|
||||
'columns_added': len(analysis_result['missing_columns'])
|
||||
}
|
||||
}
|
||||
|
||||
self._save_notification(notification)
|
||||
|
||||
def _notify_migration_failure(self, analysis_result: Dict):
|
||||
"""마이그레이션 실패 알림"""
|
||||
notification = {
|
||||
'type': 'MIGRATION_FAILURE',
|
||||
'timestamp': datetime.now().isoformat(),
|
||||
'analysis_result': analysis_result
|
||||
}
|
||||
|
||||
self._save_notification(notification)
|
||||
|
||||
def _save_notification(self, notification: Dict):
|
||||
"""알림 저장"""
|
||||
notifications_file = 'schema_notifications.json'
|
||||
|
||||
notifications = []
|
||||
if os.path.exists(notifications_file):
|
||||
with open(notifications_file, 'r', encoding='utf-8') as f:
|
||||
notifications = json.load(f)
|
||||
|
||||
notifications.append(notification)
|
||||
|
||||
# 최근 100개만 유지
|
||||
notifications = notifications[-100:]
|
||||
|
||||
with open(notifications_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(notifications, f, indent=2, ensure_ascii=False)
|
||||
|
||||
def start_monitoring(self):
|
||||
"""모니터링 시작"""
|
||||
print("👀 스키마 모니터링 시작...")
|
||||
print("모니터링 대상 파일:")
|
||||
for file_path in self.monitored_files:
|
||||
print(f" - {file_path}")
|
||||
|
||||
observer = Observer()
|
||||
observer.schedule(self, 'backend/app', recursive=True)
|
||||
observer.start()
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
observer.stop()
|
||||
print("\n🛑 모니터링 중단")
|
||||
|
||||
observer.join()
|
||||
|
||||
class SchemaValidator:
|
||||
"""스키마 검증 도구"""
|
||||
|
||||
def __init__(self):
|
||||
self.analyzer = SchemaAnalyzer()
|
||||
|
||||
def validate_deployment_readiness(self) -> bool:
|
||||
"""배포 준비 상태 검증"""
|
||||
print("🔍 배포 준비 상태 검증 중...")
|
||||
|
||||
# 스키마 분석
|
||||
self.analyzer.analyze_code_models()
|
||||
self.analyzer.analyze_db_schema()
|
||||
analysis_result = self.analyzer.compare_schemas()
|
||||
|
||||
# 검증 결과
|
||||
is_ready = not (
|
||||
analysis_result['missing_tables'] or
|
||||
analysis_result['missing_columns']
|
||||
)
|
||||
|
||||
if is_ready:
|
||||
print("✅ 배포 준비 완료! 스키마가 완전히 동기화되어 있습니다.")
|
||||
else:
|
||||
print("❌ 배포 준비 미완료! 스키마 불일치가 발견되었습니다.")
|
||||
self.analyzer.print_summary()
|
||||
|
||||
return is_ready
|
||||
|
||||
def generate_deployment_migration(self) -> str:
|
||||
"""배포용 마이그레이션 스크립트 생성"""
|
||||
print("📝 배포용 마이그레이션 스크립트 생성 중...")
|
||||
|
||||
self.analyzer.analyze_code_models()
|
||||
self.analyzer.analyze_db_schema()
|
||||
analysis_result = self.analyzer.compare_schemas()
|
||||
|
||||
if not analysis_result['missing_tables'] and not analysis_result['missing_columns']:
|
||||
print("✅ 마이그레이션이 필요하지 않습니다.")
|
||||
return ""
|
||||
|
||||
# 배포용 마이그레이션 스크립트 생성
|
||||
migration_sql = self.analyzer.generate_migration_sql()
|
||||
|
||||
# 안전성 체크 추가
|
||||
safe_migration = self._add_safety_checks(migration_sql)
|
||||
|
||||
filename = f"deployment_migration_{datetime.now().strftime('%Y%m%d_%H%M%S')}.sql"
|
||||
with open(filename, 'w', encoding='utf-8') as f:
|
||||
f.write(safe_migration)
|
||||
|
||||
print(f"📄 배포용 마이그레이션 생성: {filename}")
|
||||
return filename
|
||||
|
||||
def _add_safety_checks(self, migration_sql: str) -> str:
|
||||
"""마이그레이션에 안전성 체크 추가"""
|
||||
safety_header = """-- 배포용 마이그레이션 스크립트
|
||||
-- 생성 시간: {timestamp}
|
||||
-- 주의: 프로덕션 환경에서 실행하기 전에 백업을 수행하세요!
|
||||
|
||||
-- 트랜잭션 시작
|
||||
BEGIN;
|
||||
|
||||
-- 백업 테이블 생성 (필요시)
|
||||
-- CREATE TABLE users_backup AS SELECT * FROM users;
|
||||
|
||||
""".format(timestamp=datetime.now())
|
||||
|
||||
safety_footer = """
|
||||
-- 검증 쿼리 (필요시 주석 해제)
|
||||
-- SELECT COUNT(*) FROM users WHERE status IS NOT NULL;
|
||||
|
||||
-- 모든 것이 정상이면 커밋, 문제가 있으면 ROLLBACK 실행
|
||||
COMMIT;
|
||||
-- ROLLBACK;
|
||||
"""
|
||||
|
||||
return safety_header + migration_sql + safety_footer
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description='TK-MP-Project 스키마 모니터링 도구')
|
||||
parser.add_argument('--mode', choices=['monitor', 'validate', 'deploy'],
|
||||
default='monitor', help='실행 모드')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.mode == 'monitor':
|
||||
monitor = SchemaMonitor()
|
||||
monitor.start_monitoring()
|
||||
|
||||
elif args.mode == 'validate':
|
||||
validator = SchemaValidator()
|
||||
is_ready = validator.validate_deployment_readiness()
|
||||
sys.exit(0 if is_ready else 1)
|
||||
|
||||
elif args.mode == 'deploy':
|
||||
validator = SchemaValidator()
|
||||
migration_file = validator.generate_deployment_migration()
|
||||
if migration_file:
|
||||
print(f"배포 시 다음 명령으로 실행: psql -f {migration_file}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user