from sqlalchemy import func from calculators import Calculator from commons.models.data_network import DataNetwork 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): query = DataNetwork.get_query(self.year, self.month, name='石油沥青', spec='国产') data = query.first() price = data.price if data else 0 previous_price = int(getattr(self.previous_prices, 'price_network', price)) 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)