from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from typing import Optional from database import get_db from models import StoreSettings, Store, User from schemas import ( StoreSettings as StoreSettingsSchema, StoreSettingsCreate, StoreSettingsUpdate ) from auth import get_current_user, get_current_store router = APIRouter() @router.post("/", response_model=StoreSettingsSchema) async def create_store_settings( settings: StoreSettingsCreate, current_store: Store = Depends(get_current_store), db: Session = Depends(get_db) ): """매장 설정 생성""" # 기존 설정이 있는지 확인 (매장별) existing = db.query(StoreSettings).filter( StoreSettings.store_id == current_store.id ).first() if existing: raise HTTPException(status_code=400, detail="매장 설정이 이미 존재합니다.") db_settings = StoreSettings(**settings.dict(), store_id=current_store.id) db.add(db_settings) db.commit() db.refresh(db_settings) return db_settings @router.get("/", response_model=StoreSettingsSchema) async def get_store_settings( current_store: Store = Depends(get_current_store), db: Session = Depends(get_db) ): """매장 설정 조회""" settings = db.query(StoreSettings).filter( StoreSettings.store_id == current_store.id ).first() if not settings: # 기본 설정 생성 default_settings = StoreSettings( store_id=current_store.id, store_name=current_store.name, display_classes_count=3, list_direction="vertical", rows_per_class=1, admin_password="1234", max_waiting_limit=50, use_max_waiting_limit=True, block_last_class_registration=False, show_waiting_number=True, mask_customer_name=False, show_order_number=True, board_display_order="number,name,order", attendance_count_type="days", attendance_lookback_days=30 ) db.add(default_settings) db.commit() db.refresh(default_settings) return default_settings return settings @router.put("/", response_model=StoreSettingsSchema) async def update_store_settings( settings: StoreSettingsUpdate, current_store: Store = Depends(get_current_store), db: Session = Depends(get_db) ): """매장 설정 수정""" db_settings = db.query(StoreSettings).filter( StoreSettings.store_id == current_store.id ).first() if not db_settings: raise HTTPException(status_code=404, detail="매장 설정을 찾을 수 없습니다.") # 업데이트할 필드만 수정 update_data = settings.dict(exclude_unset=True) for field, value in update_data.items(): setattr(db_settings, field, value) db.commit() db.refresh(db_settings) return db_settings @router.post("/verify-password") async def verify_password( password: str, current_store: Store = Depends(get_current_store), db: Session = Depends(get_db) ): """관리자 비밀번호 확인""" settings = db.query(StoreSettings).filter( StoreSettings.store_id == current_store.id ).first() if not settings: raise HTTPException(status_code=404, detail="매장 설정을 찾을 수 없습니다.") if settings.admin_password != password: raise HTTPException(status_code=401, detail="비밀번호가 일치하지 않습니다.") return {"message": "인증 성공", "verified": True} @router.post("/clone/{source_store_id}", response_model=StoreSettingsSchema) async def clone_store_settings( source_store_id: int, current_store: Store = Depends(get_current_store), current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): """다른 매장의 설정 복제 Args: source_store_id: 복제할 원본 매장의 ID Returns: 복제된 현재 매장의 설정 """ # 원본 매장 조회 source_store = db.query(Store).filter(Store.id == source_store_id).first() if not source_store: raise HTTPException(status_code=404, detail="원본 매장을 찾을 수 없습니다.") # 같은 프랜차이즈 소속인지 확인 if source_store.franchise_id != current_store.franchise_id: raise HTTPException( status_code=403, detail="같은 프랜차이즈 소속 매장만 복제할 수 있습니다." ) # 자기 자신을 복제하려는 경우 if source_store_id == current_store.id: raise HTTPException( status_code=400, detail="같은 매장의 설정은 복제할 수 없습니다." ) # 원본 매장의 설정 조회 source_settings = db.query(StoreSettings).filter( StoreSettings.store_id == source_store_id ).first() if not source_settings: raise HTTPException( status_code=404, detail="원본 매장의 설정을 찾을 수 없습니다." ) # 현재 매장의 기존 설정 조회 target_settings = db.query(StoreSettings).filter( StoreSettings.store_id == current_store.id ).first() # 복제할 데이터 준비 (store_id, id 제외) settings_data = { "store_name": current_store.name, # 현재 매장 이름 유지 "display_classes_count": source_settings.display_classes_count, "list_direction": source_settings.list_direction, "rows_per_class": source_settings.rows_per_class, "admin_password": source_settings.admin_password, "max_waiting_limit": source_settings.max_waiting_limit, "use_max_waiting_limit": source_settings.use_max_waiting_limit, "block_last_class_registration": source_settings.block_last_class_registration, "auto_register_member": source_settings.auto_register_member, "business_day_start": source_settings.business_day_start, "auto_closing": source_settings.auto_closing, "closing_action": source_settings.closing_action, # 대기현황판 표시 설정 "show_waiting_number": source_settings.show_waiting_number, "mask_customer_name": source_settings.mask_customer_name, "name_display_length": source_settings.name_display_length, "show_order_number": source_settings.show_order_number, "board_display_order": source_settings.board_display_order } if target_settings: # 기존 설정이 있으면 업데이트 for field, value in settings_data.items(): setattr(target_settings, field, value) else: # 기존 설정이 없으면 새로 생성 new_settings = StoreSettings( store_id=current_store.id, **settings_data ) db.add(new_settings) # 클래스 정보 복제 from models import ClassInfo # 1. 기존 클래스 삭제 db.query(ClassInfo).filter(ClassInfo.store_id == current_store.id).delete() # 2. 원본 매장의 클래스 조회 source_classes = db.query(ClassInfo).filter(ClassInfo.store_id == source_store_id).all() # 3. 클래스 복사 for source_class in source_classes: new_class = ClassInfo( store_id=current_store.id, class_number=source_class.class_number, class_name=source_class.class_name, start_time=source_class.start_time, end_time=source_class.end_time, max_capacity=source_class.max_capacity, is_active=source_class.is_active, weekday_schedule=source_class.weekday_schedule, class_type=source_class.class_type ) db.add(new_class) db.commit() if target_settings: db.refresh(target_settings) return target_settings else: db.refresh(new_settings) return new_settings