From 1e0ddf6280689e0c49a20c6c66caa9fecf57fa4d Mon Sep 17 00:00:00 2001 From: KWON Date: Mon, 21 Jul 2025 17:36:32 +0900 Subject: [PATCH] =?UTF-8?q?=ED=88=AC=EC=96=B4=ED=8C=A8=EC=8A=A4/=EB=AE=A4?= =?UTF-8?q?=EC=A7=80=EC=97=84=ED=97=A4=EC=9D=B4=20=EC=A3=BC=EA=B0=84?= =?UTF-8?q?=EC=97=85=EB=AC=B4=20=EB=B3=B4=EA=B3=A0=EC=84=9C=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8=EC=9A=A9=20=ED=8C=8C=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/getweekdata.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 lib/getweekdata.py diff --git a/lib/getweekdata.py b/lib/getweekdata.py new file mode 100644 index 0000000..6d5b9e1 --- /dev/null +++ b/lib/getweekdata.py @@ -0,0 +1,97 @@ +# getweekdata.py +import os, sys +from datetime import datetime, timedelta +import pandas as pd +from sqlalchemy import select, and_ +from tabulate import tabulate # pip install tabulate 필요 + +# 상위 폴더의 모듈 불러오기 +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 + +# === 검색 조건 변수 === +TARGET_CA01 = '매표소' +TARGET_CA03_LIST = ['입장료', '티켓', '기업제휴'] +PRODUCT_NAMES = ['투어패스', '뮤지엄헤이'] + +def get_date_ranges(): + today = datetime.today().date() + + # 이번 달 시작~끝 + first_day_month = today.replace(day=1) + next_month = first_day_month.replace(day=28) + timedelta(days=4) + last_day_month = next_month - timedelta(days=next_month.day) + + # 지난 주 월~일 + last_week_sun = today - timedelta(days=today.weekday() + 1) + last_week_mon = last_week_sun - timedelta(days=6) + + return { + 'this_month': (first_day_month, last_day_month), + 'last_week': (last_week_mon, last_week_sun) + } + +def fetch_data(session, start_date, end_date, product_keyword): + stmt = select(pos).where( + and_( + pos.c.date.between(start_date, end_date), + pos.c.ca01.contains(TARGET_CA01), + pos.c.ca03.in_(TARGET_CA03_LIST), + pos.c.name.contains(product_keyword) + ) + ) + result = session.execute(stmt).fetchall() + return pd.DataFrame([row._mapping for row in result]) if result else pd.DataFrame() + +def main(): + + date_ranges = get_date_ranges() + session = get_session() + + today = datetime.today().date() + year_start = today.replace(month=1, day=1) + year_end = today.replace(month=12, day=31) + + # 수집용 딕셔너리 + summary = { + "금주": [], + "금월": [], + "누계": [] + } + + # --- 금주 --- + last_week_start, last_week_end = date_ranges['last_week'] + week_vals = [] + for product in PRODUCT_NAMES: + df = fetch_data(session, last_week_start, last_week_end, product) + week_vals.append(df['qty'].sum() if not df.empty else 0) + summary["금주"] = week_vals + [sum(week_vals)] + + # --- 금월 --- + this_month_start, this_month_end = date_ranges['this_month'] + month_vals = [] + for product in PRODUCT_NAMES: + df = fetch_data(session, this_month_start, this_month_end, product) + month_vals.append(df['qty'].sum() if not df.empty else 0) + summary["금월"] = month_vals + [sum(month_vals)] + + # --- 누계 (2025년 전체) --- + year_vals = [] + for product in PRODUCT_NAMES: + df = fetch_data(session, year_start, year_end, product) + year_vals.append(df['qty'].sum() if not df.empty else 0) + summary["누계"] = year_vals + [sum(year_vals)] + + session.close() + + # --- 출력 --- + table_data = [] + for key in ["금주", "금월", "누계"]: + table_data.append([key] + summary[key]) + + headers = ["구분", "경기투어패스", "뮤지엄헤이", "계"] + print(tabulate(table_data, headers=headers, tablefmt="simple", colalign=("center", "right", "right", "right"))) + +if __name__ == '__main__': + main()