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):
    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'
                )
                with engine_mysql.begin() as conn:
                    priority = site_name_pri_dict[site_name]
                    update_sql_workflow = f"""
                                            INSERT INTO workflow_manager
                            (
                                workflow_name,
                                site_name,
                                date_type,
                                date_info,
                                priority,
                                spider_name,
                                spider_is_ready,
                                spider_state,
                                bg_name,
                                bg_dol_state
                            )
                            VALUES
                            (
                                '月全流程',
                                '{site_name}',
                                'month',
                                '{date_info}',
                                {priority},
                                '{site_name}_spider_asin',
                                'yes',
                                1,
                                '{site_name}_all_cal', 
                                1
                            )
                            ON DUPLICATE KEY UPDATE
                                spider_is_ready = VALUES(spider_is_ready),
                                spider_state = VALUES(spider_state);
                    """
                    print(f"workflow_manager进度表---重置爬虫的asin抓取进度: {update_sql_workflow}")
                    conn.execute(update_sql_workflow)
                    break
            except Exception as e:
                print(f"失败：workflow_manager进度表---重置爬虫的asin抓取进度: {update_sql_workflow}, {e}, 报错信息: {traceback.format_exc()}")
                time.sleep(300)
                continue


def export_data(site_name, date_type, date_info):
    engine = get_remote_engine(
        site_name=site_name,  # -> database "selection"
        db_type="postgresql_14",  # -> 服务端 alias "mysql"
        # user="fangxingjun",         # -> 服务端 alias "mysql"
        # user_token="5f1b2e9c3a4d7f60"   # 可不传，走默认
    )
    partitions = {
        'site_name': site_name,
        'date_type': date_type,
        'date_info': date_info,
    }
    cols_list = ['asin', 'asin_is_variation', 'volume', 'weight_str', 'date_info']

    import_table = f'{site_name}_all_syn_st_{date_type}_{date_info.replace("-", "_")}'

    hive_table = 'dwt_asin_sync'
    print(f"import_table: {import_table}, hive_table: {hive_table}")
    print(f"partitions: {partitions}")

    engine.sqoop_raw_export(
        hive_table=hive_table,
        import_table=import_table,
        partitions=partitions,
        m=1,
        cols=','.join(cols_list)
    )


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
    export_data(site_name, date_type, date_info)
    # if site_name in ["us", "uk", "de"] and date_type == "month":
    #     update_workflow_manager(site_name, date_type, date_info)
