def query_sbi(sbi):
    # 取得 session 物件對象:
    session = util.get_db_session()
    # 查詢 -----------------------------------------------------
    youbikes = session.query(Youbike).filter(Youbike.sbi >= sbi).all()
    # ----------------------------------------------------------
    # 關閉 session:
    session.close()
    return youbikes
def query_bemp(bemp):
    # 取得 session 物件對象:
    session = util.get_db_session()
    # 查詢 -----------------------------------------------------
    youbikes = session.query(Youbike).filter(Youbike.bemp >= bemp).all()
    # ----------------------------------------------------------
    # 關閉 session:
    session.close()
    return youbikes
def query_by(sno_sna):
    # 取得 session 物件對象:
    session = util.get_db_session()
    # 查詢 -----------------------------------------------------
    youbikes = session.query(Youbike).filter(or_(Youbike.sno.like('%' + sno_sna + '%'), Youbike.sna.like('%' + sno_sna + '%'))).all()
    # ----------------------------------------------------------
    # 關閉 session:
    session.close()
    return youbikes
def query_all():
    # 取得 session 物件對象:
    session = util.get_db_session()
    # 查詢 -----------------------------------------------------
    youbikes = session.query(Youbike).all()
    # ----------------------------------------------------------
    # 關閉 session:
    session.close()
    return youbikes
def delete_all():
    # 取得 session 物件對象:
    session = util.get_db_session()
    try:
        session.query(Youbike).delete()
        session.commit()
    except Exception as e:
        print(e)

    session.close()
def query_sbi_bemp(sbi, bemp):
    # 取得 session 物件對象:
    session = util.get_db_session()
    # 查詢 -----------------------------------------------------
    youbikes = session.query(Youbike).filter(
        and_(Youbike.sbi >= sbi, Youbike.bemp >= bemp)
    ).all()
    # ----------------------------------------------------------
    # 關閉 session:
    session.close()
    return youbikes
def import_data():
    # 取得所有資料
    list = util.get_youbike_list()
    # 取得 session 物件對象:
    session = util.get_db_session()
    # 新增 ------------------------------------------------------
    for youbike in list:
        new_youbike = Youbike(
            sno = youbike.get('sno'),
            sna = youbike.get('sna'),
            tot = youbike.get('tot'),
            sbi = youbike.get('sbi'),
            bemp = youbike.get('bemp'),
            lat = youbike.get('lat'),
            lng = youbike.get('lng')
        )
        session.add(new_youbike)

    # 提交到資料庫中儲存:
    session.commit()
    # 關閉 session:
    session.close()

if __name__ == '__main__':

    t = threading.Thread(target=sched)
    t.start()

    while True:
        menu()
        choice = int(input('請輸入您的選擇 : '))
        if choice == 1:
            dao.delete_all()
            dao.import_data()
        elif choice == 2:
            youbikes = dao.query_all()
            util.print_youbike(youbikes)
        elif choice == 3:
            youbikes = dao.query_by(input('請輸入站號或站名 : '))
            util.print_youbike(youbikes)
        elif choice == 4:
            youbikes = dao.query_sbi(int(input('我要借(台) : ')))
            util.print_youbike(youbikes)
        elif choice == 5:
            youbikes = dao.query_bemp(int(input('我要還(台) : ')))
            util.print_youbike(youbikes)
        elif choice == 6:
            sbi, bemp = input('我要借(台), 還(台): 例如:30 30 => ').split()
            youbikes = dao.query_sbi_bemp(int(sbi), int(bemp))
            util.print_youbike(youbikes)
        elif choice == 0:
            break