87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
# app/models/material.py
|
||
from app.extensions import db
|
||
from datetime import datetime
|
||
|
||
|
||
class MaterialBase(db.Model):
|
||
"""
|
||
基础信息表模型
|
||
对应数据库表: material_base
|
||
"""
|
||
__tablename__ = 'material_base'
|
||
|
||
# 1. 基础字段 (保持不变)
|
||
id = db.Column(db.Integer, primary_key=True)
|
||
name = db.Column(db.String(255), nullable=False, comment='基础信息名称')
|
||
|
||
# 类别 (对应 SQL: category)
|
||
category = db.Column(db.String(100), comment='类别')
|
||
|
||
# 类型 (对应 SQL: material_type) -> 前端 prop="type"
|
||
material_type = db.Column(db.String(100), comment='类型')
|
||
|
||
# 规格型号 (对应 SQL: spec_model) -> 前端 prop="spec"
|
||
spec_model = db.Column(db.String(255), comment='规格型号')
|
||
|
||
unit = db.Column(db.String(50), comment='计量单位')
|
||
|
||
# 可见等级
|
||
visibility_level = db.Column(db.Integer, default=0, comment='信息可见等级')
|
||
|
||
# 链接与图片
|
||
manual_link = db.Column(db.Text, comment='通用说明书链接')
|
||
product_image = db.Column(db.Text, comment='通用产品图链接')
|
||
|
||
# 启用状态
|
||
is_enabled = db.Column(db.Boolean, default=True, comment='是否启用')
|
||
|
||
# ============================================================
|
||
# 时间字段 (保持你原本的注释状态,以免报错)
|
||
# ============================================================
|
||
# create_time = db.Column(db.DateTime, default=datetime.utcnow)
|
||
# update_time = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||
|
||
# ============================================================
|
||
# 关联关系区域 (修改重点)
|
||
# ============================================================
|
||
|
||
# 1. 关联采购库存 (StockBuy) - 保持不变
|
||
# 注意:确保 app/models/inbound/buy.py 中的 StockBuy 定义了 back_populates='material'
|
||
stock_buys = db.relationship('StockBuy', back_populates='material', lazy='dynamic')
|
||
|
||
# 2. 【新增】关联半成品库存 (StockSemi)
|
||
# 注意:确保 app/models/inbound/semi.py 中的 StockSemi 定义了 back_populates='material'
|
||
# 这样以后可以通过 material.stock_semis 来访问该物料下的所有半成品库存记录
|
||
stock_semis = db.relationship('StockSemi', back_populates='material', lazy='dynamic')
|
||
|
||
def to_dict(self):
|
||
"""
|
||
序列化方法:将模型转换为字典,供API返回JSON使用
|
||
"""
|
||
return {
|
||
'id': self.id,
|
||
'name': self.name,
|
||
'category': self.category,
|
||
|
||
# =========================================
|
||
# 关键映射区 (保持不变)
|
||
# =========================================
|
||
# 数据库叫 material_type -> 前端叫 type
|
||
'type': self.material_type,
|
||
|
||
# 数据库叫 spec_model -> 前端叫 spec
|
||
'spec': self.spec_model,
|
||
|
||
'unit': self.unit,
|
||
|
||
# 驼峰命名适配
|
||
'visibilityLevel': self.visibility_level,
|
||
'generalManual': self.manual_link,
|
||
'generalImage': self.product_image,
|
||
|
||
# 状态处理
|
||
'isEnabled': 1 if self.is_enabled else 0,
|
||
|
||
# 时间字段保持注释
|
||
# 'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if hasattr(self, 'create_time') and self.create_time else None
|
||
} |