首次提交
This commit is contained in:
commit
f1c22ca3ef
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<profile version="1.0">
|
||||||
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
|
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
||||||
|
<option name="ignoredErrors">
|
||||||
|
<list>
|
||||||
|
<option value="E305" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</inspection_tool>
|
||||||
|
</profile>
|
||||||
|
</component>
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Black">
|
||||||
|
<option name="sdkName" value="jijv" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="jijv" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/jijv.iml" filepath="$PROJECT_DIR$/.idea/jijv.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,73 @@
|
||||||
|
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']}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def process_project_relation():
|
||||||
|
"""处理bm_project_relation表数据到bm_agreement_info"""
|
||||||
|
# 使用原始SQL查询实现复杂转换逻辑
|
||||||
|
sql = """
|
||||||
|
SELECT
|
||||||
|
(bpr.ID + 500000) AS agreement_id,
|
||||||
|
bpr.`CODE` as agreement_code,
|
||||||
|
CASE
|
||||||
|
WHEN bpr.type = 'xmb' THEN (bpr.unit_id + 4000)
|
||||||
|
WHEN bpr.type = 'sgd' THEN (bpr.unit_id + 5000)
|
||||||
|
WHEN bpr.type = 'fbs' THEN (bpr.unit_id + 6000)
|
||||||
|
WHEN bpr.type = 'hq' THEN (bpr.unit_id + 6000)
|
||||||
|
ELSE 0
|
||||||
|
END AS unit_id,
|
||||||
|
(bpr.PROJECT_ID + 3000) as project_id,
|
||||||
|
bpr.IS_SLT,
|
||||||
|
bpr.CREATE_TIME
|
||||||
|
FROM
|
||||||
|
bm_project_relation bpr
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
# 执行查询并获取结果
|
||||||
|
df = pd.read_sql(sql, source_engine)
|
||||||
|
|
||||||
|
# 写入目标表
|
||||||
|
df.to_sql('bm_agreement_info', target_engine,
|
||||||
|
if_exists='append', index=False)
|
||||||
|
|
||||||
|
print(f"成功转换并导入 {len(df)} 条记录到 bm_agreement_info")
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"处理 bm_project_relation 时发生错误: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
process_project_relation()
|
||||||
|
|
@ -0,0 +1,81 @@
|
||||||
|
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']}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 定义COMPANY_ID到imp_unit的映射
|
||||||
|
company_mapping = {
|
||||||
|
5: 340, # 安徽顺全电力工程有限公司
|
||||||
|
4: 102, # 送电二分公司
|
||||||
|
3: 327, # 送电一分公司
|
||||||
|
6: 101, # 机具(物流)分公司
|
||||||
|
7: 346, # 运检分公司
|
||||||
|
8: 338, # 建筑分公司
|
||||||
|
9: 309, # 安徽宏源电力建设有限公司
|
||||||
|
10: 347, # 公司培训中心(宏源工业园)
|
||||||
|
11: 337, # 检修试验分公司
|
||||||
|
12: 100, # 变电分公司
|
||||||
|
13: 348, # 预制构件分公司
|
||||||
|
14: 344, # 机械化分公司
|
||||||
|
15: 342, # 公司机关
|
||||||
|
16: 345, # 外单位租赁业务
|
||||||
|
17: 339, # 安徽顺安电网建设有限公司
|
||||||
|
18: 341 # 班组管理中心
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def process_bm_project():
|
||||||
|
"""处理bm_project表数据"""
|
||||||
|
# 读取源数据
|
||||||
|
df = pd.read_sql("SELECT * FROM bm_project", source_engine)
|
||||||
|
|
||||||
|
# 按照规则转换数据
|
||||||
|
result = pd.DataFrame()
|
||||||
|
result['pro_id'] = df['ID'] + 3000 # ID加3000
|
||||||
|
result['pro_name'] = df['NAME'] # 直接复制
|
||||||
|
result['pro_code'] = df['CODE'] # 直接复制
|
||||||
|
result['external_id'] = df['PRO_ID'] # 直接复制
|
||||||
|
result['create_time'] = df['CREATE_TIME'] # 直接复制
|
||||||
|
result['imp_unit'] = df['COMPANY'].map(company_mapping) # 替换映射
|
||||||
|
|
||||||
|
# 写入目标表
|
||||||
|
try:
|
||||||
|
result.to_sql('bm_project', target_engine,
|
||||||
|
if_exists='append', index=False)
|
||||||
|
print(f"成功转换并导入 {len(result)} 条记录到 bm_project")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"处理 bm_project 时发生错误: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
process_bm_project()
|
||||||
|
|
@ -0,0 +1,142 @@
|
||||||
|
import configparser
|
||||||
|
import pymysql
|
||||||
|
import pandas as pd
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from urllib.parse import quote_plus
|
||||||
|
# 读取配置文件
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('config.ini')
|
||||||
|
|
||||||
|
# 从配置文件获取数据库连接信息
|
||||||
|
source_db_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_db_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')
|
||||||
|
}
|
||||||
|
|
||||||
|
# 创建SQLAlchemy引擎
|
||||||
|
source_engine = create_engine(
|
||||||
|
f"mysql+pymysql://{source_db_config['user']}:{quote_plus(source_db_config['password'])}@{source_db_config['host']}:{source_db_config['port']}/{source_db_config['database']}"
|
||||||
|
)
|
||||||
|
target_engine = create_engine(
|
||||||
|
f"mysql+pymysql://{target_db_config['user']}:{quote_plus(target_db_config['password'])}@{target_db_config['host']}:{target_db_config['port']}/{target_db_config['database']}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 定义替换映射(可以从规则文件中读取,这里保持硬编码)
|
||||||
|
type_id_mapping = {
|
||||||
|
'项目部': 36,
|
||||||
|
'施工队': 33,
|
||||||
|
'分包商': 32,
|
||||||
|
'后勤科室': 1685,
|
||||||
|
'外单位': 1704,
|
||||||
|
'修试部门': 1706
|
||||||
|
}
|
||||||
|
|
||||||
|
dept_id_mapping = {
|
||||||
|
5: 340, # 安徽顺全电力工程有限公司
|
||||||
|
4: 102, # 送电二分公司
|
||||||
|
3: 327, # 送电一分公司
|
||||||
|
6: 101, # 机具(物流)分公司
|
||||||
|
7: 346, # 运检分公司
|
||||||
|
8: 338, # 建筑分公司
|
||||||
|
9: 309, # 安徽宏源电力建设有限公司
|
||||||
|
10: 347, # 公司培训中心(宏源工业园)
|
||||||
|
11: 337, # 检修试验分公司
|
||||||
|
12: 100, # 变电分公司
|
||||||
|
13: 348, # 预制构件分公司
|
||||||
|
14: 344, # 机械化分公司
|
||||||
|
15: 342, # 公司机关
|
||||||
|
16: 345, # 外单位租赁业务
|
||||||
|
17: 339, # 安徽顺安电网建设有限公司
|
||||||
|
18: 341 # 班组管理中心
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def process_bm_project_dept():
|
||||||
|
"""处理bm_project_dept表数据"""
|
||||||
|
df = pd.read_sql("SELECT * FROM bm_project_dept", source_engine)
|
||||||
|
|
||||||
|
result = pd.DataFrame()
|
||||||
|
result['unit_id'] = df['ID'] + 4000
|
||||||
|
result['unit_name'] = df['NAME']
|
||||||
|
result['type_id'] = 36 # 项目部固定值
|
||||||
|
result['dept_id'] = df['COMPANY_ID'].map(dept_id_mapping)
|
||||||
|
result['create_time'] = df['CREATE_TIME']
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def process_bm_team_info():
|
||||||
|
"""处理bm_team_info表数据"""
|
||||||
|
df = pd.read_sql("SELECT * FROM bm_team_info where IS_ACTIVE = 1", source_engine)
|
||||||
|
|
||||||
|
result = pd.DataFrame()
|
||||||
|
result['unit_id'] = df['ID'] + 5000
|
||||||
|
result['unit_name'] = df['NAME']
|
||||||
|
result['type_id'] = 33 # 施工队固定值
|
||||||
|
result['dept_id'] = df['COMPANY_ID'].map(dept_id_mapping)
|
||||||
|
result['create_time'] = df['CREATE_TIME']
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def process_bm_sub_contractor_info():
|
||||||
|
"""处理bm_sub_contractor_info表数据"""
|
||||||
|
df = pd.read_sql("SELECT * FROM bm_sub_contractor_info", source_engine)
|
||||||
|
|
||||||
|
result = pd.DataFrame()
|
||||||
|
result['unit_id'] = df['ID'] + 6000
|
||||||
|
result['unit_name'] = df['NAME']
|
||||||
|
result['type_id'] = 32 # 分包商固定值
|
||||||
|
# 注意:此表没有dept_id字段
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def process_bm_rear_service():
|
||||||
|
"""处理bm_rear_service表数据"""
|
||||||
|
df = pd.read_sql("SELECT * FROM bm_rear_service", source_engine)
|
||||||
|
|
||||||
|
result = pd.DataFrame()
|
||||||
|
result['unit_id'] = df['id'] + 7000
|
||||||
|
result['unit_name'] = df['rear_name']
|
||||||
|
result['type_id'] = 1685 # 后勤科室固定值
|
||||||
|
result['dept_id'] = df['company_id'] # 直接复制
|
||||||
|
# 注意:此表没有create_time字段
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
# 处理所有源表
|
||||||
|
df_project = process_bm_project_dept()
|
||||||
|
df_team = process_bm_team_info()
|
||||||
|
df_sub = process_bm_sub_contractor_info()
|
||||||
|
df_rear = process_bm_rear_service()
|
||||||
|
|
||||||
|
# 合并所有数据
|
||||||
|
final_df = pd.concat([df_project, df_team, df_sub, df_rear], ignore_index=True)
|
||||||
|
|
||||||
|
# 写入目标数据库
|
||||||
|
final_df.to_sql('bm_unit', target_engine, if_exists='append', index=False)
|
||||||
|
|
||||||
|
print("数据转换和导入成功完成!")
|
||||||
|
print(f"共导入 {len(final_df)} 条记录")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"处理过程中发生错误: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
[source_db]
|
||||||
|
host = localhost
|
||||||
|
user = root
|
||||||
|
password = Bonus@caiqi0802!
|
||||||
|
database = safety_ma
|
||||||
|
port = 13306
|
||||||
|
|
||||||
|
[target_db]
|
||||||
|
host = localhost
|
||||||
|
user = root
|
||||||
|
password = Bonus@caiqi0802!
|
||||||
|
database = test_jiju
|
||||||
|
port = 13306
|
||||||
|
|
||||||
|
[settings]
|
||||||
|
rule_file = E:\workingSpace\PycharmProjects\jijv\ma_supplier_info.xlsx
|
||||||
|
|
@ -0,0 +1,97 @@
|
||||||
|
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()
|
||||||
|
|
@ -0,0 +1,63 @@
|
||||||
|
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']}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def process_suppliers():
|
||||||
|
"""处理bm_supplier表数据到ma_supplier_info"""
|
||||||
|
try:
|
||||||
|
# 读取源数据
|
||||||
|
df = pd.read_sql("SELECT * FROM bm_supplier", source_engine)
|
||||||
|
|
||||||
|
# 按照规则转换数据
|
||||||
|
result = pd.DataFrame()
|
||||||
|
result['supplier_id'] = df['ID'] + 500 # ID加500
|
||||||
|
result['supplier'] = df['NAME']
|
||||||
|
result['address'] = df['ADDRESS']
|
||||||
|
result['legal_person'] = df['LEGAL_PERSON']
|
||||||
|
result['PHONE'] = df['PHONE_NUMBER']
|
||||||
|
result['business_scope'] = df['BUSINESS']
|
||||||
|
|
||||||
|
# 写入目标表
|
||||||
|
result.to_sql('ma_supplier_info', target_engine,
|
||||||
|
if_exists='append', index=False)
|
||||||
|
|
||||||
|
print(f"成功转换并导入 {len(result)} 条记录到 ma_supplier_info")
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"处理 bm_supplier 时发生错误: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
process_suppliers()
|
||||||
|
|
@ -0,0 +1,117 @@
|
||||||
|
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']}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 定义UNIT_ID到unit_name的映射
|
||||||
|
unit_mapping = {
|
||||||
|
9: 'm', 90: '根', 91: '个', 92: '付', 93: '支', 94: '双', 95: '套',
|
||||||
|
96: '组', 97: '只', 98: '台', 99: '顶', 100: '件', 101: '米', 102: '面',
|
||||||
|
103: '片', 104: '盘', 105: '张', 106: '块', 107: '副', 108: '袋', 109: '条',
|
||||||
|
110: '节', 111: '瓶', 112: '斤', 113: '把', 114: '平方米', 115: '床',
|
||||||
|
123: 'kg', 124: '份', 125: '卷'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def clean_existing_data():
|
||||||
|
"""清理目标表中需要更新的数据"""
|
||||||
|
try:
|
||||||
|
# 删除重复的地锚规格等不需要的数据(根据实际业务需求调整)
|
||||||
|
with target_engine.connect() as conn:
|
||||||
|
conn.execute("""
|
||||||
|
DELETE FROM ma_type
|
||||||
|
WHERE type_name IN ('重复的地锚规格', '其他需要删除的规格名称')
|
||||||
|
""")
|
||||||
|
print("已清理目标表中的重复/无效数据")
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
print(f"清理数据时出错: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def process_machine_types():
|
||||||
|
"""处理ma_machine_type表数据"""
|
||||||
|
try:
|
||||||
|
# 读取源数据
|
||||||
|
df = pd.read_sql("SELECT * FROM ma_machine_type", source_engine)
|
||||||
|
|
||||||
|
# 按照规则转换数据
|
||||||
|
result = pd.DataFrame()
|
||||||
|
result['type_id'] = df['ID'] + 6000 # ID加6000
|
||||||
|
result['parent_id'] = df['PARENT_ID']
|
||||||
|
result['type_name'] = df['NAME']
|
||||||
|
result['level'] = df['LEVEL']
|
||||||
|
result['unit_name'] = df['UNIT_ID'].map(unit_mapping) # 单位映射
|
||||||
|
result['storage_num'] = df['STORAGE_NUM']
|
||||||
|
result['buy_price'] = df['PURCHASE_PRICE']
|
||||||
|
result['notax_price'] = df['NOTAX_PRICE']
|
||||||
|
result['lease_price'] = df['RENTAL_PRICE']
|
||||||
|
result['manage_type'] = df['IS_COUNT']
|
||||||
|
result['rated_load'] = df['CONSUMABLE']
|
||||||
|
|
||||||
|
# 处理新增规格(示例,根据实际情况调整)
|
||||||
|
new_types = [
|
||||||
|
{'type_id': 6601, 'type_name': '新增规格1', 'unit_name': '个', 'level': 3},
|
||||||
|
{'type_id': 6602, 'type_name': '新增规格2', 'unit_name': '套', 'level': 3}
|
||||||
|
]
|
||||||
|
new_df = pd.DataFrame(new_types)
|
||||||
|
result = pd.concat([result, new_df], ignore_index=True)
|
||||||
|
|
||||||
|
# 先清理目标表
|
||||||
|
if not clean_existing_data():
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 写入目标表(使用更新模式)
|
||||||
|
result.to_sql('ma_type', target_engine,
|
||||||
|
if_exists='append', index=False)
|
||||||
|
|
||||||
|
print(f"成功转换并导入 {len(result)} 条记录到 ma_type")
|
||||||
|
|
||||||
|
# 更新现有库存和租赁单价(根据type_id)
|
||||||
|
with target_engine.connect() as conn:
|
||||||
|
# 更新storage_num
|
||||||
|
conn.execute("""
|
||||||
|
UPDATE ma_type t, ma_machine_type s
|
||||||
|
SET t.storage_num = s.STORAGE_NUM,
|
||||||
|
t.lease_price = s.LEASE_PRICE
|
||||||
|
WHERE t.type_id = s.ID + 6000
|
||||||
|
""")
|
||||||
|
print("已更新现有库存和租赁单价")
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"处理 ma_machine_type 时发生错误: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
process_machine_types()
|
||||||
|
|
@ -0,0 +1,107 @@
|
||||||
|
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']}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def process_lease_apply_details():
|
||||||
|
"""处理租赁申请明细数据"""
|
||||||
|
try:
|
||||||
|
# 执行转换SQL
|
||||||
|
sql = """
|
||||||
|
SELECT
|
||||||
|
(be.ID + 500000) as parent_id,
|
||||||
|
(bpf.MATYPE_ID + 6000) as type_id,
|
||||||
|
bpf.ORDER_NUM as pre_num,
|
||||||
|
bpf.OUT_NUM as audit_num,
|
||||||
|
bpf.OUT_NUM as al_num,
|
||||||
|
IF(bpf.ORDER_NUM - bpf.OUT_NUM = 0, 2, 0) as status
|
||||||
|
FROM
|
||||||
|
bpm_example be
|
||||||
|
LEFT JOIN bpm_pick_form bpf on be.ID = bpf.EXAMPLE_ID
|
||||||
|
WHERE
|
||||||
|
be.DEFINITION_ID = 1 and bpf.MATYPE_ID is not null
|
||||||
|
GROUP BY
|
||||||
|
bpf.ID
|
||||||
|
"""
|
||||||
|
df = pd.read_sql(sql, source_engine)
|
||||||
|
|
||||||
|
# 写入目标表
|
||||||
|
df.to_sql('lease_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_lease_out_details():
|
||||||
|
"""处理租赁出库明细数据"""
|
||||||
|
try:
|
||||||
|
# 执行转换SQL
|
||||||
|
sql = """
|
||||||
|
SELECT
|
||||||
|
(bfd.EXAMPLE_ID + 500000) as parent_id,
|
||||||
|
(bfd.MATYPE_ID + 6000) as type_id,
|
||||||
|
(bfd.MA_ID + 70000) as ma_id,
|
||||||
|
bfd.OUT_NUM as out_num,
|
||||||
|
pu.`NAME` as create_by,
|
||||||
|
bfd.CREATE_TIME as create_time
|
||||||
|
FROM
|
||||||
|
bpm_pick_form_details bfd
|
||||||
|
LEFT JOIN pm_user pu on bfd.CREATOR = pu.ID
|
||||||
|
"""
|
||||||
|
df = pd.read_sql(sql, source_engine)
|
||||||
|
|
||||||
|
# 写入目标表
|
||||||
|
df.to_sql('lease_out_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__":
|
||||||
|
# 执行两个转换流程
|
||||||
|
success1 = process_lease_apply_details()
|
||||||
|
success2 = process_lease_out_details()
|
||||||
|
|
||||||
|
if success1 and success2:
|
||||||
|
print("所有数据转换完成!")
|
||||||
|
else:
|
||||||
|
print("数据转换过程中出现错误,请检查日志")
|
||||||
Loading…
Reference in New Issue