224 lines
6.8 KiB
Python
224 lines
6.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(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,
|
|
3 AS task_type,
|
|
IF(tt.`STATUS` = 4,2,0) as task_status,
|
|
bmb.APPLY_NUMBER as code,
|
|
bmb.CREATOR as create_by,
|
|
bmb.CREATE_TIME as create_time
|
|
FROM
|
|
ba_ma_back bmb
|
|
LEFT JOIN tm_task tt on bmb.ID = tt.ID
|
|
LEFT JOIN tm_task_status ts on tt.`STATUS` = ts.`CODE`
|
|
WHERE bmb.company_id =1
|
|
GROUP BY bmb.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_back bmb
|
|
LEFT JOIN tm_task tt on bmb.ID = tt.ID
|
|
LEFT JOIN ba_agreement_task bat on bmb.ID = bat.TASK_ID
|
|
WHERE bmb.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_back_apply_info():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bmb.ID as id,
|
|
bmb.APPLY_NUMBER as code,
|
|
tt.ID as task_id,
|
|
bmb.BACKER AS back_person,
|
|
bmb.PHONE as phone,
|
|
bmb.CREATOR as create_by,
|
|
bmb.CREATE_TIME as create_time,
|
|
IF(tt.`STATUS` = 4,1,0) as status,
|
|
bmb.IS_PRINT as print_status
|
|
FROM
|
|
ba_ma_back bmb
|
|
LEFT JOIN tm_task tt on bmb.ID = tt.ID
|
|
LEFT JOIN tm_task_status ts on tt.`STATUS` = ts.`CODE`
|
|
WHERE bmb.company_id =1
|
|
GROUP BY bmb.ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('back_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_back_apply_details():
|
|
"""处理退料申请明细数据"""
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bmb.APPLY_NUMBER as code,
|
|
bmb.id as parent_id,
|
|
ttmt.MA_TYPE_ID as type_id,
|
|
ttmt.MACHINES_NUM as pre_num,
|
|
ttmt.MACHINES_NUM as audit_num ,
|
|
ttmt.BACK_BAD_NUM as bad_num,
|
|
ttmt.BACK_GOOD_NUM as good_num,
|
|
IF(tt.`STATUS` = 4,2,0) as status,
|
|
bmb.CREATOR as create_by,
|
|
bmb.CREATE_TIME as create_time
|
|
FROM
|
|
ba_ma_back bmb
|
|
LEFT JOIN tm_task tt on bmb.ID = tt.ID
|
|
LEFT JOIN tm_task_ma_type ttmt on bmb.ID = ttmt.TASK_ID
|
|
WHERE bmb.company_id =1
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('back_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_back_check_details():
|
|
"""处理退料明细数据"""
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bmb.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 back_num ,
|
|
1 as back_status ,
|
|
1 as good_num,
|
|
0 as bad_num
|
|
FROM
|
|
ba_ma_back bmb
|
|
LEFT JOIN tm_task_ma ttm on bmb.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 mt.IS_COUNT = 0
|
|
UNION
|
|
SELECT
|
|
bmb.id as parent_id,
|
|
ttmt.MA_TYPE_ID as type_id,
|
|
null as ma_id,
|
|
pu.`NAME` as create_by,
|
|
bmb.CREATE_TIME as create_time,
|
|
ROUND(ttmt.MACHINES_NUM,3) as back_num,
|
|
1 as back_status ,
|
|
ttmt.BACK_GOOD_NUM as ood_num,
|
|
ttmt.BACK_BAD_NUM as bad_num
|
|
FROM
|
|
ba_ma_back bmb
|
|
LEFT JOIN tm_task_ma_type ttmt on bmb.ID = ttmt.TASK_ID
|
|
LEFT JOIN pm_user pu on bmb.CREATOR = pu.ID
|
|
WHERE ttmt.IS_COUNT =1
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('back_check_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_back_apply_info()
|
|
success4 = process_back_apply_details()
|
|
success5 = process_back_check_details()
|
|
|
|
if success1 and success2 and success3 and success4 and success5:
|
|
print("所有数据转换完成!")
|
|
else:
|
|
print("数据转换过程中出现错误,请检查日志") |