From bf44f13a513856939bbdaad1757ca2864244dc5e Mon Sep 17 00:00:00 2001 From: KWON Date: Tue, 29 Jul 2025 14:29:54 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EA=B3=BC=EC=A0=95?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=EB=A1=9C=EA=B7=B8=EB=A5=BC=20?= =?UTF-8?q?=EC=BD=98=EC=86=94=EC=97=90=20=EC=B6=9C=EB=A0=A5=ED=95=98?= =?UTF-8?q?=EA=B8=B0=20=EC=9C=84=ED=95=9C=20=EB=B6=80=EB=B6=84=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pos_update_upsolution.py | 42 ++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/pos_update_upsolution.py b/lib/pos_update_upsolution.py index d33883c..3d586e8 100644 --- a/lib/pos_update_upsolution.py +++ b/lib/pos_update_upsolution.py @@ -38,37 +38,43 @@ def load_excel_data(filepath: str): df = df.dropna(subset=required_cols) return df - def process_file(filepath: str, engine, session, table): + batch_size = 1000 + inserted, updated, errors = 0, 0, 0 + count = 0 + try: - # sale_date는 엑셀 내부 각 행별로 존재하므로, 파일명 기준 추출는 제외 df = load_excel_data(filepath) logger.info(f"[LOAD] {os.path.basename(filepath)} - {len(df)}건") - inserted, updated, errors = 0, 0, 0 - for _, row in df.iterrows(): try: + def safe_int(val, default=0): + try: + return int(val) + except Exception: + return default + data = { "sale_date": pd.to_datetime(row["매출일시"]), "shop_name": str(row.get("매장명", "")).strip(), - "pos_no": int(row.get("포스번호", 0)), - "bill_no": int(row.get("영수증 번호", 0)), + "pos_no": safe_int(row.get("포스번호")), + "bill_no": safe_int(row.get("영수증 번호")), "product_cd": str(row.get("품목", "")).strip(), "ca01": nan_to_none(row.get("대분류")), "ca02": nan_to_none(row.get("중분류")), "ca03": nan_to_none(row.get("소분류")), "product_name": str(row.get("품목명", "")).strip(), "barcode": nan_to_none(row.get("바코드")), - "amt": int(row.get("단가", 0)), - "qty": int(row.get("수량", 0)), - "tot_sale_amt": int(row.get("주문 금액", 0)), - "dc_amt": int(row.get("할인 금액", 0)), - "dcm_sale_amt": int(row.get("공급가액", 0)), - "net_amt": int(row.get("세금", 0)), - "vat_amt": int(row.get("부가세", 0)), - "cash_receipt": int(row.get("현금영수증", 0)), - "card": int(row.get("카드", 0)), + "amt": safe_int(row.get("단가")), + "qty": safe_int(row.get("수량")), + "tot_sale_amt": safe_int(row.get("주문 금액")), + "dc_amt": safe_int(row.get("할인 금액")), + "dcm_sale_amt": safe_int(row.get("공급가액")), + "net_amt": safe_int(row.get("세금")), + "vat_amt": safe_int(row.get("부가세")), + "cash_receipt": safe_int(row.get("현금영수증")), + "card": safe_int(row.get("카드")), } stmt = mysql_insert(table).values(**data) @@ -79,12 +85,16 @@ def process_file(filepath: str, engine, session, table): }) result = session.execute(update_stmt) - # rowcount 1이면 insert, 2면 update (MySQL 특성상) if result.rowcount == 1: inserted += 1 elif result.rowcount == 2: updated += 1 + count += 1 + if count % batch_size == 0: + session.commit() + logger.info(f"[COMMIT] {count}건 처리 완료") + except Exception as e: logger.warning(f"[ERROR:ROW] {e}") errors += 1