From a01f5f37981db1dfa8da49c9919bb50b48c130d3 Mon Sep 17 00:00:00 2001 From: KWON Date: Mon, 21 Jul 2025 17:39:47 +0900 Subject: [PATCH] =?UTF-8?q?=EC=98=A4=EB=9E=98=EB=90=9C=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=EC=97=90=EC=84=9C=20=EA=B7=9C=EC=B9=99=EC=97=90=20?= =?UTF-8?q?=EB=A7=9E=EA=B2=8C=20=EB=8D=B0=EC=9D=B4=ED=84=B0=EB=A5=BC=20?= =?UTF-8?q?=EC=82=BD=EC=9E=85=ED=95=98=EB=8A=94=20=EC=9D=BC=ED=9A=8C?= =?UTF-8?q?=EC=9A=A9=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/visitor_update.py | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 lib/visitor_update.py diff --git a/lib/visitor_update.py b/lib/visitor_update.py new file mode 100644 index 0000000..74de75b --- /dev/null +++ b/lib/visitor_update.py @@ -0,0 +1,87 @@ +# ./lib/visitor_update.py + +import os +import sys +import pandas as pd +from datetime import datetime + +# 프로젝트 루트 경로 추가 +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + +from conf.db import get_session +from conf.db_schema import pos +from sqlalchemy import select + +# 상수 정의 +FILE_PATH = os.path.join(os.path.dirname(__file__), '..', 'data', 'visitor_raw.xlsx') +CA01 = '매표소' +CA02 = 'POS' +CA03 = '입장료' +BARCODE = 11111111 +DEFAULT_INT = 0 + + +def load_excel(filepath): + df = pd.read_excel(filepath) + df.columns = ['date', 'qty'] + df['date'] = pd.to_datetime(df['date']).dt.date + return df + + +def get_existing_dates(session, dates): + """DB에 이미 존재하는 날짜 목록 조회""" + stmt = select(pos.c.date).where(pos.c.date.in_(dates)) + result = session.execute(stmt).scalars().all() + return set(result) + + +def insert_data(df): + session = get_session() + try: + all_dates = set(df['date'].unique()) + existing_dates = get_existing_dates(session, all_dates) + + # 중복 날짜 제거 + if existing_dates: + print(f"[INFO] 이미 존재하는 날짜는 건너뜁니다: {sorted(existing_dates)}") + df = df[~df['date'].isin(existing_dates)] + + if df.empty: + print("[INFO] 삽입할 신규 데이터가 없습니다.") + return + + for _, row in df.iterrows(): + record = { + 'date': row['date'], + 'ca01': CA01, + 'ca02': CA02, + 'ca03': CA03, + 'barcode': BARCODE, + 'name': '입장객', + 'qty': int(row['qty']), + 'tot_amount': DEFAULT_INT, + 'tot_discount': DEFAULT_INT, + 'actual_amount': DEFAULT_INT + } + session.execute(pos.insert().values(**record)) + + session.commit() + print(f"[INFO] {len(df)}건의 데이터가 성공적으로 삽입되었습니다.") + except Exception as e: + session.rollback() + print(f"[ERROR] 데이터 저장 중 오류 발생: {e}") + finally: + session.close() + + +def main(): + if not os.path.exists(FILE_PATH): + print(f"[ERROR] 파일을 찾을 수 없습니다: {FILE_PATH}") + return + + df = load_excel(FILE_PATH) + insert_data(df) + + +if __name__ == "__main__": + main()