60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
from sqlalchemy import Column, Integer, String
|
||
|
||
from commons.models.mixin.operation_track import OperationTrackMixin
|
||
from commons.models.model import Model
|
||
from core.extensions import db
|
||
|
||
|
||
class Material(db.Model, Model, OperationTrackMixin):
|
||
"""材料表"""
|
||
__tablename__ = 'material'
|
||
_db = db
|
||
|
||
id = Column('ID', String(128), primary_key=True)
|
||
parent_id = Column('PARENT_ID', String(128))
|
||
category1 = Column('CATEGORY1', String(128), comment='分类1')
|
||
category2 = Column('CATEGORY2', String(128), comment='分类2')
|
||
category3 = Column('CATEGORY3', String(128), comment='分类3')
|
||
category4 = Column('CATEGORY4', String(128), comment='分类4')
|
||
name = Column('NAME', String(128), comment='名称')
|
||
unit = Column('UNIT', String(128), comment='单位')
|
||
spec = Column('SPEC', String(128), comment='规格')
|
||
tax = Column('TAX', Integer, comment='税率(%)')
|
||
is_builtin = Column('IS_BUILTIN', Integer, comment='是否初始内建类型(不允许删除)')
|
||
type = Column('TYPE', Integer, comment='材料类别(主材、地材)')
|
||
is_tree = Column('IS_TREE', Integer, comment='是否树')
|
||
sort = Column('SORT', Integer, default=0, comment='排序')
|
||
code = Column('CODE', String(20), comment='材料编码')
|
||
round_bit = Column('ROUND_BIT', Integer, comment='保留小数位数')
|
||
round_method = Column('ROUND_METHOD', String(20), comment='保留小数具体方法')
|
||
|
||
__table_args__ = (
|
||
{'comment': '材料'},
|
||
)
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
super().__init__(*args, **kwargs)
|
||
|
||
@classmethod
|
||
def list(cls, type=None):
|
||
query = cls.query
|
||
if type:
|
||
query = query.filter(cls.type == type)
|
||
return query.all()
|
||
|
||
@classmethod
|
||
def get_by_key(cls, code):
|
||
return cls.query.filter(cls.code == code).one_or_none()
|
||
|
||
def upsert(self):
|
||
result = self.get_by_key(self.code)
|
||
if not result:
|
||
session = db.session
|
||
session.add(self)
|
||
session.commit()
|
||
else:
|
||
session = db.session
|
||
self.id = result.id
|
||
session.add(result)
|
||
session.commit()
|