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 wf_month_control(site_name, date_type, date_info, spider_name, wf_type="spider"):
    print(f"当前执行的参数: {site_name, date_type, date_info, spider_name, wf_type}")
    engine_mysql = get_remote_engine(
        site_name='us',
        db_type='mysql'
    )
    """
ansible dabing_all -f 10 -m shell -a "nohup /usr/local/bin/python3 /mnt/py_spider/threading_spider/Poll_site_search_term_month.py us 2026-05 >/dev/null 2>&1 &";
ansible dabing_all -f 10 -m shell -a "nohup /usr/local/bin/python3 /mnt/py_spider/threading_spider/Poll_site_seller_account.py us 2026-05 >/dev/null 2>&1 &";
ansible dabing_all -f 10 -m shell -a "nohup /usr/local/bin/python3 /mnt/py_spider/threading_spider/Poll_site_spider_month.py us month 2026-05 >/dev/null 2>&1 &";
    """
    priority_dict = {
        "us": [11, 12, 13],
        "uk": [21, 22, 23],
        "de": [31, 32, 33],
    }
    # 根据spider_name构建spider_script  爬虫启动命令
    spider_name_dict = {
        f"{site_name}_spider_st": [
            f"Poll_site_search_term_month.py {site_name} {date_info} ",
            priority_dict[site_name][0],
            f"{site_name}_asin_export",
            f"ALL站点-asin同步-pg-api",
        ],
        f"{site_name}_spider_fd": [
            f"Poll_site_seller_account.py {site_name} {date_info} ",
            priority_dict[site_name][1],
            f"{site_name}_fd_import",
            f"店铺数据导入-月流程",
        ],
        f"{site_name}_spider_asin": [
            f"Poll_site_spider_month.py {site_name} {date_type} {date_info} ",
            priority_dict[site_name][2],
            f"{site_name}_all_cal",
            f"ALL-月流程-ABA+反查+流量选品",
        ],
    }
    workflow_name = "月全流程"
    script_param = spider_name_dict[spider_name][0]
    priority = spider_name_dict[spider_name][1]
    spider_script = f'ansible dabing_all -f 10 -m shell -a "nohup /usr/local/bin/python3 /mnt/py_spider/threading_spider/{script_param} >/dev/null 2>&1 &";'
    bg_name = spider_name_dict[spider_name][2]
    bg_dol_name = spider_name_dict[spider_name][3]

    if wf_type == "spider":
        update_sql_workflow = f"""
            INSERT INTO selection.workflow_manager 
                (
                    workflow_name,
                    site_name,
                    date_type,
                    date_info,
                    priority,
                    spider_name,
                    spider_script,
                    spider_is_ready,
                    spider_state,
                    bg_name,
                    bg_dol_name,
                    bg_dol_state
                )
                VALUES
                (
                    '{workflow_name}',
                    '{site_name}',
                    '{date_type}',
                    '{date_info}',
                    {priority}, 
                    '{spider_name}',
                    '{spider_script}',
                    'yes',
                    1,
                    '{bg_name}', 
                    '{bg_dol_name}',
                    1
                )
                ON DUPLICATE KEY UPDATE
                    priority = VALUES(priority), 
                    spider_is_ready = VALUES(spider_is_ready), 
                    spider_script = VALUES(spider_script), 
                    bg_dol_state = VALUES(bg_dol_state), 
                    spider_state = VALUES(spider_state);
                """
        print(f"workflow_manager进度表---重置进度: {update_sql_workflow}")
        engine_mysql.execute(update_sql_workflow)
    elif wf_type == "cal":
        update_sql_workflow_bg = f"""update selection.workflow_manager 
        set bg_dol_state=3, 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}"""
        print(f"workflow_manager进度表---更新asin导出进度和完成次数: {update_sql_workflow_bg}")
        engine_mysql.execute(update_sql_workflow_bg)
        pass


if __name__ == '__main__':
    # 测试st写入记录
    # for site_name in ['us', 'uk', 'de']:
    #     wf_month_control(site_name=site_name, date_type='month', date_info='2026-06', spider_name=f'{site_name}_spider_asin', wf_type="spider")

    site_name = sys.argv[1]  # 参数1：站点
    date_type = sys.argv[2]  # 参数2：类型：day/week/4_week/month/quarter
    date_info = sys.argv[3]  # 参数3：年-月-日/年-周/年-月/年-季, 比如: 2022-1
    spider_name = sys.argv[4]  # 参数4：spider_name名称对应的值, 其实也是爬虫任务流
    wf_type = sys.argv[5]  # 参数4：spider或cal, 判断执行的类型
    wf_month_control(site_name=site_name, date_type=date_type, date_info=date_info, spider_name=spider_name, wf_type=wf_type)

    # site_name = 'us'
    # # 同步st搜索词
    # wf_month_control(site_name=site_name, date_type='month', date_info='2026-06', spider_name=f'{site_name}_spider_st', wf_type="spider")
    # # 抓完搜索词+同步asin -- st抓取完计算数量+1 + 重置asin抓取
    # wf_month_control(site_name=site_name, date_type='month', date_info='2026-06', spider_name=f'{site_name}_spider_asin', wf_type="spider")
    # wf_month_control(site_name=site_name, date_type='month', date_info='2026-06', spider_name=f'{site_name}_spider_st', wf_type="cal")
    # # 同步fd -- 重置asin抓取+st抓取完数量+1
    # wf_month_control(site_name=site_name, date_type='month', date_info='2026-06', spider_name=f'{site_name}_spider_fd', wf_type="spider")
    # wf_month_control(site_name=site_name, date_type='month', date_info='2026-06', spider_name=f'{site_name}_spider_fd', wf_type="cal")
    # # 抓完asin+计算全流程 -- 更改asin计算全流程数量+1
    # wf_month_control(site_name=site_name, date_type='month', date_info='2026-06', spider_name=f'{site_name}_spider_asin', wf_type="cal")
