107 lines
3.3 KiB
Python
107 lines
3.3 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']}"
|
|
)
|
|
|
|
|
|
def process_lease_apply_details():
|
|
"""处理租赁申请明细数据"""
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
(be.ID + 500000) as parent_id,
|
|
(bpf.MATYPE_ID + 6000) as type_id,
|
|
bpf.ORDER_NUM as pre_num,
|
|
bpf.OUT_NUM as audit_num,
|
|
bpf.OUT_NUM as al_num,
|
|
IF(bpf.ORDER_NUM - bpf.OUT_NUM = 0, 2, 0) as status
|
|
FROM
|
|
bpm_example be
|
|
LEFT JOIN bpm_pick_form bpf on be.ID = bpf.EXAMPLE_ID
|
|
WHERE
|
|
be.DEFINITION_ID = 1 and bpf.MATYPE_ID is not null
|
|
GROUP BY
|
|
bpf.ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('lease_apply_details', target_engine,
|
|
if_exists='append', index=False)
|
|
|
|
print(f"成功导入 {len(df)} 条租赁申请明细数据")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"处理租赁申请明细时出错: {str(e)}")
|
|
return False
|
|
|
|
|
|
def process_lease_out_details():
|
|
"""处理租赁出库明细数据"""
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
(bfd.EXAMPLE_ID + 500000) as parent_id,
|
|
(bfd.MATYPE_ID + 6000) as type_id,
|
|
(bfd.MA_ID + 70000) as ma_id,
|
|
bfd.OUT_NUM as out_num,
|
|
pu.`NAME` as create_by,
|
|
bfd.CREATE_TIME as create_time
|
|
FROM
|
|
bpm_pick_form_details bfd
|
|
LEFT JOIN pm_user pu on bfd.CREATOR = pu.ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('lease_out_details', target_engine,
|
|
if_exists='append', index=False)
|
|
|
|
print(f"成功导入 {len(df)} 条租赁出库明细数据")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"处理租赁出库明细时出错: {str(e)}")
|
|
return False
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# 执行两个转换流程
|
|
success1 = process_lease_apply_details()
|
|
success2 = process_lease_out_details()
|
|
|
|
if success1 and success2:
|
|
print("所有数据转换完成!")
|
|
else:
|
|
print("数据转换过程中出现错误,请检查日志") |