97 lines
3.4 KiB
Python
97 lines
3.4 KiB
Python
import configparser
|
||
import pandas as pd
|
||
from sqlalchemy import create_engine
|
||
from urllib.parse import quote_plus
|
||
# 读取配置文件
|
||
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')
|
||
}
|
||
|
||
# 创建数据库引擎
|
||
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']}"
|
||
)
|
||
|
||
# 定义状态映射
|
||
status_mapping = {
|
||
16: 0, # 待入库 → 0
|
||
17: 1, # 在库 → 1
|
||
18: 2, # 在用 → 2
|
||
19: 3, # 退料检修 → 3
|
||
23: 7, # 待报废 → 7
|
||
24: 8, # 已报废 → 8
|
||
64: 18, # 丢失 → 18
|
||
133: 4 # 待审批 → 4
|
||
}
|
||
|
||
|
||
def normalize_date_string(s):
|
||
if pd.isnull(s):
|
||
return None
|
||
if isinstance(s, str):
|
||
s = s.replace('-', '-').strip()
|
||
try:
|
||
return pd.to_datetime(s, errors='coerce') # 自动识别格式
|
||
except:
|
||
return None
|
||
return pd.to_datetime(s, errors='coerce') # 如果已经是日期,仍然尝试转化
|
||
|
||
def process_machines():
|
||
"""处理ma_machines表数据到ma_machine"""
|
||
try:
|
||
# 读取源数据
|
||
df = pd.read_sql("SELECT * FROM ma_machine", source_engine)
|
||
|
||
# 按照规则转换数据
|
||
result = pd.DataFrame()
|
||
result['ma_id'] = df['ID'] + 70000 # ID加70000
|
||
result['type_id'] = df['MATYPE_ID'] + 6000 # 类型ID加6000
|
||
result['ma_status'] = df['STATUS'].map(status_mapping) # 状态映射
|
||
result['ma_code'] = df['CODE']
|
||
result['out_fac_time'] = df['OUTFACTORT_TIME'].apply(normalize_date_string)
|
||
result['out_fac_code'] = df['OUTCODE']
|
||
result['ma_vender'] = df['SUPPLIER_ID'] + 500 # 供应商ID加500
|
||
result['assets_code'] = df['ASSET_NUM']
|
||
result['rfid_code'] = df['EPC_CODE']
|
||
result['this_check_time'] = df['REPAIR_TIME'].apply(normalize_date_string)
|
||
result['check_man'] = df['EXAMINER']
|
||
result['next_check_time'] = df['NEXT_REPAIR_TIME'].apply(normalize_date_string)
|
||
result['inspect_status'] = df['TESTRESULT']
|
||
result['ex_code'] = df['EX_CODE']
|
||
result['ex_url'] = df['EX_URL']
|
||
result['ex_up_time'] = df['EX_UP_TIME'].apply(normalize_date_string)
|
||
result['ex_name'] = df['EX_NAME']
|
||
|
||
# 写入目标表
|
||
result.to_sql('ma_machine', target_engine,
|
||
if_exists='append', index=False)
|
||
|
||
print(f"成功转换并导入 {len(result)} 条记录到 ma_machine")
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"处理 ma_machines 时发生错误: {str(e)}")
|
||
return False
|
||
|
||
|
||
if __name__ == "__main__":
|
||
process_machines() |