import os
import sys

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

from utils.ssh_util import SSHUtil
from utils.common_util import CommonUtil
from utils.db_util import DBUtil, DbTypes
from utils.spark_util import SparkUtil
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)
    #  不写入到PG表晚上执行 立马执行
    CommonUtil.judge_is_work_hours(
        site_name=site_name, date_type=date_type, date_info=date_info,
        principal='hejiangming', priority=1, export_tools_type=1, belonging_to_process='ABA周增长'
    )

    db_type = DbTypes.postgresql_cluster.name
    print("导出到PG集群中")

    year_str, week_str = date_info.split('-')
    year = int(year_str)
    week = int(week_str)

    export_master_tb = f"{site_name}_aba_report_week_{year}"
    #   用上一年母表 like 出当年母表(including all 带那4个分区索引)、按 week 分区；已存在则 if not exists 跳过。
    export_master_tb_before = f"{site_name}_aba_report_week_{year - 1}"
    export_tb = f"{export_master_tb}_{week}"
    next_week = week + 1

    engine = DBUtil.get_db_engine(db_type, site_name)
    with engine.connect() as connection:
        sql = f"""
            create table if not exists {export_master_tb} (like {export_master_tb_before} including all) partition by range (week);
            drop table if exists {export_tb};
            create table if not exists {export_tb}
            (
            like {export_master_tb} including defaults including comments
            );
        """
        print("================================执行sql================================")
        print(sql)
        connection.execute(sql)

    # 导出表名
    sh = CommonUtil.build_export_sh(
        site_name=site_name,
        db_type=db_type,
        hive_tb="dim_st_detail_week",
        export_tb=export_tb,
        col=[
            'st_key',
            'search_term',
            'is_search_text',
            'is_ascending_text',
            'is_first_text',
            'is_high_return_text',
            'rank',
            'search_volume',
            'orders',
            'rank_last_1_week',
            'rank_last_4_week',
            'rank_last_12_week',
            'rank_change_last_1_week',
            'rank_change_last_4_week',
            'rank_change_last_12_week',
            'rank_rate_last_1_week',
            'rank_rate_last_4_week',
            'rank_rate_last_12_week',
            'st_word_num',
            'asin1',
            'asin2',
            'asin3',
            'product_title1',
            'product_title2',
            'product_title3',
            'click_share1',
            'click_share2',
            'click_share3',
            'click_share_total',
            'conversion_share1',
            'conversion_share2',
            'conversion_share3',
            'conversion_share_total',
            'brand1',
            'brand2',
            'brand3',
            'category1',
            'category2',
            'category3',
            'quantity_being_sold',
            'category_id',
            'category_current_id',
            'market_cycle_type',
            'week',
            'rank_change_1_week_ago',
            'rank_change_2_week_ago',
            'rank_change_3_week_ago',
            'rank_rate_1_week_ago',
            'rank_rate_2_week_ago',
            'rank_rate_3_week_ago',
            'is_first_ever_text',
            'new_high_this_week',
            'new_high_1_week_ago',
            'new_high_2_week_ago',
            'new_high_3_week_ago'
        ],
        partition_dict={
            "site_name": site_name,
            "date_type": date_type,
            "date_info": date_info
        },num_mappers=2
    )

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

    with engine.connect() as connection:
        sql = f"""
        update {export_tb} set keyword_tsv = to_tsvector('english_amazonword', search_term);
        """
        print("================================执行sql================================")
        print(sql)
        connection.execute(sql)

    # 将子表加入母表中并复制母表索引
    DBUtil.add_pg_part(
        engine,
        source_tb_name=export_tb,
        part_master_tb=export_master_tb,
        part_val={
            "from": [week],
            "to": [next_week]
        },
        cp_index_flag=True,
    )

    # 趋势改由独立脚本产：dwt/dwt_st_base_report_week.py(算)+sqoop_export/dwt_st_base_report_week.py(导)，
    # 三站共用、口径逐字段一致。此处不再产趋势，避免与独立趋势任务并行写 {site}_aba_last_total_week
    # # 第2步：Spark SQL 从 dim_st_detail_week 写入 dwt_st_base_report
    # print("================================写入周趋势数据到Hive================================")
    # spark = SparkUtil.get_spark_session(f"export_dim_st_detail_week_trend:{site_name} {date_type} {date_info}")
    # dwt_hive_tb = "dwt_st_base_report"
    # dwt_hdfs_path = f"/home/big_data_selection/dwt/{dwt_hive_tb}/site_name={site_name}/date_type=week/date_info={date_info}"
    # print(f"清除hdfs目录中数据:{dwt_hdfs_path}")
    # HdfsUtils.delete_hdfs_file(dwt_hdfs_path)
    # spark.sql(f"""
    #     INSERT INTO TABLE {dwt_hive_tb} PARTITION(site_name='{site_name}', date_type='week', date_info='{date_info}')
    #     SELECT
    #         st_key,
    #         search_term,
    #         search_volume as st_volume,
    #         orders as st_orders,
    #         {year} as years,
    #         current_timestamp() as created_time,
    #         current_timestamp() as updated_time,
    #         `rank` as st_rank
    #     FROM dim_st_detail_week
    #     WHERE site_name='{site_name}' AND date_type='week' AND date_info='{date_info}'
    # """)
    # spark.stop()
    # print("================================写入周趋势数据完成================================")

    # # 第3步：Sqoop 导出 dwt_st_base_report 到 PG 趋势表
    # print("================================导出周趋势图到PG集群================================")
    # trend_master_tb = f"{site_name}_aba_last_total_week"
    # trend_year_next = str(year + 1)
    # trend_export_tb = f"{trend_master_tb}_{year_str}"
    #
    # trend_sql = f"""
    #     create table if not exists {trend_export_tb} partition of {trend_master_tb} for values from ('{year_str}') to ('{trend_year_next}');
    #     create index if not exists {trend_export_tb}_st_key_idx on {trend_export_tb} using btree (st_key);
    #     delete from {trend_export_tb} where date_info = '{date_info}';
    # """
    # DBUtil.engine_exec_sql(engine, trend_sql)
    #
    # trend_columns = ["st_key", "search_term", "st_volume", "st_rank", "st_orders", "years",
    #                  "created_time", "updated_time", "date_type", "date_info"]
    # trend_sh = CommonUtil.build_export_sh(
    #     site_name=site_name,
    #     db_type=db_type,
    #     hive_tb=dwt_hive_tb,
    #     export_tb=trend_export_tb,
    #     col=trend_columns,
    #     partition_dict={
    #         "site_name": site_name,
    #         "date_type": "week",
    #         "date_info": date_info
    #     }
    # )
    # trend_client = SSHUtil.get_ssh_client()
    # SSHUtil.exec_command_async(trend_client, trend_sh, ignore_err=False)
    # trend_client.close()
    # print("================================周趋势图导出完成================================")

    # 插入流程记录表
    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}', '{date_info}', '导出PG数据库', 14, '{site_name}_aba_report_week', 'week', 'ABA搜索词周报告', '是', 'ABA搜索词周报告表', 'postgresql_cluster');
    """
    DBUtil.engine_exec_sql(DBUtil.get_db_engine('mysql', 'us'), sql)

    print("success")

