diff --git a/inventory-backend/app/models/base.py b/inventory-backend/app/models/base.py index ab88f46..3ad07e8 100644 --- a/inventory-backend/app/models/base.py +++ b/inventory-backend/app/models/base.py @@ -32,14 +32,14 @@ class MaterialBase(db.Model): # 关联关系区域 # ============================================================ - # 1. 关联采购库存 (StockBuy) - stock_buys = db.relationship('StockBuy', back_populates='material', lazy='dynamic') + # 1. 关联采购库存 (StockBuy) - 修改 back_populates 为 'base' + stock_buys = db.relationship('StockBuy', back_populates='base', lazy='dynamic') - # 2. 关联半成品库存 (StockSemi) - stock_semis = db.relationship('StockSemi', back_populates='material', lazy='dynamic') + # 2. 关联半成品库存 (StockSemi) - 修改 back_populates 为 'base' + stock_semis = db.relationship('StockSemi', back_populates='base', lazy='dynamic') - # 3. 关联成品库存 (StockProduct) - stock_products = db.relationship('StockProduct', back_populates='material', lazy='dynamic') + # 3. 关联成品库存 (StockProduct) - 修改 back_populates 为 'base' + stock_products = db.relationship('StockProduct', back_populates='base', lazy='dynamic') def to_dict(self): """ diff --git a/inventory-backend/app/models/inbound/buy.py b/inventory-backend/app/models/inbound/buy.py index cbbeb85..fa58ecd 100644 --- a/inventory-backend/app/models/inbound/buy.py +++ b/inventory-backend/app/models/inbound/buy.py @@ -1,5 +1,8 @@ +# inventory-backend/app/models/inbound/buy.py from app.extensions import db import json +# 显式导入 MaterialBase 以防 relationship 找不到引用 +from app.models.base import MaterialBase class StockBuy(db.Model): """ @@ -48,9 +51,8 @@ class StockBuy(db.Model): # [新增] 全局打印流水号 (用于跨表连续编号,对应 Sequence: global_print_seq) global_print_id = db.Column(db.Integer) - # 关系定义 - # 注意:这里使用字符串 'MaterialBase' 引用,避免了直接 import 导致的潜在循环依赖 - material = db.relationship('MaterialBase', back_populates='stock_buys') + # 关系定义 [已修改] + base = db.relationship('MaterialBase', back_populates='stock_buys') def to_dict(self): # 辅助解析函数:将数据库存储的 JSON 字符串转为 List @@ -68,11 +70,12 @@ class StockBuy(db.Model): return { 'id': self.id, 'base_id': self.base_id, - 'material_name': self.material.name if self.material else '', - 'spec_model': self.material.spec_model if self.material else '', - 'category': self.material.category if self.material else '', - 'unit': self.material.unit if self.material else '', - 'material_type': self.material.material_type if self.material else '', + # [已修改] 使用 self.base + 'material_name': self.base.name if self.base else '', + 'spec_model': self.base.spec_model if self.base else '', + 'category': self.base.category if self.base else '', + 'unit': self.base.unit if self.base else '', + 'material_type': self.base.material_type if self.base else '', 'sku': self.sku, 'inbound_date': self.in_date.strftime('%Y-%m-%d') if self.in_date else '', diff --git a/inventory-backend/app/models/inbound/product.py b/inventory-backend/app/models/inbound/product.py index 39ca826..7e55b03 100644 --- a/inventory-backend/app/models/inbound/product.py +++ b/inventory-backend/app/models/inbound/product.py @@ -1,7 +1,7 @@ # app/models/inbound/product.py from app.extensions import db import json - +from app.models.base import MaterialBase class StockProduct(db.Model): """ @@ -57,8 +57,8 @@ class StockProduct(db.Model): # 全局打印流水号 global_print_id = db.Column(db.Integer) - # 关系定义 - material = db.relationship('MaterialBase', back_populates='stock_products') + # 关系定义 [已修改] + base = db.relationship('MaterialBase', back_populates='stock_products') def to_dict(self): raw_val = float(self.raw_material_cost or 0) @@ -79,11 +79,12 @@ class StockProduct(db.Model): return { 'id': self.id, 'base_id': self.base_id, - 'material_name': self.material.name if self.material else '', - 'spec_model': self.material.spec_model if self.material else '', - 'category': self.material.category if self.material else '', - 'unit': self.material.unit if self.material else '', - 'material_type': self.material.material_type if self.material else '', + # [已修改] 使用 self.base + 'material_name': self.base.name if self.base else '', + 'spec_model': self.base.spec_model if self.base else '', + 'category': self.base.category if self.base else '', + 'unit': self.base.unit if self.base else '', + 'material_type': self.base.material_type if self.base else '', 'sku': self.sku, 'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '', diff --git a/inventory-backend/app/models/inbound/semi.py b/inventory-backend/app/models/inbound/semi.py index a47a2bd..7150e7b 100644 --- a/inventory-backend/app/models/inbound/semi.py +++ b/inventory-backend/app/models/inbound/semi.py @@ -1,7 +1,7 @@ # app/models/inbound/semi.py from app.extensions import db import json - +from app.models.base import MaterialBase class StockSemi(db.Model): """ @@ -55,8 +55,8 @@ class StockSemi(db.Model): # [新增] 全局打印流水号 global_print_id = db.Column(db.Integer) - # 关系定义 - material = db.relationship('MaterialBase', back_populates='stock_semis') + # 关系定义 [已修改] + base = db.relationship('MaterialBase', back_populates='stock_semis') def to_dict(self): raw_val = float(self.raw_material_cost or 0) @@ -78,11 +78,12 @@ class StockSemi(db.Model): return { 'id': self.id, 'base_id': self.base_id, - 'material_name': self.material.name if self.material else '', - 'spec_model': self.material.spec_model if self.material else '', - 'category': self.material.category if self.material else '', - 'unit': self.material.unit if self.material else '', - 'material_type': self.material.material_type if self.material else '', + # [已修改] 使用 self.base + 'material_name': self.base.name if self.base else '', + 'spec_model': self.base.spec_model if self.base else '', + 'category': self.base.category if self.base else '', + 'unit': self.base.unit if self.base else '', + 'material_type': self.base.material_type if self.base else '', 'sku': self.sku, 'inbound_date': self.production_date.strftime('%Y-%m-%d') if self.production_date else '', diff --git a/inventory-backend/app/services/inbound/buy_service.py b/inventory-backend/app/services/inbound/buy_service.py index 2f8f3ce..0ea65fa 100644 --- a/inventory-backend/app/services/inbound/buy_service.py +++ b/inventory-backend/app/services/inbound/buy_service.py @@ -37,8 +37,8 @@ class BuyInboundService: exists = query.first() if exists: - # 获取占用该SN的物料名称,提示更友好 - occupied_name = exists.material.name if exists.material else "未知物料" + # [修改] 获取占用该SN的物料名称 (material -> base) + occupied_name = exists.base.name if exists.base else "未知物料" raise ValueError(f"序列号【{serial_number}】已存在!被物料 [{occupied_name}] 占用,请核查。") # 2. 批号 (BN) 同物料唯一校验 @@ -309,12 +309,12 @@ class BuyInboundService: d = { 'id': item.id, 'base_id': item.base_id, - # 确保这里从关联的 MaterialBase 获取规格型号 - 'material_name': item.material.name if item.material else '', - 'spec_model': item.material.spec_model if item.material else '', - 'category': item.material.category if item.material else '', - 'unit': item.material.unit if item.material else '', - 'material_type': item.material.material_type if item.material else '', + # [核心修改] 确保这里从关联的 .base 获取信息 + 'material_name': item.base.name if item.base else '', + 'spec_model': item.base.spec_model if item.base else '', + 'category': item.base.category if item.base else '', + 'unit': item.base.unit if item.base else '', + 'material_type': item.base.material_type if item.base else '', 'sku': item.sku, 'inbound_date': date_display, diff --git a/inventory-backend/app/services/inbound/product_service.py b/inventory-backend/app/services/inbound/product_service.py index 6677fd6..54b4e76 100644 --- a/inventory-backend/app/services/inbound/product_service.py +++ b/inventory-backend/app/services/inbound/product_service.py @@ -30,7 +30,8 @@ class ProductInboundService: exists = query.first() if exists: - occupied_name = exists.material.name if (hasattr(exists, 'material') and exists.material) else "未知物料" + # [修改] material -> base + occupied_name = exists.base.name if (hasattr(exists, 'base') and exists.base) else "未知物料" raise ValueError(f"序列号【{serial_number}】已存在!被成品 [{occupied_name}] 占用,请核查。") # ============================================================ @@ -318,6 +319,7 @@ class ProductInboundService: items = [] for item in current_items: + # [注意] 因为Model层已经修改了 to_dict 内部的 material -> base,所以这里直接调 to_dict 即可 d = item.to_dict() # 格式化日期 diff --git a/inventory-backend/app/services/inbound/semi_service.py b/inventory-backend/app/services/inbound/semi_service.py index 734a357..00e2fdb 100644 --- a/inventory-backend/app/services/inbound/semi_service.py +++ b/inventory-backend/app/services/inbound/semi_service.py @@ -32,7 +32,8 @@ class SemiInboundService: exists = query.first() if exists: - occupied_name = exists.material.name if (hasattr(exists, 'material') and exists.material) else "未知物料" + # [修改] material -> base + occupied_name = exists.base.name if (hasattr(exists, 'base') and exists.base) else "未知物料" raise ValueError(f"序列号【{serial_number}】已存在!被半成品 [{occupied_name}] 占用,请核查。") # 2. 批号 (BN) 校验 - 同物料下不能重复开单 @@ -420,6 +421,7 @@ class SemiInboundService: items = [] for item in current_items: + # [注意] 因为Model层已经修改了 to_dict 内部的 material -> base,所以这里直接调 to_dict 即可 d = item.to_dict() # 格式化展示日期