142 lines
4.4 KiB
Python
142 lines
4.4 KiB
Python
import configparser
|
||
import pymysql
|
||
import pandas as pd
|
||
from sqlalchemy import create_engine
|
||
from urllib.parse import quote_plus
|
||
# 读取配置文件
|
||
config = configparser.ConfigParser()
|
||
config.read('config.ini')
|
||
|
||
# 从配置文件获取数据库连接信息
|
||
source_db_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_db_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')
|
||
}
|
||
|
||
# 创建SQLAlchemy引擎
|
||
source_engine = create_engine(
|
||
f"mysql+pymysql://{source_db_config['user']}:{quote_plus(source_db_config['password'])}@{source_db_config['host']}:{source_db_config['port']}/{source_db_config['database']}"
|
||
)
|
||
target_engine = create_engine(
|
||
f"mysql+pymysql://{target_db_config['user']}:{quote_plus(target_db_config['password'])}@{target_db_config['host']}:{target_db_config['port']}/{target_db_config['database']}"
|
||
)
|
||
|
||
# 定义替换映射(可以从规则文件中读取,这里保持硬编码)
|
||
type_id_mapping = {
|
||
'项目部': 36,
|
||
'施工队': 33,
|
||
'分包商': 32,
|
||
'后勤科室': 1685,
|
||
'外单位': 1704,
|
||
'修试部门': 1706
|
||
}
|
||
|
||
dept_id_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_dept():
|
||
"""处理bm_project_dept表数据"""
|
||
df = pd.read_sql("SELECT * FROM bm_project_dept", source_engine)
|
||
|
||
result = pd.DataFrame()
|
||
result['unit_id'] = df['ID'] + 4000
|
||
result['unit_name'] = df['NAME']
|
||
result['type_id'] = 36 # 项目部固定值
|
||
result['dept_id'] = df['COMPANY_ID'].map(dept_id_mapping)
|
||
result['create_time'] = df['CREATE_TIME']
|
||
|
||
return result
|
||
|
||
|
||
def process_bm_team_info():
|
||
"""处理bm_team_info表数据"""
|
||
df = pd.read_sql("SELECT * FROM bm_team_info where IS_ACTIVE = 1", source_engine)
|
||
|
||
result = pd.DataFrame()
|
||
result['unit_id'] = df['ID'] + 5000
|
||
result['unit_name'] = df['NAME']
|
||
result['type_id'] = 33 # 施工队固定值
|
||
result['dept_id'] = df['COMPANY_ID'].map(dept_id_mapping)
|
||
result['create_time'] = df['CREATE_TIME']
|
||
|
||
return result
|
||
|
||
|
||
def process_bm_sub_contractor_info():
|
||
"""处理bm_sub_contractor_info表数据"""
|
||
df = pd.read_sql("SELECT * FROM bm_sub_contractor_info", source_engine)
|
||
|
||
result = pd.DataFrame()
|
||
result['unit_id'] = df['ID'] + 6000
|
||
result['unit_name'] = df['NAME']
|
||
result['type_id'] = 32 # 分包商固定值
|
||
# 注意:此表没有dept_id字段
|
||
|
||
return result
|
||
|
||
|
||
def process_bm_rear_service():
|
||
"""处理bm_rear_service表数据"""
|
||
df = pd.read_sql("SELECT * FROM bm_rear_service", source_engine)
|
||
|
||
result = pd.DataFrame()
|
||
result['unit_id'] = df['id'] + 7000
|
||
result['unit_name'] = df['rear_name']
|
||
result['type_id'] = 1685 # 后勤科室固定值
|
||
result['dept_id'] = df['company_id'] # 直接复制
|
||
# 注意:此表没有create_time字段
|
||
|
||
return result
|
||
|
||
|
||
def main():
|
||
try:
|
||
# 处理所有源表
|
||
df_project = process_bm_project_dept()
|
||
df_team = process_bm_team_info()
|
||
df_sub = process_bm_sub_contractor_info()
|
||
df_rear = process_bm_rear_service()
|
||
|
||
# 合并所有数据
|
||
final_df = pd.concat([df_project, df_team, df_sub, df_rear], ignore_index=True)
|
||
|
||
# 写入目标数据库
|
||
final_df.to_sql('bm_unit', target_engine, if_exists='append', index=False)
|
||
|
||
print("数据转换和导入成功完成!")
|
||
print(f"共导入 {len(final_df)} 条记录")
|
||
|
||
except Exception as e:
|
||
print(f"处理过程中发生错误: {str(e)}")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main() |