Bonus-Transfer-Machines/安全/结算.py

187 lines
6.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import configparser
import pandas as pd
from sqlalchemy import create_engine
from urllib.parse import quote_plus
import numpy as np
# 读取配置文件
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']}"
)
# 定义type映射关系
type_mapping = {
6: 'sgd', # 独立施工队
7: 'xmb', # 项目部
122: 'fbs', # 分包商
126: 'hq' # 后勤
}
def get_agreement_id(unit_id, project_id, type_code):
"""根据组合条件查询agreement_id"""
try:
# 检查参数有效性
if pd.isna(unit_id) or pd.isna(project_id) or pd.isna(type_code):
return None
sql = f"""
SELECT (bpr.ID + 500000) as agreement_id
FROM bm_project_relation bpr
WHERE bpr.UNIT_ID = {int(unit_id)}
AND bpr.project_id = {int(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出错: unit_id={unit_id}, project_id={project_id}, type={type_code}, 错误: {str(e)}")
return None
def process_slt_agreement():
"""处理租赁结算信息"""
try:
# 第一步:查询基础数据
base_sql = """
SELECT
mps.DEPT_ID,
mps.SUB_ID,
mps.TEAM_ID,
mps.REAR_ID,
mps.PROJECT_ID,
mps.TYPE_ID as type,
(mps.MACHINE_ID+70000) as ma_id,
(mps.MATYPE_ID+6000) as type_id,
mps.NUM,
IF(mps.`STATUS` = 1, 0, 1) as status,
mps.LEASE_PRICE as lease_price,
mps.OUT_TIME as start_time,
mps.BACK_TIME as end_time,
(mps.PICK_ID+500000) as lease_id,
(mps.BACK_ID+500000) as back_id,
mps.IS_SLT as is_slt,
mps.SETTLEMENT_TIME as slt_time
FROM ma_type_project_storage mps
"""
base_df = pd.read_sql(base_sql, source_engine)
# 填充空值并确保数值类型
base_df = base_df.fillna({
'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'
})
# 第二步处理每个记录获取agreement_id
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 = row['TEAM_ID'] + 5000
type_code = 'sgd'
elif row['type'] == 7: # 项目部
if row['DEPT_ID'] > 0:
unit_id = row['DEPT_ID'] + 4000
type_code = 'xmb'
elif row['type'] == 122: # 分包商
if row['SUB_ID'] > 0:
unit_id = row['SUB_ID'] + 6000
type_code = 'fbs'
elif row['type'] == 126: # 后勤
if row['REAR_ID'] > 0:
unit_id = row['REAR_ID'] + 6000
type_code = 'hq'
# 检查必要参数是否有效
if not unit_id or not type_code or row['PROJECT_ID'] <= 0:
skipped_records += 1
continue
# 查询agreement_id
agreement_id = get_agreement_id(unit_id, row['PROJECT_ID'] + 3000, type_code)
if agreement_id is None:
skipped_records += 1
continue
# 组装结果
result = {
'agreement_id': agreement_id,
'type_id': row['type_id'],
'ma_id': row['ma_id'],
'num': row['NUM'],
'status': row['status'],
'lease_price': row['lease_price'],
'start_time': row['start_time'],
'end_time': row['end_time'],
'lease_id': row['lease_id'],
'back_id': row['back_id'],
'is_slt': row['is_slt'],
'slt_time': row['slt_time'],
'buy_price': None # 根据业务需要补充
}
results.append(result)
except Exception as e:
print(f"处理记录时出错: {str(e)},记录内容: {row}")
skipped_records += 1
continue
# 转换为DataFrame并写入
if results:
result_df = pd.DataFrame(results)
result_df.to_sql('slt_agreement_info', target_engine,
if_exists='append', index=False)
print(f"成功导入 {len(result_df)} 条租赁结算信息,跳过 {skipped_records} 条无效记录")
else:
print("没有符合条件的数据需要导入")
return True
except Exception as e:
print(f"处理租赁结算信息时出错: {str(e)}")
return False
if __name__ == "__main__":
process_slt_agreement()