From 9ef5eaa08f765e2e44a8e356aaf3d48864fed403 Mon Sep 17 00:00:00 2001 From: mashuai Date: Tue, 5 Aug 2025 13:51:13 +0800 Subject: [PATCH] Initial commit --- .idea/jijv.iml | 2 +- .idea/misc.xml | 2 +- machines/config.ini | 16 ++++ machines/新购机具.py | 144 ++++++++++++++++++++++++++++ machines/新购配件.py | 143 +++++++++++++++++++++++++++ machines/标准箱.py | 105 ++++++++++++++++++++ machines/退料.py | 224 +++++++++++++++++++++++++++++++++++++++++++ machines/领料.py | 217 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 851 insertions(+), 2 deletions(-) create mode 100644 machines/config.ini create mode 100644 machines/新购机具.py create mode 100644 machines/新购配件.py create mode 100644 machines/标准箱.py create mode 100644 machines/退料.py create mode 100644 machines/领料.py diff --git a/.idea/jijv.iml b/.idea/jijv.iml index d0876a7..9199d9d 100644 --- a/.idea/jijv.iml +++ b/.idea/jijv.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index c1622b9..97fc4e8 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/machines/config.ini b/machines/config.ini new file mode 100644 index 0000000..21fb073 --- /dev/null +++ b/machines/config.ini @@ -0,0 +1,16 @@ +[source_db] +host = 192.168.1.114 +user = boot +password = boot@123 +database = newimt +port = 13306 + +[target_db] +host = 192.168.1.114 +user = boot +password = boot@123 +database = test_jiju +port = 13306 + +[settings] +rule_file = E:\workingSpace\PycharmProjects\jijv\ma_supplier_info.xlsx diff --git a/machines/新购机具.py b/machines/新购机具.py new file mode 100644 index 0000000..86b45e7 --- /dev/null +++ b/machines/新购机具.py @@ -0,0 +1,144 @@ +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_tm_task(): + """处理新购待验收数据""" + try: + # 执行转换SQL + sql = """ + SELECT + bms.ID as task_id, + 0 as task_type, + 2 as task_status, + bms.APPLY_NUMBER as code, + bms.BUYER as create_by, + tt.CREATE_TIME as create_time + FROM + ba_ma_shop bms + LEFT JOIN tm_task tt on bms.ID = tt.ID + LEFT JOIN tm_task_status tts on tt.`STATUS` = tts.`CODE` + LEFT JOIN tm_task_ma_type ttmt on bms.ID = ttmt.TASK_ID + WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW() and tt.`STATUS` in (18,19) + GROUP BY bms.ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + 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 + sql = """ + SELECT + bms.ID as task_id, + bms.BUY_TIME as purchase_time, + bms.ACCEPT_TIME as arrival_time, + ttmt.MANUFACTURER_ID as supplier_id, + bms.BUYER as create_by, + tt.CREATE_TIME as create_time + FROM + ba_ma_shop bms + LEFT JOIN tm_task tt on bms.ID = tt.ID + LEFT JOIN tm_task_status tts on tt.`STATUS` = tts.`CODE` + LEFT JOIN tm_task_ma_type ttmt on bms.ID = ttmt.TASK_ID + WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW() + GROUP BY bms.ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + 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 + sql = """ + SELECT + bms.ID as task_id, + ttmt.MA_TYPE_ID as type_id, + ttmt.PRICE as purchase_price, + ttmt.MACHINES_NUM as purchase_num, + ttmt.MACHINES_NUM as check_num, + ttmt.ACTUAL_NUM as bind_num, + ttmt.INPUT_NUM as input_num, + ttmt.MANUFACTURER_ID as supplier_id, + IF(ttmt.MACHINES_NUM - IFNULL(ttmt.INPUT_NUM,0) = 0,19,4) as status, + bms.BUYER as create_by, + tt.CREATE_TIME as create_time + FROM + ba_ma_shop bms + LEFT JOIN tm_task tt on bms.ID = tt.ID + LEFT JOIN tm_task_ma_type ttmt on bms.ID = ttmt.TASK_ID + WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW() + GROUP BY bms.ID,ttmt.MA_TYPE_ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + 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__": + # 执行3个转换流程 + success1 = process_tm_task() + success2 = process_purchase_check_info() + success3 = process_purchase_check_details() + + if success1 and success2 and success3: + print("所有数据转换完成!") + else: + print("数据转换过程中出现错误,请检查日志") \ No newline at end of file diff --git a/machines/新购配件.py b/machines/新购配件.py new file mode 100644 index 0000000..eb3baea --- /dev/null +++ b/machines/新购配件.py @@ -0,0 +1,143 @@ +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_tm_task(): + """处理新购任务数据""" + try: + # 执行转换SQL + sql = """ + SELECT + bps.ID as task_id, + 12 as task_type, + IF( SUM(ttmt.MACHINES_NUM) - IFNULL(SUM(ttmt.INPUT_NUM),0) =0,1,0) as task_status, + tt.`CODE` as code, + bps.BUYER as create_by, + tt.CREATE_TIME as create_time + FROM + ba_pa_shop bps + LEFT JOIN tm_task tt on bps.ID = tt.ID + LEFT JOIN tm_task_status tts on tt.`STATUS` = tts.`CODE` + LEFT JOIN tm_task_ma_type ttmt on bps.ID = ttmt.TASK_ID + WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW() + GROUP BY bps.ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + 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_part_info(): + try: + # 执行转换SQL + sql = """ + SELECT + bps.ID as task_id, + bps.BUY_TIME as purchase_time, + bps.ACCEPT_TIME as arrival_time, + ttmt.MANUFACTURER_ID as supplier_id, + bps.BUYER as create_by, + tt.CREATE_TIME as create_time + FROM + ba_pa_shop bps + LEFT JOIN tm_task tt on bps.ID = tt.ID + LEFT JOIN tm_task_status tts on tt.`STATUS` = tts.`CODE` + LEFT JOIN tm_task_ma_type ttmt on bps.ID = ttmt.TASK_ID + WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW() + GROUP BY bps.ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + df.to_sql('purchase_part_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_part_details(): + try: + # 执行转换SQL + sql = """ + SELECT + bps.ID as task_id, + ttmt.MA_TYPE_ID as part_id, + ttmt.PRICE as purchase_price, + ttmt.MACHINES_NUM as purchase_num, + ttmt.MACHINES_NUM as check_num, + ttmt.INPUT_NUM as input_num, + ttmt.MANUFACTURER_ID as supplier_id, + IF(ttmt.MACHINES_NUM - IFNULL(ttmt.INPUT_NUM,0) = 0,1,0) as status, + bps.BUYER as create_by, + tt.CREATE_TIME as as create_time + FROM + ba_pa_shop bps + LEFT JOIN tm_task tt on bps.ID = tt.ID + LEFT JOIN tm_task_ma_type ttmt on bps.ID = ttmt.TASK_ID + WHERE tt.CREATE_TIME BETWEEN '2025-01-01' and NOW() + GROUP BY bps.ID,ttmt.MA_TYPE_ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + df.to_sql('purchase_part_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__": + # 执行3个转换流程 + success1 = process_tm_task() + success2 = process_purchase_part_info() + success3 = process_purchase_part_details() + + if success1 and success2 and success3: + print("所有数据转换完成!") + else: + print("数据转换过程中出现错误,请检查日志") \ No newline at end of file diff --git a/machines/标准箱.py b/machines/标准箱.py new file mode 100644 index 0000000..2a7bd70 --- /dev/null +++ b/machines/标准箱.py @@ -0,0 +1,105 @@ +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("数据转换过程中出现错误,请检查日志") \ No newline at end of file diff --git a/machines/退料.py b/machines/退料.py new file mode 100644 index 0000000..17b5615 --- /dev/null +++ b/machines/退料.py @@ -0,0 +1,224 @@ +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_tm_task(): + try: + # 执行转换SQL + sql = """ + SELECT + tt.ID as task_id, + 3 AS task_type, + IF(tt.`STATUS` = 4,2,0) as task_status, + bmb.APPLY_NUMBER as code, + bmb.CREATOR as create_by, + bmb.CREATE_TIME as create_time + FROM + ba_ma_back bmb + LEFT JOIN tm_task tt on bmb.ID = tt.ID + LEFT JOIN tm_task_status ts on tt.`STATUS` = ts.`CODE` + WHERE bmb.company_id =1 + GROUP BY bmb.ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + 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_tm_task_agreement(): + try: + # 执行转换SQL + sql = """ + SELECT + tt.ID as task_id, + bat.AGREEMENT_ID as agreement_id + FROM + ba_ma_back bmb + LEFT JOIN tm_task tt on bmb.ID = tt.ID + LEFT JOIN ba_agreement_task bat on bmb.ID = bat.TASK_ID + WHERE bmb.company_id =1 + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + df.to_sql('tm_task_agreement', 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_back_apply_info(): + try: + # 执行转换SQL + sql = """ + SELECT + bmb.ID as id, + bmb.APPLY_NUMBER as code, + tt.ID as task_id, + bmb.BACKER AS back_person, + bmb.PHONE as phone, + bmb.CREATOR as create_by, + bmb.CREATE_TIME as create_time, + IF(tt.`STATUS` = 4,1,0) as status, + bmb.IS_PRINT as print_status + FROM + ba_ma_back bmb + LEFT JOIN tm_task tt on bmb.ID = tt.ID + LEFT JOIN tm_task_status ts on tt.`STATUS` = ts.`CODE` + WHERE bmb.company_id =1 + GROUP BY bmb.ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + df.to_sql('back_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_back_apply_details(): + """处理退料申请明细数据""" + try: + # 执行转换SQL + sql = """ + SELECT + bmb.APPLY_NUMBER as code, + bmb.id as parent_id, + ttmt.MA_TYPE_ID as type_id, + ttmt.MACHINES_NUM as pre_num, + ttmt.MACHINES_NUM as audit_num , + ttmt.BACK_BAD_NUM as bad_num, + ttmt.BACK_GOOD_NUM as good_num, + IF(tt.`STATUS` = 4,2,0) as status, + bmb.CREATOR as create_by, + bmb.CREATE_TIME as create_time + FROM + ba_ma_back bmb + LEFT JOIN tm_task tt on bmb.ID = tt.ID + LEFT JOIN tm_task_ma_type ttmt on bmb.ID = ttmt.TASK_ID + WHERE bmb.company_id =1 + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + df.to_sql('back_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_back_check_details(): + """处理退料明细数据""" + try: + # 执行转换SQL + sql = """ + SELECT + bmb.ID as parent_id, + mm.TYPE as type_id, + mm.ID as ma_id, + ttm.CREATOR as create_by, + ttm.CREATE_TIME as create_time, + 1 as back_num , + 1 as back_status , + 1 as good_num, + 0 as bad_num + FROM + ba_ma_back bmb + LEFT JOIN tm_task_ma ttm on bmb.ID = ttm.TASK_ID + LEFT JOIN ma_machines mm on ttm.MA_ID = mm.ID + LEFT JOIN ma_type mt on mm.TYPE = mt.ID + WHERE mt.IS_COUNT = 0 + UNION + SELECT + bmb.id as parent_id, + ttmt.MA_TYPE_ID as type_id, + null as ma_id, + pu.`NAME` as create_by, + bmb.CREATE_TIME as create_time, + ROUND(ttmt.MACHINES_NUM,3) as back_num, + 1 as back_status , + ttmt.BACK_GOOD_NUM as ood_num, + ttmt.BACK_BAD_NUM as bad_num + FROM + ba_ma_back bmb + LEFT JOIN tm_task_ma_type ttmt on bmb.ID = ttmt.TASK_ID + LEFT JOIN pm_user pu on bmb.CREATOR = pu.ID + WHERE ttmt.IS_COUNT =1 + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + df.to_sql('back_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__": + # 执行5个转换流程 + success1 = process_tm_task() + success2 = process_tm_task_agreement() + success3 = process_back_apply_info() + success4 = process_back_apply_details() + success5 = process_back_check_details() + + if success1 and success2 and success3 and success4 and success5: + print("所有数据转换完成!") + else: + print("数据转换过程中出现错误,请检查日志") \ No newline at end of file diff --git a/machines/领料.py b/machines/领料.py new file mode 100644 index 0000000..8b8c896 --- /dev/null +++ b/machines/领料.py @@ -0,0 +1,217 @@ +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_tm_task(): + try: + # 执行转换SQL + sql = """ + SELECT + tt.ID as task_id, + 2 as task_type, + IF(SUM(ttmt.MACHINES_NUM)- SUM(ttmt.ACTUAL_NUM )>0,3,4) as task_status, + bmc.APPLY_NUMBER as code, + pu.`NAME` as create_by, + bmc.CREATE_TIME as create_time + FROM + ba_ma_collar bmc + LEFT JOIN tm_task tt on bmc.ID = tt.ID + LEFT JOIN tm_task_ma_type ttmt on bmc.ID = ttmt.TASK_ID + LEFT JOIN pm_user pu on bmc.CREATOR = pu.ID + WHERE bmc.company_id =1 + GROUP BY bmc.ID + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + 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_tm_task_agreement(): + try: + # 执行转换SQL + sql = """ + SELECT + tt.ID as task_id, + bat.AGREEMENT_ID as agreement_id + FROM + ba_ma_collar bmc + LEFT JOIN tm_task tt on bmc.ID = tt.ID + LEFT JOIN ba_agreement_task bat on bmc.ID = bat.TASK_ID + WHERE bmc.company_id =1 + """ + df = pd.read_sql(sql, source_engine) + + # 写入目标表 + df.to_sql('tm_task_agreement', 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_info(): + try: + # 执行转换SQL + sql = """ + SELECT + bmc.ID as id, + bmc.APPLY_NUMBER as code, + tt.ID as task_id, + bmc.COLLAR_MAN as lease_person, + bmc.COLLAR_PHONE as phone, + pu.`NAME` as create_by, + bmc.CREATE_TIME as create_time, + bmc.direct_id as direct_id, + bma.LEASE_COMPANY as unit_id, + bma.PROJECT as project_id + FROM + ba_ma_collar bmc + LEFT JOIN tm_task tt on bmc.ID = tt.ID + LEFT JOIN ba_agreement_task bat on bmc.ID = bat.TASK_ID + LEFT JOIN ba_ma_agreement bma on bat.AGREEMENT_ID = bma.ID + LEFT JOIN pm_user pu on bmc.CREATOR = pu.ID + WHERE bmc.company_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(): + """处理租赁申请明细数据""" + try: + # 执行转换SQL + sql = """ + SELECT + bmc.ID as parent_id, + ttmt.MA_TYPE_ID as type_id, + ttmt.MACHINES_NUM as pre_num, + ttmt.ACTUAL_NUM as al_num, + IF(ttmt.MACHINES_NUM - ttmt.ACTUAL_NUM =0,2,0) AS status + FROM + ba_ma_collar bmc + LEFT JOIN tm_task_ma_type ttmt on bmc.ID = ttmt.TASK_ID + WHERE bmc.company_id =1 + AND ttmt.MA_TYPE_ID IS NOT NULL + """ + 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 + bmc.ID as parent_id, + mm.TYPE as type_id, + mm.ID as ma_id, + ttm.CREATOR as create_by, + ttm.CREATE_TIME as create_time, + 1 as out_num + FROM + ba_ma_collar bmc + LEFT JOIN tm_task_ma ttm on bmc.ID = ttm.TASK_ID + LEFT JOIN ma_machines mm on ttm.MA_ID = mm.ID + LEFT JOIN ma_type mt on mm.TYPE = mt.ID + WHERE bmc.company_id =1 AND mt.IS_COUNT = 0 + UNION + SELECT + bmc.ID as parent_id, + ttot.MA_TYPE_ID as type_id, + null as ma_id, + pu.`NAME` as create_by, + ttot.CREATE_TIME as create_time, + ttot.NUM as out_num + FROM + ba_ma_collar bmc + LEFT JOIN tm_task_out_type ttot on bmc.ID = ttot.TASK_ID + LEFT JOIN ma_type mt on ttot.MA_TYPE_ID = mt.ID + LEFT JOIN pm_user pu on ttot.CREATOR = pu.ID + WHERE bmc.company_id =1 AND ttot.IS_COUNT =1 + """ + 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__": + # 执行5个转换流程 + success1 = process_tm_task() + success2 = process_tm_task_agreement() + success3 = process_lease_apply_info() + success4 = process_lease_apply_details() + success5 = process_lease_out_details() + + if success1 and success2 and success3 and success4 and success5: + print("所有数据转换完成!") + else: + print("数据转换过程中出现错误,请检查日志") \ No newline at end of file