# -*- 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


bulk_dict = {
    0: [0, 0, 0],
    10: [0, 0, 0],
    50: [0, 0, 0],
    100: [0, 0, 0],
    200: [0, 0, 0],
    500: [0, 0, 0],
    1000: [0, 0, 0],
}
task_list = []
user_portrait_scan = user_portrait_scan_info()
for res in user_portrait_scan:
    second_demands = []
    projects = []
    total_answer_content_num = 0
    total_tractate_content_num = 0
    total_diary_content_num = 0
    if res.get("second_demands"):
        second_demands = res.get("second_demands")
        count_res = get_es_article_num({"second_demands": second_demands[0:3]}, allow_tag=["second_demands"])
        print(count_res)
        for article_type in count_res:
            for tag in count_res[article_type]:
                for key in tag:
                    total_answer_content, total_tractate_content, total_diary_content, sum_num = \
                        tag[key]
                    total_answer_content_num += total_answer_content
                    total_tractate_content_num += total_tractate_content
                    total_diary_content_num += total_diary_content
    if res.get("projects"):
        projects = res.get("projects")
        count_res = get_es_article_num({"tags_v3": projects[0:3]}, allow_tag=["tags_v3"])
        print(count_res)
        for article_type in count_res:
            for tag in count_res[article_type]:
                for key in tag:
                    total_answer_content, total_tractate_content, total_diary_content, sum_num = \
                        tag[key]
                    total_answer_content_num += total_answer_content
                    total_tractate_content_num += total_tractate_content
                    total_diary_content_num += total_diary_content
    print(total_answer_content_num, total_tractate_content_num, total_diary_content_num)
    tmp_count_num = 0

    if 0 <= total_answer_content_num < 10:
        bulk_dict[0][0] += 1
    elif 10 <= total_answer_content_num < 50:
        bulk_dict[10][0] += 1
    elif 50 <= total_answer_content_num < 100:
        bulk_dict[50][0] += 1
    elif 100 <= total_answer_content_num < 200:
        bulk_dict[100][0] += 1
    elif 200 <= total_answer_content_num < 500:
        bulk_dict[200][0] += 1
    elif 500 <= total_answer_content_num < 1000:
        bulk_dict[500][0] += 1
    else:
        bulk_dict[1000][0] += 1

    if 0 <= total_tractate_content_num < 10:
        bulk_dict[0][1] += 1
    elif 10 <= total_tractate_content_num < 50:
        bulk_dict[10][1] += 1
    elif 50 <= total_tractate_content_num < 100:
        bulk_dict[50][1] += 1
    elif 100 <= total_tractate_content_num < 200:
        bulk_dict[100][1] += 1
    elif 200 <= total_tractate_content_num < 500:
        bulk_dict[200][1] += 1
    elif 500 <= total_tractate_content_num < 1000:
        bulk_dict[500][1] += 1
    else:
        bulk_dict[1000][1] += 1

    if 0 <= total_diary_content_num < 10:
        bulk_dict[0][2] += 1
    elif 10 <= total_diary_content_num < 50:
        bulk_dict[10][2] += 1
    elif 50 <= total_diary_content_num < 100:
        bulk_dict[50][2] += 1
    elif 100 <= total_diary_content_num < 200:
        bulk_dict[100][2] += 1
    elif 200 <= total_diary_content_num < 500:
        bulk_dict[200][2] += 1
    elif 500 <= total_diary_content_num < 1000:
        bulk_dict[500][2] += 1
    else:
        bulk_dict[1000][2] += 1

    print(bulk_dict)

for page_type in range(3):
    if page_type == 0:
        page_type_str = "回答"
    elif page_type == 1:
        page_type_str = "帖子"
    else:
        page_type_str = "日记"
    pid = str(page_type)
    instert_sql = """replace into portary_article_distribution(
    page_type,C0,C10,C50,C100,C200,C500,C1000,pid) VALUES('{page_type_str}',{C0},{C10},{C50},{C100},{C200},{C500},{C1000},'{pid}');""".format(
        page_type_str=page_type_str,C0=bulk_dict[0][page_type],C10=bulk_dict[10][page_type],C50=bulk_dict[50][page_type],
        C100=bulk_dict[100][page_type],C200=bulk_dict[200][page_type],C500=bulk_dict[500][page_type],
        C1000=bulk_dict[1000][page_type],pid=pid
    )
    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()
