144 lines
4.8 KiB
Python
144 lines
4.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
|
|
bms.ID as task_id,
|
|
0 as task_type,
|
|
2 as task_status,
|
|
bms.APPLY_NUMBER as code,
|
|
bms.BUYER as create_by,
|
|
tt.CREATE_TIME as create_time
|
|
FROM
|
|
ba_ma_shop bms
|
|
LEFT JOIN tm_task tt on bms.ID = tt.ID
|
|
LEFT JOIN tm_task_status tts on tt.`STATUS` = tts.`CODE`
|
|
LEFT JOIN tm_task_ma_type ttmt on bms.ID = ttmt.TASK_ID
|
|
WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW() and tt.`STATUS` in (18,19)
|
|
GROUP BY bms.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_purchase_check_info():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bms.ID as task_id,
|
|
bms.BUY_TIME as purchase_time,
|
|
bms.ACCEPT_TIME as arrival_time,
|
|
ttmt.MANUFACTURER_ID as supplier_id,
|
|
bms.BUYER as create_by,
|
|
tt.CREATE_TIME as create_time
|
|
FROM
|
|
ba_ma_shop bms
|
|
LEFT JOIN tm_task tt on bms.ID = tt.ID
|
|
LEFT JOIN tm_task_status tts on tt.`STATUS` = tts.`CODE`
|
|
LEFT JOIN tm_task_ma_type ttmt on bms.ID = ttmt.TASK_ID
|
|
WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW()
|
|
GROUP BY bms.ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('purchase_check_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_purchase_check_details():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bms.ID as task_id,
|
|
ttmt.MA_TYPE_ID as type_id,
|
|
ttmt.PRICE as purchase_price,
|
|
ttmt.MACHINES_NUM as purchase_num,
|
|
ttmt.MACHINES_NUM as check_num,
|
|
ttmt.ACTUAL_NUM as bind_num,
|
|
ttmt.INPUT_NUM as input_num,
|
|
ttmt.MANUFACTURER_ID as supplier_id,
|
|
IF(ttmt.MACHINES_NUM - IFNULL(ttmt.INPUT_NUM,0) = 0,19,4) as status,
|
|
bms.BUYER as create_by,
|
|
tt.CREATE_TIME as create_time
|
|
FROM
|
|
ba_ma_shop bms
|
|
LEFT JOIN tm_task tt on bms.ID = tt.ID
|
|
LEFT JOIN tm_task_ma_type ttmt on bms.ID = ttmt.TASK_ID
|
|
WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW()
|
|
GROUP BY bms.ID,ttmt.MA_TYPE_ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('purchase_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__":
|
|
# 执行3个转换流程
|
|
success1 = process_tm_task()
|
|
success2 = process_purchase_check_info()
|
|
success3 = process_purchase_check_details()
|
|
|
|
if success1 and success2 and success3:
|
|
print("所有数据转换完成!")
|
|
else:
|
|
print("数据转换过程中出现错误,请检查日志") |