105 lines
3.2 KiB
Python
105 lines
3.2 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_bm_qrcode_box():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bqm.BOX_ID as box_id,
|
|
bqb.BOX_QRCODE as box_code,
|
|
CASE
|
|
WHEN bqb.box_status = 1 THEN 4
|
|
WHEN bqb.box_status = 2 THEN 3
|
|
WHEN bqb.box_status = 4 THEN 6
|
|
ELSE 5
|
|
END AS box_status,
|
|
132 as input_user
|
|
FROM
|
|
bm_qrcode_ma bqm
|
|
LEFT JOIN bm_qrcode_box bqb on bqb.ID = bqm.BOX_ID
|
|
WHERE bqm.TIME BETWEEN '2025-01-01' and NOW()
|
|
GROUP BY bqm.BOX_ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('bm_qrcode_box', 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_bm_qrcode_box_bind():
|
|
try:
|
|
# 执行转换SQL
|
|
sql = """
|
|
SELECT
|
|
bqm.BOX_ID as box_id,
|
|
bqm.MA_ID as ma_id,
|
|
pu.`NAME` as create_by,
|
|
bqm.TIME as create_time
|
|
FROM
|
|
bm_qrcode_ma bqm
|
|
LEFT JOIN bm_qrcode_box bqb on bqb.ID = bqm.BOX_ID
|
|
LEFT JOIN pm_user pu on bqm.`USER` = pu.ID
|
|
WHERE bqm.TIME BETWEEN '2025-01-01' and NOW()
|
|
GROUP BY bqm.BOX_ID,bqm.MA_ID
|
|
"""
|
|
df = pd.read_sql(sql, source_engine)
|
|
|
|
# 写入目标表
|
|
df.to_sql('bm_qrcode_box_bind', 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__":
|
|
# 执行2个转换流程
|
|
success1 = process_bm_qrcode_box()
|
|
success2 = process_bm_qrcode_box_bind()
|
|
|
|
if success1 and success2:
|
|
print("所有数据转换完成!")
|
|
else:
|
|
print("数据转换过程中出现错误,请检查日志") |