diff --git a/web/calculators/asphalt_domestic.py b/web/calculators/asphalt_domestic.py index 3a67d52..20f5f31 100644 --- a/web/calculators/asphalt_domestic.py +++ b/web/calculators/asphalt_domestic.py @@ -1,12 +1,9 @@ -import datetime - from sqlalchemy import func -from calculators import Calculator, Helper +from calculators import Calculator +from commons.models.data_network import DataNetwork from commons.models.fujian_survey import FujianSurvey -from commons.models.asphalt_domestic import AsphaltDomestic - class AsphaltDomesticCalculator(Calculator): name = "国产沥青" @@ -19,33 +16,11 @@ class AsphaltDomesticCalculator(Calculator): 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) + 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): diff --git a/web/commons/models/data_network.py b/web/commons/models/data_network.py new file mode 100644 index 0000000..982556c --- /dev/null +++ b/web/commons/models/data_network.py @@ -0,0 +1,56 @@ +import datetime + +from dateutil.relativedelta import relativedelta +from sqlalchemy import Column, Integer, String, Date, UniqueConstraint, Numeric, Text + +from commons.models.mixin.base import BaseModelMixin +from commons.models.model import Model +from core.extensions import db + + +class DataNetwork(db.Model, Model, BaseModelMixin): + __tablename__ = 'DATA_NETWORK' + + id = Column('ID', Integer, primary_key=True) + material_id = Column('MATERIAL_ID', String(128), comment='') + spec = Column('SPEC', String(128), comment='') + unit = Column('UNIT', String(128), comment='') + brand = Column('BRAND', String(128), comment='') + name = Column('NAME', String(128), comment='') + price = Column('PRICE', Numeric(16, 4), comment='') + source = Column('SOURCE', String(128), comment='') + remark = Column('REMARK', Text, comment='') + date = Column('DATE', Date, comment='') + region = Column('REGION', String(64), comment='') + + __table_args__ = ( + UniqueConstraint(name, spec, region, date, name='Idx_key'), + {'comment': '网络数据'}, + ) + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def find_by_key(self): + query = DataNetwork.query + query = query.filter(DataNetwork.name == self.name) + query = query.filter(DataNetwork.spec == self.spec) + query = query.filter(DataNetwork.region == self.region) + query = query.filter(DataNetwork.date == self.date) + result = query.one_or_none() + return result + + @classmethod + def get_query(cls, year, month, name, spec, region="福州"): + start_date = datetime.date(year, month, 1) + end_date = start_date + relativedelta(months=1) + query = cls.query + query = query.filter(cls.date >= start_date) + 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/commons/models/material_task.py b/web/commons/models/material_task.py index a1e5350..48ca221 100644 --- a/web/commons/models/material_task.py +++ b/web/commons/models/material_task.py @@ -6,7 +6,7 @@ from commons.models.model import Model from core.extensions import db -class MaterialTask(db.Model, Model, OperationTrackMixin, Model): +class MaterialTask(db.Model, Model, OperationTrackMixin): id = Column('ID', Integer, primary_key=True) name = Column('NAME', String(128), default='', comment='任务名称') status = Column('STATUS', Integer, default=0, comment='状态(待采集、已采集、采集中)')