采购件图像上传初实现,支持多图,检测报告的图片以及链接上传
This commit is contained in:
@ -5,6 +5,7 @@ from app.models.base import MaterialBase
|
||||
from datetime import datetime
|
||||
from sqlalchemy import or_, func, text
|
||||
import traceback
|
||||
import json
|
||||
|
||||
|
||||
class BuyInboundService:
|
||||
@ -46,6 +47,9 @@ class BuyInboundService:
|
||||
|
||||
@staticmethod
|
||||
def handle_inbound(data):
|
||||
"""
|
||||
处理入库逻辑
|
||||
"""
|
||||
try:
|
||||
base_id = data.get('base_id')
|
||||
if not base_id:
|
||||
@ -83,17 +87,26 @@ class BuyInboundService:
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 3. 条码逻辑处理
|
||||
# 如果前端没传条码(barcode),则默认使用系统生成的 SKU 作为条码
|
||||
# ------------------------------------------------------------------
|
||||
final_barcode = data.get('barcode')
|
||||
if not final_barcode:
|
||||
final_barcode = generated_sku
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 4. 图片列表转 JSON 字符串处理
|
||||
# ------------------------------------------------------------------
|
||||
arrival_list = data.get('arrival_photo', [])
|
||||
report_list = data.get('inspection_report', [])
|
||||
|
||||
# 确保是列表类型,防止前端传错导致报错
|
||||
if not isinstance(arrival_list, list): arrival_list = []
|
||||
if not isinstance(report_list, list): report_list = []
|
||||
|
||||
new_stock = StockBuy(
|
||||
base_id=material.id,
|
||||
global_print_id=next_global_id,
|
||||
sku=generated_sku,
|
||||
barcode=final_barcode,
|
||||
sku=generated_sku, # 自动生成的SKU
|
||||
barcode=final_barcode, # 如果未输入,则存入SKU值
|
||||
|
||||
in_date=in_date_val,
|
||||
serial_number=data.get('serial_number'),
|
||||
@ -113,15 +126,16 @@ class BuyInboundService:
|
||||
buyer_email=data.get('purchaser_email'),
|
||||
original_link=data.get('source_link'),
|
||||
detail_link=data.get('detail_link'),
|
||||
arrival_photo=data.get('arrival_photo'),
|
||||
|
||||
# [新增] 保存检测报告字段
|
||||
inspection_report=data.get('inspection_report')
|
||||
# [核心修改] 将列表转为 JSON 字符串存储
|
||||
arrival_photo=json.dumps(arrival_list),
|
||||
inspection_report=json.dumps(report_list)
|
||||
)
|
||||
|
||||
db.session.add(new_stock)
|
||||
db.session.commit()
|
||||
|
||||
# 返回创建的对象实例
|
||||
return new_stock
|
||||
|
||||
except Exception as e:
|
||||
@ -130,6 +144,9 @@ class BuyInboundService:
|
||||
|
||||
@staticmethod
|
||||
def update_inbound(stock_id, data):
|
||||
"""
|
||||
更新入库记录
|
||||
"""
|
||||
try:
|
||||
print(f"----- UPDATE DEBUG: ID={stock_id} -----")
|
||||
|
||||
@ -137,6 +154,7 @@ class BuyInboundService:
|
||||
if not stock:
|
||||
raise ValueError("记录不存在")
|
||||
|
||||
# 基础字段映射
|
||||
field_mapping = {
|
||||
'sku': 'sku',
|
||||
'barcode': 'barcode',
|
||||
@ -147,21 +165,29 @@ class BuyInboundService:
|
||||
'inspection_status': 'inspection_status',
|
||||
'supplier_name': 'supplier_name',
|
||||
'detail_link': 'detail_link',
|
||||
'arrival_photo': 'arrival_photo',
|
||||
'currency': 'currency',
|
||||
'exchange_rate': 'exchange_rate',
|
||||
'purchaser': 'buyer_name',
|
||||
'purchaser_email': 'buyer_email',
|
||||
'source_link': 'original_link',
|
||||
|
||||
# [新增] 允许更新检测报告
|
||||
'inspection_report': 'inspection_report'
|
||||
'source_link': 'original_link'
|
||||
}
|
||||
|
||||
for frontend_key, db_attr in field_mapping.items():
|
||||
if frontend_key in data:
|
||||
setattr(stock, db_attr, data[frontend_key])
|
||||
|
||||
# [核心修改] 图片字段更新 (List -> JSON String)
|
||||
if 'arrival_photo' in data:
|
||||
imgs = data['arrival_photo']
|
||||
if isinstance(imgs, list):
|
||||
stock.arrival_photo = json.dumps(imgs)
|
||||
|
||||
if 'inspection_report' in data:
|
||||
imgs = data['inspection_report']
|
||||
if isinstance(imgs, list):
|
||||
stock.inspection_report = json.dumps(imgs)
|
||||
|
||||
# 数量与金额联动更新逻辑
|
||||
qty_changed = False
|
||||
price_changed = False
|
||||
|
||||
@ -197,6 +223,9 @@ class BuyInboundService:
|
||||
|
||||
@staticmethod
|
||||
def delete_inbound(stock_id):
|
||||
"""
|
||||
删除入库记录
|
||||
"""
|
||||
try:
|
||||
stock = StockBuy.query.get(stock_id)
|
||||
if not stock:
|
||||
@ -210,7 +239,11 @@ class BuyInboundService:
|
||||
|
||||
@staticmethod
|
||||
def get_list(page, limit, keyword=None):
|
||||
"""
|
||||
获取分页列表
|
||||
"""
|
||||
try:
|
||||
# 1. 查询分页数据
|
||||
query = db.session.query(StockBuy).outerjoin(MaterialBase, StockBuy.base_id == MaterialBase.id)
|
||||
|
||||
if keyword:
|
||||
@ -226,6 +259,9 @@ class BuyInboundService:
|
||||
|
||||
pagination = query.order_by(StockBuy.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# 计算总库存 (聚合)
|
||||
# ---------------------------------------------------------------------
|
||||
current_items = pagination.items
|
||||
base_ids = list(set([item.base_id for item in current_items if item.base_id]))
|
||||
|
||||
@ -243,6 +279,18 @@ class BuyInboundService:
|
||||
'total_avail': float(agg.total_avail or 0)
|
||||
}
|
||||
|
||||
# 辅助函数:解析JSON图片列表
|
||||
def parse_img_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 []
|
||||
|
||||
items = []
|
||||
for item in current_items:
|
||||
mat_name = item.material.name if item.material else '未知物料'
|
||||
@ -287,10 +335,10 @@ class BuyInboundService:
|
||||
'purchaser_email': item.buyer_email,
|
||||
'source_link': item.original_link,
|
||||
'detail_link': item.detail_link,
|
||||
'arrival_photo': item.arrival_photo,
|
||||
|
||||
# [新增] 返回检测报告
|
||||
'inspection_report': item.inspection_report,
|
||||
# [核心修改] 解析 JSON 字符串为数组返回给前端
|
||||
'arrival_photo': parse_img_list(item.arrival_photo),
|
||||
'inspection_report': parse_img_list(item.inspection_report),
|
||||
|
||||
'global_print_id': item.global_print_id,
|
||||
'global_print_id_str': f"{item.global_print_id:08d}" if item.global_print_id else ""
|
||||
|
||||
Reference in New Issue
Block a user