from db_util import DBUtil
import datetime
import time

current_day = datetime.date.today()
db = DBUtil('localhost', 3306, 'root', 'root', '李森')
def login_menu():
    '''登录菜单'''
    print('#'*40)
    print('1.登录')
    print('2.注册')
    print('#'*40)

def login(name,pwd):
    '''登录功能'''
    # 创建数据库对象
    db = DBUtil('localhost', 3306, 'root', 'root', '李森')
    sql ="select * from users where uname =%s and pwd =%s"
    params =[name,pwd]
    result =db.get_one(sql,params)
    return result
'''注册'''
def register(uname,pwd):
    db = DBUtil("localhost",3306,"root",'root','李森')
    sql = "insert into users(uname,pwd) values(%s,%s)"
    params=(uname,pwd)
    num = db.my_execute(sql,params)
    return num

def sys_menu():
    '''系统菜单'''
    print('*'*40)
    print('1.生成今日体温记录')
    print('2.根据名字修改体温记录')
    print('3.根据日期查询统计')
    print('*'*40)

    num = int(input('请选择菜单:'))
    if num==1:
        gener_jilu()
    elif num ==2:
        update_jilu()
    elif num ==3:
        mycount()  #统计
    else:
        print('输入错误!')

def gener_jilu():
    print(f'生成:{current_day}体温记录!')
    # 1.创建数据库对象
    db = DBUtil('localhost', 3306, 'root', 'root', '李森')

    # 判断是否生成过
    reselt = db.get_one('select * from flag where create_time=%s',(current_day))

    if reselt is None:
        try:
            student_1 = db.get_all('select * from student_1',None)
            # 3.生成记录
            list_jilu = []
            for stu in student_1:
                jilu = (stu[0], current_day, 1, '正常')
                list_jilu.append(jilu)


                # 4.调用批量插入
            sql = 'insert into jilu(sid,create_time,status,shuoming) values(%s,%s,%s,%s)'
            db.my_executemany(sql, list_jilu)
                # 生成一条标记!
            db.my_execute('insert into flag(create_time,flag) values(%s,%s)', (current_day, 1))
            print('无错误生成成功!')
        except Exception as e:
                print(e)
    else:
        print('今日已生成!')

def update_jilu():
    print('根据名字修改记录资料')
    name = input("请输入体温异常的人名字")
    status = input("输入体温状态(1,正常2,异常,3缺失")
    shuoming = input("请输入说明")
    temp = input('请输入体温:')

    try:

        # 根据名字查学生id
        sql = 'select id from student_1 where name = %s '
        result = db.get_one(sql,[name])
        sid = result[0]
        # 2改
        sql2= 'update jilu set status =%s,shuoming = %s where sid=%s and create_time =%s'
        params = (status,shuoming,temp,sid,current_day)
        db.my_execute(sql2, params)
        print('修改成功')
    except Exception as e:
        print(e)


def mycount():
    print('统计!')
    current_day = input('请输入日期(xxxx-xx-xx):')
    sql = 'select status,count(*) from jilu where create_time =%s group by status '
    result = db.get_all(sql, (current_day))
    print(f'日期:{current_day}')
    print('状态1:正常  2.异常 3缺失!')
    for item in result:
        print(f'状态:{item[0]}\t,人数:{item[1]}')

if __name__ == '__main__':
    # 1.显示菜单

    login_menu()
    num = int(input('请选择菜单:'))
    if num ==1:
        name = input('请输入名字:')
        pwd = input('请输入密码:')
        if login(name, pwd) is not None:
            print('登录成功;')
            sys_menu()
        else:
            print('用户名或密码错误!')
    elif num ==2:
        print('注册功能')
        print("实现注册")
        uname = input("请输入用户名")
        pwd = input("请输入密码")
        if register(uname, pwd) > 0:
            print("注册成功")
        else:
            print("注册失败")

    else:
        print('输入有误!')