217 lines
6.9 KiB
Python
217 lines
6.9 KiB
Python
import configparser
|
|
import pandas as pd
|
|
from sqlalchemy import create_engine
|
|
from urllib.parse import quote_plus
|
|
# 读取配置文件
|
|
config = configparser.ConfigParser()
|
|
config.read(r'D:\code\Bonus-Transfer-Machines\machines\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_tm_task():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
tt.ID as task_id,
|
|
2 as task_type,
|
|
IF(SUM(ttmt.MACHINES_NUM)- SUM(ttmt.ACTUAL_NUM )>0,3,4) as task_status,
|
|
bmc.APPLY_NUMBER as code,
|
|
pu.`NAME` as create_by,
|
|
bmc.CREATE_TIME as create_time
|
|
FROM
|
|
ba_ma_collar bmc
|
|
LEFT JOIN tm_task tt on bmc.ID = tt.ID
|
|
LEFT JOIN tm_task_ma_type ttmt on bmc.ID = ttmt.TASK_ID
|
|
LEFT JOIN pm_user pu on bmc.CREATOR = pu.ID
|
|
WHERE bmc.company_id =1
|
|
GROUP BY bmc.ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('tm_task', 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_tm_task_agreement():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
tt.ID as task_id,
|
|
bat.AGREEMENT_ID as agreement_id
|
|
FROM
|
|
ba_ma_collar bmc
|
|
LEFT JOIN tm_task tt on bmc.ID = tt.ID
|
|
LEFT JOIN ba_agreement_task bat on bmc.ID = bat.TASK_ID
|
|
WHERE bmc.company_id =1
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('tm_task_agreement', 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_apply_info():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bmc.ID as id,
|
|
bmc.APPLY_NUMBER as code,
|
|
tt.ID as task_id,
|
|
bmc.COLLAR_MAN as lease_person,
|
|
bmc.COLLAR_PHONE as phone,
|
|
pu.`NAME` as create_by,
|
|
bmc.CREATE_TIME as create_time,
|
|
bmc.direct_id as direct_id,
|
|
bma.LEASE_COMPANY as unit_id,
|
|
bma.PROJECT as project_id
|
|
FROM
|
|
ba_ma_collar bmc
|
|
LEFT JOIN tm_task tt on bmc.ID = tt.ID
|
|
LEFT JOIN ba_agreement_task bat on bmc.ID = bat.TASK_ID
|
|
LEFT JOIN ba_ma_agreement bma on bat.AGREEMENT_ID = bma.ID
|
|
LEFT JOIN pm_user pu on bmc.CREATOR = pu.ID
|
|
WHERE bmc.company_id =1
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('lease_apply_info', 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_apply_details():
|
|
"""处理租赁申请明细数据"""
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bmc.ID as parent_id,
|
|
ttmt.MA_TYPE_ID as type_id,
|
|
ttmt.MACHINES_NUM as pre_num,
|
|
ttmt.ACTUAL_NUM as al_num,
|
|
IF(ttmt.MACHINES_NUM - ttmt.ACTUAL_NUM =0,2,0) AS status
|
|
FROM
|
|
ba_ma_collar bmc
|
|
LEFT JOIN tm_task_ma_type ttmt on bmc.ID = ttmt.TASK_ID
|
|
WHERE bmc.company_id =1
|
|
AND ttmt.MA_TYPE_ID IS NOT NULL
|
|
"""
|
|
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
|
|
bmc.ID as parent_id,
|
|
mm.TYPE as type_id,
|
|
mm.ID as ma_id,
|
|
ttm.CREATOR as create_by,
|
|
ttm.CREATE_TIME as create_time,
|
|
1 as out_num
|
|
FROM
|
|
ba_ma_collar bmc
|
|
LEFT JOIN tm_task_ma ttm on bmc.ID = ttm.TASK_ID
|
|
LEFT JOIN ma_machines mm on ttm.MA_ID = mm.ID
|
|
LEFT JOIN ma_type mt on mm.TYPE = mt.ID
|
|
WHERE bmc.company_id =1 AND mt.IS_COUNT = 0
|
|
UNION
|
|
SELECT
|
|
bmc.ID as parent_id,
|
|
ttot.MA_TYPE_ID as type_id,
|
|
null as ma_id,
|
|
pu.`NAME` as create_by,
|
|
ttot.CREATE_TIME as create_time,
|
|
ttot.NUM as out_num
|
|
FROM
|
|
ba_ma_collar bmc
|
|
LEFT JOIN tm_task_out_type ttot on bmc.ID = ttot.TASK_ID
|
|
LEFT JOIN ma_type mt on ttot.MA_TYPE_ID = mt.ID
|
|
LEFT JOIN pm_user pu on ttot.CREATOR = pu.ID
|
|
WHERE bmc.company_id =1 AND ttot.IS_COUNT =1
|
|
"""
|
|
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__":
|
|
# 执行5个转换流程
|
|
success1 = process_tm_task()
|
|
success2 = process_tm_task_agreement()
|
|
success3 = process_lease_apply_info()
|
|
success4 = process_lease_apply_details()
|
|
success5 = process_lease_out_details()
|
|
|
|
if success1 and success2 and success3 and success4 and success5:
|
|
print("所有数据转换完成!")
|
|
else:
|
|
print("数据转换过程中出现错误,请检查日志") |