|
@@ -4,6 +4,7 @@ import xlrd
|
|
|
|
|
|
from crossborder.utils.constants import REGION_MAPPING
|
|
|
from crossborder.utils.db_helper import DBHelper
|
|
|
+from crossborder.utils.log import log
|
|
|
from crossborder.utils.parse_utils import extract_year_month_from_path, parse_value
|
|
|
|
|
|
|
|
@@ -19,7 +20,7 @@ def parse_region_table_excel(file_path):
|
|
|
workbook = xlrd.open_workbook(file_path)
|
|
|
sheet = workbook.sheet_by_index(0)
|
|
|
except Exception as e:
|
|
|
- print(f"文件读取失败:{file_path}\n错误:{str(e)}")
|
|
|
+ log.critical(f"文件读取失败:{file_path}\n错误:{str(e)}")
|
|
|
return 0
|
|
|
|
|
|
params_old = [] # 老逻辑的数据(非地级市)
|
|
@@ -82,9 +83,9 @@ def parse_region_table_excel(file_path):
|
|
|
None
|
|
|
)
|
|
|
if not region_info:
|
|
|
- print(f"⚠️ 地区匹配失败:{region_name}")
|
|
|
+ log.debug(f"⚠️ 地区匹配失败:{region_name}")
|
|
|
continue
|
|
|
-
|
|
|
+ log.info(f"匹配行政单位:{region_name}")
|
|
|
is_municipality = region_name in ["北京市","天津市","上海市","重庆市"]
|
|
|
is_city = region_name.endswith("市") and not is_municipality
|
|
|
|
|
@@ -120,24 +121,24 @@ def parse_region_table_excel(file_path):
|
|
|
params_old.append(param_dict_old)
|
|
|
|
|
|
except Exception as e:
|
|
|
- print(f"行{row_idx}处理失败:{str(e)}")
|
|
|
+ log.critical(f"行{row_idx}处理失败:{str(e)}")
|
|
|
continue
|
|
|
|
|
|
# 使用DBHelper执行批量SQL
|
|
|
try:
|
|
|
if params_old:
|
|
|
row_count = db.execute_sql_with_params(sql_template_old, params_old)
|
|
|
- print(f"✅ 成功处理 {len(params_old)} 条记录(老表),插入/更新 {row_count} 行")
|
|
|
+ log.info(f"✅ 成功处理 {len(params_old)} 条记录(t_yujin_crossborder_region_trade),插入/更新 {row_count} 行")
|
|
|
except Exception as e:
|
|
|
- print(f"数据库操作失败(老表):{str(e)}")
|
|
|
+ log.critical(f"数据库操作失败(t_yujin_crossborder_region_trade):{str(e)}")
|
|
|
|
|
|
# 执行新表插入
|
|
|
try:
|
|
|
if params_new:
|
|
|
row_count = db.execute_sql_with_params(sql_template_new, params_new)
|
|
|
- print(f"✅ 成功处理 {len(params_new)} 条记录(新表),插入/更新 {row_count} 行")
|
|
|
+ log.info(f"✅ 成功处理 {len(params_new)} 条记录(t_yujin_crossborder_prov_region_trade),插入/更新 {row_count} 行")
|
|
|
except Exception as e:
|
|
|
- print(f"数据库操作失败(新表):{str(e)}")
|
|
|
+ log.critical(f"数据库操作失败(t_yujin_crossborder_prov_region_trade):{str(e)}")
|
|
|
|
|
|
return len(params_old) + len(params_new)
|
|
|
|
|
@@ -146,4 +147,4 @@ if __name__ == "__main__":
|
|
|
# 测试用文件路径
|
|
|
file_path = r"D:\pythonSpace\crossborder\downloads\total\2025\04\(8)2025年进出口商品收发货人所在地总值表.xls"
|
|
|
result = parse_region_table_excel(file_path)
|
|
|
- print(f"处理结果:{result}")
|
|
|
+ log.info(f"处理结果:{result}")
|