197 lines
6.7 KiB
Python
197 lines
6.7 KiB
Python
import configparser
|
||
import pandas as pd
|
||
from sqlalchemy import create_engine
|
||
from urllib.parse import quote_plus
|
||
from datetime import datetime
|
||
|
||
# 读取配置文件
|
||
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')
|
||
}
|
||
|
||
# 创建数据库引擎(增加charset参数避免中文乱码)
|
||
source_engine = create_engine(
|
||
f"mysql+pymysql://{source_config['user']}:{quote_plus(source_config['password'])}@{source_config['host']}:{source_config['port']}/{source_config['database']}?charset=utf8mb4"
|
||
)
|
||
target_engine = create_engine(
|
||
f"mysql+pymysql://{target_config['user']}:{quote_plus(target_config['password'])}@{target_config['host']}:{target_config['port']}/{target_config['database']}?charset=utf8mb4"
|
||
)
|
||
|
||
|
||
def safe_convert_to_int(series):
|
||
"""安全转换为整数,处理空字符串、浮点字符串等情况"""
|
||
str_series = series.astype(str)
|
||
str_series = str_series.replace({'': '0', 'None': '0', 'nan': '0'})
|
||
return pd.to_numeric(str_series, errors='coerce').fillna(0).astype(int)
|
||
|
||
|
||
def safe_convert_to_float(series):
|
||
"""安全转换为浮点数"""
|
||
str_series = series.astype(str)
|
||
str_series = str_series.replace({'': '0.0', 'None': '0.0', 'nan': '0.0'})
|
||
return pd.to_numeric(str_series, errors='coerce').fillna(0.0)
|
||
|
||
|
||
def process_tm_task():
|
||
"""处理新购任务主表"""
|
||
try:
|
||
sql = """
|
||
SELECT
|
||
(be.ID + 500000) as task_id,
|
||
0 as task_type,
|
||
IF(be.`STATUS` = 17, 19, 4) as task_status,
|
||
be.`CODE` as code,
|
||
pu.`NAME` as create_by,
|
||
be.CREATE_TIME
|
||
FROM
|
||
bpm_example be
|
||
LEFT JOIN sys_data_dict sdd on be.`STATUS` = sdd.ID
|
||
LEFT JOIN pm_user pu on be.CREATOR = pu.ID
|
||
WHERE be.DEFINITION_ID = 2
|
||
AND be.CREATE_TIME BETWEEN '2025-01-01' AND NOW()
|
||
"""
|
||
df = pd.read_sql(sql, source_engine)
|
||
|
||
# 转换状态字段
|
||
df['task_status'] = safe_convert_to_int(df['task_status'])
|
||
|
||
# 确保时间字段格式正确
|
||
if 'CREATE_TIME' in df.columns:
|
||
df['CREATE_TIME'] = pd.to_datetime(df['CREATE_TIME'])
|
||
|
||
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 = """
|
||
SELECT
|
||
(be.ID + 500000) as task_id,
|
||
baf.BUY_TIME as purchase_time,
|
||
be.CREATE_TIME as arrival_time,
|
||
(baf.SUPPLIER_ID + 500) as supplier_id,
|
||
pu.`NAME` as create_by,
|
||
be.CREATE_TIME
|
||
FROM
|
||
bpm_example be
|
||
LEFT JOIN bpm_accept_form baf on be.ID = baf.EXAMPLE_ID
|
||
LEFT JOIN sys_data_dict sdd on be.`STATUS` = sdd.ID
|
||
LEFT JOIN pm_user pu on be.CREATOR = pu.ID
|
||
WHERE be.DEFINITION_ID = 2
|
||
AND be.CREATE_TIME BETWEEN '2025-01-01' AND NOW()
|
||
"""
|
||
df = pd.read_sql(sql, source_engine)
|
||
|
||
# 处理时间字段
|
||
time_cols = ['purchase_time', 'arrival_time', 'CREATE_TIME']
|
||
for col in time_cols:
|
||
if col in df.columns:
|
||
df[col] = pd.to_datetime(df[col])
|
||
|
||
# 处理供应商ID
|
||
if 'supplier_id' in df.columns:
|
||
df['supplier_id'] = safe_convert_to_int(df['supplier_id'])
|
||
|
||
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 = """
|
||
SELECT
|
||
(be.ID + 500000) as task_id,
|
||
(baf.MATYPE_ID + 6000) as type_id,
|
||
baf.PURCHASE_PRICE as purchase_price,
|
||
baf.NOTAX_PRICE as purchase_tax_price,
|
||
(baf.SUPPLIER_ID + 500) as supplier_id,
|
||
baf.ACCEP_NUM as purchase_num,
|
||
baf.QUALIFIED_NUM as check_num,
|
||
baf.ACTUALNUM as bind_num,
|
||
baf.INPUT_NUM as input_num,
|
||
IF(baf.PURCHASE_STATUS = 17, 19, 4) as status,
|
||
pu.`NAME` as create_by,
|
||
be.CREATE_TIME
|
||
FROM
|
||
bpm_example be
|
||
LEFT JOIN bpm_accept_form baf on be.ID = baf.EXAMPLE_ID
|
||
LEFT JOIN sys_data_dict sdd on be.`STATUS` = sdd.ID
|
||
LEFT JOIN pm_user pu on be.CREATOR = pu.ID
|
||
WHERE be.DEFINITION_ID = 2
|
||
AND be.CREATE_TIME BETWEEN '2025-01-01' AND NOW()
|
||
"""
|
||
df = pd.read_sql(sql, source_engine)
|
||
|
||
# 处理数值字段
|
||
num_cols = [
|
||
'type_id', 'purchase_num', 'check_num',
|
||
'bind_num', 'input_num', 'status', 'supplier_id'
|
||
]
|
||
for col in num_cols:
|
||
if col in df.columns:
|
||
df[col] = safe_convert_to_int(df[col])
|
||
|
||
# 处理价格字段
|
||
price_cols = ['purchase_price', 'purchase_tax_price']
|
||
for col in price_cols:
|
||
if col in df.columns:
|
||
df[col] = safe_convert_to_float(df[col])
|
||
|
||
# 添加默认的生产时间(当前时间)
|
||
df['production_time'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||
|
||
# 计算验收结果
|
||
df['check_result'] = df.apply(
|
||
lambda row: 1 if row['check_num'] == row['purchase_num'] else 0,
|
||
axis=1
|
||
)
|
||
|
||
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__":
|
||
# 执行所有转换流程
|
||
processes = [
|
||
process_tm_task,
|
||
process_purchase_check_info,
|
||
process_purchase_check_details
|
||
]
|
||
|
||
success = all([p() for p in processes])
|
||
|
||
if success:
|
||
print("所有新购待验收相关表转换完成!")
|
||
else:
|
||
print("!!! 部分转换失败,请检查错误日志 !!!") |