82 lines
3.7 KiB
Python
82 lines
3.7 KiB
Python
import datetime
|
|
|
|
from dateutil.relativedelta import relativedelta
|
|
from sqlalchemy import Column, Integer, String, Numeric
|
|
|
|
from commons.models.mixin.base import BaseModelMixin
|
|
from commons.models.mixin.operation_track import OperationTrackMixin
|
|
from core.extensions import db
|
|
|
|
|
|
class PricePublish(db.Model, OperationTrackMixin, BaseModelMixin):
|
|
id = Column('ID', Integer, primary_key=True)
|
|
year = Column('YEAR', Integer, default='', comment='统计年份')
|
|
month = Column('MONTH', Integer, default='', comment='统计月份')
|
|
material_id = Column('MATERIAL_ID', String(128), default='', comment='编号')
|
|
name = Column('NAME', String(128), default='', comment='材料名称')
|
|
spec = Column('SPEC', String(128), default='', comment='规格')
|
|
price = Column('PRICE', Numeric(16, 4), default=0, comment='价格')
|
|
price_fuzhou = Column('PRICE_FUZHOU', Numeric(16, 4), default=0, comment='福州价格')
|
|
price_xiamen = Column('PRICE_XIAMEN', Numeric(16, 4), default=0, comment='厦门价格')
|
|
price_putian = Column('PRICE_PUTIAN', Numeric(16, 4), default=0, comment='莆田价格')
|
|
price_sanming = Column('PRICE_SANMING', Numeric(16, 4), default=0, comment='三明价格')
|
|
price_quanzhou = Column('PRICE_QUANZHOU', Numeric(16, 4), default=0, comment='泉州价格')
|
|
price_zhangzhou = Column('PRICE_ZHANGZHOU', Numeric(16, 4), default=0, comment='漳州价格')
|
|
price_nanpin = Column('PRICE_NANPIN', Numeric(16, 4), default=0, comment='南平价格')
|
|
price_longyan = Column('PRICE_LONGYAN', Numeric(16, 4), default=0, comment='龙岩价格')
|
|
price_ningde = Column('PRICE_NINGDE', Numeric(16, 4), default=0, comment='宁德价格')
|
|
price_pintan = Column('PRICE_PINTAN', Numeric(16, 4), default=0, comment='平潭价格')
|
|
tax = Column('TAX', Numeric(4, 2), default=0, comment='税率')
|
|
status = Column('STATUS', Integer, default=0, comment='状态')
|
|
type = Column('TYPE', Integer, default=0, comment='类型')
|
|
unit = Column('UNIT', String(128), default='', comment='单位')
|
|
|
|
__tablename__ = 'PRICE_PUBLISH'
|
|
__table_args__ = (
|
|
{'comment': '发布价格'},
|
|
)
|
|
|
|
@classmethod
|
|
def get_by_key(cls, name, year, month, type):
|
|
query = cls.query
|
|
query = query.filter(cls.name == name).filter(cls.year == year).filter(cls.month == month).filter(
|
|
cls.type == type)
|
|
return query.one_or_none()
|
|
|
|
def find_by_key(self):
|
|
cls = self.__class__
|
|
query = cls.query
|
|
query = query.filter(cls.year == self.year)
|
|
query = query.filter(cls.month == self.month)
|
|
query = query.filter(cls.name == self.name)
|
|
query = query.filter(cls.type == self.type)
|
|
result = query.one_or_none()
|
|
return result
|
|
|
|
# @classmethod
|
|
# def query_previous_month(cls, query, start_date: datetime.date, count=6):
|
|
# end_date = start_date + relativedelta(months=1)
|
|
# query = query.filter(cls.date >= end_date - relativedelta(months=count))
|
|
# query = query.filter(cls.date < end_date)
|
|
# return query
|
|
|
|
@classmethod
|
|
def query_previous_month(cls, query, start_date: datetime.date, count=6):
|
|
condition = False
|
|
for i in range(count):
|
|
date = start_date - relativedelta(months=i)
|
|
condition = condition or (cls.year == date.year and cls.month == date.month)
|
|
query.filter(condition)
|
|
return query
|
|
|
|
@classmethod
|
|
def get_query(cls, year=None, month=None, name_in=None):
|
|
query = cls.query
|
|
if year:
|
|
query = query.filter(cls.year == year)
|
|
if month:
|
|
query = query.filter(cls.month == month)
|
|
if name_in:
|
|
query = query.filter(cls.name.in_(name_in))
|
|
return query
|