Files
material-api/web/commons/models/steel_rebar.py

46 lines
1.8 KiB
Python
Raw Normal View History

2024-05-29 10:21:31 +08:00
import datetime
from sqlalchemy import Column, Integer, String, Numeric, Date, UniqueConstraint, func
from calculators import Helper
from commons.models.mixin.steel import SteelMixin
2024-06-05 09:04:48 +08:00
from commons.models.model import Model
2024-05-29 10:21:31 +08:00
from core.extensions import db
2024-06-05 09:04:48 +08:00
class SteelRebar(db.Model, Model, SteelMixin):
2024-05-29 10:21:31 +08:00
__tablename__ = 'STEEL_REBAR'
id = Column('ID', Integer, primary_key=True)
name = Column('NAME', String(128), default='', comment='名称')
spec = Column('SPEC', String(128), default='', comment='规格')
material = Column('MATERIAL', String(64), default='', comment='材质')
source = Column('SOURCE', String(64), default='', comment='产地')
price = Column('PRICE', Numeric(16, 4), default=0, comment='价格')
fluctuating = Column('FLUCTUATING', Numeric(16, 4), default=0, comment='浮动')
date = Column('DATE', Date, comment='日期')
__table_args__ = (
UniqueConstraint(name, spec, material, source, date, name='Idx_key'),
{'comment': '钢筋'},
)
@classmethod
def query_by_month(cls, query, year, month):
last_month_year, last_month = Helper.get_last_month(year, month)
query = query.filter(cls.date >= datetime.date(last_month_year, last_month, 26))
query = query.filter(cls.date <= datetime.date(year, month, 25))
return query
@classmethod
def get_items(cls, year, month, material, source='三钢闽光', name_in=('高线', '螺纹钢')):
query = cls.query
query = cls.query_by_month(query, year, month)
query = query.filter(cls.material == material)
query = query.filter(cls.source == source)
query = query.filter(cls.name.in_(name_in))
query = query.with_entities(cls.spec, cls.material, func.avg(cls.price))
query = query.group_by(cls.spec, cls.material)
result = query.all()
return result