74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
import datetime
|
|
|
|
from sqlalchemy import func
|
|
|
|
from calculators import Calculator, Helper
|
|
from commons.models.fujian_survey import FujianSurvey
|
|
|
|
from commons.models.asphalt_domestic import AsphaltDomestic
|
|
|
|
|
|
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):
|
|
name_in = (
|
|
'浙江省—镇海炼化(70#,90#,A级)',
|
|
'福建省—联合石化(70#,A级)',
|
|
'广东省—茂名石化(70#,90#,A级)',
|
|
'广东省—中油高富(70#,90#,A级)华南公司',
|
|
)
|
|
last_month_year, last_month = Helper.get_last_month(self.year, self.month)
|
|
|
|
end_date = AsphaltDomestic.get_last_date_from(date=datetime.date(last_month_year, last_month, 26))
|
|
start_date = AsphaltDomestic.get_last_date_from(date=datetime.date(self.year, self.month, 25))
|
|
|
|
previous_items = AsphaltDomestic.get_items(date=end_date, name_in=name_in)
|
|
previous_prices = {material: float(price or 0) for material, price in previous_items}
|
|
|
|
current_items = AsphaltDomestic.get_items(date=start_date, name_in=name_in)
|
|
current_prices = {material: float(price or 0) for material, price in current_items}
|
|
|
|
fluctuating_a = current_prices.get(name_in[0], 0) - previous_prices.get(name_in[0], 0)
|
|
fluctuating_b = current_prices.get(name_in[1], 0) - previous_prices.get(name_in[1], 0)
|
|
fluctuating_c = current_prices.get(name_in[2], 0) - previous_prices.get(name_in[2], 0)
|
|
fluctuating_d = current_prices.get(name_in[3], 0) - previous_prices.get(name_in[3], 0)
|
|
|
|
previous_price = int(getattr(self.previous_prices, 'price_network', 0))
|
|
|
|
price = previous_price + 0.4 * fluctuating_a + 0.2 * (fluctuating_b + fluctuating_c + fluctuating_d)
|
|
fluctuating = price - previous_price
|
|
|
|
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)
|