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

98 lines
4.4 KiB
Python

import datetime
from operator import and_
from dateutil.relativedelta import relativedelta
from sqlalchemy import Column, Integer, String, Numeric, or_
from commons.models.mixin.base import BaseModelMixin
from commons.models.mixin.operation_track import OperationTrackMixin
from commons.models.model import Model
from core.extensions import db
class PricePublish(db.Model, 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='平潭价格')
price_zhangzhoukfq = Column('PRICE_ZHANGZHOUKFQ', 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='单位')
display_digit = Column('DISPLAY_DIGIT', Integer, default='', comment='显示小数位数')
__tablename__ = 'PRICE_PUBLISH'
__table_args__ = (
{'comment': '发布价格'},
)
@classmethod
def clean(cls, year, month, type=None):
query = cls.query.filter(cls.year == year, cls.month == month)
if type:
query.filter(cls.type == type)
query.delete()
@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.spec == self.spec)
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(0, count):
date = start_date - relativedelta(months=i)
condition = or_(condition, and_(cls.year == str(date.year), cls.month == str(date.month)))
query = query.filter(condition)
return query
@classmethod
def get_query(cls, year=None, month=None, name_in=None, material_id_in=None, type=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))
if material_id_in:
query = query.filter(cls.material_id.in_(material_id_in))
if type:
query = query.filter(cls.type == type)
return query