# -*- coding:UTF-8 -*-
# @Time  : 2020/11/25 10:51
# @File  : func_get_uesr_event.py
# @email : litao@igengmei.com
# @author : litao
import pymysql


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

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


def get_user_event_from_mysql(keyword,timestamp=None):
    if not timestamp:
        sql_str = """select cl_id, from_unixtime(log_time) as date, event_cn, projects from kafka_tag3_log
      where cl_id = '%s'""" % keyword
        sql_result = con_sql(sql_str)
        for data in sql_result:
            clid,date,event_cn,projects = data
            yield event_cn,projects
    else:
        tomorrow_timestamp = timestamp + 86400
        sql_str = """select cl_id, from_unixtime(log_time) as date, event_cn, projects from kafka_tag3_log
              where cl_id = '%s' and log_time < %s and log_time > %s""" % (keyword,str(tomorrow_timestamp),str(timestamp))
        sql_result = con_sql(sql_str)
        for data in sql_result:
            clid, date, event_cn, projects = data
            yield event_cn, projects