import os
import sys
from datetime import datetime

sys.path.append(os.path.dirname(sys.path[0]))
from utils.ssh_util import SSHUtil
from utils.common_util import CommonUtil, DateTypes
from utils.db_util import DBUtil
from utils.hdfs_utils import HdfsUtils

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}")

    print(test_flag)

    if test_flag == 'test':
        db_type = 'postgresql_test'
        print("导出到测试库中")
    else:
        CommonUtil.judge_is_work_hours(site_name=site_name, date_type=date_type, date_info=date_info,
                                       principal='chenyuanjie',
                                       priority=2, export_tools_type=1, belonging_to_process=f'新ABA流程_{date_type}')

        if date_type in (DateTypes.month.name, DateTypes.week.name,DateTypes.month_week.name):
            db_type = 'postgresql_cluster'
            print("导出到PG-Cluster库中")
        else:
            db_type = "postgresql"
            print("导出到PG库中")

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

    # 导出前校验 Hive 分区是否有数据，避免空分区触发交换导致 PG 数据被清空
    hive_partition_path = f"/home/big_data_selection/dwt/dwt_aba_st_analytics/site_name={site_name}/date_type={date_type}/date_info={date_info}"
    hive_files = HdfsUtils.read_list(hive_partition_path)
    if not hive_files:
        print(f"[ERROR] Hive 分区无数据文件，路径：{hive_partition_path}，跳过导出，请先检查 DWT 计算任务是否正常写入！")
        sys.exit(1)
    print(f"Hive 分区文件数：{len(hive_files)}，路径：{hive_partition_path}，继续导出")

    suffix = str(date_info).replace("-", "_")

    # 导出表--基准表名
    export_base_tb = f"{site_name}_aba_last"
    export_base_cols = [
        "id",
        "search_term",
        "rank",
        "category_id",
        "category_current_id",
        "orders",
        "bsr_orders",
        "search_volume",
        "quantity_being_sold",
        "st_ao_avg",
        "st_ao_val_rate",
        "new_bsr_orders_proportion",
        "new_asin_proportion",
        "page1_title_proportion",
        "price_avg",
        "total_comments_avg",
        "rating_avg",
        "weight_avg",
        "volume_avg",
        "title_length_avg",
        "st_num",
        "aadd_proportion",
        "sp_proportion",
        "fbm_proportion",
        "cn_proportion",
        "amzon_proportion",
        "most_proportion",
        "max_num",
        "asin1",
        "asin2",
        "asin3",
        "click_share1",
        "click_share2",
        "click_share3",
        "total_click_share",
        "conversion_share1",
        "conversion_share2",
        "conversion_share3",
        "total_conversion_share",
        "new_asin_num",
        "total_asin_num",
        "new_asin_orders",
        "new_asin_bsr_orders",
        "is_first_text",
        "is_ascending_text",
        "is_search_text",
        "top3_seller_orders",
        "top3_seller_bsr_orders",
        "top3_brand_orders",
        "top3_brand_bsr_orders",
        "page3_brand_num",
        "page3_seller_num",
        "brand_monopoly",
        "seller_monopoly",
        "max_num_asin",
        "is_self_max_num_asin",
        "date_info",
        "created_time",
        "updated_time",
        "gross_profit_fee_air",
        "gross_profit_fee_sea",
        "multi_color_proportion",
        "multi_size_proportion",
        "st_word_num",
        "st_movie_label",
        "st_brand_label",
        "st_brand1",
        "st_category1",
        "st_brand2",
        "st_category2",
        "st_brand3",
        "st_category3",
        "st_bsr_cate_1_id_new",
        "st_bsr_cate_current_id_new",
        "st_crawl_date",
        "st_zr_page123_title_appear_rate",
	    "st_sp_page123_title_appear_rate",
        "st_competition_level",
        "amazon_monthly_sales",
        "st_zr_flow_proportion",
        "st_ao_val_matrix",
        "st_flow_proportion_matrix",
        "lang",
        "st_dd50_proportion",
        "st_dd100_proportion",
        "st_dd200_proportion"
    ]

    if date_type == DateTypes.last30day.name or date_type == DateTypes.month_week.name:
        export_tb_target = f"{export_base_tb}_30_day"
        export_tb_copy = f"{export_tb_target}_copy"
        export_table = export_tb_copy
        sql = f"""
               create table if not exists {export_tb_copy} 
              (
                  like {export_tb_target} including all
              );
              truncate table {export_tb_copy};
              """
        # 执行SQL语句
        DBUtil.engine_exec_sql(engine, sql)

        # 补全动态30天字段
        tb_cols = ["color_proportion", "is_new_market_segment", "supply_demand", "market_cycle_type", "is_high_return_text"]
        export_cols = export_base_cols + tb_cols
        print("导出的字段：", export_cols)

    elif date_type in (DateTypes.day.name, DateTypes.week.name, DateTypes.month.name):
        # aba基础分区表名
        export_table = f"{export_base_tb}_{date_type}_{suffix}"
        next_val = CommonUtil.get_next_val(date_type, date_info)
        year_str = CommonUtil.safeIndex(date_info.split("-"), 0, None)
        if date_type == DateTypes.day.name:
            # 特有导出字段
            tb_cols = []
            # 处理导出表
            year_month = CommonUtil.reformat_date(date_info, "%Y-%m-%d", "%Y-%m")
            year_month_before = CommonUtil.get_month_offset(year_month, -1).replace("-", "_")
            suffix_y_m = year_month.replace("-", "_")
            export_master_tb = f"{export_base_tb}_{date_type}_{suffix_y_m}"
            export_tb_before = f"{export_base_tb}_{date_type}_{year_month_before}"

        elif date_type == DateTypes.week.name:
            # week特有导出字段
            tb_cols = ["is_new_market_segment", "color_proportion"]
            # 处理导出表
            export_master_tb = f"{export_base_tb}_{date_type}_{year_str}"
            year_month_before = str(int(year_str) - 1)
            export_tb_before = f"{export_base_tb}_{date_type}_{year_month_before}"
        else:
            # month特有导出字段
            tb_cols = [
                "is_new_market_segment", "color_proportion", "supply_demand", "market_cycle_type", "is_high_return_text",
                "st_zr_counts", "st_sp_counts", "st_self_asin_counts", "st_self_asin_proportion",
                "is_first_ever_text",
                "brand_asin_proportion",
                "seller_asin_proportion",
                # 搜索词属性标签（材质/颜色/细分人群等），仅 month 计算
                # Hive 端是逗号分隔 STRING（如 "材质,颜色"），sqoop 写入 PG copy 表需先 ALTER 成 VARCHAR
                # 交换前再 ALTER 回 VARCHAR[]（用 string_to_array 转换），与 dwt_aba_last365 处理 st_movie_brand_label 同款思路
                "st_attribute_label",
                # 峰值月：Hive 端是逗号分隔 STRING（如 "2025-11,2026-04"），PG 端是 VARCHAR[]
                # 同 st_attribute_label：copy 表先 ALTER 成 VARCHAR 让 sqoop 写字符串，交换前再 string_to_array 转回 VARCHAR[]
                "peak_month",
                # 常年可卖标记：标量 int，sqoop 直写，无需中转
                "all_year_text_flag",
                # 搜索词市场筛选维度：Hive 端是清洗后 JSON STRING（如 '{"Flavor":["Chocolate"]}'），PG 端是 jsonb
                # Sqoop 不能直接写 jsonb：copy 表先把该列 ALTER 成 varchar 让 sqoop 写字符串，
                # 交换前再 ALTER 回 jsonb（USING ...::jsonb），同 st_attribute_label 的中转思路
                "st_filter_refinements"
            ]
            # 处理导出表
            export_master_tb = f"{export_base_tb}_{date_type}_{year_str}"
            year_month_before = str(int(year_str) - 1)
            export_tb_before = f"{export_base_tb}_{date_type}_{year_month_before}"

        # 导出字段补全
        export_cols = export_base_cols + tb_cols

        # sql建表和创建分区
        # INCLUDING ALL：继承列定义、生成列、索引、约束等
        # 补充显式创建GIN索引：LIKE从分区父表(relkind='p')复制时，分区索引(relkind='I')可能丢失
        sql = f"""
              create table if not exists {export_master_tb}
                          (
                              like {export_tb_before} including all
                          )
              partition by range (date_info);

              create index if not exists {export_master_tb}_keyword_tsv_idx
              on {export_master_tb} using gin (keyword_tsv);

              create table if not exists {export_table} partition of {export_master_tb} for values from ('{date_info}') to ('{next_val}');
              """
        DBUtil.engine_exec_sql(engine, sql)

        if date_type == DateTypes.month.name:
            # month类型：Sqoop先导入到copy表，再通过分区交换替换正式分区，避免truncate造成的空窗期
            export_tb_copy = f"{export_table}_copy"
            sql_copy = f"""
                  create table if not exists {export_tb_copy}
                  (
                      like {export_table} including all
                  );
                  truncate table {export_tb_copy};
                  """
            DBUtil.engine_exec_sql(engine, sql_copy)

            # copy 表继承自正式分区表（含 st_attribute_label VARCHAR[]），
            # 但 Sqoop 不支持直接写入 PG 数组类型，必须先把 copy 表的该列临时改成 VARCHAR
            # 等 Sqoop 完成后、分区交换之前，再 ALTER 回 VARCHAR[]（见下方 exchange_pg_part_tb 前的处理）
            # st_filter_refinements 在 master/copy 里是 jsonb 且带 DEFAULT '{}'::jsonb，
            # Sqoop 不能写 jsonb → 先 DROP DEFAULT（避免改类型时默认值转换报错）再 ALTER 成 varchar
            # USING st_filter_refinements::text 把 jsonb 显式转字符串；varchar 不限长，避免长 JSON 被截断
            sql_alter_to_varchar = f"""
                  ALTER TABLE {export_tb_copy} ALTER COLUMN st_attribute_label TYPE VARCHAR(200);
                  ALTER TABLE {export_tb_copy} ALTER COLUMN peak_month TYPE VARCHAR(200);
                  ALTER TABLE {export_tb_copy} ALTER COLUMN st_filter_refinements DROP DEFAULT;
                  ALTER TABLE {export_tb_copy} ALTER COLUMN st_filter_refinements TYPE VARCHAR USING st_filter_refinements::text;
                  """
            DBUtil.engine_exec_sql(engine, sql_alter_to_varchar)

            # keyword_tsv 是 master 分区表的生成列（GENERATED ALWAYS AS to_tsvector(...) STORED，用于全文检索 GIN 索引）
            # PG 已知行为：LIKE 从【分区子表】复制时，GENERATED 表达式不会保留（表达式只挂在 master 上，子表自己不存）
            #   → copy 表里 keyword_tsv 会退化成普通可空 tsvector 列
            #   → 后续 exchange_pg_part_tb 的 ATTACH PARTITION 会因列定义与 master 不一致而报错：
            #     "column keyword_tsv in child table must be a generated column"
            # 解决：显式 drop 后以生成列方式重建。CASCADE 是为了同时把 LIKE 时附带继承的 keyword_tsv 索引一并清掉，
            # 后面 ATTACH 时 master 的分区索引会自动给新分区补建对应索引。
            sql_fix_keyword_tsv = f"""
                  ALTER TABLE {export_tb_copy} DROP COLUMN IF EXISTS keyword_tsv CASCADE;
                  ALTER TABLE {export_tb_copy} ADD COLUMN keyword_tsv tsvector
                      GENERATED ALWAYS AS (to_tsvector('english_amazonword', search_term)) STORED;
                  """
            DBUtil.engine_exec_sql(engine, sql_fix_keyword_tsv)

            # Sqoop导入的目标表改为copy表
            export_table_original = export_table
            export_table = export_tb_copy
        else:
            # day/week类型：直接truncate正式分区后导入（保持原有逻辑）
            DBUtil.engine_exec_sql(engine, f"truncate table {export_table};")

    else:
        print("输入的date_type有误，请检查！！")
        quit()

    if test_flag == "month_append":
        partition_dict = {
            "site_name": site_name,
            "date_type": "month",
            "date_info": date_info
        }
    else:
        partition_dict = {
            "site_name": site_name,
            "date_type": date_type,
            "date_info": date_info
        }

    # 导出执行sqoop的sh编写
    sh = CommonUtil.build_export_sh(
        site_name=site_name,
        db_type=db_type,
        hive_tb="dwt_aba_st_analytics",
        export_tb=export_table,
        col=export_cols,
        partition_dict=partition_dict
    )

    client = SSHUtil.get_ssh_client()
    SSHUtil.exec_command_async(client, sh, ignore_err=False)
    client.close()

    # 处理流程更新
    if date_type in (DateTypes.month_week.name, DateTypes.last30day.name):
        DBUtil.exchange_tb(engine,
                           source_tb_name=export_tb_copy,
                           target_tb_name=export_tb_target,
                           cp_index_flag=False)
        update_workflow_sql = f"""
            replace INTO selection.workflow_everyday
            (site_name, report_date, status, status_val, table_name, date_type, page, is_end, remark,export_db_type)
            VALUES('{site_name}', '{datetime.now().date()}', '导出PG数据库完成', 14, 'us_aba_last_30_day', '30_day', 'ABA搜索词', '是', 'ABA搜索词最近30天表','{db_type}');
        """
    elif date_type == DateTypes.month.name:
        # 分区交换前必须把 copy 表的 st_attribute_label 从 VARCHAR 转回 VARCHAR[]
        # 否则与 master 分区表 schema 不一致，exchange_pg_part_tb 会失败
        # USING string_to_array(...) 把 Sqoop 写入的逗号串（如 "材质,颜色"）拆成数组（如 {材质,颜色}
        sql_alter_back = f"""
              ALTER TABLE {export_tb_copy}
              ALTER COLUMN st_attribute_label TYPE VARCHAR[]
              USING string_to_array(st_attribute_label, ',')::varchar[];

              ALTER TABLE {export_tb_copy}
              ALTER COLUMN peak_month TYPE VARCHAR[]
              USING string_to_array(coalesce(peak_month, ''), ',')::varchar[];

              ALTER TABLE {export_tb_copy}
              ALTER COLUMN st_filter_refinements TYPE jsonb
              USING st_filter_refinements::jsonb;
              """
        DBUtil.engine_exec_sql(engine, sql_alter_back)

        # month类型：Sqoop导入到copy表完成后，通过分区交换替换正式分区，避免空窗期
        DBUtil.exchange_pg_part_tb(engine,
                                   source_tb_name=export_tb_copy,
                                   part_master_tb=export_master_tb,
                                   part_target_tb=export_table_original,
                                   cp_index_flag=False,
                                   part_val={"from": [date_info], "to": [next_val]})
        # 交换完成后删除copy表，节省空间（交换后copy表里存的是旧数据）
        DBUtil.engine_exec_sql(engine, f"drop table if exists {export_tb_copy};")
        update_workflow_sql = f"""
                UPDATE selection.workflow_everyday SET status='导出PG数据库完成', status_val=14,is_end ='是',export_db_type = '{db_type}'
                WHERE site_name= '{site_name}' and date_type='{date_type}' and report_date= '{date_info}'  and page ='ABA搜索词'
            """
    else:
        update_workflow_sql = f"""
                UPDATE selection.workflow_everyday SET status='导出PG数据库完成', status_val=14,is_end ='是',export_db_type = '{db_type}'
                WHERE site_name= '{site_name}' and date_type='{date_type}' and report_date= '{date_info}'  and page ='ABA搜索词'
            """

    # 往导出流程表插入导出完成数据，方便监听导出脚本是否全部完成
    CommonUtil.modify_export_workflow_status(update_workflow_sql, site_name, date_type, date_info)

