Files
material-api/web/calculators/asphalt_domestic.py

51 lines
1.6 KiB
Python
Raw Normal View History

2024-05-29 10:21:31 +08:00
from sqlalchemy import func
2024-07-09 20:16:29 +08:00
from calculators import Calculator
from commons.models.data_network import DataNetwork
2024-05-29 10:21:31 +08:00
from commons.models.fujian_survey import FujianSurvey
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):
2024-07-09 20:16:29 +08:00
query = DataNetwork.get_query(self.year, self.month, name='石油沥青', spec='国产')
data = query.first()
price = data.price if data else 0
2024-07-10 16:08:47 +08:00
2024-07-09 20:16:29 +08:00
previous_price = int(getattr(self.previous_prices, 'price_network', price))
2024-05-29 10:21:31 +08:00
fluctuating = price - previous_price
2024-07-10 16:08:47 +08:00
2024-05-29 10:21:31 +08:00
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)