import os
import sys

sys.path.append(os.path.dirname(sys.path[0]))

from utils.db_util import DBUtil
from utils.ssh_util import SSHUtil
from utils.common_util import CommonUtil, DateTypes

if __name__ == '__main__':
    site_name = CommonUtil.get_sys_arg(1, None)
    date_type = CommonUtil.get_sys_arg(2, None)
    date_info = CommonUtil.get_sys_arg(3, None)
    #  获取最后一个参数
    test_flag = CommonUtil.get_sys_arg(len(sys.argv) - 1, None)
    print(f"执行参数为{sys.argv}")

    CommonUtil.judge_is_work_hours(site_name=site_name, date_type=date_type, date_info=date_info,
                                   principal='hejiangming',
                                   priority=3, export_tools_type=1, belonging_to_process='店铺流程')

    if test_flag == 'test':
        db_type = 'postgresql_test'
        print("导出到测试库中")
    else:
        # db_type = "postgresql"
        # print("导出到PG库中")
        db_type = "postgresql_cluster"
        print("导出到PG集群库库中")

    year_str = CommonUtil.safeIndex(date_info.split("-"), 0, None)
    suffix = str(date_info).replace("-", "_")
    base_tb = f"{site_name}_fb_top20_asin_info"
    export_master_tb = f"{base_tb}_{year_str}"
    # 上一年母表，跨年时用它 like 出当年母表（见下面建表 sql）
    export_master_tb_before = f"{base_tb}_{int(year_str) - 1}"
    export_tb = f"{base_tb}_{suffix}"
    next_month = CommonUtil.get_next_val(date_type, date_info)

    # 获取数据库连接
    engine = DBUtil.get_db_engine(db_type, site_name)


    # 跨年自动建当年母表：用上一年母表 like 出来（including all 带上索引/约束/默认值/注释），按 date_info 分区
    # 不建的话跨年第一个月（如 2027-01）下面 like {export_master_tb} 会报表不存在
    # if not exists 幂等，已存在直接跳过、只加不删
    # 保证幂等性，先删除原始表同周期的数据
    sql = f"""
         create table if not exists {export_master_tb}
                (like {export_master_tb_before} including all)
         partition by range (date_info);
         drop table if exists {export_tb};
         create table if not exists {export_tb}
                (
                like {export_master_tb}  including comments
         );
    """
    DBUtil.engine_exec_sql(engine, sql)

    # 导出agg表
    sh_report = CommonUtil.build_export_sh(
        site_name=site_name,
        db_type=db_type,
        hive_tb="dwt_fb_top20_asin_info",
        export_tb=export_tb,
        col=[
            "seller_id",
            "account_name",
            "asin",
            "asin_title",
            "asin_launch_time",
            "is_asin_new",
            "created_time",
            "updated_time",
            "date_info",
            "asin_img_url",
            "asin_price",
            "asin_rating",
            "asin_total_comments",
            "asin_weight",
            "asin_volume",
            "fb_row_num"
        ],
        partition_dict={
            "site_name": site_name,
            "date_type": date_type,
            "date_info": date_info
        }
    )

    client = SSHUtil.get_ssh_client()
    SSHUtil.exec_command_async(client, sh_report, ignore_err=False)


    client.close()
    # 创建索引并交换分区
    DBUtil.add_pg_part(
        engine,
        source_tb_name=export_tb,
        part_master_tb=export_master_tb,
        part_val={
            "from": [date_info],
            "to": [next_month]
        },
        cp_index_flag=False,
    )

    update_workflow_sql = f"""                
                        update selection.workflow_progress set `status`='导出pg集群完成', status_val=6, over_date=CURRENT_TIME, is_end='是' where page='店铺Feedback' 
                        and `date_info`='{date_info}' and date_type='{date_type}' and site_name='{site_name}'
                    """
    CommonUtil.modify_export_workflow_status(update_workflow_sql, site_name, date_type, date_info)
    # 关闭链接
    engine.dispose()
    print("success")

    print("success")

