Files
KCGL/inventory-backend/app/models/material.py

62 lines
2.6 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.

# app/models/material.py
from app.extensions import db
from datetime import datetime
class MaterialBase(db.Model):
"""
基础信息表模型
对应数据库表: material_base
"""
__tablename__ = 'material_base'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), nullable=False, comment='基础信息名称')
category = db.Column(db.String(100), comment='类别') # 例如: 采购件, 自制件
material_type = db.Column(db.String(100), comment='类型') # 例如: 电子料, 结构件 (对应前端 type)
spec_model = db.Column(db.String(255), comment='规格型号') # (对应前端 spec)
unit = db.Column(db.String(50), comment='计量单位')
# 根据你提供的代码,可见等级设为 Integer默认为 0
visibility_level = db.Column(db.Integer, default=0, comment='信息可见等级')
manual_link = db.Column(db.Text, comment='通用说明书链接') # (对应前端 generalManual)
product_image = db.Column(db.Text, comment='通用产品图链接') # (对应前端 generalImage)
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)
# 【核心关联】
# 关联采购库存表lazy='dynamic' 允许使用 .count() 等查询方法
stock_buys = db.relationship('StockBuy', back_populates='material', lazy='dynamic')
def to_dict(self):
"""
序列化方法将模型转换为字典供API返回JSON使用
此处进行了字段名的映射,以适配 list.vue 前端的 prop 属性
"""
return {
'id': self.id,
'name': self.name,
'category': self.category,
# --- 字段映射区域 (后端字段 -> 前端字段) ---
'type': self.material_type, # 前端 prop="type"
'spec': self.spec_model, # 前端 prop="spec"
'unit': self.unit,
# 转为驼峰命名,适配前端习惯
'visibilityLevel': self.visibility_level,
'generalManual': self.manual_link,
'generalImage': self.product_image,
# Element Plus Switch 组件通常接受 1/0 或 true/false
# 这里转为 1/0 方便前端 el-switch :active-value="1" :inactive-value="0"
'isEnabled': 1 if self.is_enabled else 0,
# 补充时间信息
'createTime': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None
}