65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
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)
|