|
@@ -92,6 +92,25 @@ def data_process():
|
|
area_map[x['area_id']] = x # 将区域ID与详细信息存入area_map
|
|
area_map[x['area_id']] = x # 将区域ID与详细信息存入area_map
|
|
# 读取Excel文件中的数据,并跳过第一行
|
|
# 读取Excel文件中的数据,并跳过第一行
|
|
df = pd.read_excel(io=input_path, skiprows=1)
|
|
df = pd.read_excel(io=input_path, skiprows=1)
|
|
|
|
+ # 获取当前 DataFrame 的列名列表
|
|
|
|
+ columns = df.columns.tolist()
|
|
|
|
+ # 定义所需的字段列表
|
|
|
|
+ required_columns = ['序号', '数据编码', '租入房屋名称', '房屋所有权人属性', '租入形式', '标准地址(一级)',
|
|
|
|
+ '标准地址(二级)', '标准地址(三级)', '街/路/村门牌号', '城市区域', '地段', '地址经度坐标',
|
|
|
|
+ '地址纬度坐标', '租入建筑面积(平米)', '租入使用面积(平米)', '投资主体', '使用单位层级',
|
|
|
|
+ '使用单位隶属的省级公司', '使用单位隶属的地市级公司', '使用单位隶属的区县级公司', '使用专业线',
|
|
|
|
+ '租入实际用途', '租入其他用途说明', '自用建筑面积(平米)', '转租建筑面积(平米)', '首次租用时间',
|
|
|
|
+ '合同编号', '合同名称', '合同类型', '签订时间', '我方签约主体', '出租方名称',
|
|
|
|
+ '合同总金额(含税)(元)', '增值税金额(元)', '租入开始时间(合同生效时间)',
|
|
|
|
+ '租入终止时间(合同终止时间)', '承办部门', '承办人', '联系电话', '累计计提金额(元)',
|
|
|
|
+ '费用报账总金额(元)', '合同性质', '合同状态']
|
|
|
|
+ # 检查是否有缺失的字段
|
|
|
|
+ missing_columns = [col for col in required_columns if col not in columns]
|
|
|
|
+ # 检查是否有多余的字段
|
|
|
|
+ ex_columns = [col for col in columns if col not in required_columns]
|
|
|
|
+ # 如果存在缺失字段,则抛出运行时错误并提示缺少哪些字段
|
|
|
|
+ if missing_columns or ex_columns:
|
|
|
|
+ raise RuntimeError(f"缺少以下字段: {missing_columns};存在以下多余字段:{ex_columns}")
|
|
# 删除指定列中的空白字符
|
|
# 删除指定列中的空白字符
|
|
columns_to_clean = list(filter(lambda x: x not in ('签订时间'), df.columns)) # 排除“签订时间”列
|
|
columns_to_clean = list(filter(lambda x: x not in ('签订时间'), df.columns)) # 排除“签订时间”列
|
|
df[columns_to_clean] = df[columns_to_clean].map(lambda x: re.sub(r'\s+', '', x) if type(x) is str else x)
|
|
df[columns_to_clean] = df[columns_to_clean].map(lambda x: re.sub(r'\s+', '', x) if type(x) is str else x)
|
|
@@ -110,22 +129,22 @@ def data_process():
|
|
return area_no
|
|
return area_no
|
|
raise RuntimeError(f'二级组织机构编码匹配失败: {second_unit}')
|
|
raise RuntimeError(f'二级组织机构编码匹配失败: {second_unit}')
|
|
|
|
|
|
- df['二级组织机构编码'] = df.apply(get_area_no, axis=1)
|
|
|
|
|
|
+ df['area_no'] = df.apply(get_area_no, axis=1)
|
|
|
|
|
|
def get_area_name(x):
|
|
def get_area_name(x):
|
|
"""根据二级组织机构编码获取二级组织机构名称"""
|
|
"""根据二级组织机构编码获取二级组织机构名称"""
|
|
- area_no = x['二级组织机构编码']
|
|
|
|
|
|
+ area_no = x['area_no']
|
|
second_org = org_map[area_no]
|
|
second_org = org_map[area_no]
|
|
area_name = second_org['name']
|
|
area_name = second_org['name']
|
|
return area_name
|
|
return area_name
|
|
|
|
|
|
- df['二级组织机构名称'] = df.apply(get_area_name, axis=1)
|
|
|
|
|
|
+ df['area_name'] = df.apply(get_area_name, axis=1)
|
|
|
|
|
|
def get_city_no(x):
|
|
def get_city_no(x):
|
|
"""根据使用单位隶属的区县级公司名称获取三级组织机构编码"""
|
|
"""根据使用单位隶属的区县级公司名称获取三级组织机构编码"""
|
|
third_unit = x['使用单位隶属的区县级公司']
|
|
third_unit = x['使用单位隶属的区县级公司']
|
|
- area_name = x['二级组织机构名称']
|
|
|
|
- area_no = x['二级组织机构编码']
|
|
|
|
|
|
+ area_name = x['area_name']
|
|
|
|
+ area_no = x['area_no']
|
|
if area_name == '石家庄':
|
|
if area_name == '石家庄':
|
|
if '矿区' in third_unit:
|
|
if '矿区' in third_unit:
|
|
return 'D0130185'
|
|
return 'D0130185'
|
|
@@ -208,16 +227,16 @@ def data_process():
|
|
return 'D0130830'
|
|
return 'D0130830'
|
|
return 'HE001'
|
|
return 'HE001'
|
|
|
|
|
|
- df['三级组织机构编码'] = df.apply(get_city_no, axis=1)
|
|
|
|
|
|
+ df['city_no'] = df.apply(get_city_no, axis=1)
|
|
|
|
|
|
def get_city_name(x):
|
|
def get_city_name(x):
|
|
"""根据三级组织机构编码获取三级组织机构名称"""
|
|
"""根据三级组织机构编码获取三级组织机构名称"""
|
|
- city_no = x['三级组织机构编码']
|
|
|
|
|
|
+ city_no = x['city_no']
|
|
third_org = org_map[city_no]
|
|
third_org = org_map[city_no]
|
|
city_name = third_org['name']
|
|
city_name = third_org['name']
|
|
return city_name
|
|
return city_name
|
|
|
|
|
|
- df['三级组织机构名称'] = df.apply(get_city_name, axis=1)
|
|
|
|
|
|
+ df['city_name'] = df.apply(get_city_name, axis=1)
|
|
|
|
|
|
def get_rent_months(x):
|
|
def get_rent_months(x):
|
|
"""根据租入开始时间和终止时间计算租期月数"""
|
|
"""根据租入开始时间和终止时间计算租期月数"""
|
|
@@ -231,32 +250,32 @@ def data_process():
|
|
rent_months = delta.years * 12 + delta.months + (1 if delta.days > 0 else 0)
|
|
rent_months = delta.years * 12 + delta.months + (1 if delta.days > 0 else 0)
|
|
return rent_months
|
|
return rent_months
|
|
|
|
|
|
- df['租期月数'] = df.apply(get_rent_months, axis=1)
|
|
|
|
|
|
+ df['rent_months'] = df.apply(get_rent_months, axis=1)
|
|
|
|
|
|
def get_gross_amount_month(x):
|
|
def get_gross_amount_month(x):
|
|
"""根据合同总金额和租期月数计算月含税合同额"""
|
|
"""根据合同总金额和租期月数计算月含税合同额"""
|
|
gross_amount = x['合同总金额(含税)(元)']
|
|
gross_amount = x['合同总金额(含税)(元)']
|
|
- rent_months = x['租期月数']
|
|
|
|
|
|
+ rent_months = x['rent_months']
|
|
if pd.notna(gross_amount) and pd.notna(rent_months) and rent_months and rent_months > 0:
|
|
if pd.notna(gross_amount) and pd.notna(rent_months) and rent_months and rent_months > 0:
|
|
return (decimal.Decimal(gross_amount) / decimal.Decimal(rent_months)).quantize(decimal.Decimal('0.00'))
|
|
return (decimal.Decimal(gross_amount) / decimal.Decimal(rent_months)).quantize(decimal.Decimal('0.00'))
|
|
return None
|
|
return None
|
|
|
|
|
|
- df['月含税合同额'] = df.apply(get_gross_amount_month, axis=1)
|
|
|
|
|
|
+ df['gross_amount_month'] = df.apply(get_gross_amount_month, axis=1)
|
|
|
|
|
|
def get_unit_price(x):
|
|
def get_unit_price(x):
|
|
"""根据租入建筑面积和月含税合同额计算每平米单价"""
|
|
"""根据租入建筑面积和月含税合同额计算每平米单价"""
|
|
building_area = x['租入建筑面积(平米)']
|
|
building_area = x['租入建筑面积(平米)']
|
|
- gross_amount_month = x['月含税合同额']
|
|
|
|
|
|
+ gross_amount_month = x['gross_amount_month']
|
|
if pd.notna(building_area) and pd.notna(gross_amount_month) and building_area > 0 and gross_amount_month > 0:
|
|
if pd.notna(building_area) and pd.notna(gross_amount_month) and building_area > 0 and gross_amount_month > 0:
|
|
return (decimal.Decimal(gross_amount_month) / decimal.Decimal(building_area)).quantize(
|
|
return (decimal.Decimal(gross_amount_month) / decimal.Decimal(building_area)).quantize(
|
|
decimal.Decimal('0.00'))
|
|
decimal.Decimal('0.00'))
|
|
return None
|
|
return None
|
|
|
|
|
|
- df['每平米单价'] = df.apply(get_unit_price, axis=1)
|
|
|
|
|
|
+ df['unit_price'] = df.apply(get_unit_price, axis=1)
|
|
|
|
|
|
def get_rent_years(x):
|
|
def get_rent_years(x):
|
|
"""根据租期月数计算租期年数"""
|
|
"""根据租期月数计算租期年数"""
|
|
- rent_months = x['租期月数']
|
|
|
|
|
|
+ rent_months = x['rent_months']
|
|
if pd.isna(rent_months) or not rent_months:
|
|
if pd.isna(rent_months) or not rent_months:
|
|
return None
|
|
return None
|
|
return (decimal.Decimal(rent_months) / decimal.Decimal('12')).quantize(decimal.Decimal('0.00'))
|
|
return (decimal.Decimal(rent_months) / decimal.Decimal('12')).quantize(decimal.Decimal('0.00'))
|
|
@@ -290,22 +309,36 @@ def data_process():
|
|
|
|
|
|
df['地址经度坐标'] = df['地址经度坐标'].map(remove_extra_dots)
|
|
df['地址经度坐标'] = df['地址经度坐标'].map(remove_extra_dots)
|
|
df['地址纬度坐标'] = df['地址纬度坐标'].map(remove_extra_dots)
|
|
df['地址纬度坐标'] = df['地址纬度坐标'].map(remove_extra_dots)
|
|
- df.insert(0, '年月', year_month) # 在数据框的第一列插入年月字段
|
|
|
|
|
|
+ df.insert(0, 'year_month', year_month) # 在数据框的第一列插入年月字段
|
|
|
|
+ df.rename(
|
|
|
|
+ columns={'序号': 'serial_no', '数据编码': 'data_num', '租入房屋名称': 'house_name', '房屋所有权人属性': 'owner_type',
|
|
|
|
+ '租入形式': 'rent_type', '标准地址(一级)': 'first_address', '标准地址(二级)': 'second_address',
|
|
|
|
+ '标准地址(三级)': 'third_address', '街/路/村门牌号': 'fourth_address', '城市区域': 'city_region',
|
|
|
|
+ '地段': 'area_sector', '地址经度坐标': 'lng', '地址纬度坐标': 'lat', '租入建筑面积(平米)': 'building_area',
|
|
|
|
+ '租入使用面积(平米)': 'usable_area', '投资主体': 'investor', '使用单位层级': 'unit_level',
|
|
|
|
+ '使用单位隶属的省级公司': 'first_unit', '使用单位隶属的地市级公司': 'second_unit',
|
|
|
|
+ '使用单位隶属的区县级公司': 'third_unit', '使用专业线': 'field', '租入实际用途': 'use_type',
|
|
|
|
+ '租入其他用途说明': 'use_description', '自用建筑面积(平米)': 'building_area_self_use',
|
|
|
|
+ '转租建筑面积(平米)': 'building_area_sublet', '首次租用时间': 'first_rent_date', '合同编号': 'contract_no',
|
|
|
|
+ '合同名称': 'contract_name', '合同类型': 'contract_type', '签订时间': 'sign_date', '我方签约主体': 'lessee',
|
|
|
|
+ '出租方名称': 'lessor', '合同总金额(含税)(元)': 'gross_amount', '增值税金额(元)': 'vat',
|
|
|
|
+ '租入开始时间(合同生效时间)': 'rent_start_date', '租入终止时间(合同终止时间)': 'rent_end_date',
|
|
|
|
+ '承办部门': 'undertaking_department', '承办人': 'undertaker', '联系电话': 'phone',
|
|
|
|
+ '累计计提金额(元)': 'amount_accrued', '费用报账总金额(元)': 'amount_reimbursement',
|
|
|
|
+ '合同性质': 'contract_nature', '合同状态': 'contract_status'}, inplace=True)
|
|
|
|
+ df = df[['year_month', 'serial_no', 'data_num', 'house_name', 'owner_type', 'rent_type', 'first_address',
|
|
|
|
+ 'second_address', 'third_address', 'fourth_address', 'city_region', 'area_sector', 'lng', 'lat',
|
|
|
|
+ 'building_area', 'usable_area', 'investor', 'unit_level', 'first_unit', 'second_unit',
|
|
|
|
+ 'third_unit', 'field', 'use_type', 'use_description', 'building_area_self_use',
|
|
|
|
+ 'building_area_sublet', 'first_rent_date', 'contract_no', 'contract_name', 'contract_type',
|
|
|
|
+ 'sign_date', 'lessee', 'lessor', 'gross_amount', 'vat', 'rent_start_date', 'rent_end_date',
|
|
|
|
+ 'undertaking_department', 'undertaker', 'phone', 'amount_accrued', 'amount_reimbursement',
|
|
|
|
+ 'contract_nature', 'contract_status', 'area_no', 'area_name', 'city_no', 'city_name',
|
|
|
|
+ 'rent_months', 'gross_amount_month', 'unit_price', 'rent_years', 'unit_price2']]
|
|
# 打印数据框的基本信息
|
|
# 打印数据框的基本信息
|
|
- print(df.info())
|
|
|
|
|
|
+ df.info()
|
|
# 将处理后的数据保存到CSV文件中
|
|
# 将处理后的数据保存到CSV文件中
|
|
- df.to_csv(path_or_buf=output_path,
|
|
|
|
- index=False,
|
|
|
|
- header=['year_month', 'serial_no', 'data_num', 'house_name', 'owner_type', 'rent_type', 'first_address',
|
|
|
|
- 'second_address', 'third_address', 'fourth_address', 'city_region', 'area_sector', 'lng', 'lat',
|
|
|
|
- 'building_area', 'usable_area', 'investor', 'unit_level', 'first_unit', 'second_unit',
|
|
|
|
- 'third_unit', 'field', 'use_type', 'use_description', 'building_area_self_use',
|
|
|
|
- 'building_area_sublet', 'first_rent_date', 'contract_no', 'contract_name', 'contract_type',
|
|
|
|
- 'sign_date', 'lessee', 'lessor', 'gross_amount', 'vat', 'rent_start_date', 'rent_end_date',
|
|
|
|
- 'undertaking_department', 'undertaker', 'phone', 'amount_accrued', 'amount_reimbursement',
|
|
|
|
- 'contract_nature', 'contract_status', 'area_no', 'area_name', 'city_no', 'city_name',
|
|
|
|
- 'rent_months', 'gross_amount_month', 'unit_price', 'rent_years', 'unit_price2'],
|
|
|
|
- encoding='utf-8-sig')
|
|
|
|
|
|
+ df.to_csv(path_or_buf=output_path, index=False, encoding='utf-8-sig', lineterminator='\n')
|
|
|
|
|
|
|
|
|
|
def data_import():
|
|
def data_import():
|
|
@@ -316,7 +349,7 @@ def data_import():
|
|
# 表字段列名,用于指定导入数据的列顺序
|
|
# 表字段列名,用于指定导入数据的列顺序
|
|
columns = "year_month,serial_no,data_num,house_name,owner_type,rent_type,first_address,second_address,third_address,fourth_address,city_region,area_sector,lng,lat,building_area,usable_area,investor,unit_level,first_unit,second_unit,third_unit,field,use_type,use_description,building_area_self_use,building_area_sublet,first_rent_date,contract_no,contract_name,contract_type,sign_date,lessee,lessor,gross_amount,vat,rent_start_date,rent_end_date,undertaking_department,undertaker,phone,amount_accrued,amount_reimbursement,contract_nature,contract_status,area_no,area_name,city_no,city_name,rent_months,gross_amount_month,unit_price,rent_years,unit_price2"
|
|
columns = "year_month,serial_no,data_num,house_name,owner_type,rent_type,first_address,second_address,third_address,fourth_address,city_region,area_sector,lng,lat,building_area,usable_area,investor,unit_level,first_unit,second_unit,third_unit,field,use_type,use_description,building_area_self_use,building_area_sublet,first_rent_date,contract_no,contract_name,contract_type,sign_date,lessee,lessor,gross_amount,vat,rent_start_date,rent_end_date,undertaking_department,undertaker,phone,amount_accrued,amount_reimbursement,contract_nature,contract_status,area_no,area_name,city_no,city_name,rent_months,gross_amount_month,unit_price,rent_years,unit_price2"
|
|
# 构造执行 PowerShell 脚本的命令
|
|
# 构造执行 PowerShell 脚本的命令
|
|
- command = f"powershell -File {script_path} -db_host {db_host} -db_port {db_port} -db_username {db_username} -db_password {db_password} -dbname {dbname} -table {table} -filename {output_path} -columns {columns}"
|
|
|
|
|
|
+ command = f"powershell -NoProfile -NonInteractive -File {script_path} -db_host {db_host} -db_port {db_port} -db_username {db_username} -db_password {db_password} -dbname {dbname} -table {table} -filename {output_path} -columns {columns}"
|
|
# 打印生成的命令,方便调试和日志记录
|
|
# 打印生成的命令,方便调试和日志记录
|
|
logger.info("command: {}", command)
|
|
logger.info("command: {}", command)
|
|
# 使用 subprocess 模块运行 PowerShell 命令,并捕获输出
|
|
# 使用 subprocess 模块运行 PowerShell 命令,并捕获输出
|