import pymysql
import datetime
import json
import redis


def get_esmm_users():
    db = pymysql.connect(host='172.16.40.158', port=4000, user='root',
                         passwd='3SYz54LS9#^9sBvC', db='jerry_prod')
    cursor = db.cursor()
    stat_date = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    print(stat_date)
    sql = "select distinct device_id,city_id from data_feed_exposure_precise " \
          "where stat_date = '{}'".format(stat_date)
    cursor.execute(sql)
    result = list(cursor.fetchall())
    return result


def get_user_profile():
    r = redis.Redis(host="172.16.40.135", port=5379, password="", db=2)
    cl_id = "9C5E7C73-380C-4623-8F48-A64C8034E315"
    key = "user:portrait_tags:cl_id:" + str(cl_id)
    tmp = json.loads(r.get(key).decode('utf-8'))
    # todo 找赵晨把search word映射成tag
    tags = {}
    for i in tmp:
        tags[i["content"]] = i["score"]
    tags = sorted(tags.items(), key=lambda x: x[1], reverse=True)
    tag_score = {}
    a = tags.keys()
    print(a)
    if len(tmp) > 5:
        for i in list(a)[:5]:
            tag_score[i] = tags[i]
        print(tag_score)
        return tag_score
    else:
        print(tags)
        return tags









def transform_to_tag(user_profile):
    pass


def get_queues(device_id,city_id):
    pass


def tag_boost(cid_list, tag_score):
    pass


def to_data_base(df,table_name = "tag_boost_device_diary_queue"):
    pass


def make_sample(uid,city_id,native_queue,nearby_queue,megacity_queue,nation_queue):
    pass


if __name__ == "__main__":
    # users_list = get_esmm_users()
    # total_samples = list()
    # for i in users_list:
    #     tag_score = get_user_profile(i[0])
    #     native,nearby,megacity,nation = get_queues(i[0],i[1])
    #
    #     native_sort_list = tag_boost(native, tag_score)
    #     nearby_sort_list = tag_boost(nearby, tag_score)
    #
    #     sample = make_sample(uid,city_id,native_queue,nearby_queue,megacity_queue,nation_queue)
    #     total_samples.append(sample)
    #
    # total_samples.todf
    # to_data_base(df)
    get_user_profile()













