init
This commit is contained in:
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)
|
Reference in New Issue
Block a user