feat: 更新十一地市计算逻辑

This commit is contained in:
han0
2024-07-10 16:08:47 +08:00
parent 5afcfc4846
commit 967bb57136
19 changed files with 144 additions and 139 deletions

View File

@@ -1,6 +1,7 @@
import datetime
from calculators import Calculator, Helper
from commons.models.data_network import DataNetwork
from commons.models.oil import Oil
from commons.models.price_result import PriceResult
@@ -17,27 +18,10 @@ class Oil92Calculator(Calculator):
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
query = DataNetwork.get_query(self.year, self.month, name='汽油', spec='92号')
data = query.first()
price = data.price if data else 0
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)
@@ -57,7 +41,7 @@ if __name__ == '__main__':
from core.factory import ClientApp
with ClientApp().app_context():
calculator = Oil92Calculator(year=2023, month=11)
calculator = Oil92Calculator(year=2024, month=5)
result = calculator.run()
calculator.save()
print(result)