2026-01-30 11:21:10 +08:00
|
|
|
|
# app/models/base.py
|
|
|
|
|
|
from app.extensions import db
|
2026-02-09 16:08:47 +08:00
|
|
|
|
import json
|
2026-01-30 11:21:10 +08:00
|
|
|
|
|
2026-02-11 10:12:10 +08:00
|
|
|
|
|
2026-01-30 11:21:10 +08:00
|
|
|
|
class MaterialBase(db.Model):
|
|
|
|
|
|
"""
|
|
|
|
|
|
基础信息表模型
|
|
|
|
|
|
对应数据库表: material_base
|
|
|
|
|
|
"""
|
|
|
|
|
|
__tablename__ = 'material_base'
|
|
|
|
|
|
|
|
|
|
|
|
# 1. 基础字段
|
|
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2026-02-24 15:13:37 +08:00
|
|
|
|
company_name = db.Column(db.String(255), comment='所属公司')
|
|
|
|
|
|
|
2026-02-04 14:29:59 +08:00
|
|
|
|
name = db.Column(db.String(255), nullable=False, comment='名称')
|
2026-02-09 16:08:47 +08:00
|
|
|
|
common_name = db.Column(db.String(255), comment='俗名')
|
2026-01-30 11:21:10 +08:00
|
|
|
|
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='信息可见等级')
|
|
|
|
|
|
|
2026-02-09 16:08:47 +08:00
|
|
|
|
# 链接与图片 (现在存储 JSON 字符串)
|
2026-01-30 11:21:10 +08:00
|
|
|
|
manual_link = db.Column(db.Text, comment='通用说明书')
|
|
|
|
|
|
product_image = db.Column(db.Text, comment='通用产品图')
|
|
|
|
|
|
|
|
|
|
|
|
# 启用状态
|
|
|
|
|
|
is_enabled = db.Column(db.Boolean, default=True, comment='是否启用')
|
|
|
|
|
|
|
2026-03-17 11:56:04 +08:00
|
|
|
|
# 强制质检标记(采购入库时必须上传检测报告)
|
|
|
|
|
|
is_inspection_required = db.Column(db.Boolean, default=False, comment='是否强制要求质检')
|
|
|
|
|
|
|
2026-01-30 11:21:10 +08:00
|
|
|
|
# ============================================================
|
|
|
|
|
|
# 关联关系区域
|
|
|
|
|
|
# ============================================================
|
|
|
|
|
|
|
2026-02-11 10:12:10 +08:00
|
|
|
|
# 1. 关联采购库存 (StockBuy)
|
2026-02-10 11:13:07 +08:00
|
|
|
|
stock_buys = db.relationship('StockBuy', back_populates='base', lazy='dynamic')
|
2026-01-30 11:21:10 +08:00
|
|
|
|
|
2026-02-11 10:12:10 +08:00
|
|
|
|
# 2. 关联半成品库存 (StockSemi)
|
2026-02-10 11:13:07 +08:00
|
|
|
|
stock_semis = db.relationship('StockSemi', back_populates='base', lazy='dynamic')
|
2026-01-30 11:21:10 +08:00
|
|
|
|
|
2026-02-11 10:12:10 +08:00
|
|
|
|
# 3. 关联成品库存 (StockProduct)
|
2026-02-10 11:13:07 +08:00
|
|
|
|
stock_products = db.relationship('StockProduct', back_populates='base', lazy='dynamic')
|
2026-01-30 11:21:10 +08:00
|
|
|
|
|
2026-02-24 15:13:37 +08:00
|
|
|
|
# 4. 关联服务库存 (StockService)
|
2026-02-11 10:12:10 +08:00
|
|
|
|
stock_services = db.relationship('StockService', back_populates='base', lazy='dynamic')
|
|
|
|
|
|
|
2026-03-11 13:11:16 +08:00
|
|
|
|
# 5. 关联预警设置 (MaterialWarningSetting)
|
|
|
|
|
|
warning_settings = db.relationship('MaterialWarningSetting', back_populates='material', lazy='dynamic', cascade='all, delete-orphan')
|
|
|
|
|
|
|
2026-01-30 11:21:10 +08:00
|
|
|
|
def to_dict(self):
|
|
|
|
|
|
"""
|
|
|
|
|
|
序列化方法
|
|
|
|
|
|
"""
|
2026-02-11 10:12:10 +08:00
|
|
|
|
|
2026-02-09 16:08:47 +08:00
|
|
|
|
# 辅助解析函数:将数据库存储的 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 []
|
|
|
|
|
|
|
2026-01-30 11:21:10 +08:00
|
|
|
|
return {
|
|
|
|
|
|
'id': self.id,
|
2026-02-24 15:13:37 +08:00
|
|
|
|
'companyName': self.company_name,
|
2026-01-30 11:21:10 +08:00
|
|
|
|
'name': self.name,
|
2026-02-09 16:08:47 +08:00
|
|
|
|
'commonName': self.common_name,
|
2026-01-30 11:21:10 +08:00
|
|
|
|
'category': self.category,
|
2026-02-09 16:08:47 +08:00
|
|
|
|
'type': self.material_type,
|
|
|
|
|
|
'spec': self.spec_model,
|
2026-01-30 11:21:10 +08:00
|
|
|
|
'unit': self.unit,
|
|
|
|
|
|
'visibilityLevel': self.visibility_level,
|
2026-02-09 16:08:47 +08:00
|
|
|
|
'generalManual': parse_list(self.manual_link),
|
|
|
|
|
|
'generalImage': parse_list(self.product_image),
|
2026-03-03 17:29:21 +08:00
|
|
|
|
# 【核心修改】:直接返回布尔值,不再转成 1 或 0
|
|
|
|
|
|
'isEnabled': bool(self.is_enabled),
|
2026-03-17 11:56:04 +08:00
|
|
|
|
# 强制质检标记
|
|
|
|
|
|
'isInspectionRequired': bool(self.is_inspection_required),
|
2026-03-11 13:11:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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='红色预警阈值')
|
2026-04-29 15:40:43 +08:00
|
|
|
|
yellow_emails = db.Column(db.String(500), nullable=True, comment='黄色预警通知邮箱')
|
|
|
|
|
|
red_emails = db.Column(db.String(500), nullable=True, comment='红色预警通知邮箱')
|
|
|
|
|
|
is_ordered = db.Column(db.Boolean, default=False, comment='是否已处理采购')
|
|
|
|
|
|
last_notified_at = db.Column(db.DateTime, nullable=True, comment='上次邮件通知时间')
|
2026-03-11 13:11:16 +08:00
|
|
|
|
|
|
|
|
|
|
# 关联关系
|
|
|
|
|
|
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,
|
2026-04-29 15:40:43 +08:00
|
|
|
|
'redThreshold': float(self.red_threshold) if self.red_threshold is not None else None,
|
|
|
|
|
|
'yellowEmails': self.yellow_emails or '',
|
|
|
|
|
|
'redEmails': self.red_emails or '',
|
|
|
|
|
|
'isOrdered': bool(self.is_ordered),
|
|
|
|
|
|
'lastNotifiedAt': self.last_notified_at.strftime('%Y-%m-%d %H:%M:%S') if self.last_notified_at else None
|
2026-01-30 11:21:10 +08:00
|
|
|
|
}
|