Files
material-api/web/commons/models/fuzhou_transportation_bureau.py
2024-07-11 16:01:15 +08:00

51 lines
2.1 KiB
Python

import datetime
from dateutil.relativedelta import relativedelta
from sqlalchemy import Column, Integer, String, Date, UniqueConstraint, Numeric
from commons.models.mixin.base import BaseModelMixin
from commons.models.model import Model
from core.extensions import db
class FuzhouTransportationBureau(db.Model, Model, BaseModelMixin):
__tablename__ = 'FUZHOU_TRANSPORTATION_BUREAU'
id = Column('ID', Integer, primary_key=True)
name = Column('NAME', String(128), default='', comment='名称')
spec = Column('SPEC', String(128), default='', comment='规格')
price = Column('PRICE', Numeric(16, 4), default=0, comment='价格')
date = Column('DATE', Date, comment='日期')
material_id = Column('MATERIAL_ID', String(128), comment='材料id')
unit = Column('UNIT', String(128), comment='单位')
brand = Column('BRAND', String(128), comment='品牌')
region = Column('REGION', String(128), comment='地区')
__table_args__ = (
UniqueConstraint(name, spec, date, region, name='Idx_key'),
{'comment': '福州交通局'},
)
def find_by_key(self):
query = FuzhouTransportationBureau.query
query = query.filter(FuzhouTransportationBureau.name == self.name)
query = query.filter(FuzhouTransportationBureau.spec == self.spec)
query = query.filter(FuzhouTransportationBureau.region == self.region)
query = query.filter(FuzhouTransportationBureau.date == self.date)
result = query.one_or_none()
return result
@classmethod
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
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