import os
import sys

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

from utils.common_util import CommonUtil
from utils.secure_db_client import get_remote_engine

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)
    assert site_name is not None, "site_name 不能为空！"
    assert date_type is not None, "date_type 不能为空！"
    assert date_info is not None, "date_info 不能为空！"
    year = date_info.split("-")[0]
    db_type = 'mysql'
    import_table = f"{site_name}_self_asin_detail"

    hive_table = "ods_self_asin_detail"
    partition_dict = {
        "site_name": site_name,
        "date_type": date_type,
        "date_info": date_info
    }

    engine = get_remote_engine(
        site_name=site_name,
        db_type=db_type
    )

    query = f"""
    SELECT
  asin,
  REPLACE(REPLACE(REPLACE(img_url, '\n',' '), '\r',' '), '\t',' ') AS img_url,
  REPLACE(REPLACE(REPLACE(title, '\n',' '), '\r',' '), '\t',' ') AS title,
  title_len,
  price,
  rating,
  total_comments,
  buy_box_seller_type,
  page_inventory,
  REPLACE(REPLACE(REPLACE(category, '\n',' '), '\r',' '), '\t',' ') AS category,
  REPLACE(REPLACE(REPLACE(volume, '\n',' '), '\r',' '), '\t',' ') AS volume,
  weight,
  `rank`,
  launch_time,
  REPLACE(REPLACE(REPLACE(video_url, '\n',' '), '\r',' '), '\t',' ') AS video_url,
  REPLACE(REPLACE(REPLACE(add_url, '\n',' '), '\r',' '), '\t',' ') AS add_url,
  REPLACE(REPLACE(REPLACE(material, '\n',' '), '\r',' '), '\t',' ') AS material,
  img_num,
  REPLACE(REPLACE(REPLACE(img_type, '\n',' '), '\r',' '), '\t',' ') AS img_type,
  qa_num,
  REPLACE(REPLACE(REPLACE(brand, '\n',' '), '\r',' '), '\t',' ') AS brand,
  REPLACE(REPLACE(REPLACE(ac_name, '\n',' '), '\r',' '), '\t',' ') AS ac_name,
  REPLACE(REPLACE(REPLACE(node_id, '\n',' '), '\r',' '), '\t',' ') AS node_id,
  REPLACE(REPLACE(REPLACE(sp_num, '\n',' '), '\r',' '), '\t',' ') AS sp_num,
  REPLACE(REPLACE(REPLACE(mpn, '\n',' '), '\r',' '), '\t',' ') AS mpn,
  online_time,
  REPLACE(REPLACE(REPLACE(`describe`, '\n',' '), '\r',' '), '\t',' ') AS `describe`,
  one_star,
  two_star,
  three_star,
  four_star,
  five_star,
  low_star,
  asin_type,
  REPLACE(REPLACE(REPLACE(is_coupon, '\n',' '), '\r',' '), '\t',' ') AS is_coupon,
  REPLACE(REPLACE(REPLACE(search_category, '\n',' '), '\r',' '), '\t',' ') AS search_category,
  REPLACE(REPLACE(REPLACE(weight_str, '\n',' '), '\r',' '), '\t',' ') AS weight_str,
  REPLACE(REPLACE(REPLACE(account_name, '\n',' '), '\r',' '), '\t',' ') AS account_name,
  REPLACE(REPLACE(REPLACE(other_seller_name, '\n',' '), '\r',' '), '\t',' ') AS other_seller_name,
  REPLACE(REPLACE(REPLACE(bsr_date_info, '\n',' '), '\r',' '), '\t',' ') AS bsr_date_info,
  REPLACE(REPLACE(REPLACE(account_id, '\n',' '), '\r',' '), '\t',' ') AS account_id,
  REPLACE(REPLACE(REPLACE(package_quantity, '\n',' '), '\r',' '), '\t',' ') AS package_quantity,
  REPLACE(REPLACE(REPLACE(pattern_name, '\n',' '), '\r',' '), '\t',' ') AS pattern_name,
  REPLACE(REPLACE(REPLACE(together_asin, '\n',' '), '\r',' '), '\t',' ') AS together_asin,
  REPLACE(REPLACE(REPLACE(activity_type, '\n',' '), '\r',' '), '\t',' ') AS activity_type,
  one_two_val, 
  three_four_val, 
  five_six_val, 
  eight_val, 
  REPLACE(REPLACE(REPLACE(product_description, '\n',' '), '\r',' '), '\t',' ') AS product_description,
  follow_sellers,
  REPLACE(REPLACE(REPLACE(buy_sales, '\n',' '), '\r',' '), '\t',' ') AS buy_sales,
  image_view, 
  REPLACE(REPLACE(REPLACE(product_json, '\n',' '), '\r',' '), '\t',' ') AS product_json,
  REPLACE(REPLACE(REPLACE(productdetail_json, '\n',' '), '\r',' '), '\t',' ') AS productdetail_json,
  REPLACE(REPLACE(REPLACE(review_ai_text, '\n',' '), '\r',' '), '\t',' ') AS review_ai_text,
  REPLACE(REPLACE(REPLACE(review_label_json, '\n',' '), '\r',' '), '\t',' ') AS review_label_json,
  REPLACE(REPLACE(REPLACE(lob_asin_json, '\n',' '), '\r',' '), '\t',' ') AS lob_asin_json,
  REPLACE(REPLACE(REPLACE(sp_initial_seen_asins_json, '\n',' '), '\r',' '), '\t',' ') AS sp_initial_seen_asins_json,
  REPLACE(REPLACE(REPLACE(sp_4stars_initial_seen_asins_json, '\n',' '), '\r',' '), '\t',' ') AS sp_4stars_initial_seen_asins_json,
  REPLACE(REPLACE(REPLACE(sp_delivery_initial_seen_asins_json, '\n',' '), '\r',' '), '\t',' ') AS sp_delivery_initial_seen_asins_json,
  REPLACE(REPLACE(REPLACE(compare_similar_asin_json, '\n',' '), '\r',' '), '\t',' ') AS compare_similar_asin_json,
  REPLACE(REPLACE(REPLACE(customer_reviews_json, '\n',' '), '\r',' '), '\t',' ') AS customer_reviews_json,
  REPLACE(REPLACE(REPLACE(together_asin_json, '\n',' '), '\r',' '), '\t',' ') AS together_asin_json,
  REPLACE(REPLACE(REPLACE(min_match_asin_json, '\n',' '), '\r',' '), '\t',' ') AS min_match_asin_json,
  REPLACE(REPLACE(REPLACE(seller_json, '\n',' '), '\r',' '), '\t',' ') AS seller_json,
  `returns`,
  created_at, updated_at,
  REPLACE(REPLACE(REPLACE(result_list_json, '\n',' '), '\r',' '), '\t',' ') AS result_list_json,
  REPLACE(REPLACE(REPLACE(variat_list, '\n',' '), '\r',' '), '\t',' ') AS variat_list,
  REPLACE(REPLACE(REPLACE(bundle_asin_component_json, '\n',' '), '\r',' '), '\t',' ') AS bundle_asin_component_json,
  REPLACE(REPLACE(REPLACE(cart_type, '\n',' '), '\r',' '), '\t',' ') AS cart_type, 
  REPLACE(REPLACE(REPLACE(site, '\n',' '), '\r',' '), '\t',' ') AS site,
  REPLACE(REPLACE(REPLACE(comment_json, '\n',' '), '\r',' '), '\t',' ') AS comment_json

FROM {import_table}
WHERE  date_info='{date_info}' and 1=1 AND \$CONDITIONS
"""

#   spider_int,
#   variat_num,
#   REPLACE(REPLACE(REPLACE(img_list, '\n',' '), '\r',' '), '\t',' ') AS img_list,
#   REPLACE(REPLACE(REPLACE(parent_asin, '\n',' '), '\r',' '), '\t',' ') AS parent_asin,
#   REPLACE(REPLACE(REPLACE(video_m3u8_url, '\n',' '), '\r',' '), '\t',' ') AS video_m3u8_url,
#   REPLACE(REPLACE(REPLACE(review_json_list, '\n',' '), '\r',' '), '\t',' ') AS review_json_list,
#   fbm_delivery_price

    engine.sqoop_raw_import(
        # query=f"SELECT {cols} FROM {import_table} WHERE 1=1 and \$CONDITIONS",
        query=query,
        hive_table=hive_table,
        # hdfs_path=hdfs_path,
        partitions=partition_dict,
        # m=50,
        # split_by='id'
    )

