110 lines
4.2 KiB
Python
110 lines
4.2 KiB
Python
# app/models/base.py
|
||
from app.extensions import db
|
||
import json
|
||
|
||
|
||
class MaterialBase(db.Model):
|
||
"""
|
||
基础信息表模型
|
||
对应数据库表: material_base
|
||
"""
|
||
__tablename__ = 'material_base'
|
||
|
||
# 1. 基础字段
|
||
id = db.Column(db.Integer, primary_key=True)
|
||
company_name = db.Column(db.String(255), comment='所属公司')
|
||
|
||
name = db.Column(db.String(255), nullable=False, comment='名称')
|
||
common_name = db.Column(db.String(255), comment='俗名')
|
||
category = db.Column(db.String(100), comment='类别')
|
||
material_type = db.Column(db.String(100), comment='类型')
|
||
spec_model = db.Column(db.String(255), comment='规格型号')
|
||
unit = db.Column(db.String(50), comment='计量单位')
|
||
|
||
# 可见等级
|
||
visibility_level = db.Column(db.Integer, default=0, comment='信息可见等级')
|
||
|
||
# 链接与图片 (现在存储 JSON 字符串)
|
||
manual_link = db.Column(db.Text, comment='通用说明书')
|
||
product_image = db.Column(db.Text, comment='通用产品图')
|
||
|
||
# 启用状态
|
||
is_enabled = db.Column(db.Boolean, default=True, comment='是否启用')
|
||
|
||
# ============================================================
|
||
# 关联关系区域
|
||
# ============================================================
|
||
|
||
# 1. 关联采购库存 (StockBuy)
|
||
stock_buys = db.relationship('StockBuy', back_populates='base', lazy='dynamic')
|
||
|
||
# 2. 关联半成品库存 (StockSemi)
|
||
stock_semis = db.relationship('StockSemi', back_populates='base', lazy='dynamic')
|
||
|
||
# 3. 关联成品库存 (StockProduct)
|
||
stock_products = db.relationship('StockProduct', back_populates='base', lazy='dynamic')
|
||
|
||
# 4. 关联服务库存 (StockService)
|
||
stock_services = db.relationship('StockService', back_populates='base', lazy='dynamic')
|
||
|
||
# 5. 关联预警设置 (MaterialWarningSetting)
|
||
warning_settings = db.relationship('MaterialWarningSetting', back_populates='material', lazy='dynamic', cascade='all, delete-orphan')
|
||
|
||
def to_dict(self):
|
||
"""
|
||
序列化方法
|
||
"""
|
||
|
||
# 辅助解析函数:将数据库存储的 JSON 字符串转为 List
|
||
def parse_list(json_str):
|
||
if not json_str:
|
||
return []
|
||
try:
|
||
# 兼容旧数据:如果不是 JSON 格式(比如是单个 URL),则包装成 list
|
||
if not json_str.startswith('['):
|
||
return [json_str]
|
||
return json.loads(json_str)
|
||
except:
|
||
return []
|
||
|
||
return {
|
||
'id': self.id,
|
||
'companyName': self.company_name,
|
||
'name': self.name,
|
||
'commonName': self.common_name,
|
||
'category': self.category,
|
||
'type': self.material_type,
|
||
'spec': self.spec_model,
|
||
'unit': self.unit,
|
||
'visibilityLevel': self.visibility_level,
|
||
'generalManual': parse_list(self.manual_link),
|
||
'generalImage': parse_list(self.product_image),
|
||
# 【核心修改】:直接返回布尔值,不再转成 1 或 0
|
||
'isEnabled': bool(self.is_enabled),
|
||
}
|
||
|
||
|
||
class MaterialWarningSetting(db.Model):
|
||
"""
|
||
物料预警设置表模型
|
||
对应数据库表: material_warning_settings
|
||
"""
|
||
__tablename__ = 'material_warning_settings'
|
||
|
||
id = db.Column(db.Integer, primary_key=True)
|
||
base_id = db.Column(db.Integer, db.ForeignKey('material_base.id'), nullable=False, comment='物料基础信息ID')
|
||
is_enabled = db.Column(db.Boolean, default=False, comment='是否启用预警')
|
||
yellow_threshold = db.Column(db.Numeric(10, 2), nullable=True, comment='黄色预警阈值')
|
||
red_threshold = db.Column(db.Numeric(10, 2), nullable=True, comment='红色预警阈值')
|
||
|
||
# 关联关系
|
||
material = db.relationship('MaterialBase', back_populates='warning_settings')
|
||
|
||
def to_dict(self):
|
||
return {
|
||
'id': self.id,
|
||
'baseId': self.base_id,
|
||
'isEnabled': bool(self.is_enabled),
|
||
'yellowThreshold': float(self.yellow_threshold) if self.yellow_threshold is not None else None,
|
||
'redThreshold': float(self.red_threshold) if self.red_threshold is not None else None
|
||
} |