首次提交

This commit is contained in:
jiang 2025-08-07 09:07:50 +08:00
parent 5e7926835c
commit 2a8d774975
16 changed files with 431 additions and 62 deletions

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="D:\develop\miniconda3" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="jijv (2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -3,5 +3,5 @@
<component name="Black">
<option name="sdkName" value="jijv" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="D:\develop\miniconda3" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="jijv (2)" project-jdk-type="Python SDK" />
</project>

View File

@ -33,14 +33,14 @@ target_engine = create_engine(
# 定义状态映射
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
'16': 0, # 待入库 → 0
'17': 1, # 在库 → 1
'18': 2, # 在用 → 2
'19': 3, # 退料检修 → 3
'23': 7, # 待报废 → 7
'24': 8, # 已报废 → 8
'64': 18, # 丢失 → 18
'133': 4 # 待审批 → 4
}

View File

@ -86,8 +86,7 @@ def process_machine_types():
result = pd.concat([result, new_df], ignore_index=True)
# 先清理目标表
if not clean_existing_data():
return False
# 写入目标表(使用更新模式)
result.to_sql('ma_type', target_engine,

View File

@ -118,19 +118,19 @@ def process_slt_agreement():
if row['type'] == 6: # 施工队
if row['TEAM_ID'] > 0:
unit_id = row['TEAM_ID'] + 5000
unit_id = row['TEAM_ID']
type_code = 'sgd'
elif row['type'] == 7: # 项目部
if row['DEPT_ID'] > 0:
unit_id = row['DEPT_ID'] + 4000
unit_id = row['DEPT_ID']
type_code = 'xmb'
elif row['type'] == 122: # 分包商
if row['SUB_ID'] > 0:
unit_id = row['SUB_ID'] + 6000
unit_id = row['SUB_ID']
type_code = 'fbs'
elif row['type'] == 126: # 后勤
if row['REAR_ID'] > 0:
unit_id = row['REAR_ID'] + 6000
unit_id = row['REAR_ID']
type_code = 'hq'
# 检查必要参数是否有效
@ -139,7 +139,7 @@ def process_slt_agreement():
continue
# 查询agreement_id
agreement_id = get_agreement_id(unit_id, row['PROJECT_ID'] + 3000, type_code)
agreement_id = get_agreement_id(unit_id, row['PROJECT_ID'], type_code)
if agreement_id is None:
skipped_records += 1
continue

View File

@ -42,25 +42,20 @@ type_mapping = {
def get_agreement_id(unit_id, project_id, type_code):
"""根据组合条件查询agreement_id(使用参数化查询)"""
"""根据组合条件查询agreement_id"""
try:
# 修正2添加参数有效性检查
if pd.isna(unit_id) or pd.isna(project_id) or pd.isna(type_code):
return None
sql = """
sql = f"""
SELECT (bpr.ID + 500000) as agreement_id
FROM bm_project_relation bpr
WHERE bpr.UNIT_ID = %s
AND bpr.project_id = %s
AND bpr.type = %s
WHERE bpr.UNIT_ID = {unit_id}
AND bpr.project_id = {project_id}
AND bpr.type = '{type_code}'
LIMIT 1
"""
params = (int(unit_id), int(project_id), str(type_code))
result = pd.read_sql(sql, source_engine, params=params)
result = pd.read_sql(sql, source_engine)
return result['agreement_id'].iloc[0] if not result.empty else None
except Exception as e:
print(f"查询agreement_id出错: unit_id={unit_id}, project_id={project_id}, type={type_code}, 错误: {str(e)}")
print(f"查询agreement_id出错: {str(e)}")
return None
@ -106,18 +101,27 @@ def process_tm_task_agreement():
be.SUB_ID,
be.TEAM_ID,
be.REAR_ID,
be.TYPE,
be2.TYPE,
be.PROJECT_ID
FROM bpm_example be
LEFT JOIN bpm_example be2 on be.PARENT_ID = be2.id
WHERE be.DEFINITION_ID = 7
"""
base_df = pd.read_sql(base_sql, source_engine)
# 修正4更安全的空值处理
base_df = base_df.fillna({
'DEPT_ID': 0, 'SUB_ID': 0,
'TEAM_ID': 0, 'REAR_ID': 0,
'PROJECT_ID': 0, 'TYPE': 0
'DEPT_ID': 0,
'SUB_ID': 0,
'TEAM_ID': 0,
'REAR_ID': 0,
'PROJECT_ID': 0
}).astype({
'DEPT_ID': 'int64',
'SUB_ID': 'int64',
'TEAM_ID': 'int64',
'REAR_ID': 'int64',
'PROJECT_ID': 'int64'
})
# 修正5确保所有ID为整数
@ -132,18 +136,17 @@ def process_tm_task_agreement():
# 确定unit_id和type_code
unit_id = None
type_code = None
if row['TYPE'] == 6 and row['TEAM_ID'] > 0: # 施工队
unit_id = row['TEAM_ID'] + 5000
unit_id = row['TEAM_ID']
type_code = 'sgd'
elif row['TYPE'] == 7 and row['DEPT_ID'] > 0: # 项目部
unit_id = row['DEPT_ID'] + 4000
unit_id = row['DEPT_ID']
type_code = 'xmb'
elif row['TYPE'] == 122 and row['SUB_ID'] > 0: # 分包商
unit_id = row['SUB_ID'] + 6000
unit_id = row['SUB_ID']
type_code = 'fbs'
elif row['TYPE'] == 126 and row['REAR_ID'] > 0: # 后勤
unit_id = row['REAR_ID'] + 6000
unit_id = row['REAR_ID']
type_code = 'hq'
if not unit_id or row['PROJECT_ID'] <= 0:

View File

@ -2,6 +2,7 @@ import configparser
import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus
# 读取配置文件
config = configparser.ConfigParser()
config.read('config.ini')
@ -90,7 +91,7 @@ def process_tm_task_agreement():
# 第一步查询基础数据
base_sql = """
SELECT
(be.ID + 600000) as task_id,
(be.ID + 500000) as task_id,
be.DEPT_ID,
be.SUB_ID,
be.TEAM_ID,
@ -120,19 +121,19 @@ def process_tm_task_agreement():
if row['TYPE'] == 6: # 施工队
if row['TEAM_ID'] > 0:
unit_id = int(row['TEAM_ID']) + 5000
unit_id = int(row['TEAM_ID'])
type_code = 'sgd'
elif row['TYPE'] == 7: # 项目部
if row['DEPT_ID'] > 0:
unit_id = int(row['DEPT_ID']) + 4000
unit_id = int(row['DEPT_ID'])
type_code = 'xmb'
elif row['TYPE'] == 122: # 分包商
if row['SUB_ID'] > 0:
unit_id = int(row['SUB_ID']) + 6000
unit_id = int(row['SUB_ID'])
type_code = 'fbs'
elif row['TYPE'] == 126: # 后勤
if row['REAR_ID'] > 0:
unit_id = int(row['REAR_ID']) + 6000
unit_id = int(row['REAR_ID'])
type_code = 'hq'
# 检查必要参数是否有效
@ -189,6 +190,7 @@ def process_back_apply_info():
try:
sql = """
SELECT
(be.ID + 500000) as id,
be.`CODE`,
(be.ID + 500000) as task_id,
be.LINK_MAN as back_person,
@ -288,4 +290,4 @@ if __name__ == "__main__":
if all(results):
print("所有退料相关表转换完成!")
else:
print("部分转换失败,请检查错误日志")
print("部分转换失败,请检查错误日志")

View File

@ -2,6 +2,8 @@ import configparser
import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus
from sqlalchemy.exc import SQLAlchemyError
# 读取配置文件
config = configparser.ConfigParser()
config.read('config.ini')
@ -31,6 +33,178 @@ 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_TYPE_MAPPING = {
6: 'sgd',
7: 'xmb',
122: 'fbs',
126: 'hq'
}
def process_tm_task():
"""处理任务表数据"""
try:
# 执行转换SQL
sql = """
SELECT
(be.ID + 500000) as task_id,
2 as task_type,
IF(be.`STATUS` = 32, 3, 4) as task_status,
be.`CODE` as code,
pu.`NAME` as create_by,
be.CREATE_TIME as 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 = 1
"""
df = pd.read_sql(sql, source_engine)
# 写入目标表
df.to_sql('tm_task', target_engine, if_exists='append', index=False)
print(f"成功导入 {len(df)} 条任务数据到tm_task表")
return True
except Exception as e:
print(f"处理任务数据时出错: {str(e)}")
return False
def process_tm_task_agreement():
"""处理任务协议关联表数据"""
try:
# 第一步:查询基础数据
base_sql = """
SELECT
(be.ID + 500000) as task_id,
2 as task_type,
be.DEPT_ID,
be.SUB_ID,
be.TEAM_ID,
be.REAR_ID,
be.TYPE,
be.PROJECT_ID
FROM
bpm_example be
WHERE
be.DEFINITION_ID = 1
"""
base_df = pd.read_sql(base_sql, source_engine)
# 准备存储结果
results = []
skipped_records = 0
# 对每条记录处理
for _, row in base_df.iterrows():
try:
# 确定unit_id和type_code
unit_id = None
type_code = None
if row['TYPE'] == 6: # 施工队
if row['TEAM_ID'] > 0:
unit_id = int(row['TEAM_ID'])
type_code = 'sgd'
elif row['TYPE'] == 7: # 项目部
if row['DEPT_ID'] > 0:
unit_id = int(row['DEPT_ID'])
type_code = 'xmb'
elif row['TYPE'] == 122: # 分包商
if row['SUB_ID'] > 0:
unit_id = int(row['SUB_ID'])
type_code = 'fbs'
elif row['TYPE'] == 126: # 后勤
if row['REAR_ID'] > 0:
unit_id = int(row['REAR_ID'])
type_code = 'hq'
# 检查必要参数是否有效
if not unit_id or not type_code or row['PROJECT_ID'] <= 0:
skipped_records += 1
continue
# 使用参数化查询避免字符串拼接问题
agreement_id = get_agreement_id(unit_id, int(row['PROJECT_ID']), type_code)
if agreement_id:
if agreement_id:
results.append({
'task_id': int(row['task_id']),
'agreement_id': int(agreement_id)
})
except Exception as e:
print(f"查询agreement_id时出错: {str(e)}")
continue
# 转换为DataFrame并写入
if results:
result_df = pd.DataFrame(results)
result_df.to_sql('tm_task_agreement', target_engine, if_exists='append', index=False)
print(f"成功导入 {len(result_df)} 条任务协议关联数据到tm_task_agreement表")
return True
else:
print("没有找到匹配的任务协议关联数据")
return False
except Exception as e:
print(f"处理任务协议关联数据时出错: {str(e)}")
return False
def get_agreement_id(unit_id, project_id, type_code):
"""根据组合条件查询agreement_id"""
try:
sql = f"""
SELECT (bpr.ID + 500000) as agreement_id
FROM bm_project_relation bpr
WHERE bpr.UNIT_ID = {unit_id}
AND bpr.project_id = {project_id}
AND bpr.type = '{type_code}'
LIMIT 1
"""
result = pd.read_sql(sql, source_engine)
return result['agreement_id'].iloc[0] if not result.empty else None
except Exception as e:
print(f"查询agreement_id出错: {str(e)}")
return None
def process_lease_apply_info():
"""处理租赁申请信息表数据"""
try:
# 执行转换SQL
sql = """
SELECT
(be.ID + 500000) as id,
be.`CODE` as code,
(be.ID + 500000) as task_id,
be.LINK_MAN as lease_person,
be.PHONE_NUMBER as phone,
pu.`NAME` as create_by,
be.CREATE_TIME as create_time,
be.DEPT_ID as unit_id,
be.PROJECT_ID as project_id
FROM
bpm_example be
LEFT JOIN pm_user pu on be.CREATOR = pu.ID
WHERE
be.DEFINITION_ID = 1
"""
df = pd.read_sql(sql, source_engine)
# 写入目标表
df.to_sql('lease_apply_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_lease_apply_details():
"""处理租赁申请明细数据"""
@ -55,8 +229,7 @@ def process_lease_apply_details():
df = pd.read_sql(sql, source_engine)
# 写入目标表
df.to_sql('lease_apply_details', target_engine,
if_exists='append', index=False)
df.to_sql('lease_apply_details', target_engine, if_exists='append', index=False)
print(f"成功导入 {len(df)} 条租赁申请明细数据")
return True
@ -85,8 +258,7 @@ def process_lease_out_details():
df = pd.read_sql(sql, source_engine)
# 写入目标表
df.to_sql('lease_out_details', target_engine,
if_exists='append', index=False)
df.to_sql('lease_out_details', target_engine, if_exists='append', index=False)
print(f"成功导入 {len(df)} 条租赁出库明细数据")
return True
@ -97,11 +269,16 @@ def process_lease_out_details():
if __name__ == "__main__":
# 执行两个转换流程
success1 = process_lease_apply_details()
success2 = process_lease_out_details()
# 执行所有转换流程
results = [
process_tm_task(),
process_tm_task_agreement(),
process_lease_apply_info(),
process_lease_apply_details(),
process_lease_out_details()
]
if success1 and success2:
if all(results):
print("所有数据转换完成!")
else:
print("数据转换过程中出现错误,请检查日志")
print("数据转换过程中出现错误,请检查日志")

View File

@ -114,9 +114,7 @@ def transform_and_load_ma_machines(config_file_path):
# 写入目标表
print("\n正在写入目标表ma_machine...")
# 步骤1清空目标表
with target_engine.connect() as conn:
conn.execute(text("TRUNCATE TABLE ma_machine")) # 注意需要从sqlalchemy导入text
conn.commit() # 显式提交事务
# 步骤2写入去重后的数据
target_df.drop_duplicates(['type_id', 'ma_code']).to_sql(

190
机具/修试入库.py Normal file
View File

@ -0,0 +1,190 @@
import configparser
import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus
from sqlalchemy.exc import SQLAlchemyError
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')
}
# 创建数据库引擎
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 migrate_repair_tasks():
"""迁移修试入库任务数据到tm_task表"""
try:
print("开始迁移修试入库任务数据...")
# 执行转换SQL
sql = """
SELECT
bri.ID as task_id,
11 as task_type,
bri.`STATUS` as task_status,
bri.APPLY_NUMBER as code,
tt.CREATOR as create_by,
tt.CREATE_TIME as create_time
FROM
ba_ma_repair_input bri
LEFT JOIN tm_task tt on bri.ID = tt.ID
WHERE
tt.CREATE_TIME BETWEEN "2025-01-01" AND NOW()
"""
# 读取数据
df = pd.read_sql(sql, source_engine)
if df.empty:
print("没有找到需要迁移的修试入库任务数据")
return True
# 数据清洗和处理
df['task_type'] = 11 # 修试入库类型
# 写入目标表
df.to_sql('tm_task', target_engine, if_exists='append', index=False)
print(f"成功导入 {len(df)} 条修试入库任务数据到tm_task表")
return True
except Exception as e:
print(f"迁移修试入库任务数据时出错: {str(e)}")
return False
def migrate_repair_input_details():
"""迁移修试入库明细数据到repair_input_details表"""
try:
print("开始迁移修试入库明细数据...")
# 执行转换SQL包含UNION的两部分查询
sql = """
-- 第一部分查询具体设备
SELECT
bmri.ID as task_id,
bmri.AUDIT_ID as audit_id,
bmr.id as repair_id,
mm.ID as ma_id,
mm.TYPE as type_id,
1 as repair_num,
IF(mm.BATCH_STATUS = 9, 0, 1) as input_num,
bmri.`STATUS` as status,
tt.CREATE_TIME as create_time
FROM
ba_ma_repair_input bmri
LEFT JOIN tm_task tt on tt.ID = bmri.ID
LEFT JOIN ba_ma_input_check bmic on bmic.ID = bmri.AUDIT_ID
LEFT JOIN ba_ma_repair bmr on bmic.repair_id = bmr.id
LEFT JOIN tm_task_ma ttm on bmri.ID = ttm.TASK_ID
LEFT JOIN ma_machines mm on ttm.MA_ID = mm.ID
WHERE
tt.CREATE_TIME BETWEEN "2025-01-01" AND NOW()
UNION
-- 第二部分查询设备类型
SELECT
bmri.ID as task_id,
bmri.AUDIT_ID as audit_id,
bmr.id as repair_id,
NULL AS ma_id,
ttmt.MA_TYPE_ID as type_id,
ttmt.MACHINES_NUM as repair_num,
ttmt.ACTUAL_NUM as input_num,
ttmt.IS_SURE as status,
tt.CREATE_TIME as create_time
FROM
ba_ma_repair_input bmri
LEFT JOIN tm_task tt on tt.ID = bmri.ID
LEFT JOIN tm_task_ma_type ttmt on ttmt.TASK_ID = bmri.ID
LEFT JOIN ba_ma_input_check bmic on bmic.ID = bmri.AUDIT_ID
LEFT JOIN ba_ma_repair bmr on bmic.repair_id = bmr.id
WHERE
tt.CREATE_TIME BETWEEN "2025-01-01" AND NOW()
AND ttmt.IS_COUNT = 1
"""
# 读取数据
df = pd.read_sql(sql, source_engine)
if df.empty:
print("没有找到需要迁移的修试入库明细数据")
return True
# 数据清洗和处理
# 确保数值字段为整数类型
int_columns = ['task_id', 'audit_id', 'repair_id', 'ma_id', 'type_id',
'repair_num', 'input_num', 'status']
for col in int_columns:
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors='coerce').fillna(0).astype(int)
# 写入目标表
df.to_sql('repair_input_details', target_engine, if_exists='append', index=False)
print(f"成功导入 {len(df)} 条修试入库明细数据到repair_input_details表")
return True
except Exception as e:
print(f"迁移修试入库明细数据时出错: {str(e)}")
return False
def main():
"""主函数,执行所有迁移任务"""
print("=" * 50)
print("开始数据迁移流程")
print(f"当前时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 50)
# 执行所有迁移任务
migration_tasks = [
("修试入库任务数据", migrate_repair_tasks),
("修试入库明细数据", migrate_repair_input_details)
]
success = True
for task_name, task_func in migration_tasks:
print(f"\n正在处理: {task_name}...")
if not task_func():
success = False
print(f"处理失败: {task_name}")
else:
print(f"处理成功: {task_name}")
print("\n" + "=" * 50)
if success:
print("所有数据迁移任务完成!")
else:
print("数据迁移过程中出现错误,请检查日志")
print(f"完成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 50)
if __name__ == "__main__":
main()

View File

@ -4,7 +4,7 @@ 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')
config.read(r'config.ini')
# 获取数据库连接配置
source_config = {

View File

@ -4,7 +4,7 @@ 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')
config.read('config.ini')
# 获取数据库连接配置
source_config = {

View File

@ -4,7 +4,7 @@ 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')
config.read('config.ini')
# 获取数据库连接配置
source_config = {

View File

@ -58,7 +58,7 @@ def process_tm_task():
ba_ma_repair bmr
LEFT JOIN tm_task tt on bmr.ID = tt.ID
LEFT JOIN tm_task_status tts on tt.`STATUS` = tts.`CODE`
WHERE bmr.company_id = 1
WHERE bmr.company_id = 1 and tt.ID is not null
"""
df = pd.read_sql(sql, source_engine)

View File

@ -4,7 +4,7 @@ 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')
config.read('config.ini')
# 获取数据库连接配置
source_config = {

View File

@ -4,7 +4,7 @@ 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')
config.read('config.ini')
# 获取数据库连接配置
source_config = {