81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
import configparser
|
|
import pandas as pd
|
|
from sqlalchemy import create_engine
|
|
from urllib.parse import quote_plus
|
|
# 读取配置文件
|
|
config = configparser.ConfigParser()
|
|
config.read('config.ini')
|
|
|
|
# 获取数据库连接配置
|
|
source_config = {
|
|
'host': config.get('source_db', 'host'),
|
|
'user': config.get('source_db', 'user'),
|
|
'password': config.get('source_db', 'password'),
|
|
'database': config.get('source_db', 'database'),
|
|
'port': config.getint('source_db', 'port')
|
|
}
|
|
|
|
target_config = {
|
|
'host': config.get('target_db', 'host'),
|
|
'user': config.get('target_db', 'user'),
|
|
'password': config.get('target_db', 'password'),
|
|
'database': config.get('target_db', 'database'),
|
|
'port': config.getint('target_db', 'port')
|
|
}
|
|
|
|
# 创建数据库引擎
|
|
source_engine = create_engine(
|
|
f"mysql+pymysql://{source_config['user']}:{quote_plus(source_config['password'])}@{source_config['host']}:{source_config['port']}/{source_config['database']}"
|
|
)
|
|
target_engine = create_engine(
|
|
f"mysql+pymysql://{target_config['user']}:{quote_plus(target_config['password'])}@{target_config['host']}:{target_config['port']}/{target_config['database']}"
|
|
)
|
|
|
|
# 定义COMPANY_ID到imp_unit的映射
|
|
company_mapping = {
|
|
5: 340, # 安徽顺全电力工程有限公司
|
|
4: 102, # 送电二分公司
|
|
3: 327, # 送电一分公司
|
|
6: 101, # 机具(物流)分公司
|
|
7: 346, # 运检分公司
|
|
8: 338, # 建筑分公司
|
|
9: 309, # 安徽宏源电力建设有限公司
|
|
10: 347, # 公司培训中心(宏源工业园)
|
|
11: 337, # 检修试验分公司
|
|
12: 100, # 变电分公司
|
|
13: 348, # 预制构件分公司
|
|
14: 344, # 机械化分公司
|
|
15: 342, # 公司机关
|
|
16: 345, # 外单位租赁业务
|
|
17: 339, # 安徽顺安电网建设有限公司
|
|
18: 341 # 班组管理中心
|
|
}
|
|
|
|
|
|
def process_bm_project():
|
|
"""处理bm_project表数据"""
|
|
# 读取源数据
|
|
df = pd.read_sql("SELECT * FROM bm_project", source_engine)
|
|
|
|
# 按照规则转换数据
|
|
result = pd.DataFrame()
|
|
result['pro_id'] = df['ID'] + 3000 # ID加3000
|
|
result['pro_name'] = df['NAME'] # 直接复制
|
|
result['pro_code'] = df['CODE'] # 直接复制
|
|
result['external_id'] = df['PRO_ID'] # 直接复制
|
|
result['create_time'] = df['CREATE_TIME'] # 直接复制
|
|
result['imp_unit'] = df['COMPANY'].map(company_mapping) # 替换映射
|
|
|
|
# 写入目标表
|
|
try:
|
|
result.to_sql('bm_project', target_engine,
|
|
if_exists='append', index=False)
|
|
print(f"成功转换并导入 {len(result)} 条记录到 bm_project")
|
|
return True
|
|
except Exception as e:
|
|
print(f"处理 bm_project 时发生错误: {str(e)}")
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
process_bm_project() |