import pymysql
from datetime import datetime
import traceback
from noteparse.configReader import readConfig
import atexit

db_config = readConfig('host','port','user','password','db',section='dbConfig')


# 创建数据库连接
def create_connection():
    try:
        print(datetime.now(),'开启数据库链接')
        # 连接数据库
        connection = pymysql.connect(host=db_config['host'], port=int(db_config['port']),
                        user=db_config['user'], password=db_config['password'],
                        db=db_config['db'])
        print(datetime.now(),'数据库链接成功')
    except pymysql.MySQLError as e:
        print(datetime.now(),'数据库链接失败',e)
    return connection

# 确保在程序退出时关闭数据库连接
def close_connection():
    if connection and connection.open:
        print(datetime.now(),'关闭SQL连接')
        cursor.close()
        connection.close()
        
connection = create_connection()
cursor = connection.cursor()

atexit.register(close_connection)
# 通过城市名称查询城市信息
def queryCityInfo(cityName):
    query = f'''
    (SELECT 
        a.areaid,
        a.area,
        c.cityid,
        c.city,
        p.provinceid,
        p.province
    FROM 
        unvbasicx_khgz.areas a
    JOIN 
        unvbasicx_khgz.cities c ON a.cityid = c.cityid
    JOIN 
        unvbasicx_khgz.provinces p ON c.provinceid = p.provinceid
    WHERE 
        a.area like '%{cityName}%')
    UNION ALL
    (SELECT 
        '' AS areaid,
        '' AS area,
        c.cityid,
        c.city,
        p.provinceid,
        p.province
    FROM 
        unvbasicx_khgz.cities c
    JOIN 
        unvbasicx_khgz.provinces p ON c.provinceid = p.provinceid
    WHERE 
        c.city like '%{cityName}%'
        AND NOT EXISTS (
            SELECT 1 FROM unvbasicx_khgz.areas WHERE area like '%{cityName}%'
        )
    )
    UNION ALL
    (SELECT 
        '' AS areaid,
        '' AS area,
        '' AS cityid,
        '' AS city,
        p.provinceid,
        p.province
    FROM 
        unvbasicx_khgz.provinces p
    WHERE 
        p.province like '%{cityName}%'
        AND NOT EXISTS (
            SELECT 1 FROM unvbasicx_khgz.cities WHERE city like '%{cityName}%'
        )
        AND NOT EXISTS (
            SELECT 1 FROM unvbasicx_khgz.areas WHERE area like '%{cityName}%'
        )
    )
    LIMIT 1
    '''
    if connection == None:
        create_connection()
    try:
        connection.ping(reconnect=True)
        cursor.execute(query)
        result = cursor.fetchall()  # 获取所有查询结果
        return result
    except Exception as e:
        print(datetime.now(),'通过城市名查询城市信息异常',e)
        traceback.print_exc()



# 通过城市名称查询城市信息
def queryCityInfoByAreaId(cityCode):
    query = f'''
    SELECT 
        a.areaid,
        a.area,
        c.cityid,
        c.city,
        p.provinceid,
        p.province
    FROM 
        unvbasicx_khgz.areas a
    JOIN 
        unvbasicx_khgz.cities c ON a.cityid = c.cityid
    JOIN 
        unvbasicx_khgz.provinces p ON c.provinceid = p.provinceid
    WHERE 
        a.areaid like '%{cityCode}%'
    
    LIMIT 1
    '''
    if connection == None:
        create_connection()
    try:
        # 先检查是否端口,断开就重连
        connection.ping(reconnect=True)
        cursor.execute(query)
        result = cursor.fetchall()  # 获取所有查询结果
        return result
    # except pymysql.OperationalError:
    #     connection = create_connection()
    #     cursor = connection.cursor()
    #     cursor.execute(query)
    #     result = cursor.fetchall()  # 获取所有查询结果
    #     return result
    except Exception as e:
        print(datetime.now(),'通过城市code查询城市信息异常',e)
        traceback.print_exc()