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

53 lines
2.2 KiB
Python
Raw Normal View History

2024-05-29 10:21:31 +08:00
import datetime
from dateutil.relativedelta import relativedelta
from sqlalchemy import Column, Integer, String, Date, UniqueConstraint, Numeric
2024-07-11 09:13:30 +08:00
from commons.models.mixin.base import BaseModelMixin
from commons.models.model import Model
2024-05-29 10:21:31 +08:00
from core.extensions import db
2024-07-11 09:13:30 +08:00
class FuzhouTransportationBureau(db.Model, Model, BaseModelMixin):
2024-05-29 10:21:31 +08:00
__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='品牌')
2024-07-10 16:08:47 +08:00
region = Column('REGION', String(128), comment='地区')
2024-05-29 10:21:31 +08:00
__table_args__ = (
2024-07-10 16:08:47 +08:00
UniqueConstraint(name, spec, date, region, name='Idx_key'),
2024-05-29 10:21:31 +08:00
{'comment': '福州交通局'},
)
2024-07-11 09:13:30 +08:00
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
2024-05-29 10:21:31 +08:00
@classmethod
2024-12-09 17:26:32 +08:00
def get_query(cls, year, month, name=None, spec=None, region='福州', material_id=None):
2024-05-29 10:21:31 +08:00
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)
2024-07-11 16:01:15 +08:00
if spec:
query = query.filter(cls.spec.like(f'%{spec}%'))
2024-07-10 16:08:47 +08:00
if region:
query = query.filter(cls.region.like(f'%{region}%'))
2024-12-09 17:26:32 +08:00
if material_id:
query = query.filter(cls.material_id == material_id)
2024-05-29 10:21:31 +08:00
return query