From 8f502b42b7b29242bd867d3e75fe56a370f34f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E8=89=B3?= Date: Wed, 16 Sep 2026 10:15:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(poi):=20=E5=B0=86=E7=BC=BA=E5=B0=91?= =?UTF-8?q?=E6=9C=89=E6=95=88=E7=BB=8F=E7=BA=AC=E5=BA=A6=E7=9A=84=E5=8C=BA?= =?UTF-8?q?=E5=9F=9F=E5=8D=95=E7=8B=AC=E5=AF=BC=E5=87=BA=E5=88=B0Excel?= =?UTF-8?q?=E7=9A=84'=E7=BC=BA=E5=B0=91=E7=BB=8F=E7=BA=AC=E5=BA=A6?= =?UTF-8?q?=E5=8C=BA=E5=9F=9F'Sheet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/poi/dag_check_gov_township_diff.py | 45 ++++++++++++++++--------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/dags/poi/dag_check_gov_township_diff.py b/dags/poi/dag_check_gov_township_diff.py index d0ae72f..21752cb 100644 --- a/dags/poi/dag_check_gov_township_diff.py +++ b/dags/poi/dag_check_gov_township_diff.py @@ -93,6 +93,7 @@ def check_gov_township_diff(**context): district_docs = es_client.scan(index=INDEX_NAME, query=query_district) diff_list = [] + missing_loc_list = [] total_districts = 0 processed_count = 0 @@ -117,18 +118,26 @@ def check_gov_township_diff(**context): tjj_district_code = source.get("tjj_district_code", "") location = source.get("location", {}) - if not location or "lon" not in location or "lat" not in location: - print(f"区域 {province}-{city}-{district}({mzb_district_code}) 缺少有效经纬度,跳过") - continue + lon = location.get("lon") if isinstance(location, dict) else None + lat = location.get("lat") if isinstance(location, dict) else None - lon = location.get("lon") - lat = location.get("lat") - if not lon or not lat: + if not location or lon is None or lat is None or not str(lon).strip() or not str(lat).strip(): + print(f"区域 {province}-{city}-{district}({mzb_district_code}) 缺少有效经纬度,记录并跳过") + missing_loc_list.append({ + "ES文档ID": doc_id, + "省份": province, + "城市": city, + "区域": district, + "民政部区划代码": mzb_district_code, + "统计局区划代码": tjj_district_code, + "原因": "缺少有效经纬度(location)", + "登记时间": datetime.now().strftime("%Y-%m-%d %H:%M:%S") + }) continue processed_count += 1 if processed_count % 50 == 0: - print(f"已处理 {processed_count} 个县/市/旗, 当前发现差异 {len(diff_list)} 条...") + print(f"已处理 {processed_count} 个县/市/旗, 当前发现差异 {len(diff_list)} 条, 缺少经纬度 {len(missing_loc_list)} 条...") # 2. 调用高德 API 获取 township(传入可配置的 amap_key) amap_township = get_amap_township(lon, lat, key=amap_key) @@ -198,17 +207,22 @@ def check_gov_township_diff(**context): "排查时间": datetime.now().strftime("%Y-%m-%d %H:%M:%S") }) - print(f"[{datetime.now()}] 扫描完成!共遍历区域: {processed_count} 个,发现不一致/缺失记录: {len(diff_list)} 条") + print(f"[{datetime.now()}] 扫描完成!共遍历有效区域: {processed_count} 个,缺少经纬度区域: {len(missing_loc_list)} 个,发现不一致/缺失街道: {len(diff_list)} 条") - # 5. 生成 Excel 并邮件发送 + # 5. 生成 Excel 并邮件发送(包含两个 Sheet) tmp_dir = os.path.join(os.path.expanduser('~'), 'tmp', 'gov_diff') os.makedirs(tmp_dir, exist_ok=True) today_str = datetime.now().strftime('%Y%m%d_%H%M%S') excel_path = os.path.join(tmp_dir, f"县政府所在乡镇街道差异排查_{today_str}.xlsx") - df = pd.DataFrame(diff_list) - df.to_excel(excel_path, index=False, engine='openpyxl') - print(f"排查明细已写入 Excel: {excel_path}") + with pd.ExcelWriter(excel_path, engine='openpyxl') as writer: + df_diff = pd.DataFrame(diff_list) + df_diff.to_excel(writer, sheet_name='街道差异名单', index=False) + + df_missing = pd.DataFrame(missing_loc_list) + df_missing.to_excel(writer, sheet_name='缺少经纬度区域', index=False) + + print(f"排查明细已写入 Excel: {excel_path} (含 '街道差异名单' 与 '缺少经纬度区域' 两个 Sheet)") # 发送邮件 receiver = "wangwenyan@fangjia.com" @@ -219,10 +233,11 @@ def check_gov_township_diff(**context):

您好!

系统已完成 206 ES {INDEX_NAME} 索引中县政府所在乡镇街道(has_gov='是')与高德逆地理街道(township)的比对排查,详情如下:

-

详细差异名单已附在附件 Excel 中,请人工审核确认。

+

详细名单已整理在附件 Excel 中,请人工审核确认。

""" print(f"正在发送审核邮件至: {receiver}, 抄送: {cc_receiver} ...")