增加了知识图谱导出excel
This commit is contained in:
@@ -15,9 +15,9 @@ from typing import Any, Dict, Optional, Set
|
||||
import sys
|
||||
import re
|
||||
|
||||
# 添加一个全局变量和一个缓存字典来存储清单数量
|
||||
_BILL_QUANTITY = 1.0
|
||||
bill_quantity_cache = {}
|
||||
# 移除全局变量声明
|
||||
# _BILL_QUANTITY = 1.0
|
||||
# bill_quantity_cache = {}
|
||||
|
||||
|
||||
# 为每种软件类型定义特定的计算策略
|
||||
@@ -90,46 +90,42 @@ class MainGridBillCalculationStrategy(DefaultCalculationStrategy):
|
||||
|
||||
result = calculator.calculate(var_name, context)
|
||||
|
||||
# 获取清单数量 - 首先从全局变量获取
|
||||
global _BILL_QUANTITY # 在模块级别定义一个全局变量
|
||||
parent_quantity = _BILL_QUANTITY if "_BILL_QUANTITY" in globals() else 1.0
|
||||
# 获取清单数量 - 首先尝试从上下文中获取
|
||||
parent_quantity = 1.0 # 默认值
|
||||
|
||||
print(f"\n===== 获取清单数量 =====")
|
||||
print(f"从全局变量获取清单数量: {parent_quantity}")
|
||||
|
||||
# 如果全局变量没有设置正确的值,尝试从bill_quantity_cache获取
|
||||
if parent_quantity == 1.0 and hasattr(context, "__dict__"):
|
||||
module = sys.modules.get("quantity_fee_calculator")
|
||||
if module and hasattr(module, "bill_quantity_cache"):
|
||||
bill_cache = getattr(module, "bill_quantity_cache")
|
||||
bill_id = None
|
||||
# 1. 首先尝试从计算策略中获取
|
||||
if hasattr(self, "bill_quantity"):
|
||||
parent_quantity = self.bill_quantity
|
||||
print(f"从计算策略获取清单数量: {parent_quantity}")
|
||||
|
||||
# 尝试从上下文中获取bill_id
|
||||
if hasattr(context, "bill_id"):
|
||||
bill_id = context.bill_id
|
||||
elif hasattr(context, "datasource") and context.datasource:
|
||||
for item in context.datasource:
|
||||
if hasattr(item, "object") and hasattr(item.object, "bill_id"):
|
||||
bill_id = item.object.bill_id
|
||||
# 2. 如果计算策略中没有,尝试从上下文中获取
|
||||
if parent_quantity == 1.0 and context:
|
||||
# 尝试从上下文的数据源中获取
|
||||
if hasattr(context, "datasource") and context.datasource:
|
||||
for item in context.datasource:
|
||||
# 检查是否有bill_quantity属性
|
||||
if hasattr(item, "bill_quantity"):
|
||||
parent_quantity = item.bill_quantity
|
||||
print(f"从数据源项获取清单数量: {parent_quantity}")
|
||||
break
|
||||
# 检查是否有object属性,且object有quantity属性
|
||||
if hasattr(item, "object") and hasattr(item.object, "quantity"):
|
||||
parent_quantity = item.object.quantity
|
||||
print(f"从数据源项对象获取清单数量: {parent_quantity}")
|
||||
break
|
||||
|
||||
# 尝试从上下文的父级上下文中获取
|
||||
if parent_quantity == 1.0 and hasattr(context, "prevContext"):
|
||||
prev_context = context.prevContext
|
||||
if prev_context and hasattr(prev_context, "datasource") and prev_context.datasource:
|
||||
for item in prev_context.datasource:
|
||||
if hasattr(item, "object") and hasattr(item.object, "quantity"):
|
||||
parent_quantity = item.object.quantity
|
||||
print(f"从父级上下文获取清单数量: {parent_quantity}")
|
||||
break
|
||||
|
||||
# 如果找到bill_id并且在缓存中,使用缓存的数量
|
||||
if bill_id and bill_id in bill_cache:
|
||||
parent_quantity = bill_cache[bill_id]
|
||||
print(f"从bill_quantity_cache获取清单{bill_id}的数量: {parent_quantity}")
|
||||
|
||||
# 设置一个临时环境变量作为最后的手段
|
||||
if parent_quantity == 1.0:
|
||||
import os
|
||||
|
||||
env_quantity = os.environ.get("BILL_QUANTITY")
|
||||
if env_quantity:
|
||||
try:
|
||||
parent_quantity = float(env_quantity)
|
||||
print(f"从环境变量获取清单数量: {parent_quantity}")
|
||||
except:
|
||||
pass
|
||||
|
||||
print(f"===== 获取清单数量结束 =====\n")
|
||||
|
||||
# 将结果乘以父级清单的数量
|
||||
@@ -260,20 +256,18 @@ class TechnicalRenovationBudgetCalculationStrategy(DefaultCalculationStrategy):
|
||||
class TechnicalRenovationBillCalculationStrategy(DefaultCalculationStrategy):
|
||||
"""技改清单计算策略"""
|
||||
|
||||
# def calculate_fee_base(
|
||||
# self, fee_base: str, cost_table: dict, project_node: dict, context, in_calculation=None
|
||||
# ) -> float:
|
||||
# """计算取费基数,可以重写以添加技改清单特定的计算规则"""
|
||||
# # 示例:如果取费基数是特定值,使用特定的计算规则
|
||||
# if fee_base == "人工费+材料费+机械费":
|
||||
# print(f"应用技改清单特定的取费基数计算规则: {fee_base}")
|
||||
# # 这里可以添加特定的计算逻辑
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.bill_quantity = 1.0 # 默认值
|
||||
|
||||
# # 暂时仍然使用默认实现
|
||||
# return super().calculate_fee_base(fee_base, cost_table, project_node, context, in_calculation)
|
||||
def set_bill_quantity(self, quantity):
|
||||
"""设置清单数量"""
|
||||
try:
|
||||
self.bill_quantity = float(quantity)
|
||||
print(f"设置计算策略的清单数量: {self.bill_quantity}")
|
||||
except (TypeError, ValueError):
|
||||
print(f"无法将 {quantity} 转换为浮点数")
|
||||
|
||||
# # 对于其他取费基数,使用默认实现
|
||||
# return super().calculate_fee_base(fee_base, cost_table, project_node, context, in_calculation)
|
||||
def calculate_external_variable(self, var_name: str, context: Any) -> float:
|
||||
"""计算表外变量,并乘以父级清单的数量"""
|
||||
# 使用默认实现计算表外变量
|
||||
@@ -281,46 +275,42 @@ class TechnicalRenovationBillCalculationStrategy(DefaultCalculationStrategy):
|
||||
|
||||
result = calculator.calculate(var_name, context)
|
||||
|
||||
# 获取清单数量 - 首先从全局变量获取
|
||||
global _BILL_QUANTITY # 在模块级别定义一个全局变量
|
||||
parent_quantity = _BILL_QUANTITY if "_BILL_QUANTITY" in globals() else 1.0
|
||||
# 获取清单数量 - 首先尝试从上下文中获取
|
||||
parent_quantity = 1.0 # 默认值
|
||||
|
||||
print(f"\n===== 获取清单数量 =====")
|
||||
print(f"从全局变量获取清单数量: {parent_quantity}")
|
||||
|
||||
# 如果全局变量没有设置正确的值,尝试从bill_quantity_cache获取
|
||||
if parent_quantity == 1.0 and hasattr(context, "__dict__"):
|
||||
module = sys.modules.get("quantity_fee_calculator")
|
||||
if module and hasattr(module, "bill_quantity_cache"):
|
||||
bill_cache = getattr(module, "bill_quantity_cache")
|
||||
bill_id = None
|
||||
# 1. 首先尝试从计算策略中获取
|
||||
if hasattr(self, "bill_quantity"):
|
||||
parent_quantity = self.bill_quantity
|
||||
print(f"从计算策略获取清单数量: {parent_quantity}")
|
||||
|
||||
# 尝试从上下文中获取bill_id
|
||||
if hasattr(context, "bill_id"):
|
||||
bill_id = context.bill_id
|
||||
elif hasattr(context, "datasource") and context.datasource:
|
||||
for item in context.datasource:
|
||||
if hasattr(item, "object") and hasattr(item.object, "bill_id"):
|
||||
bill_id = item.object.bill_id
|
||||
# 2. 如果计算策略中没有,尝试从上下文中获取
|
||||
if parent_quantity == 1.0 and context:
|
||||
# 尝试从上下文的数据源中获取
|
||||
if hasattr(context, "datasource") and context.datasource:
|
||||
for item in context.datasource:
|
||||
# 检查是否有bill_quantity属性
|
||||
if hasattr(item, "bill_quantity"):
|
||||
parent_quantity = item.bill_quantity
|
||||
print(f"从数据源项获取清单数量: {parent_quantity}")
|
||||
break
|
||||
# 检查是否有object属性,且object有quantity属性
|
||||
if hasattr(item, "object") and hasattr(item.object, "quantity"):
|
||||
parent_quantity = item.object.quantity
|
||||
print(f"从数据源项对象获取清单数量: {parent_quantity}")
|
||||
break
|
||||
|
||||
# 尝试从上下文的父级上下文中获取
|
||||
if parent_quantity == 1.0 and hasattr(context, "prevContext"):
|
||||
prev_context = context.prevContext
|
||||
if prev_context and hasattr(prev_context, "datasource") and prev_context.datasource:
|
||||
for item in prev_context.datasource:
|
||||
if hasattr(item, "object") and hasattr(item.object, "quantity"):
|
||||
parent_quantity = item.object.quantity
|
||||
print(f"从父级上下文获取清单数量: {parent_quantity}")
|
||||
break
|
||||
|
||||
# 如果找到bill_id并且在缓存中,使用缓存的数量
|
||||
if bill_id and bill_id in bill_cache:
|
||||
parent_quantity = bill_cache[bill_id]
|
||||
print(f"从bill_quantity_cache获取清单{bill_id}的数量: {parent_quantity}")
|
||||
|
||||
# 设置一个临时环境变量作为最后的手段
|
||||
if parent_quantity == 1.0:
|
||||
import os
|
||||
|
||||
env_quantity = os.environ.get("BILL_QUANTITY")
|
||||
if env_quantity:
|
||||
try:
|
||||
parent_quantity = float(env_quantity)
|
||||
print(f"从环境变量获取清单数量: {parent_quantity}")
|
||||
except:
|
||||
pass
|
||||
|
||||
print(f"===== 获取清单数量结束 =====\n")
|
||||
|
||||
# 将结果乘以父级清单的数量
|
||||
@@ -401,14 +391,6 @@ class MainGridBudgetCalculator(CalculatorBase):
|
||||
"""创建主网预算计算策略"""
|
||||
return MainGridBudgetCalculationStrategy()
|
||||
|
||||
def set_output_dir(self, output_dir):
|
||||
"""
|
||||
设置计算结果的输出目录
|
||||
|
||||
:param output_dir: 输出目录的路径
|
||||
"""
|
||||
self.output_dir = output_dir
|
||||
|
||||
def apply_quantity_fee_rules(self) -> None:
|
||||
"""应用主网预算特定的工程量取费规则"""
|
||||
print(f"应用{self.software_type.name}特定的工程量取费规则 - 对节点进行预处理")
|
||||
@@ -475,14 +457,6 @@ class MainGridBillCalculator(CalculatorBase):
|
||||
"""创建主网清单计算策略"""
|
||||
return MainGridBillCalculationStrategy()
|
||||
|
||||
def set_output_dir(self, output_dir):
|
||||
"""
|
||||
设置计算结果的输出目录
|
||||
|
||||
:param output_dir: 输出目录的路径
|
||||
"""
|
||||
self.output_dir = output_dir
|
||||
|
||||
def apply_quantity_fee_rules(self) -> None:
|
||||
"""应用主网清单特定的工程量取费规则"""
|
||||
print(f"应用{self.software_type.name}特定的工程量取费规则 - 对节点进行预处理")
|
||||
@@ -547,14 +521,6 @@ class DistributionBudgetCalculator(CalculatorBase):
|
||||
def __init__(self):
|
||||
super().__init__(DISTRIBUTION_BUDGET)
|
||||
|
||||
def set_output_dir(self, output_dir):
|
||||
"""
|
||||
设置计算结果的输出目录
|
||||
|
||||
:param output_dir: 输出目录的路径
|
||||
"""
|
||||
self.output_dir = output_dir
|
||||
|
||||
def create_calculation_strategy(self) -> CalculationStrategy:
|
||||
"""创建配网预算计算策略"""
|
||||
return DistributionBudgetCalculationStrategy()
|
||||
@@ -586,14 +552,6 @@ class DistributionBillCalculator(CalculatorBase):
|
||||
"""创建配网清单计算策略"""
|
||||
return DistributionBillCalculationStrategy()
|
||||
|
||||
def set_output_dir(self, output_dir):
|
||||
"""
|
||||
设置计算结果的输出目录
|
||||
|
||||
:param output_dir: 输出目录的路径
|
||||
"""
|
||||
self.output_dir = output_dir
|
||||
|
||||
def apply_quantity_fee_rules(self) -> None:
|
||||
"""应用配网清单特定的工程量取费规则"""
|
||||
print(f"应用{self.software_type.name}特定的工程量取费规则")
|
||||
@@ -621,14 +579,6 @@ class TechnicalRenovationBudgetCalculator(CalculatorBase):
|
||||
"""创建技改预算计算策略"""
|
||||
return TechnicalRenovationBudgetCalculationStrategy()
|
||||
|
||||
def set_output_dir(self, output_dir):
|
||||
"""
|
||||
设置计算结果的输出目录
|
||||
|
||||
:param output_dir: 输出目录的路径
|
||||
"""
|
||||
self.output_dir = output_dir
|
||||
|
||||
def apply_quantity_fee_rules(self) -> None:
|
||||
"""应用技改预算特定的工程量取费规则"""
|
||||
print(f"应用{self.software_type.name}特定的工程量取费规则")
|
||||
@@ -656,14 +606,6 @@ class TechnicalRenovationBillCalculator(CalculatorBase):
|
||||
"""创建技改清单计算策略"""
|
||||
return TechnicalRenovationBillCalculationStrategy()
|
||||
|
||||
def set_output_dir(self, output_dir):
|
||||
"""
|
||||
设置计算结果的输出目录
|
||||
|
||||
:param output_dir: 输出目录的路径
|
||||
"""
|
||||
self.output_dir = output_dir
|
||||
|
||||
def apply_quantity_fee_rules(self) -> None:
|
||||
"""应用技改清单特定的工程量取费规则"""
|
||||
print(f"应用{self.software_type.name}特定的工程量取费规则")
|
||||
|
||||
Reference in New Issue
Block a user