Commit aedd2700 by 江名洋

feat(etl): 新增门店日目标达成宽表同步任务

1 parent 3a1665dc
# Python
__pycache__/
*.py[cod]
*.pyo
*.egg-info/
dist/
build/
*.egg
# Virtual environments
venv/
.venv/
env/
.env
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# Logs
logs/*.log
# Temporary / upload files
temp_uploads/
# OS
.DS_Store
Thumbs.db
# Environment / secrets
.env
*.local
# Jupyter
.ipynb_checkpoints/
import pymysql
from app.core.config import settings
c = pymysql.connect(host=settings.DB_HOST, port=settings.DB_PORT,
user=settings.DB_USER, password=settings.DB_PASSWORD,
database=settings.DB_NAME, charset='utf8mb4')
cur = c.cursor()
for t in ['ods_goods_order', 'ods_goods_sub_order', 'ods_goods_order_customer']:
cur.execute(f"SHOW COLUMNS FROM {t}")
cols = [r[0] for r in cur.fetchall()]
print(f"\n{t} ({len(cols)} cols):")
for col in cols:
print(f" {col}")
c.close()
import pymysql
from app.core.config import settings
from app.services.etl_service import ETLService
conn = ETLService.get_target_conn()
cur = conn.cursor()
table_name = 'dws_order_detail_wide'
print(f"Truncating {table_name}...")
cur.execute(f"TRUNCATE TABLE `{table_name}`")
conn.commit()
print(f"Inserting data into {table_name}...")
sql = ETLService._get_sql_order_detail_wide(None, None)
affected = cur.execute(sql)
conn.commit()
print(f"Done: {affected} rows inserted into {table_name}")
cur.execute(f"SELECT COUNT(*) cnt, MIN(install_date) min_d, MAX(install_date) max_d FROM `{table_name}`")
r = cur.fetchone()
print(f" Verify: {r['cnt']} rows, {r['min_d']} ~ {r['max_d']}")
cur.close()
conn.close()
...@@ -448,7 +448,8 @@ class ETLService: ...@@ -448,7 +448,8 @@ class ETLService:
sync_tasks = [ sync_tasks = [
("dws_staff_daily_wide", cls._get_sql_staff_daily_wide), ("dws_staff_daily_wide", cls._get_sql_staff_daily_wide),
("dws_merchant_daily_wide", cls._get_sql_merchant_daily_wide), ("dws_merchant_daily_wide", cls._get_sql_merchant_daily_wide),
("dws_order_detail_wide", cls._get_sql_order_detail_wide) ("dws_order_detail_wide", cls._get_sql_order_detail_wide),
("dws_merchant_daily_target_wide", cls._get_sql_merchant_daily_target_wide)
] ]
elif layer == 'DWM': elif layer == 'DWM':
sync_tasks = [ sync_tasks = [
...@@ -745,7 +746,201 @@ class ETLService: ...@@ -745,7 +746,201 @@ class ETLService:
@staticmethod @staticmethod
def _get_sql_order_detail_wide(start, end): def _get_sql_order_detail_wide(start, end):
return "INSERT INTO dws_order_detail_wide (order_code, sub_order_id, merchant_id, etl_date) SELECT order_code, id, merchant_id, CURDATE() FROM ods_goods_order LIMIT 1" """订单明细大宽表 — ods_goods_order + ods_goods_sub_order + 门店/客户信息"""
filter_str = f"AND DATE(go.install_upgrade_time) BETWEEN '{start}' AND '{end}'" if start and end else ""
return f"""
INSERT INTO dws_order_detail_wide (
order_code, sub_order_id,
merchant_id, merchant_name, store_code, province_name, city_name, region, channel_type,
area_supervision, online_supervision, flight_supervision,
seller_user_id,
scene_type, business_type, sales_type,
upgrade_goods_type, upgrade_secondary_goods_type, upgrade_third_level_goods_type,
car_brand_id, car_brand, car_childs_brand_id, car_childs_brand_name,
car_series_id, car_series_name, car_type_id, car_type_name,
stand_no, plate_number, customer_name, customer_phone, new_car_flag,
total_price, refund_amount, net_price,
install_upgrade_time, install_date,
order_status, sub_order_status, etl_date
)
SELECT
go.order_code,
gso.id AS sub_order_id,
go.merchant_id,
sm.merchant_name,
sm.store_code, sm.province_name, sm.city_name, sm.region, sm.channel_type,
dir_area.director_name, dir_online.director_name, dir_flight.director_name,
go.seller_user_id,
go.scene_type, go.business_type, goc.sales_type,
gso.upgrade_goods_type, gso.upgrade_secondary_goods_type, gso.upgrade_third_level_goods_type,
goc.car_brand_id, NULL AS car_brand,
goc.car_childs_brand_id, NULL AS car_childs_brand_name,
NULL AS car_series_id, NULL AS car_series_name,
NULL AS car_type_id, NULL AS car_type_name,
NULL AS stand_no, NULL AS plate_number,
NULL AS customer_name, NULL AS customer_phone, NULL AS new_car_flag,
gso.total_price,
IFNULL(gso.refund_amount, 0),
gso.total_price - IFNULL(gso.refund_amount, 0) AS net_price,
go.install_upgrade_time,
DATE(go.install_upgrade_time) AS install_date,
go.status,
gso.status,
CURDATE()
FROM ods_goods_order go
INNER JOIN ods_goods_sub_order gso
ON go.order_code = gso.order_code AND gso.deleted_flag = 'N'
LEFT JOIN ods_goods_order_customer goc
ON go.order_code = goc.order_code AND goc.deleted_flag = 'N'
LEFT JOIN ods_service_merchant sm
ON go.merchant_id = sm.merchant_id AND sm.deleted_flag = 'N'
LEFT JOIN (
SELECT merchant_id,
GROUP_CONCAT(DISTINCT director_name SEPARATOR '/') AS director_name
FROM ods_shop_merchant_director_config
WHERE director_type IN (9, 8) AND deleted_flag = 'N'
GROUP BY merchant_id
) dir_area ON go.merchant_id = dir_area.merchant_id
LEFT JOIN (
SELECT merchant_id,
GROUP_CONCAT(DISTINCT director_name SEPARATOR '/') AS director_name
FROM ods_shop_merchant_director_config
WHERE director_type IN (7, 5) AND deleted_flag = 'N'
GROUP BY merchant_id
) dir_online ON go.merchant_id = dir_online.merchant_id
LEFT JOIN (
SELECT merchant_id,
GROUP_CONCAT(DISTINCT director_name SEPARATOR '/') AS director_name
FROM ods_shop_merchant_director_config
WHERE director_type IN (13, 6) AND deleted_flag = 'N'
GROUP BY merchant_id
) dir_flight ON go.merchant_id = dir_flight.merchant_id
WHERE go.deleted_flag = 'N'
{filter_str}
ON DUPLICATE KEY UPDATE
merchant_name = VALUES(merchant_name),
store_code = VALUES(store_code),
province_name = VALUES(province_name),
city_name = VALUES(city_name),
region = VALUES(region),
channel_type = VALUES(channel_type),
area_supervision = VALUES(area_supervision),
online_supervision = VALUES(online_supervision),
flight_supervision = VALUES(flight_supervision),
seller_user_id = VALUES(seller_user_id),
scene_type = VALUES(scene_type),
business_type = VALUES(business_type),
sales_type = VALUES(sales_type),
upgrade_goods_type = VALUES(upgrade_goods_type),
upgrade_secondary_goods_type = VALUES(upgrade_secondary_goods_type),
upgrade_third_level_goods_type = VALUES(upgrade_third_level_goods_type),
car_brand_id = VALUES(car_brand_id),
car_brand = VALUES(car_brand),
car_childs_brand_id = VALUES(car_childs_brand_id),
car_childs_brand_name = VALUES(car_childs_brand_name),
car_series_id = VALUES(car_series_id),
car_series_name = VALUES(car_series_name),
car_type_id = VALUES(car_type_id),
car_type_name = VALUES(car_type_name),
stand_no = VALUES(stand_no),
plate_number = VALUES(plate_number),
customer_name = VALUES(customer_name),
customer_phone = VALUES(customer_phone),
new_car_flag = VALUES(new_car_flag),
total_price = VALUES(total_price),
refund_amount = VALUES(refund_amount),
net_price = VALUES(net_price),
install_upgrade_time = VALUES(install_upgrade_time),
install_date = VALUES(install_date),
order_status = VALUES(order_status),
sub_order_status = VALUES(sub_order_status),
etl_date = VALUES(etl_date)
"""
@staticmethod
def _get_sql_merchant_daily_target_wide(start, end):
"""门店日目标达成宽表 — 基于 dws_merchant_daily_wide + 产值补充"""
filter_str = f"AND t.report_date BETWEEN '{start}' AND '{end}'" if start and end else ""
return f"""
INSERT IGNORE INTO dws_merchant_daily_target_wide (
report_date, merchant_id, merchant_name, store_code,
province_name, city_name, region, channel_type,
area_supervision, online_supervision, flight_supervision,
store_delivery_count, host_plant_delivery_count, total_delivery_count,
customer_loss_count,
active_staff, on_break_staff,
total_pre_sale_touch, total_after_sale_touch, total_touch_count, should_touch_count,
total_order_count, total_extended_warranty, total_battery_extended,
total_battery_money, total_output_value,
month_deliver_target, month_output_target,
avg_deal_rate, etl_date
)
SELECT
t.report_date,
t.merchant_id,
t.merchant_name,
t.store_code,
t.province_name, t.city_name, t.region, t.channel_type,
t.area_supervision, t.online_supervision, t.flight_supervision,
t.delivery_count,
IFNULL(hpd.delivery_count, 0) AS host_plant_delivery,
t.delivery_count + IFNULL(hpd.delivery_count, 0) AS total_delivery,
t.customer_loss_count,
t.active_staff, t.on_break_staff,
t.sum_equity_to_shop AS total_pre_sale_touch,
t.sum_after_sale_to_shop AS total_after_sale_touch,
t.sum_equity_to_shop + t.sum_after_sale_to_shop AS total_touch,
0 AS should_touch_count,
t.sum_order_count,
t.sum_extended_warranty,
t.sum_battery_extended,
t.sum_battery_money,
IFNULL((SELECT SUM(net_price)
FROM dws_order_detail_wide o
WHERE o.merchant_id = t.merchant_id
AND o.install_date = t.report_date
AND o.order_status IN (2, 4, 6)), 0) AS total_output_value,
NULL AS month_deliver_target,
NULL AS month_output_target,
NULL AS avg_deal_rate,
CURDATE()
FROM dws_merchant_daily_wide t
LEFT JOIN ods_host_plant_delivery hpd
ON t.store_code = hpd.store_code
AND t.report_date = hpd.delivery_date
AND hpd.deleted_flag = 'N'
WHERE 1=1
{filter_str}
ON DUPLICATE KEY UPDATE
merchant_name = VALUES(merchant_name),
store_code = VALUES(store_code),
province_name = VALUES(province_name),
city_name = VALUES(city_name),
region = VALUES(region),
channel_type = VALUES(channel_type),
area_supervision = VALUES(area_supervision),
online_supervision = VALUES(online_supervision),
flight_supervision = VALUES(flight_supervision),
store_delivery_count = VALUES(store_delivery_count),
host_plant_delivery_count = VALUES(host_plant_delivery_count),
total_delivery_count = VALUES(total_delivery_count),
customer_loss_count = VALUES(customer_loss_count),
active_staff = VALUES(active_staff),
on_break_staff = VALUES(on_break_staff),
total_pre_sale_touch = VALUES(total_pre_sale_touch),
total_after_sale_touch = VALUES(total_after_sale_touch),
total_touch_count = VALUES(total_touch_count),
should_touch_count = VALUES(should_touch_count),
total_order_count = VALUES(total_order_count),
total_extended_warranty = VALUES(total_extended_warranty),
total_battery_extended = VALUES(total_battery_extended),
total_battery_money = VALUES(total_battery_money),
total_output_value = VALUES(total_output_value),
month_deliver_target = VALUES(month_deliver_target),
month_output_target = VALUES(month_output_target),
avg_deal_rate = VALUES(avg_deal_rate),
etl_date = VALUES(etl_date)
"""
@staticmethod @staticmethod
def _get_sql_dwm_staff_touch(start, end): def _get_sql_dwm_staff_touch(start, end):
......
This diff could not be displayed because it is too large.
...@@ -276,3 +276,16 @@ ...@@ -276,3 +276,16 @@
20:31:12 INFO [Scheduler] Initializing APScheduler... [scheduler.py:28] 20:31:12 INFO [Scheduler] Initializing APScheduler... [scheduler.py:28]
20:31:12 INFO [Scheduler] APScheduler started successfully. [scheduler.py:53] 20:31:12 INFO [Scheduler] APScheduler started successfully. [scheduler.py:53]
20:41:12 INFO [Scheduler] System health check at 2026-06-23 20:41:12.414040 [scheduler.py:77] 20:41:12 INFO [Scheduler] System health check at 2026-06-23 20:41:12.414040 [scheduler.py:77]
20:51:12 INFO [Scheduler] System health check at 2026-06-23 20:51:12.420044 [scheduler.py:77]
21:01:12 INFO [Scheduler] System health check at 2026-06-23 21:01:12.417880 [scheduler.py:77]
21:01:12 INFO [Scheduler] Start sync_ods_task at 2026-06-23 21:01:12.560506 [scheduler.py:66]
21:01:22 INFO [Scheduler] sync_ods_task completed. Results: [{'source': 'dj_car_life_service_merchant', 'target': 'ods_service_merchant', 'rows': 3, 'status': 'success', 'range_start': '2026-06-23 18:10:11', 'range_end': '2026-06-23 18:10:11'}, {'source': 'dj_car_life_shop_merchant_director_config', 'target': 'ods_shop_merchant_director_config', 'rows': 3, 'status': 'success', 'range_start': '2026-06-23 17:30:47', 'range_end': '2026-06-23 17:30:47'}, {'source': 'dj_car_life_merchant_brand_model', 'target': 'ods_merchant_brand_model', 'rows': 6, 'status': 'success', 'range_start': '2026-06-12 12:02:22', 'range_end': '2026-06-12 12:02:22'}, {'source': 'dj_car_life_staff_info', 'target': 'ods_staff_info', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:52:56', 'range_end': '2026-06-23 18:52:56'}, {'source': 'dj_car_life_base_goods', 'target': 'ods_base_goods', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 20:21:18', 'range_end': '2026-06-23 20:21:18'}, {'source': 'dj_car_life_goods_info', 'target': 'ods_goods_info', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:13:44', 'range_end': '2026-06-23 18:13:44'}, {'source': 'dj_car_life_upgrade_goods', 'target': 'ods_upgrade_goods', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 20:21:18', 'range_end': '2026-06-23 20:21:18'}, {'source': 'dj_car_life_merchant_goods_info', 'target': 'ods_merchant_goods_info', 'rows': 70, 'status': 'success', 'range_start': '2026-06-22 15:39:25', 'range_end': '2026-06-22 15:39:25'}, {'source': 'dj_car_life_merchant_package_goods_info', 'target': 'ods_merchant_package_goods_info', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:17:19', 'range_end': '2026-06-23 18:17:19'}, {'source': 'dj_car_life_staff_daily_report_huawei', 'target': 'ods_staff_daily_report_huawei', 'rows': 69, 'status': 'success', 'range_start': '2026-06-23 20:26:33', 'range_end': '2026-06-23 21:00:39'}, {'source': 'dj_car_life_staff_daily_report', 'target': 'ods_staff_daily_report', 'rows': 34, 'status': 'success', 'range_start': '2026-06-23 20:26:33', 'range_end': '2026-06-23 21:00:39'}, {'source': 'dj_car_life_merchant_daily_report_huawei', 'target': 'ods_merchant_daily_report_huawei', 'rows': 45, 'status': 'success', 'range_start': '2026-06-23 20:26:33', 'range_end': '2026-06-23 21:00:39'}, {'source': 'dj_car_life_goods_order', 'target': 'ods_goods_order', 'rows': 22, 'status': 'success', 'range_start': '2026-06-23 20:26:02', 'range_end': '2026-06-23 20:57:40'}, {'source': 'dj_car_life_goods_sub_order', 'target': 'ods_goods_sub_order', 'rows': 41, 'status': 'success', 'range_start': '2026-06-23 20:27:06', 'range_end': '2026-06-23 20:57:10'}, {'source': 'dj_car_life_goods_order_customer', 'target': 'ods_goods_order_customer', 'rows': 12, 'status': 'success', 'range_start': '2026-06-23 20:24:28', 'range_end': '2026-06-23 20:57:10'}, {'source': 'dj_car_life_scene_merchant_ref', 'target': 'ods_scene_merchant_ref', 'rows': 2, 'status': 'success', 'range_start': '2026-06-23 18:56:59', 'range_end': '2026-06-23 18:56:59'}, {'source': 'dj_car_life_yqmerchant_config', 'target': 'ods_yqmerchant_config', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:10:11', 'range_end': '2026-06-23 18:10:11'}, {'source': 'dj_car_life_merchant_deliver_target_config', 'target': 'ods_merchant_deliver_target_config', 'rows': 1, 'status': 'success', 'range_start': '2026-06-15 23:49:36', 'range_end': '2026-06-15 23:49:36'}, {'source': 'dj_car_life_merchant_output_target_config', 'target': 'ods_merchant_output_target_config', 'rows': 4, 'status': 'success', 'range_start': '2026-06-23 11:50:21', 'range_end': '2026-06-23 11:50:21'}, {'source': 'employee_delivery_deal_rate', 'target': 'ods_employee_delivery_deal_rate', 'rows': 1, 'status': 'success', 'range_start': '2025-12-30 23:25:51', 'range_end': '2025-12-30 23:25:51'}] [scheduler.py:70]
21:09:55 INFO [Scheduler] APScheduler shut down (immediate). [scheduler.py:61]
21:09:59 INFO [Scheduler] Initializing APScheduler... [scheduler.py:28]
21:09:59 INFO [Scheduler] APScheduler started successfully. [scheduler.py:53]
21:19:59 INFO [Scheduler] System health check at 2026-06-23 21:19:59.620735 [scheduler.py:77]
21:29:59 INFO [Scheduler] System health check at 2026-06-23 21:29:59.619557 [scheduler.py:77]
21:39:59 INFO [Scheduler] System health check at 2026-06-23 21:39:59.614241 [scheduler.py:77]
21:40:02 INFO [Scheduler] Start sync_ods_task at 2026-06-23 21:40:02.351184 [scheduler.py:66]
21:40:11 INFO [Scheduler] sync_ods_task completed. Results: [{'source': 'dj_car_life_service_merchant', 'target': 'ods_service_merchant', 'rows': 3, 'status': 'success', 'range_start': '2026-06-23 18:10:11', 'range_end': '2026-06-23 18:10:11'}, {'source': 'dj_car_life_shop_merchant_director_config', 'target': 'ods_shop_merchant_director_config', 'rows': 3, 'status': 'success', 'range_start': '2026-06-23 17:30:47', 'range_end': '2026-06-23 17:30:47'}, {'source': 'dj_car_life_merchant_brand_model', 'target': 'ods_merchant_brand_model', 'rows': 6, 'status': 'success', 'range_start': '2026-06-12 12:02:22', 'range_end': '2026-06-12 12:02:22'}, {'source': 'dj_car_life_staff_info', 'target': 'ods_staff_info', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:52:56', 'range_end': '2026-06-23 18:52:56'}, {'source': 'dj_car_life_base_goods', 'target': 'ods_base_goods', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 20:21:18', 'range_end': '2026-06-23 20:21:18'}, {'source': 'dj_car_life_goods_info', 'target': 'ods_goods_info', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:13:44', 'range_end': '2026-06-23 18:13:44'}, {'source': 'dj_car_life_upgrade_goods', 'target': 'ods_upgrade_goods', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 20:21:18', 'range_end': '2026-06-23 20:21:18'}, {'source': 'dj_car_life_merchant_goods_info', 'target': 'ods_merchant_goods_info', 'rows': 70, 'status': 'success', 'range_start': '2026-06-22 15:39:25', 'range_end': '2026-06-22 15:39:25'}, {'source': 'dj_car_life_merchant_package_goods_info', 'target': 'ods_merchant_package_goods_info', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:17:19', 'range_end': '2026-06-23 18:17:19'}, {'source': 'dj_car_life_staff_daily_report_huawei', 'target': 'ods_staff_daily_report_huawei', 'rows': 86, 'status': 'success', 'range_start': '2026-06-23 21:00:39', 'range_end': '2026-06-23 21:38:26'}, {'source': 'dj_car_life_staff_daily_report', 'target': 'ods_staff_daily_report', 'rows': 28, 'status': 'success', 'range_start': '2026-06-23 21:00:39', 'range_end': '2026-06-23 21:38:26'}, {'source': 'dj_car_life_merchant_daily_report_huawei', 'target': 'ods_merchant_daily_report_huawei', 'rows': 51, 'status': 'success', 'range_start': '2026-06-23 21:00:39', 'range_end': '2026-06-23 21:38:56'}, {'source': 'dj_car_life_goods_order', 'target': 'ods_goods_order', 'rows': 9, 'status': 'success', 'range_start': '2026-06-23 20:57:40', 'range_end': '2026-06-23 21:37:10'}, {'source': 'dj_car_life_goods_sub_order', 'target': 'ods_goods_sub_order', 'rows': 25, 'status': 'success', 'range_start': '2026-06-23 20:57:10', 'range_end': '2026-06-23 21:38:38'}, {'source': 'dj_car_life_goods_order_customer', 'target': 'ods_goods_order_customer', 'rows': 4, 'status': 'success', 'range_start': '2026-06-23 20:57:10', 'range_end': '2026-06-23 21:36:57'}, {'source': 'dj_car_life_scene_merchant_ref', 'target': 'ods_scene_merchant_ref', 'rows': 2, 'status': 'success', 'range_start': '2026-06-23 18:56:59', 'range_end': '2026-06-23 18:56:59'}, {'source': 'dj_car_life_yqmerchant_config', 'target': 'ods_yqmerchant_config', 'rows': 1, 'status': 'success', 'range_start': '2026-06-23 18:10:11', 'range_end': '2026-06-23 18:10:11'}, {'source': 'dj_car_life_merchant_deliver_target_config', 'target': 'ods_merchant_deliver_target_config', 'rows': 1, 'status': 'success', 'range_start': '2026-06-15 23:49:36', 'range_end': '2026-06-15 23:49:36'}, {'source': 'dj_car_life_merchant_output_target_config', 'target': 'ods_merchant_output_target_config', 'rows': 4, 'status': 'success', 'range_start': '2026-06-23 11:50:21', 'range_end': '2026-06-23 11:50:21'}, {'source': 'employee_delivery_deal_rate', 'target': 'ods_employee_delivery_deal_rate', 'rows': 1, 'status': 'success', 'range_start': '2025-12-30 23:25:51', 'range_end': '2025-12-30 23:25:51'}] [scheduler.py:70]
21:49:59 INFO [Scheduler] System health check at 2026-06-23 21:49:59.606425 [scheduler.py:77]
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!