import json
import pandas as pd
from sqlalchemy import create_engine


class mysqlUtil:
    __mysql_username = 'test'
    __mysql_password = 'test'
    # 填写真实数库ip
    __mysql_ip = 'home.hddly.cn'
    __port = 53306
    __db = 'test'

    def __init__(self):
        self.OpenDB(self.__db)

    def OpenDB(self, dbname):
        self.__db = dbname
        self.__mysql_ip = 'home.hddly.cn'
        self.__port = 53306
        self.engine = create_engine(
            'mysql+pymysql://{}:{}@{}:{}/{}'.format(self.__mysql_username,
                                                    self.__mysql_password,
                                                    self.__mysql_ip,
                                                    self.__port,
                                                    self.__db))

    def OpenDBYgxy(self, dbname):
        self.__db = dbname
        self.__mysql_ip = '10.255.10.51'
        self.__port = 3306
        self.engine = create_engine(
            'mysql+pymysql://{}:{}@{}:{}/{}'.format(self.__mysql_username,
                                                    self.__mysql_password,
                                                    self.__mysql_ip,
                                                    self.__port,
                                                    self.__db))

    def OpenDBAny(self, ip, port, dbname):
        self.__db = dbname
        self.__mysql_ip = ip
        self.__port = port
        self.engine = create_engine(
            'mysql+pymysql://{}:{}@{}:{}/{}'.format(self.__mysql_username,
                                                    self.__mysql_password,
                                                    self.__mysql_ip,
                                                    self.__port,
                                                    self.__db))

    # 查询mysql数据库
    def query(self, sql):
        df = pd.read_sql_query(sql, self.engine)
        # df = pandas.read_sql(sql,self.engine)     这种读取方式也可以

        # 返回dateframe格式
        return df

    def select_rand_db(self, types=None):
        if types:
            sql = "select ip,port,types from eie_ip where types='{}' order by rand() limit 1".format(types)
        else:
            sql = "select ip,port,types from eie_ip order by rand() limit 1 "
        df = pd.read_sql(sql, self.engine)
        results = json.loads(df.to_json(orient='records'))
        if results and len(results) == 1:
            return results[0]
        return None

    # 添加上传文件记录
    def sftp_file_ins(self, filename, url, stud, fdesc, fsize, duration, ftype,pid):
        try:
            df = pd.DataFrame(columns=['filename', 'url', 'stud', 'fdesc', 'fsize', 'duration', 'ftype','pid'])
            # print(df)
            row1 = pd.DataFrame(
                {'filename': filename, 'url': url, 'stud': stud, 'fdesc': fdesc, 'fsize': fsize, 'duration': duration,
                 'ftype': ftype,'pid':pid}, index=[1])
            # print(row1)
            # df=df.append(row1,ignore_index=True)
            df = pd.concat([df, row1], ignore_index=True)  # append过时，改用concat
            # print(df)
            df.to_sql("sftp_files", self.engine, if_exists='append', index=False)  # 'sftp_files'

        except Exception as ex:
            print("日志记录出现如下异常%s" % ex)

    # 获了上传文件记录
    def get_file_list(self, ftype):
        if ftype:
            ftype = str(ftype).lower()
            sql = 'select id,url,fdesc,fsize,duration,ftype,uptime,pid from sftp_files where ftype="{}" order by uptime desc limit 100'.format(
                ftype)
        else:
            ftype = ".mp4"
            sql ='select id,url,fdesc,fsize,duration,ftype,uptime,pid from sftp_files where ftype="{}" order by uptime desc limit 100'.format(
                ftype)
        connection = self.engine.connect()
        df = pd.read_sql(sql, con=connection)
        results = json.loads(df.to_json(orient='records'))
        if results and len(results) == 1:
            return results[0]
        elif results and len(results)>1:
            return results
        else:
            return None
        # 获了上传文件记录

    def get_files_bypid(self, pid):
        if pid:
            pid = str(pid).lower()
            sql = 'select id,url,fdesc,fsize,duration,ftype,uptime,pid from sftp_files where pid="{}" order by uptime desc limit 100'.format(
                pid)
        else:
            pid = "p023101".lower()
            sql ='select id,url,fdesc,fsize,duration,ftype,uptime,pid from sftp_files where pid="{}" order by uptime desc limit 100'.format(
                pid)
        connection = self.engine.connect()
        df = pd.read_sql(sql=sql,con=connection)
        results = json.loads(df.to_json(orient='records'))
        if results and len(results) == 1:
            return results[0]
        elif results and len(results) > 1:
            return results
        else:
            return None

    def process_item(self, item, tblname):
        data = pd.DataFrame(dict(item), index=[0])
        data.to_sql(tblname, self.engine, if_exists='append', index=False)  # 'taobao_data'
        return item
