"""
1、随机生成11位手机号  前3位+8位
2、进行数据校验
"""
import random

from Common.handle_db import HandleDB

db = HandleDB()


# 获取一个没有注册的手机号码
def get_new_phone():
    # 1、获取手机号码
    mobile_phone = __generator_phone()
    # 2、对手机号码进行校验
    while True:
        data = check_phone_in_db(mobile_phone)
        if data == 0:
            return mobile_phone
        else:
            print("该手机号码已经注册了！")


# 随机生产一个手机号码
def __generator_phone():
    prefix = [133, 153, 177, 180, 181, 189, 130, 131, 132, 145, 155, 156, 176, 185, 186,
              134, 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 178, 182, 183, 184, 187, 188
              ]
    eight = ''
    for i in range(0, 8):
        eight = eight + str(random.randint(0, 9))
    mobile_phone = int(str(random.choice(prefix)) + str(eight))
    return mobile_phone


# 通过随机生产的号码去数据库进行查询
def check_phone_in_db(mobile_phone):
    sql = 'SELECT * FROM FlowMicros.micros_customer WHERE phone = "{}";'.format(mobile_phone)
    data = db.select_count(sql)
    return data


# print(get_new_phone())
