91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
from sqlalchemy import func
|
|
|
|
from calculators import Calculator
|
|
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
|
|
from commons.models.sanming_steel import SanmingSteel
|
|
from commons.models.steel_rebar import SteelRebar
|
|
|
|
|
|
class Reber400Calculator(Calculator):
|
|
name = "带肋钢筋"
|
|
material_id = "15.03.60.00"
|
|
unit = "t"
|
|
spec = ""
|
|
|
|
# todo price result 加 year 字段
|
|
|
|
def __init__(self, year, month):
|
|
self.year = year
|
|
self.month = month
|
|
|
|
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)
|
|
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)
|
|
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)
|
|
return price, fluctuating
|
|
|
|
def _get_network_price(self):
|
|
result = SteelRebar.get_items(self.year, self.month, 'HRB400E')
|
|
price_dict = {spec: price for spec, material, price in result}
|
|
price = 0.2 * float(price_dict.get('Φ12', 0)) + 0.2 * float(price_dict.get('Φ14', 0)) + \
|
|
0.4 * float(price_dict.get('Φ18-22', 0)) + 0.2 * float(price_dict.get('Φ28-32', 0))
|
|
price = round(price)
|
|
|
|
fluctuating = price - getattr(self.previous_prices, '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)
|
|
return price, fluctuating
|
|
|
|
def _get_recommend_price(self):
|
|
self._fluctuatings = [self.fluctuating_network, self.fluctuating_survey, self.fluctuating_fhb, self.fluctuating_ftb, self.fluctuating_ss]
|
|
return super()._get_recommend_price()
|
|
|
|
def save(self):
|
|
result = self.result()
|
|
PriceResult(**result).upsert()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
from core.factory import ClientApp
|
|
|
|
with ClientApp().app_context():
|
|
calculator = Reber400Calculator(year=2023, month=10)
|
|
result = calculator.run()
|
|
calculator.save()
|
|
print(result)
|