2024-05-29 10:21:31 +08:00
|
|
|
|
import datetime
|
|
|
|
|
|
2024-07-10 16:08:47 +08:00
|
|
|
|
from calculators import Calculator
|
|
|
|
|
from commons.models.data_network import DataNetwork
|
2024-05-29 10:21:31 +08:00
|
|
|
|
|
|
|
|
|
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):
|
2024-07-10 16:08:47 +08:00
|
|
|
|
query = DataNetwork.get_query(self.year, self.month, name='汽油', spec='89号')
|
|
|
|
|
data = query.first()
|
|
|
|
|
price = data.price if data else 0
|
|
|
|
|
|
2024-05-29 10:21:31 +08:00
|
|
|
|
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)
|