python">import mysql. connector
from mysql. connector import Error
from datetime import datetime
db_config = { 'host' : 'xxx' , 'user' : 'xxx' , 'password' : 'xxx' , 'database' : 'xxx' , 'port' : 3306
}
current_id = 745689123879789456
current_time = datetime. now( ) . strftime( '%Y-%m-%d %H:%M:%S' )
def create_connection ( ) : connection = None try : connection = mysql. connector. connect( ** db_config) if connection. is_connected( ) : print ( "连接 MySQL 数据库成功" ) except Error as e: print ( f"Error: ' { e} '" ) return connection
def get_id_path ( cursor, org_id) : id_path = [ ] name_path = [ ] while org_id: query = """SELECT id, name, parent_idFROM t_departWHERE id = %s""" cursor. execute( query, ( org_id, ) ) result = cursor. fetchone( ) if result: current_id, name, parent_id = resultid_path. insert( 0 , current_id) name_path. insert( 0 , name) org_id = parent_id else : break return id_path, name_path
def get_org_id_by_code ( cursor, code) : query = """SELECT idFROM t_departWHERE code = %s""" cursor. execute( query, ( code, ) ) result = cursor. fetchone( ) return result[ 0 ] if result else None
def generate_insert_sql ( identity_id, code_list, cursor) : global current_id global current_time scope_entries = [ ] display = [ ] display_label = [ ] for code in code_list: org_id = get_org_id_by_code( cursor, code) if org_id: id_path, name_path = get_id_path( cursor, org_id) level = len ( id_path) scope_entries. append( f'{{"scopeKey":" { org_id} ","brandCode":"","uniqueCode":"","level":" { level} ","type":""}}' ) display. append( id_path) display_label. append( name_path) scope_value = f'[ { "," . join( scope_entries) } ]' display_value = f' { display} ' . replace( "'" , "\"" ) display_label_value = f' { display_label} ' . replace( "'" , "\"" ) insert_sql = f"""INSERT INTO `ram`.`t_data_scope` (`id`, `app_id`, `identity_id`, `scope_type`, `scope_value`, `scope_operating_type`, `status`, `remark`, `create_time`, `create_by`, `update_time`, `update_by`, `deleted`, `version`, `delete_time`, `display`, `display_label`) VALUES (' { current_id} ', '10000', ' { identity_id} ', 'depart', ' { scope_value} ', '行政组织', 1, NULL, ' { current_time} ', 'script', ' { current_time} ', 'script', 0, NULL, NULL, ' { display_value} ', ' { display_label_value} ');""" current_id += 1 return insert_sql
def process_collect_data ( connection) : cursor = connection. cursor( ) cursor. execute( "SELECT identity_id, code FROM ram.t_depart_collect where exist = 1" ) rows = cursor. fetchall( ) with open ( "initDepartDataScope_SIT.sql" , 'w' , encoding= 'utf-8' ) as file : for row in rows: identity_id, code_str = rowcode_list = code_str. split( '、' ) delete_sql = f"""DELETE FROM `ram`.`t_data_scope` WHERE scope_type = 'depart' AND identity_id = ' { identity_id} ';""" print ( delete_sql) file . write( delete_sql + '\n' ) insert_sql = generate_insert_sql( identity_id, code_list, cursor) print ( insert_sql) file . write( insert_sql + '\n' ) cursor. close( )
if __name__ == "__main__" : connection = create_connection( ) if connection is not None and connection. is_connected( ) : try : process_collect_data( connection) finally : connection. close( ) print ( "数据库连接已关闭" )