import os
import sys
import time
import traceback

sys.path.append(os.path.dirname(sys.path[0]))
from utils.secure_db_client import get_remote_engine


def update_workflow_manager(site_name, date_type, date_info):
    print(f"当前执行的参数: {site_name, date_type, date_info}")
    if date_type == "month":
        while True:
            try:
                site_name_pri_dict = {
                    "us": 2,
                    "uk": 4,
                    "de": 6,
                }
                engine_mysql = get_remote_engine(
                    site_name='us',
                    db_type='mysql'
                )
                priority = site_name_pri_dict[site_name]
                update_sql_workflow_bg = f"""update selection.workflow_manager 
set bg_dol_state=3, kafka_state=1, finished_count=COALESCE(finished_count, 0) + 1
WHERE workflow_name='月全流程' and site_name='{site_name}' and date_type='{date_type}' and date_info='{date_info}' and priority={priority}"""
                engine_mysql.execute(update_sql_workflow_bg)
                print(f"成功：workflow_manager进度表---更新月全流程的进度表, 以流量选品为准: {update_sql_workflow_bg}")
                break
            except Exception as e:
                print(f"失败：workflow_manager进度表----更新月全流程的进度表, 以流量选品为准: {update_sql_workflow_bg}, {e}, 报错信息: {traceback.format_exc()}")
                time.sleep(300)
                continue


if __name__ == '__main__':
    # site_name = 'us'
    # date_type = 'month'
    # date_info = '2026-01'
    site_name = sys.argv[1]  # 参数1：站点
    date_type = sys.argv[2]  # 参数2：类型：week/4_week/month/quarter/day
    date_info = sys.argv[3]  # 参数3：年-周/年-月/年-季/年-月-日, 比如: 2022-1
    if site_name in ["us", "uk", "de"] and date_type == "month":
        update_workflow_manager(site_name, date_type, date_info)
