# -*- coding:UTF-8 -*-
# @Time  : 2020/9/11 10:59
# @File  : portary_article_distribution.py
# @email : litao@igengmei.com
# @author : litao


import hashlib
import json

import pymysql
import xlwt, datetime
import redis
from meta_base_code.utils.func_from_redis_get_portrait import user_portrait_scan_info
from meta_base_code.utils.func_from_es_get_article import get_es_article_num
# from pyhive import hive
from maintenance.func_send_email_with_file import send_file_email
from typing import Dict, List
from elasticsearch_7 import Elasticsearch
from elasticsearch_7.helpers import scan
import sys
import time
from pyspark import SparkConf
from pyspark.sql import SparkSession, DataFrame


# from pyspark.sql.functions import lit
# import pytispark.pytispark as pti


def con_sql(sql):
    # 从数据库的表里获取数据

    db = pymysql.connect(host='172.16.40.158', port=4000, user='st_user', passwd='aqpuBLYzEV7tML5RPsN1pntUzFy',
                         db='jerry_prod')
    cursor = db.cursor()
    cursor.execute(sql)
    result = cursor.fetchall()
    db.close()
    return result


task_list = []
user_portrait_scan = user_portrait_scan_info()
for res in user_portrait_scan:
    second_demands = []
    projects = []
    if res.get("second_demands"):
        second_demands = res.get("second_demands")
        count_res = get_es_article_num({"second_demands": second_demands},allow_tag=["second_demands"])
        print(count_res)
    if res.get("projects"):
        projects = res.get("projects")
        count_res = get_es_article_num({"tags_v3": projects}, allow_tag=["tags_v3"])
        print(count_res)

#     for active_type in res_dict:
#         for device_os_type in res_dict[active_type]:
#             partition_date = yesterday_str
#             pid = hashlib.md5((partition_date + device_os_type + active_type).encode("utf8")).hexdigest()
#             click_num = res_dict[active_type][device_os_type]["click_num"]
#             exposure = res_dict[active_type][device_os_type]["exposure"]
#             try:
#                 search_ctr = round(click_num / exposure, 5)
#             except:
#                 search_ctr = 0
#             instert_sql = """replace into search_tractate_ctr(
#             partition_date,device_os_type,active_type,pid,click_num,exposure,search_ctr) VALUES('{partition_date}','{device_os_type}','{active_type}','{pid}',{click_num},{exposure},{search_ctr});""".format(
#                 partition_date=partition_date, device_os_type=device_os_type, active_type=active_type, pid=pid, click_num=click_num,
#                 exposure=exposure, search_ctr=search_ctr
#             )
#             print(instert_sql)
#             # cursor.execute("set names 'UTF8'")
#             db = pymysql.connect(host='172.16.40.158', port=4000, user='st_user', passwd='aqpuBLYzEV7tML5RPsN1pntUzFy',
#                                  db='jerry_prod')
#             cursor = db.cursor()
#             res = cursor.execute(instert_sql)
#             db.commit()
#             print(res)
# # cursor.executemany()
# db.close()
