From fa029882a2a845202067ffe453bababc07a6df5b Mon Sep 17 00:00:00 2001 From: han0 Date: Thu, 11 Jul 2024 16:01:15 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=A1=E7=AE=97=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/calculators/__init__.py | 7 +++- web/calculators/asphalt_domestic.py | 34 ++++++++++----- web/calculators/asphalt_domestic_modifier.py | 32 ++++++--------- web/calculators/asphalt_imported.py | 27 +++++++++--- web/calculators/asphalt_imported_modifier.py | 32 ++++++--------- web/calculators/cement_325.py | 12 ++---- web/calculators/cement_425.py | 17 ++++---- web/calculators/oil_0.py | 16 ++++++-- web/calculators/oil_89.py | 12 ++++-- web/calculators/oil_92.py | 12 ++++-- web/calculators/steel_plate.py | 29 +++++-------- web/calculators/steel_rebar_300.py | 41 +++++++------------ web/calculators/steel_rebar_400.py | 41 +++++++------------ web/calculators/steel_section.py | 30 +++++--------- web/calculators/steel_strand.py | 30 +++++--------- web/commons/models/data_network.py | 2 +- web/commons/models/fujian_survey.py | 2 +- .../models/fuzhou_transportation_bureau.py | 4 +- web/tasks/once/calculate.py | 8 +++- 19 files changed, 187 insertions(+), 201 deletions(-) diff --git a/web/calculators/__init__.py b/web/calculators/__init__.py index 8bf82fb..3571b13 100644 --- a/web/calculators/__init__.py +++ b/web/calculators/__init__.py @@ -127,7 +127,7 @@ class Calculator: previous_price = int(getattr(self.previous_prices, 'price_recommend', 0)) if not previous_price: - return 0, 0 + previous_price = int(getattr(self.previous_prices, 'price_calculate', 0)) fluctuating = sum(self._fluctuatings) / len(self._fluctuatings) fluctuating = round(fluctuating / round_by) * round_by @@ -141,6 +141,11 @@ class Calculator: result = self.result() PriceResult(**result).upsert() + def _get_fluctuating(self, field_name, price): + previous_price = getattr(self.previous_prices, field_name, 0) or price + fluctuating = price - previous_price if price else 0 + return fluctuating + if __name__ == '__main__': from calculators.asphalt_domestic import AsphaltDomesticCalculator diff --git a/web/calculators/asphalt_domestic.py b/web/calculators/asphalt_domestic.py index 5a3407e..32dd8b9 100644 --- a/web/calculators/asphalt_domestic.py +++ b/web/calculators/asphalt_domestic.py @@ -3,6 +3,8 @@ from sqlalchemy import func from calculators import Calculator from commons.models.data_network import DataNetwork from commons.models.fujian_survey import FujianSurvey +from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau +from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau class AsphaltDomesticCalculator(Calculator): @@ -15,24 +17,36 @@ class AsphaltDomesticCalculator(Calculator): self.year = year self.month = month + def _get_ftb_price(self): + query = FuzhouTransportationBureau.get_query(self.year, self.month, name='石油沥青', spec='国产') + query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) + data = query.first() + price = int(data[0]) if data[0] else 0 + + fluctuating = int(self._get_fluctuating('price_ftb', price)) + return price, fluctuating + + def _get_fhb_price(self): + query = FuzhouHighwayBureau.get_query(self.year, self.month, name='石油沥青', spec='国产') + query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) + data = query.first() + price = int(data[0]) if data[0] else 0 + fluctuating = int(self._get_fluctuating('price_fhb', price)) + return price, fluctuating + 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 - + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): - query = FujianSurvey.get_query(self.year, self.month, name='石油沥青', spec='国产 (路面用)') + 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)) + data = query.first() + price = int(data[0]) if data[0] else 0 + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating def _get_recommend_price(self): diff --git a/web/calculators/asphalt_domestic_modifier.py b/web/calculators/asphalt_domestic_modifier.py index 0de47a2..a0cd42b 100644 --- a/web/calculators/asphalt_domestic_modifier.py +++ b/web/calculators/asphalt_domestic_modifier.py @@ -17,32 +17,24 @@ class AsphaltDomesticModifierCalculator(Calculator): self.month = month def _get_recommend_price(self): - previous_price = int(getattr(self.previous_prices, 'price_recommend', 0)) - if not previous_price: - return 0, 0 - query = PriceResult.get_query(self.year, self.month, name='国产沥青') - result = query.all() - if not result: + asphalt = query.first() + fluctuating_1 = asphalt.fluctuating_recommend if asphalt else 0 + if not asphalt.price_recommend: return 0, 0 - raw_data = result[0] - - fluctuating_1 = int(raw_data.fluctuating_recommend) - query = AsphaltModifier.get_query(self.year, self.month) - query = query.with_entities(func.avg(AsphaltModifier.price)) - result_current = query.one_or_none() - if not result_current or not result_current[0]: - return 0, 0 - + modifier_current = query.first() query = AsphaltModifier.get_query(*Helper.get_last_month(self.year, self.month)) - query = query.with_entities(func.avg(AsphaltModifier.price)) - result_previous = query.one_or_none() - if not result_previous or not result_previous[0]: - return 0, 0 + modifier_previous = query.first() + if modifier_current and modifier_previous: + fluctuating_2 = int((modifier_current.price - modifier_previous.price) * 5 / 100) + else: + fluctuating_2 = 0 - fluctuating_2 = int((result_current[0] - result_previous[0]) * 5 / 100) + previous_price = int(getattr(self.previous_prices, 'price_recommend', 0)) + if not previous_price: + previous_price = asphalt.price_recommend + modifier_current.price * 5 / 100 price = previous_price + fluctuating_1 + fluctuating_2 price = round(price / 10) * 10 diff --git a/web/calculators/asphalt_imported.py b/web/calculators/asphalt_imported.py index 546e5fd..10114f0 100644 --- a/web/calculators/asphalt_imported.py +++ b/web/calculators/asphalt_imported.py @@ -3,6 +3,8 @@ from sqlalchemy import func from calculators import Calculator from commons.models.data_network import DataNetwork from commons.models.fujian_survey import FujianSurvey +from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau +from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau from commons.models.price_result import PriceResult @@ -16,22 +18,35 @@ class AsphaltImportedCalculator(Calculator): self.year = year self.month = month + def _get_ftb_price(self): + query = FuzhouTransportationBureau.get_query(self.year, self.month, name='石油沥青', spec='进口') + query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) + data = query.first() + price = int(data[0]) if data[0] else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) + return price, fluctuating + + def _get_fhb_price(self): + query = FuzhouHighwayBureau.get_query(self.year, self.month, name='石油沥青', spec='进口') + query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) + data = query.first() + price = int(data[0]) if data[0] else 0 + fluctuating = int(self._get_fluctuating('price_fhb', price)) + return price, fluctuating + 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 - + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): - query = FujianSurvey.get_query(self.year, self.month, name='石油沥青', spec='进口 (路面用)') + query = FujianSurvey.get_query(self.year, self.month, name='石油沥青', spec='进口') query = query.with_entities(func.avg(FujianSurvey.price)) data = query.first() price = int(data[0]) if data[0] else 0 - fluctuating = price - int(getattr(self.previous_prices, 'price_survey', price)) + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating def _get_recommend_price(self): diff --git a/web/calculators/asphalt_imported_modifier.py b/web/calculators/asphalt_imported_modifier.py index c758d09..a90d06b 100644 --- a/web/calculators/asphalt_imported_modifier.py +++ b/web/calculators/asphalt_imported_modifier.py @@ -17,32 +17,24 @@ class AsphaltImportedModifierCalculator(Calculator): self.month = month def _get_recommend_price(self): - previous_price = int(getattr(self.previous_prices, 'price_recommend', 0)) - if not previous_price: - return 0, 0 - query = PriceResult.get_query(self.year, self.month, name='进口沥青') - result = query.all() - if not result: + asphalt = query.first() + fluctuating_1 = asphalt.fluctuating_recommend if asphalt else 0 + if not asphalt.price_recommend: return 0, 0 - raw_data = result[0] - - fluctuating_1 = int(raw_data.fluctuating_recommend) - query = AsphaltModifier.get_query(self.year, self.month) - query = query.with_entities(func.avg(AsphaltModifier.price)) - result_current = query.one_or_none() - if not result_current or not result_current[0]: - return 0, 0 - + modifier_current = query.first() query = AsphaltModifier.get_query(*Helper.get_last_month(self.year, self.month)) - query = query.with_entities(func.avg(AsphaltModifier.price)) - result_previous = query.one_or_none() - if not result_previous or not result_previous[0]: - return 0, 0 + modifier_previous = query.first() + if modifier_current and modifier_previous: + fluctuating_2 = int((modifier_current.price - modifier_previous.price) * 5 / 100) + else: + fluctuating_2 = 0 - fluctuating_2 = int((result_current[0] - result_previous[0]) * 5 / 100) + previous_price = int(getattr(self.previous_prices, 'price_recommend', 0)) + if not previous_price: + previous_price = asphalt.price_recommend + modifier_current.price * 5 / 100 price = previous_price + fluctuating_1 + fluctuating_2 price = round(price / 10) * 10 diff --git a/web/calculators/cement_325.py b/web/calculators/cement_325.py index 1d8c886..e1cf1e0 100644 --- a/web/calculators/cement_325.py +++ b/web/calculators/cement_325.py @@ -24,8 +24,7 @@ class Cement325Calculator(Calculator): query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) data = query.first() price = int(data[0]) if data[0] else 0 - - fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price)) + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating @@ -34,8 +33,7 @@ class Cement325Calculator(Calculator): query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) data = query.first() price = int(data[0]) if data[0] else 0 - - fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price)) + fluctuating = int(self._get_fluctuating('price_fhb', price)) return price, fluctuating @@ -43,8 +41,7 @@ class Cement325Calculator(Calculator): query = DataNetwork.get_query(self.year, self.month, name='水泥', spec='32.5') data = query.first() price = data.price if data else 0 - - fluctuating = price - int(getattr(self.previous_prices, 'price_network', price)) + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating @@ -53,8 +50,7 @@ class Cement325Calculator(Calculator): query = query.with_entities(func.avg(FujianSurvey.price)) data = query.first() price = int(data[0]) if data[0] else 0 - - fluctuating = price - int(getattr(self.previous_prices, 'price_survey', price)) + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating diff --git a/web/calculators/cement_425.py b/web/calculators/cement_425.py index 4b694df..4bfbf83 100644 --- a/web/calculators/cement_425.py +++ b/web/calculators/cement_425.py @@ -21,13 +21,10 @@ class Cement425Calculator(Calculator): def _get_ftb_price(self): query = FuzhouTransportationBureau.get_query(self.year, self.month, name='42.5级水泥') - query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 + data = query.first() + price = data.price if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) - price = int(result[0][0]) - fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price)) return price, fluctuating # def _get_ss_price(self): @@ -40,14 +37,15 @@ class Cement425Calculator(Calculator): if not result[0][0]: return 0, 0 price = int(result[0][0]) - fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price)) + fluctuating = int(self._get_fluctuating('price_fhb', price)) + return price, fluctuating def _get_network_price(self): query = DataNetwork.get_query(self.year, self.month, name='水泥', spec='42.5') data = query.first() price = data.price if data else 0 - fluctuating = price - int(getattr(self.previous_prices, 'price_network', price)) + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): @@ -57,7 +55,8 @@ class Cement425Calculator(Calculator): if not result[0][0]: return 0, 0 price = int(result[0][0]) - fluctuating = price - int(getattr(self.previous_prices, 'price_survey', price)) + fluctuating = int(self._get_fluctuating('price_survey', price)) + return price, fluctuating def _get_recommend_price(self, round_by=5): diff --git a/web/calculators/oil_0.py b/web/calculators/oil_0.py index 83e06fa..1ae13b0 100644 --- a/web/calculators/oil_0.py +++ b/web/calculators/oil_0.py @@ -2,6 +2,9 @@ import datetime from calculators import Calculator, Helper from commons.models.data_network import DataNetwork +from commons.models.fujian_survey import FujianSurvey +from commons.models.fuzhou_highway_bureau import FuzhouHighwayBureau +from commons.models.fuzhou_transportation_bureau import FuzhouTransportationBureau from commons.models.price_result import PriceResult @@ -16,13 +19,18 @@ class Oil0Calculator(Calculator): self.year = year self.month = month - def _get_network_price(self): - query = DataNetwork.get_query(self.year, self.month, name='柴油', spec='0号') + def _get_survey_price(self): + query = FujianSurvey.get_query(self.year, self.month, name='柴油', spec='0#') data = query.first() price = data.price if data else 0 + fluctuating = self._get_fluctuating('price_survey', price) + return round(price, 2), round(fluctuating, 2) - fluctuating = price - int(getattr(self.previous_prices, 'price_network', price)) - + def _get_network_price(self): + query = DataNetwork.get_query(self.year, self.month, spec='0号') + data = query.first() + price = data.price if data else 0 + fluctuating = self._get_fluctuating('price_network', price) return round(price, 2), round(fluctuating, 2) def _get_calculate_price(self, round_dit=2): diff --git a/web/calculators/oil_89.py b/web/calculators/oil_89.py index e9c62e3..ddc8f47 100644 --- a/web/calculators/oil_89.py +++ b/web/calculators/oil_89.py @@ -2,6 +2,7 @@ import datetime from calculators import Calculator from commons.models.data_network import DataNetwork +from commons.models.fujian_survey import FujianSurvey from commons.models.price_result import PriceResult @@ -16,13 +17,18 @@ class Oil89Calculator(Calculator): self.year = year self.month = month + def _get_survey_price(self): + query = FujianSurvey.get_query(self.year, self.month, name='汽油', spec='89#') + data = query.first() + price = data.price if data else 0 + fluctuating = self._get_fluctuating('price_survey', price) + return round(price, 2), round(fluctuating, 2) + def _get_network_price(self): query = DataNetwork.get_query(self.year, self.month, name='汽油', spec='89号') data = query.first() price = data.price if data else 0 - - fluctuating = price - int(getattr(self.previous_prices, 'price_network', price)) - + fluctuating = self._get_fluctuating('price_network', price) return round(price, 2), round(fluctuating, 2) def _get_calculate_price(self, round_dit=2): diff --git a/web/calculators/oil_92.py b/web/calculators/oil_92.py index d710862..12fc271 100644 --- a/web/calculators/oil_92.py +++ b/web/calculators/oil_92.py @@ -2,6 +2,7 @@ import datetime from calculators import Calculator, Helper from commons.models.data_network import DataNetwork +from commons.models.fujian_survey import FujianSurvey from commons.models.oil import Oil from commons.models.price_result import PriceResult @@ -17,13 +18,18 @@ class Oil92Calculator(Calculator): self.year = year self.month = month + def _get_survey_price(self): + query = FujianSurvey.get_query(self.year, self.month, name='汽油', spec='92#') + data = query.first() + price = data.price if data else 0 + fluctuating = self._get_fluctuating('price_survey', price) + return round(price, 2), round(fluctuating, 2) + def _get_network_price(self): query = DataNetwork.get_query(self.year, self.month, name='汽油', spec='92号') data = query.first() price = data.price if data else 0 - - fluctuating = price - int(getattr(self.previous_prices, 'price_network', price)) - + fluctuating = self._get_fluctuating('price_network', price) return round(price, 2), round(fluctuating, 2) def _get_calculate_price(self, round_dit=2): diff --git a/web/calculators/steel_plate.py b/web/calculators/steel_plate.py index 8ef2aa3..0e35656 100644 --- a/web/calculators/steel_plate.py +++ b/web/calculators/steel_plate.py @@ -20,12 +20,9 @@ class SteelPlateCalculator(Calculator): def _get_ftb_price(self): query = FuzhouTransportationBureau.get_query(self.year, self.month, name='钢板') - query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price)) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating # def _get_ss_price(self): @@ -33,12 +30,9 @@ class SteelPlateCalculator(Calculator): def _get_fhb_price(self): query = FuzhouHighwayBureau.get_query(self.year, self.month, name='钢板') - query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - int(getattr(self.previous_prices, 'ftb_price', price)) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_fhb', price)) return price, fluctuating def _get_network_price(self): @@ -49,17 +43,14 @@ class SteelPlateCalculator(Calculator): price = 0.2 * prices.get('12mm', 0) + 0.6 * prices.get('16-20mm', 0) + 0.2 * prices.get('22-28mm', 0) price = round(price) - fluctuating = price - getattr(self.previous_prices, 'price_network', price) + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): query = FujianSurvey.get_query(self.year, self.month, name='钢板') - 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 - getattr(self.previous_prices, 'price_survey', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating def _get_recommend_price(self): diff --git a/web/calculators/steel_rebar_300.py b/web/calculators/steel_rebar_300.py index cd1af22..f703ba0 100644 --- a/web/calculators/steel_rebar_300.py +++ b/web/calculators/steel_rebar_300.py @@ -22,50 +22,37 @@ class Reber300Calculator(Calculator): def _get_ftb_price(self): query = FuzhouTransportationBureau.get_query(self.year, self.month, name='光圆钢筋') - query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating def _get_ss_price(self): query = SanmingSteel.get_query(self.year, self.month, name='高线', spec='φ10mm') - query = query.with_entities(func.avg(SanmingSteel.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ss_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ss', price)) return price, fluctuating def _get_fhb_price(self): query = FuzhouHighwayBureau.get_query(self.year, self.month, name='光圆钢筋') - query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating def _get_network_price(self): query = DataNetwork.get_query(self.year, self.month, name='光圆钢筋', spec='综合') data = query.first() - price = round(data.price) if data else 0 - - fluctuating = price - getattr(self.previous_prices, 'price_network', price) + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): query = FujianSurvey.get_query(self.year, self.month, name='光圆钢筋') - 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 - getattr(self.previous_prices, 'price_survey', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating def _get_recommend_price(self): diff --git a/web/calculators/steel_rebar_400.py b/web/calculators/steel_rebar_400.py index 2b1727e..84d63c7 100644 --- a/web/calculators/steel_rebar_400.py +++ b/web/calculators/steel_rebar_400.py @@ -22,50 +22,37 @@ class Reber400Calculator(Calculator): def _get_ftb_price(self): query = FuzhouTransportationBureau.get_query(self.year, self.month, name='带肋钢筋') - query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating def _get_ss_price(self): query = SanmingSteel.get_query(self.year, self.month, name='Ⅲ级螺纹钢筋', spec='HRB400Ф16-25') - query = query.with_entities(func.avg(SanmingSteel.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ss_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ss', price)) return price, fluctuating def _get_fhb_price(self): query = FuzhouHighwayBureau.get_query(self.year, self.month, name='带肋钢筋') - query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating def _get_network_price(self): query = DataNetwork.get_query(self.year, self.month, name='带肋钢筋') # spec='综合' 部分历史表格没有填写规格 data = query.first() - price = round(data.price) if data else 0 - - fluctuating = price - getattr(self.previous_prices, 'price_network', price) + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): query = FujianSurvey.get_query(self.year, self.month, name='带肋钢筋') - 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 - getattr(self.previous_prices, 'price_survey', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating def _get_recommend_price(self): diff --git a/web/calculators/steel_section.py b/web/calculators/steel_section.py index eccfef0..688a775 100644 --- a/web/calculators/steel_section.py +++ b/web/calculators/steel_section.py @@ -21,12 +21,9 @@ class SteelSectionCalculator(Calculator): def _get_ftb_price(self): query = FuzhouTransportationBureau.get_query(self.year, self.month, name='型钢') - query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating # def _get_ss_price(self): @@ -34,30 +31,23 @@ class SteelSectionCalculator(Calculator): def _get_fhb_price(self): query = FuzhouHighwayBureau.get_query(self.year, self.month, name='型钢') - query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_fhb', price)) return price, fluctuating def _get_network_price(self): query = DataNetwork.get_query(self.year, self.month, name='型钢') data = query.first() price = round(data.price) if data else 0 - - fluctuating = price - getattr(self.previous_prices, 'price_network', price) + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): query = FujianSurvey.get_query(self.year, self.month, name='型钢') - 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 - getattr(self.previous_prices, 'price_survey', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating def _get_recommend_price(self): diff --git a/web/calculators/steel_strand.py b/web/calculators/steel_strand.py index 327e656..ad7a419 100644 --- a/web/calculators/steel_strand.py +++ b/web/calculators/steel_strand.py @@ -21,12 +21,9 @@ class SteelStrandCalculator(Calculator): def _get_ftb_price(self): query = FuzhouTransportationBureau.get_query(self.year, self.month, name='钢绞线') - query = query.with_entities(func.avg(FuzhouTransportationBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_ftb', price)) return price, fluctuating # def _get_ss_price(self): @@ -34,30 +31,23 @@ class SteelStrandCalculator(Calculator): def _get_fhb_price(self): query = FuzhouHighwayBureau.get_query(self.year, self.month, name='钢绞线') - query = query.with_entities(func.avg(FuzhouHighwayBureau.price)) - result = query.all() - if not result[0][0]: - return 0, 0 - price = int(result[0][0]) - fluctuating = price - getattr(self.previous_prices, 'ftb_price', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_fhb', price)) return price, fluctuating def _get_network_price(self): query = DataNetwork.get_query(self.year, self.month, name='钢绞线') data = query.first() price = round(data.price) if data else 0 - - fluctuating = price - getattr(self.previous_prices, 'price_network', price) + fluctuating = int(self._get_fluctuating('price_network', price)) return price, fluctuating def _get_survey_price(self): query = FujianSurvey.get_query(self.year, self.month, name='钢绞线') - 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 - getattr(self.previous_prices, 'price_survey', price) + data = query.first() + price = int(data.price) if data else 0 + fluctuating = int(self._get_fluctuating('price_survey', price)) return price, fluctuating def _get_recommend_price(self): diff --git a/web/commons/models/data_network.py b/web/commons/models/data_network.py index 75ccab0..ce74568 100644 --- a/web/commons/models/data_network.py +++ b/web/commons/models/data_network.py @@ -41,7 +41,7 @@ class DataNetwork(db.Model, Model, BaseModelMixin): return result @classmethod - def get_query(cls, year, month, name, spec=None, region="福州"): + def get_query(cls, year, month, name=None, spec=None, region="福州"): start_date = datetime.date(year, month, 1) end_date = start_date + relativedelta(months=1) query = cls.query diff --git a/web/commons/models/fujian_survey.py b/web/commons/models/fujian_survey.py index 9af2807..6b59f07 100644 --- a/web/commons/models/fujian_survey.py +++ b/web/commons/models/fujian_survey.py @@ -48,7 +48,7 @@ class FujianSurvey(db.Model, Model, BaseModelMixin): if name_in: query = query.filter(cls.name.in_(name_in)) if spec: - query = query.filter(cls.spec == spec) + query = query.filter(cls.spec.like(f'%{spec}%')) if region: query = query.filter(cls.region.like(f'%{region}%')) return query diff --git a/web/commons/models/fuzhou_transportation_bureau.py b/web/commons/models/fuzhou_transportation_bureau.py index 2db9cdf..73366db 100644 --- a/web/commons/models/fuzhou_transportation_bureau.py +++ b/web/commons/models/fuzhou_transportation_bureau.py @@ -35,7 +35,7 @@ class FuzhouTransportationBureau(db.Model, Model, BaseModelMixin): return result @classmethod - def get_query(cls, year, month, name, region='福州'): + def get_query(cls, year, month, name, spec=None, region='福州'): start_date = datetime.date(year, month, 1) end_date = start_date + relativedelta(months=1) query = cls.query @@ -43,6 +43,8 @@ class FuzhouTransportationBureau(db.Model, Model, BaseModelMixin): query = query.filter(cls.date < end_date) if name: query = query.filter(cls.name == name) + if spec: + query = query.filter(cls.spec.like(f'%{spec}%')) if region: query = query.filter(cls.region.like(f'%{region}%')) return query diff --git a/web/tasks/once/calculate.py b/web/tasks/once/calculate.py index 07e05f5..b8c7b9e 100644 --- a/web/tasks/once/calculate.py +++ b/web/tasks/once/calculate.py @@ -45,4 +45,10 @@ if __name__ == '__main__': from core.factory import ClientApp with ClientApp().app_context(): - calculate(2024, 5) + calculate(2023, 1) + for i in range(2, 12): + calculate(2022, i+1) + for i in range(1, 12): + calculate(2023, i+1) + for i in range(0, 5): + calculate(2024, i+1)