import pymysql


class DataBase:

    def __init__(self):
        self.ip_address = "rm-2zewoy4d914af4lepqo.mysql.rds.aliyuncs.com"
        self.admin_id = "test_account"
        self.admin_password = "QWer1234"
        self.using_database = "ai_database"     
        self.database = pymysql.connect(host=self.ip_address, port=3306, user=self.admin_id, password=self.admin_password, database=self.using_database)
        self.cursor = self.database.cursor()
    
    def user_register(self):
        """用户注册"""
        while True:
            account=str(input("请注册，输入用户名："))
            password=eval(input("请输入密码："))
            #判断密码长度是否在6-16位之间
            if 5<len(password)<17:
                self.cursor.execute("select user_name from user_information")
                name_list = []
                #判断用户名是否在表中
                for name in self.cursor.fetchall():
                    name_list.append(name)
                if account in name_list:
                    print("用户名已存在！")
                else:
                    self.cursor.execute("insert into user_information (user_name,user_password) values (account,password)" )
                    print("注册成功！")
                self.database.commit()
                break
            else:
                print("密码设置不合法！")
                continue

    def user_signin(my_user,my_password):
        """用户登录"""
        while True:
            my_user = input("请输入用户名：")
            my_password = eval(input("请输入密码："))
        #创建列表用于判段用户名密码是否在表中以及是否对应
        my_list = []
        my_user_list = []
        my_password_list = []
        #连接数据库
        db = pymysql.connect(host=ip_address, port=3306, user=admin_id, password=admin_password, database=using_database)
        u2 = db.cursor()
        u2.execute("select user_name,user_password from user_information")
        data2 = u2.fetchall()
        u2.close()
        db.close()
        
        for item in data2:
            my_list.append(item)
            #用户数据列表
            my_user_list.append(item[1])
            #密码数据列表
            my_password_list.append(item[2])
            
        for i,list in enumerate(my_list):
            #判断用户名是否在表中
            if my_user in my_user_list:
                #判断密码是都在表中
                if my_password in my_password_list:
                    #判断用户名与密码是否对应
                    for list in my_list:
                        if my_user_list.index(my_user) == my_password_list.index(my_password):
                            print("登陆成功！")
                        else:
                            print("密码错误！")
                else:
                    print("密码错误！")
            
            else:
                print("用户名错误！")
                
#群组注册
def creat_group():
    while True:
        a = str(input('请输入想要创建的群组名：'))
        b = str(input('请输入创建者用户名：'))
        #连接数据库
        db = pymysql.connect(host=ip_address, port=3306, user=admin_id, password=admin_password, database=using_database)
        u1 = db.cursor()
        u1.execute("select group_name from group_information")
        data1 = u1.fetchall()
        u2 = db.cuisor()
        u2.execute('select user_name from user_information')
        data2 = u2.fetchall()
        group_lst = []
        name_lst = []
        #判断用户名是否在表中
        for group in data1:
            group_lst.append()
        for name in data2:
            name_lst.append()
        if a in group_lst:
            print("群组已存在！")
        elif b in name_lst:
            print('用户名不在范围内！')
        else:
            u1.execute("insert into group_information (group_name,group_creator_id) values (a,b)" )
            print("创建成功！")
        db.commit()
        u1.close()
        u2.close()
        db.close()
      
        
    
    
                
                
                
                
                
                
                
                
                
                
            