init
This commit is contained in:
183
web/calculators/__init__.py
Normal file
183
web/calculators/__init__.py
Normal file
@@ -0,0 +1,183 @@
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class Helper:
|
||||
|
||||
@staticmethod
|
||||
def get_last_month(year, month):
|
||||
if month == 1:
|
||||
last_month_year = year - 1
|
||||
last_month = 12
|
||||
else:
|
||||
last_month_year = year
|
||||
last_month = month - 1
|
||||
|
||||
return last_month_year, last_month
|
||||
|
||||
@staticmethod
|
||||
def get_next_month(year, month):
|
||||
if month == 12:
|
||||
last_month_year = year + 1
|
||||
last_month = 1
|
||||
else:
|
||||
last_month_year = year
|
||||
last_month = month + 1
|
||||
|
||||
return last_month_year, last_month
|
||||
|
||||
|
||||
class Calculator:
|
||||
material_id = ""
|
||||
name = ""
|
||||
year = 0
|
||||
month = 0
|
||||
price_ftb = 0
|
||||
fluctuating_ftb = 0
|
||||
price_ss = 0
|
||||
fluctuating_ss = 0
|
||||
price_fhb = 0
|
||||
fluctuating_fhb = 0
|
||||
price_network = 0
|
||||
fluctuating_network = 0
|
||||
price_survey = 0
|
||||
fluctuating_survey = 0
|
||||
price_last_month = 0
|
||||
price_calculate = 0
|
||||
price_recommend = 0
|
||||
fluctuating_recommend = 0
|
||||
unit = ""
|
||||
spec = ''
|
||||
|
||||
_previous_prices = None
|
||||
_fluctuatings = []
|
||||
|
||||
@property
|
||||
def previous_prices(self):
|
||||
if not self._previous_prices:
|
||||
previous_price = PriceResult.get_by_key(self.name, *Helper.get_last_month(self.year, self.month))
|
||||
self._previous_prices = previous_price
|
||||
return self._previous_prices
|
||||
|
||||
def result(self):
|
||||
return {
|
||||
'material_id': self.material_id,
|
||||
'name': self.name,
|
||||
'year': self.year,
|
||||
'month': self.month,
|
||||
'price_ftb': self.price_ftb,
|
||||
'fluctuating_ftb': self.fluctuating_ftb,
|
||||
'price_ss': self.price_ss,
|
||||
'fluctuating_ss': self.fluctuating_ss,
|
||||
'price_fhb': self.price_fhb,
|
||||
'fluctuating_fhb': self.fluctuating_fhb,
|
||||
'price_network': self.price_network,
|
||||
'fluctuating_network': self.fluctuating_network,
|
||||
'price_survey': self.price_survey,
|
||||
'fluctuating_survey': self.fluctuating_survey,
|
||||
'price_last_month': self.price_last_month,
|
||||
'price_calculate': self.price_calculate,
|
||||
'price_recommend': self.price_recommend,
|
||||
'fluctuating_recommend': self.fluctuating_recommend,
|
||||
'unit': self.unit,
|
||||
'spec': self.spec,
|
||||
}
|
||||
|
||||
def run(self):
|
||||
self.price_ftb, self.fluctuating_ftb = self._get_ftb_price()
|
||||
self.price_ss, self.fluctuating_ss = self._get_ss_price()
|
||||
self.price_fhb, self.fluctuating_fhb = self._get_fhb_price()
|
||||
self.price_network, self.fluctuating_network = self._get_network_price()
|
||||
self.price_survey, self.fluctuating_survey = self._get_survey_price()
|
||||
self.price_last_month = self._get_last_month_price()
|
||||
self.price_calculate = self._get_calculate_price()
|
||||
self.price_recommend, self.fluctuating_recommend = self._get_recommend_price()
|
||||
return self
|
||||
|
||||
def _get_ftb_price(self):
|
||||
return 0, 0
|
||||
|
||||
def _get_ss_price(self):
|
||||
return 0, 0
|
||||
|
||||
def _get_fhb_price(self):
|
||||
return 0, 0
|
||||
|
||||
def _get_network_price(self):
|
||||
return 0, 0
|
||||
|
||||
def _get_survey_price(self):
|
||||
return 0, 0
|
||||
|
||||
def _get_last_month_price(self):
|
||||
return getattr(self.previous_prices, 'price_recommend', 0)
|
||||
|
||||
def _get_calculate_price(self, round_dit=0):
|
||||
prices = (
|
||||
self.price_ftb, self.price_ss, self.price_fhb, self.price_network, self.price_survey, self.price_last_month)
|
||||
total = sum(float(i) for i in prices)
|
||||
if total == 0:
|
||||
return 0
|
||||
result = total / len([i for i in prices if i != 0])
|
||||
result = round(result, round_dit)
|
||||
return result
|
||||
|
||||
def _get_recommend_price(self, round_by=10):
|
||||
if not self.previous_prices:
|
||||
return 0, 0
|
||||
|
||||
previous_price = int(getattr(self.previous_prices, 'price_recommend', 0))
|
||||
if not previous_price:
|
||||
return 0, 0
|
||||
|
||||
fluctuating = sum(self._fluctuatings) / len(self._fluctuatings)
|
||||
fluctuating = round(fluctuating / round_by) * round_by
|
||||
|
||||
price = fluctuating + previous_price
|
||||
price = round(price / round_by) * round_by
|
||||
|
||||
return price, fluctuating
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from calculators.asphalt_domestic import AsphaltDomesticCalculator
|
||||
from calculators.asphalt_imported import AsphaltImportedCalculator
|
||||
from calculators.cement_325 import Cement325Calculator
|
||||
from calculators.cement_425 import Cement425Calculator
|
||||
from calculators.oil_0 import Oil0Calculator
|
||||
from calculators.oil_89 import Oil89Calculator
|
||||
from calculators.oil_92 import Oil92Calculator
|
||||
from calculators.steel_plate import SteelPlateCalculator
|
||||
from calculators.steel_rebar_300 import Reber300Calculator
|
||||
from calculators.steel_rebar_400 import Reber400Calculator
|
||||
from calculators.steel_section import SteelSectionCalculator
|
||||
from calculators.steel_strand import SteelStrandCalculator
|
||||
from calculators.asphalt_domestic_modifier import AsphaltDomesticModifierCalculator
|
||||
from calculators.asphalt_imported_modifier import AsphaltImportedModifierCalculator
|
||||
from core.factory import ClientApp
|
||||
|
||||
for Calculator in [
|
||||
AsphaltDomesticCalculator,
|
||||
AsphaltImportedCalculator,
|
||||
Cement325Calculator,
|
||||
Cement425Calculator,
|
||||
Oil0Calculator,
|
||||
Oil89Calculator,
|
||||
Oil92Calculator,
|
||||
SteelPlateCalculator,
|
||||
Reber300Calculator,
|
||||
Reber400Calculator,
|
||||
SteelSectionCalculator,
|
||||
SteelStrandCalculator,
|
||||
AsphaltDomesticModifierCalculator,
|
||||
AsphaltImportedModifierCalculator,
|
||||
]:
|
||||
with ClientApp().app_context():
|
||||
for month in range(8, 13):
|
||||
calculator = Calculator(year=2023, month=month)
|
||||
_result = calculator.run()
|
||||
calculator.save()
|
||||
print(_result)
|
73
web/calculators/asphalt_domestic.py
Normal file
73
web/calculators/asphalt_domestic.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import datetime
|
||||
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator, Helper
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
|
||||
from commons.models.asphalt_domestic import AsphaltDomestic
|
||||
|
||||
|
||||
class AsphaltDomesticCalculator(Calculator):
|
||||
name = "国产沥青"
|
||||
material_id = "06.60.61.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_network_price(self):
|
||||
name_in = (
|
||||
'浙江省—镇海炼化(70#,90#,A级)',
|
||||
'福建省—联合石化(70#,A级)',
|
||||
'广东省—茂名石化(70#,90#,A级)',
|
||||
'广东省—中油高富(70#,90#,A级)华南公司',
|
||||
)
|
||||
last_month_year, last_month = Helper.get_last_month(self.year, self.month)
|
||||
|
||||
end_date = AsphaltDomestic.get_last_date_from(date=datetime.date(last_month_year, last_month, 26))
|
||||
start_date = AsphaltDomestic.get_last_date_from(date=datetime.date(self.year, self.month, 25))
|
||||
|
||||
previous_items = AsphaltDomestic.get_items(date=end_date, name_in=name_in)
|
||||
previous_prices = {material: float(price or 0) for material, price in previous_items}
|
||||
|
||||
current_items = AsphaltDomestic.get_items(date=start_date, name_in=name_in)
|
||||
current_prices = {material: float(price or 0) for material, price in current_items}
|
||||
|
||||
fluctuating_a = current_prices.get(name_in[0], 0) - previous_prices.get(name_in[0], 0)
|
||||
fluctuating_b = current_prices.get(name_in[1], 0) - previous_prices.get(name_in[1], 0)
|
||||
fluctuating_c = current_prices.get(name_in[2], 0) - previous_prices.get(name_in[2], 0)
|
||||
fluctuating_d = current_prices.get(name_in[3], 0) - previous_prices.get(name_in[3], 0)
|
||||
|
||||
previous_price = int(getattr(self.previous_prices, 'price_network', 0))
|
||||
|
||||
price = previous_price + 0.4 * fluctuating_a + 0.2 * (fluctuating_b + fluctuating_c + fluctuating_d)
|
||||
fluctuating = price - previous_price
|
||||
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='石油沥青', spec='国产 (路面用)')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_survey', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey]
|
||||
return super()._get_recommend_price()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = AsphaltDomesticCalculator(year=2023, month=12)
|
||||
_result = calculator.run()
|
||||
calculator.save()
|
||||
print(_result)
|
64
web/calculators/asphalt_domestic_modifier.py
Normal file
64
web/calculators/asphalt_domestic_modifier.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator, Helper
|
||||
from commons.models.asphalt_modifier import AsphaltModifier
|
||||
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class AsphaltDomesticModifierCalculator(Calculator):
|
||||
name = "国产改性沥青"
|
||||
material_id = "06.12.61.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_recommend_price(self):
|
||||
previous_price = int(getattr(self.previous_prices, 'price_recommend', 0))
|
||||
if not previous_price:
|
||||
return 0, 0
|
||||
|
||||
query = PriceResult.get_query(self.year, self.month, name='国产沥青')
|
||||
result = query.all()
|
||||
if not result:
|
||||
return 0, 0
|
||||
|
||||
raw_data = result[0]
|
||||
|
||||
fluctuating_1 = int(raw_data.fluctuating_recommend)
|
||||
|
||||
query = AsphaltModifier.get_query(self.year, self.month)
|
||||
query = query.with_entities(func.avg(AsphaltModifier.price))
|
||||
result_current = query.one_or_none()
|
||||
if not result_current or not result_current[0]:
|
||||
return 0, 0
|
||||
|
||||
query = AsphaltModifier.get_query(*Helper.get_last_month(self.year, self.month))
|
||||
query = query.with_entities(func.avg(AsphaltModifier.price))
|
||||
result_previous = query.one_or_none()
|
||||
if not result_previous or not result_previous[0]:
|
||||
return 0, 0
|
||||
|
||||
fluctuating_2 = int((result_current[0] - result_previous[0]) * 5 / 100)
|
||||
|
||||
price = previous_price + fluctuating_1 + fluctuating_2
|
||||
price = round(price / 10) * 10
|
||||
|
||||
return price, fluctuating_1 + fluctuating_2
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = AsphaltDomesticModifierCalculator(year=2023, month=9)
|
||||
_result = calculator.run()
|
||||
calculator.save()
|
||||
print(_result)
|
55
web/calculators/asphalt_imported.py
Normal file
55
web/calculators/asphalt_imported.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.asphalt_imported import AsphaltImported
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class AsphaltImportedCalculator(Calculator):
|
||||
name = "进口沥青"
|
||||
material_id = "06.60.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_network_price(self):
|
||||
items = AsphaltImported.get_items(year=self.year, month=self.month, name_in=('新加坡—华东',))
|
||||
prices = {name: float(price or 0) for name, price in items}
|
||||
|
||||
price = prices.get('新加坡—华东', 0)
|
||||
previous_price = int(getattr(self.previous_prices, 'price_network', 0))
|
||||
fluctuating = price - previous_price
|
||||
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='石油沥青', spec='进口 (路面用)')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_survey', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey]
|
||||
return super()._get_recommend_price()
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = AsphaltImportedCalculator(year=2023, month=10)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
64
web/calculators/asphalt_imported_modifier.py
Normal file
64
web/calculators/asphalt_imported_modifier.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator, Helper
|
||||
from commons.models.asphalt_modifier import AsphaltModifier
|
||||
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class AsphaltImportedModifierCalculator(Calculator):
|
||||
name = "进口改性沥青"
|
||||
material_id = "06.12.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_recommend_price(self):
|
||||
previous_price = int(getattr(self.previous_prices, 'price_recommend', 0))
|
||||
if not previous_price:
|
||||
return 0, 0
|
||||
|
||||
query = PriceResult.get_query(self.year, self.month, name='进口沥青')
|
||||
result = query.all()
|
||||
if not result:
|
||||
return 0, 0
|
||||
|
||||
raw_data = result[0]
|
||||
|
||||
fluctuating_1 = int(raw_data.fluctuating_recommend)
|
||||
|
||||
query = AsphaltModifier.get_query(self.year, self.month)
|
||||
query = query.with_entities(func.avg(AsphaltModifier.price))
|
||||
result_current = query.one_or_none()
|
||||
if not result_current or not result_current[0]:
|
||||
return 0, 0
|
||||
|
||||
query = AsphaltModifier.get_query(*Helper.get_last_month(self.year, self.month))
|
||||
query = query.with_entities(func.avg(AsphaltModifier.price))
|
||||
result_previous = query.one_or_none()
|
||||
if not result_previous or not result_previous[0]:
|
||||
return 0, 0
|
||||
|
||||
fluctuating_2 = int((result_current[0] - result_previous[0]) * 5 / 100)
|
||||
|
||||
price = previous_price + fluctuating_1 + fluctuating_2
|
||||
price = round(price / 10) * 10
|
||||
|
||||
return price, fluctuating_1 + fluctuating_2
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = AsphaltImportedModifierCalculator(year=2023, month=9)
|
||||
_result = calculator.run()
|
||||
calculator.save()
|
||||
print(_result)
|
77
web/calculators/cement_325.py
Normal file
77
web/calculators/cement_325.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.cement import Cement
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau
|
||||
from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau
|
||||
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class Cement325Calculator(Calculator):
|
||||
name = "32.5 水泥"
|
||||
material_id = "09.60.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_ftb_price(self):
|
||||
query = FuzhouTransportationBureau.get_query(self.year, self.month, name='32.5级水泥')
|
||||
query = query.with_entities(func.avg(FuzhouTransportationBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_fhb_price(self):
|
||||
query = FuzhouHighwayBureau.get_query(self.year, self.month, name='32.5级水泥', spec='旋窑(散装)')
|
||||
query = query.with_entities(func.avg(FuzhouHighwayBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_network_price(self):
|
||||
result = Cement.get_items(self.year, self.month, spec_in=('台泥', '金牛', '炼石牌'), name_in=('P.S.A32.5', 'M32.5', 'P.P 32.5R'), pack='散装')
|
||||
prices = result[0][0]
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = round(prices / 5) * 5
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_network', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='32.5级水泥', spec='旋窑')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_survey', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self, round_by=5):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb]
|
||||
return super()._get_recommend_price(round_by)
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = Cement325Calculator(year=2023, month=12)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
81
web/calculators/cement_425.py
Normal file
81
web/calculators/cement_425.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.cement import Cement
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau
|
||||
from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau
|
||||
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class Cement425Calculator(Calculator):
|
||||
name = "42.5 水泥"
|
||||
material_id = "09.61.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_ftb_price(self):
|
||||
query = FuzhouTransportationBureau.get_query(self.year, self.month, name='42.5级水泥')
|
||||
query = query.with_entities(func.avg(FuzhouTransportationBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price))
|
||||
return price, fluctuating
|
||||
|
||||
# def _get_ss_price(self):
|
||||
# return 4000, -10
|
||||
|
||||
def _get_fhb_price(self):
|
||||
query = FuzhouHighwayBureau.get_query(self.year, self.month, name='42.5级水泥', spec='旋窑(散装)')
|
||||
query = query.with_entities(func.avg(FuzhouHighwayBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_network_price(self):
|
||||
result = Cement.get_items(self.year, self.month, spec_in=('台泥', '金牛', '炼石牌'), name_in=('P.O 42.5',), pack='散装')
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
prices = result[0][0]
|
||||
price = round(prices / 5) * 5
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_network', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='42.5级水泥', spec='旋窑')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_survey', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self, round_by=5):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb]
|
||||
return super()._get_recommend_price(round_by)
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = Cement425Calculator(year=2023, month=11)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
63
web/calculators/oil_0.py
Normal file
63
web/calculators/oil_0.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import datetime
|
||||
|
||||
from calculators import Calculator, Helper
|
||||
from commons.models.oil import Oil
|
||||
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class Oil0Calculator(Calculator):
|
||||
name = "柴油(0)"
|
||||
material_id = "69.61.60.00"
|
||||
unit = "kg"
|
||||
spec = "0#"
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_network_price(self):
|
||||
name_in = ('车用0号柴油(Ⅵ)',)
|
||||
# 获取上月末价格
|
||||
previous_items = Oil.get_items(*Helper.get_last_month(self.year, self.month), name_in=name_in)
|
||||
previous_item = previous_items[-1]
|
||||
previous_price = previous_item.price
|
||||
|
||||
current_items = Oil.get_items(self.year, self.month, name_in=name_in)
|
||||
# 从本月1号开始计算加权总数
|
||||
start_date = datetime.date(self.year, self.month, 1)
|
||||
previous_date = start_date
|
||||
total_with_weight = 0
|
||||
for item in current_items:
|
||||
total_with_weight += previous_price * (item.date - previous_date).days
|
||||
previous_price = item.price
|
||||
previous_date = item.date
|
||||
# 直到月末
|
||||
end_date = datetime.date(*Helper.get_next_month(self.year, self.month), 1) - datetime.timedelta(days=1)
|
||||
if end_date > previous_date:
|
||||
total_with_weight += previous_price * (end_date - previous_date).days
|
||||
# 计算
|
||||
price = round(total_with_weight / (end_date - start_date).days) / 1000
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_network', price))
|
||||
|
||||
return round(price, 2), round(fluctuating, 2)
|
||||
|
||||
def _get_calculate_price(self, round_dit=2):
|
||||
return super()._get_calculate_price(round_dit)
|
||||
|
||||
def _get_recommend_price(self):
|
||||
return self.price_network, self.fluctuating_network
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = Oil0Calculator(year=2023, month=11)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
63
web/calculators/oil_89.py
Normal file
63
web/calculators/oil_89.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import datetime
|
||||
|
||||
from calculators import Calculator, Helper
|
||||
from commons.models.oil import Oil
|
||||
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class Oil89Calculator(Calculator):
|
||||
name = "汽油(89)"
|
||||
material_id = "69.60.60.00"
|
||||
unit = "kg"
|
||||
spec = "89#"
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_network_price(self):
|
||||
name_in = ('车用89号汽油(ⅥB)',)
|
||||
# 获取上月末价格
|
||||
previous_items = Oil.get_items(*Helper.get_last_month(self.year, self.month), name_in=name_in)
|
||||
previous_item = previous_items[-1]
|
||||
previous_price = previous_item.price
|
||||
|
||||
current_items = Oil.get_items(self.year, self.month, name_in=name_in)
|
||||
# 从本月1号开始计算加权总数
|
||||
start_date = datetime.date(self.year, self.month, 1)
|
||||
previous_date = start_date
|
||||
total_with_weight = 0
|
||||
for item in current_items:
|
||||
total_with_weight += previous_price * (item.date - previous_date).days
|
||||
previous_price = item.price
|
||||
previous_date = item.date
|
||||
# 直到月末
|
||||
end_date = datetime.date(*Helper.get_next_month(self.year, self.month), 1) - datetime.timedelta(days=1)
|
||||
if end_date > previous_date:
|
||||
total_with_weight += previous_price * (end_date - previous_date).days
|
||||
# 计算
|
||||
price = round(total_with_weight / (end_date - start_date).days) / 1000
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_network', price))
|
||||
|
||||
return round(price, 2), round(fluctuating, 2)
|
||||
|
||||
def _get_calculate_price(self, round_dit=2):
|
||||
return super()._get_calculate_price(round_dit)
|
||||
|
||||
def _get_recommend_price(self):
|
||||
return self.price_network, self.fluctuating_network
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = Oil89Calculator(year=2023, month=11)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
63
web/calculators/oil_92.py
Normal file
63
web/calculators/oil_92.py
Normal file
@@ -0,0 +1,63 @@
|
||||
import datetime
|
||||
|
||||
from calculators import Calculator, Helper
|
||||
from commons.models.oil import Oil
|
||||
|
||||
from commons.models.price_result import PriceResult
|
||||
|
||||
|
||||
class Oil92Calculator(Calculator):
|
||||
name = "汽油(92)"
|
||||
material_id = "69.60.61.00"
|
||||
unit = "kg"
|
||||
spec = "92#"
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_network_price(self):
|
||||
name_in = ('车用92号汽油(ⅥB)',)
|
||||
# 获取上月末价格
|
||||
previous_items = Oil.get_items(*Helper.get_last_month(self.year, self.month), name_in=name_in)
|
||||
previous_item = previous_items[-1]
|
||||
previous_price = previous_item.price
|
||||
|
||||
current_items = Oil.get_items(self.year, self.month, name_in=name_in)
|
||||
# 从本月1号开始计算加权总数
|
||||
start_date = datetime.date(self.year, self.month, 1)
|
||||
previous_date = start_date
|
||||
total_with_weight = 0
|
||||
for item in current_items:
|
||||
total_with_weight += previous_price * (item.date - previous_date).days
|
||||
previous_price = item.price
|
||||
previous_date = item.date
|
||||
# 直到月末
|
||||
end_date = datetime.date(*Helper.get_next_month(self.year, self.month), 1) - datetime.timedelta(days=1)
|
||||
if end_date > previous_date:
|
||||
total_with_weight += previous_price * (end_date - previous_date).days
|
||||
# 计算
|
||||
price = round(total_with_weight / (end_date - start_date).days) / 1000
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'price_network', price))
|
||||
|
||||
return round(price, 2), round(fluctuating, 2)
|
||||
|
||||
def _get_calculate_price(self, round_dit=2):
|
||||
return super()._get_calculate_price(round_dit)
|
||||
|
||||
def _get_recommend_price(self):
|
||||
return self.price_network, self.fluctuating_network
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = Oil92Calculator(year=2023, month=11)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
79
web/calculators/steel_plate.py
Normal file
79
web/calculators/steel_plate.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau
|
||||
from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau
|
||||
from commons.models.price_result import PriceResult
|
||||
from commons.models.steel_plate import SteelPlate
|
||||
|
||||
|
||||
class SteelPlateCalculator(Calculator):
|
||||
name = "中厚板"
|
||||
material_id = "17.01.61.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_ftb_price(self):
|
||||
query = FuzhouTransportationBureau.get_query(self.year, self.month, name='钢板')
|
||||
query = query.with_entities(func.avg(FuzhouTransportationBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price))
|
||||
return price, fluctuating
|
||||
|
||||
# def _get_ss_price(self):
|
||||
# return 4000, -10
|
||||
|
||||
def _get_fhb_price(self):
|
||||
query = FuzhouHighwayBureau.get_query(self.year, self.month, name='钢板')
|
||||
query = query.with_entities(func.avg(FuzhouHighwayBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price))
|
||||
return price, fluctuating
|
||||
|
||||
def _get_network_price(self):
|
||||
result = SteelPlate.get_items(self.year, self.month)
|
||||
prices = {spec: float(price) for spec, price in result}
|
||||
price = 0.2 * prices.get('12', 0) + 0.6 * prices.get('16-20', 0) + 0.2 * prices.get('22-28', 0)
|
||||
price = round(price)
|
||||
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_network', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='钢板')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_survey', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb]
|
||||
return super()._get_recommend_price()
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = SteelPlateCalculator(year=2023, month=11)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
88
web/calculators/steel_rebar_300.py
Normal file
88
web/calculators/steel_rebar_300.py
Normal file
@@ -0,0 +1,88 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau
|
||||
from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau
|
||||
from commons.models.price_result import PriceResult
|
||||
from commons.models.sanming_steel import SanmingSteel
|
||||
from commons.models.steel_rebar import SteelRebar
|
||||
|
||||
|
||||
class Reber300Calculator(Calculator):
|
||||
name = "光圆钢筋"
|
||||
material_id = "15.05.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_ftb_price(self):
|
||||
query = FuzhouTransportationBureau.get_query(self.year, self.month, name='光圆钢筋')
|
||||
query = query.with_entities(func.avg(FuzhouTransportationBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_ss_price(self):
|
||||
query = SanmingSteel.get_query(self.year, self.month, name='高线', spec='φ10mm')
|
||||
query = query.with_entities(func.avg(SanmingSteel.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ss_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_fhb_price(self):
|
||||
query = FuzhouHighwayBureau.get_query(self.year, self.month, name='光圆钢筋')
|
||||
query = query.with_entities(func.avg(FuzhouHighwayBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_network_price(self):
|
||||
result = SteelRebar.get_items(self.year, self.month, 'HPB300')
|
||||
price_dict = {spec: price for spec, material, price in result}
|
||||
price = 0.3 * float(price_dict.get('Φ6', 0)) + 0.7 * float(price_dict.get('Φ8-10', 0))
|
||||
price = round(price)
|
||||
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_network', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='光圆钢筋')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_survey', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb, self.fluctuating_ss]
|
||||
return super()._get_recommend_price()
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = Reber300Calculator(year=2023, month=10)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
||||
|
90
web/calculators/steel_rebar_400.py
Normal file
90
web/calculators/steel_rebar_400.py
Normal file
@@ -0,0 +1,90 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau
|
||||
from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau
|
||||
from commons.models.price_result import PriceResult
|
||||
from commons.models.sanming_steel import SanmingSteel
|
||||
from commons.models.steel_rebar import SteelRebar
|
||||
|
||||
|
||||
class Reber400Calculator(Calculator):
|
||||
name = "带肋钢筋"
|
||||
material_id = "15.03.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
# todo price result 加 year 字段
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_ftb_price(self):
|
||||
query = FuzhouTransportationBureau.get_query(self.year, self.month, name='带肋钢筋')
|
||||
query = query.with_entities(func.avg(FuzhouTransportationBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_ss_price(self):
|
||||
query = SanmingSteel.get_query(self.year, self.month, name='Ⅲ级螺纹钢筋', spec='HRB400Ф16-25')
|
||||
query = query.with_entities(func.avg(SanmingSteel.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ss_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_fhb_price(self):
|
||||
query = FuzhouHighwayBureau.get_query(self.year, self.month, name='带肋钢筋')
|
||||
query = query.with_entities(func.avg(FuzhouHighwayBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_network_price(self):
|
||||
result = SteelRebar.get_items(self.year, self.month, 'HRB400E')
|
||||
price_dict = {spec: price for spec, material, price in result}
|
||||
price = 0.2 * float(price_dict.get('Φ12', 0)) + 0.2 * float(price_dict.get('Φ14', 0)) + \
|
||||
0.4 * float(price_dict.get('Φ18-22', 0)) + 0.2 * float(price_dict.get('Φ28-32', 0))
|
||||
price = round(price)
|
||||
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_network', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='带肋钢筋')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_survey', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb, self.fluctuating_ss]
|
||||
return super()._get_recommend_price()
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = Reber400Calculator(year=2023, month=10)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
79
web/calculators/steel_section.py
Normal file
79
web/calculators/steel_section.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau
|
||||
from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau
|
||||
from commons.models.price_result import PriceResult
|
||||
from commons.models.steel_section import SteelSection
|
||||
|
||||
|
||||
class SteelSectionCalculator(Calculator):
|
||||
name = "型钢"
|
||||
material_id = "16.05.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_ftb_price(self):
|
||||
query = FuzhouTransportationBureau.get_query(self.year, self.month, name='型钢')
|
||||
query = query.with_entities(func.avg(FuzhouTransportationBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
# def _get_ss_price(self):
|
||||
# return 4000, -10
|
||||
|
||||
def _get_fhb_price(self):
|
||||
query = FuzhouHighwayBureau.get_query(self.year, self.month, name='型钢')
|
||||
query = query.with_entities(func.avg(FuzhouHighwayBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_network_price(self):
|
||||
result = SteelSection.get_items(self.year, self.month)
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = round(result[0][0])
|
||||
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_network', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='型钢')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_survey', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb]
|
||||
return super()._get_recommend_price()
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = SteelSectionCalculator(year=2023, month=11)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
78
web/calculators/steel_strand.py
Normal file
78
web/calculators/steel_strand.py
Normal file
@@ -0,0 +1,78 @@
|
||||
from sqlalchemy import func
|
||||
|
||||
from calculators import Calculator
|
||||
from commons.models.fujian_survey import FujianSurvey
|
||||
from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau
|
||||
from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau
|
||||
from commons.models.price_result import PriceResult
|
||||
from commons.models.steel_strand import SteelStrand
|
||||
|
||||
|
||||
class SteelStrandCalculator(Calculator):
|
||||
name = "钢绞线"
|
||||
material_id = "19.04.60.00"
|
||||
unit = "t"
|
||||
spec = ""
|
||||
|
||||
def __init__(self, year, month):
|
||||
self.year = year
|
||||
self.month = month
|
||||
|
||||
def _get_ftb_price(self):
|
||||
query = FuzhouTransportationBureau.get_query(self.year, self.month, name='钢绞线')
|
||||
query = query.with_entities(func.avg(FuzhouTransportationBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
# def _get_ss_price(self):
|
||||
# return 4000, -10
|
||||
|
||||
def _get_fhb_price(self):
|
||||
query = FuzhouHighwayBureau.get_query(self.year, self.month, name='钢绞线')
|
||||
query = query.with_entities(func.avg(FuzhouHighwayBureau.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'ftb_price', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_network_price(self):
|
||||
result = SteelStrand.get_items(self.year, self.month)
|
||||
prices = {material: float(price) for material, price in result}
|
||||
price = round(prices.get('SWRH82B', 0))
|
||||
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_network', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_survey_price(self):
|
||||
query = FujianSurvey.get_query(self.year, self.month, name='钢绞线')
|
||||
query = query.with_entities(func.avg(FujianSurvey.price))
|
||||
result = query.all()
|
||||
if not result[0][0]:
|
||||
return 0, 0
|
||||
price = int(result[0][0])
|
||||
fluctuating = price - getattr(self.previous_prices, 'price_survey', price)
|
||||
return price, fluctuating
|
||||
|
||||
def _get_recommend_price(self):
|
||||
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb]
|
||||
return super()._get_recommend_price()
|
||||
|
||||
def save(self):
|
||||
result = self.result()
|
||||
PriceResult(**result).upsert()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from core.factory import ClientApp
|
||||
|
||||
with ClientApp().app_context():
|
||||
calculator = SteelStrandCalculator(year=2023, month=11)
|
||||
result = calculator.run()
|
||||
calculator.save()
|
||||
print(result)
|
Reference in New Issue
Block a user