"""
author: 方星钧(ffman)
description: 清洗6大站点对应的 单周的zr，sp，sb，ac，bs，er，tr等7大类型数据表（计算zr,sp类型表的page_rank+合并7张表）
table_read_name: ods_st_rank_zr/sp/sb/ac/bs/er/tr
table_save_name: dim_st_asin_info
table_save_level: dim
version: 3.0
created_date: 2022-05-10
updated_date: 2022-11-07
"""

import os
import sys

sys.path.append(os.path.dirname(sys.path[0]))  # 上级目录
from utils.templates import Templates
# from ..utils.templates import Templates
from pyspark.sql.types import IntegerType


class DimStAsinInfo(Templates):

    def __init__(self, site_name='us', date_type="day", date_info='2022-10-01'):
        super().__init__()
        self.site_name = site_name
        self.date_type = date_type
        self.date_info = date_info
        self.db_save = f'temp_me_asin'
        self.spark = self.create_spark_object(
            app_name=f"{self.db_save}: {self.site_name},{self.date_type}, {self.date_info}")
        self.df_date = self.get_year_week_tuple()
        self.partitions_by = ['site_name', 'date_type', 'date_info']
        self.reset_partitions(partitions_num=10)

    def read_data(self):
        sql = """SELECT DISTINCT asin
            FROM (
                SELECT asin
                FROM ods_search_term_zr
                WHERE site_name = 'us'
                  AND date_type = 'month_aba_me'
                  AND date_info = '2025-11'
            
                UNION ALL
            
                SELECT asin
                FROM ods_search_term_sp
                WHERE site_name = 'us'
                  AND date_type = 'month_aba_me'
                  AND date_info = '2025-11'
            
                UNION ALL
            
                SELECT asin
                FROM ods_search_term_sb
                WHERE site_name = 'us'
                  AND date_type = 'month_aba_me'
                  AND date_info = '2025-11'
            
                UNION ALL
            
                SELECT asin
                FROM ods_search_term_ac
                WHERE site_name = 'us'
                  AND date_type = 'month_aba_me'
                  AND date_info = '2025-11'
            
                UNION ALL
            
                SELECT asin
                FROM ods_search_term_bs
                WHERE site_name = 'us'
                  AND date_type = 'month_aba_me'
                  AND date_info = '2025-11'
            ) t;
            """
        df = self.spark.sql(sqlQuery=sql)
        df.show(10, truncate=False)
        pdf = df.toPandas()
        print(f"pdf.shape: {pdf.shape}")
        pdf.to_csv("temp_me_asin.csv", index=False)

        # self.df_save.show(n=10, truncate=False)
        # print("self.df_save.count():", self.df_save.count())


if __name__ == '__main__':
    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
    handle_obj = DimStAsinInfo(site_name=site_name, date_type=date_type, date_info=date_info)
    handle_obj.read_data()
