添加半成品页面进行数据
This commit is contained in:
@ -1,18 +1,62 @@
|
||||
# 文件路径: app/services/inbound/base_service.py
|
||||
|
||||
from app.extensions import db
|
||||
from app.models.material import MaterialBase
|
||||
from app.models.stock import StockBuy # 需要引入库存表做删除时的依赖检查
|
||||
|
||||
# ==============================================================================
|
||||
# ✅ 正确的引用方式
|
||||
# ==============================================================================
|
||||
from app.models.inbound.buy import StockBuy # 引用采购库存模型
|
||||
from app.models.inbound.semi import StockSemi # 引用半成品库存模型
|
||||
from sqlalchemy import or_
|
||||
import traceback
|
||||
|
||||
|
||||
class MaterialBaseService:
|
||||
"""
|
||||
基础物料服务层
|
||||
负责处理 MaterialBase 的增删改查及搜索逻辑
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def search_material(keyword):
|
||||
"""
|
||||
根据关键字搜索已启用的基础物料
|
||||
(供 /api/v1/inbound/base/search 接口调用)
|
||||
"""
|
||||
try:
|
||||
if not keyword:
|
||||
return []
|
||||
|
||||
# 搜索名称或规格型号,且必须是启用的
|
||||
query = MaterialBase.query.filter(
|
||||
MaterialBase.is_enabled == True,
|
||||
or_(
|
||||
MaterialBase.name.ilike(f'%{keyword}%'),
|
||||
MaterialBase.spec_model.ilike(f'%{keyword}%')
|
||||
)
|
||||
).limit(20)
|
||||
|
||||
results = []
|
||||
for item in query.all():
|
||||
results.append({
|
||||
'id': item.id,
|
||||
'name': item.name,
|
||||
'spec': item.spec_model,
|
||||
'category': item.category,
|
||||
'unit': item.unit,
|
||||
'type': item.material_type,
|
||||
'status': '启用'
|
||||
})
|
||||
return results
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def get_list(page, limit, filters=None):
|
||||
"""
|
||||
获取基础信息列表
|
||||
:param page: 页码
|
||||
:param limit: 每页条数
|
||||
:param filters: 筛选条件字典 {keyword, category, type, isEnabled}
|
||||
获取基础信息列表 (带分页和筛选)
|
||||
"""
|
||||
try:
|
||||
query = MaterialBase.query
|
||||
@ -46,7 +90,6 @@ class MaterialBaseService:
|
||||
|
||||
except Exception as e:
|
||||
print(f"查询基础信息列表失败: {e}")
|
||||
# 生产环境建议记录日志
|
||||
return {"total": 0, "items": []}
|
||||
|
||||
@staticmethod
|
||||
@ -65,16 +108,16 @@ class MaterialBaseService:
|
||||
if exist:
|
||||
raise ValueError(f"已存在相同名称和规格的数据 (ID: {exist.id})")
|
||||
|
||||
# 2. 创建对象 (注意前端驼峰 -> 后端下划线映射)
|
||||
# 2. 创建对象
|
||||
new_material = MaterialBase(
|
||||
name=data['name'],
|
||||
spec_model=data['spec'], # 映射
|
||||
spec_model=data['spec'],
|
||||
category=data.get('category'),
|
||||
material_type=data.get('type'), # 映射
|
||||
material_type=data.get('type'),
|
||||
unit=data.get('unit'),
|
||||
visibility_level=data.get('visibilityLevel'), # 映射
|
||||
manual_link=data.get('generalManual'), # 映射
|
||||
product_image=data.get('generalImage'), # 映射
|
||||
visibility_level=data.get('visibilityLevel'),
|
||||
manual_link=data.get('generalManual'),
|
||||
product_image=data.get('generalImage'),
|
||||
is_enabled=True if data.get('isEnabled', 1) == 1 else False
|
||||
)
|
||||
|
||||
@ -94,7 +137,7 @@ class MaterialBaseService:
|
||||
if not material:
|
||||
raise ValueError("数据不存在")
|
||||
|
||||
# 更新字段 (仅更新传入的字段)
|
||||
# 更新字段
|
||||
if 'name' in data: material.name = data['name']
|
||||
if 'spec' in data: material.spec_model = data['spec']
|
||||
if 'category' in data: material.category = data['category']
|
||||
@ -116,19 +159,32 @@ class MaterialBaseService:
|
||||
|
||||
@staticmethod
|
||||
def delete_material(m_id):
|
||||
"""删除基础信息 (带依赖检查)"""
|
||||
"""
|
||||
删除基础信息 (带依赖检查)
|
||||
✅ 已升级:同时检查采购库(Buy)和半成品库(Semi)
|
||||
"""
|
||||
try:
|
||||
material = MaterialBase.query.get(m_id)
|
||||
if not material:
|
||||
raise ValueError("数据不存在")
|
||||
|
||||
# 1. 依赖检查:如果该基础信息已经在库存表(StockBuy)中使用,禁止物理删除
|
||||
# 这里假设 StockBuy 表有一个外键或字段指向 MaterialBase (e.g., base_id)
|
||||
usage_count = StockBuy.query.filter_by(base_id=m_id).count()
|
||||
if usage_count > 0:
|
||||
raise ValueError(f"无法删除:该基础信息已被 {usage_count} 条库存记录引用,请先清理库存或仅禁用此条目。")
|
||||
# 1. 依赖检查:采购入库引用
|
||||
buy_usage_count = StockBuy.query.filter_by(base_id=m_id).count()
|
||||
|
||||
# 2. 执行删除
|
||||
# 2. 依赖检查:半成品入库引用
|
||||
semi_usage_count = StockSemi.query.filter_by(base_id=m_id).count()
|
||||
|
||||
total_usage = buy_usage_count + semi_usage_count
|
||||
|
||||
if total_usage > 0:
|
||||
raise ValueError(
|
||||
f"无法删除:该基础物料正被使用中。\n"
|
||||
f"- 采购库存记录: {buy_usage_count} 条\n"
|
||||
f"- 半成品库存记录: {semi_usage_count} 条\n"
|
||||
f"请先清理相关库存或仅‘禁用’此条目。"
|
||||
)
|
||||
|
||||
# 3. 执行删除
|
||||
db.session.delete(material)
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user