Dish_Recognition/models/Dish_categories.py

55 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
"""菜品名称表(总服务器权威;SQLite 可缓存镜像)。
说明:一张记录对应一道菜的标准名称,不是 XLS 里的「食材二级形态库」。
"""
from datetime import datetime
from sqlalchemy import Column, DateTime, Index, Integer, String
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from dao.db import Base
class DishCategory(Base):
__tablename__ = 'dish_categories'
id = Column(Integer, primary_key=True, autoincrement=True, comment='主键ID')
# 业务上仅对 is_active=1 的名称保证唯一;软删除时由业务层改名避免撞约束
name = Column(String(100), nullable=False, unique=True, comment='菜品标准名称')
alias = Column(String(255), nullable=True, comment='别名(逗号分隔)')
create_time = Column(
DateTime,
nullable=False,
default=datetime.now,
server_default=func.now(),
comment='创建时间',
)
update_time = Column(
DateTime,
nullable=False,
default=datetime.now,
server_default=func.now(),
onupdate=datetime.now,
comment='更新时间(增量同步关键)',
)
is_active = Column(Integer, nullable=False, default=1, server_default='1', comment='是否启用:1启用 0软删除')
templates = relationship('GlobalTemplate', back_populates='category')
__table_args__ = (
Index('idx_dish_categories_active', 'is_active'),
Index('idx_dish_categories_update_time', 'update_time'),
)
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'alias': self.alias,
'is_active': self.is_active,
'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
}