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

51 lines
1.8 KiB
Python
Raw Normal View History

2024-05-29 10:21:31 +08:00
from sqlalchemy import Column, Integer, String
from commons.models.mixin.operation_track import OperationTrackMixin
2024-06-05 09:04:48 +08:00
from commons.models.model import Model
2024-05-29 10:21:31 +08:00
from core.extensions import db
2024-06-05 09:04:48 +08:00
class Material(db.Model, Model, OperationTrackMixin):
2025-02-21 13:05:50 +08:00
"""材料表"""
__tablename__ = 'material'
_db = db
2024-05-29 10:21:31 +08:00
id = Column('ID', String(128), primary_key=True)
parent_id = Column('PARENT_ID', String(128))
2025-02-21 13:05:50 +08:00
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='材料编码')
2024-05-29 10:21:31 +08:00
__table_args__ = (
{'comment': '材料'},
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@classmethod
2025-02-21 13:05:50 +08:00
def get_by_key(cls, code):
return cls.query.filter(cls.code == code).one_or_none()
2024-05-29 10:21:31 +08:00
def upsert(self):
2025-02-21 13:05:50 +08:00
result = self.get_by_key(self.code)
2024-05-29 10:21:31 +08:00
if not result:
session = db.session
session.add(self)
session.commit()
else:
session = db.session
self.id = result.id
session.add(result)
session.commit()