采购件图像上传初实现,支持多图,检测报告的图片以及链接上传
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
# app/models/inbound/buy.py
|
# app/models/inbound/buy.py
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
class StockBuy(db.Model):
|
class StockBuy(db.Model):
|
||||||
@ -36,22 +37,35 @@ class StockBuy(db.Model):
|
|||||||
exchange_rate = db.Column(db.Numeric(15, 6), default=1.0)
|
exchange_rate = db.Column(db.Numeric(15, 6), default=1.0)
|
||||||
|
|
||||||
supplier_name = db.Column(db.String(255))
|
supplier_name = db.Column(db.String(255))
|
||||||
buyer_name = db.Column(db.String(100))
|
buyer_name = db.Column(db.String(100)) # 对应 SQL: buyer_name
|
||||||
buyer_email = db.Column(db.String(100))
|
buyer_email = db.Column(db.String(100)) # 对应 SQL: buyer_email
|
||||||
original_link = db.Column(db.Text)
|
original_link = db.Column(db.Text) # 对应 SQL: original_link
|
||||||
detail_link = db.Column(db.Text)
|
detail_link = db.Column(db.Text)
|
||||||
arrival_photo = db.Column(db.Text)
|
|
||||||
|
|
||||||
# [新增] 检测报告图片路径
|
# 图片字段 (存储 JSON 字符串)
|
||||||
|
arrival_photo = db.Column(db.Text)
|
||||||
|
# [新增] 检测报告图片路径 (存储 JSON 字符串)
|
||||||
inspection_report = db.Column(db.Text)
|
inspection_report = db.Column(db.Text)
|
||||||
|
|
||||||
# 全局打印流水号
|
# [新增] 全局打印流水号 (用于跨表连续编号,对应 Sequence: global_print_seq)
|
||||||
global_print_id = db.Column(db.Integer)
|
global_print_id = db.Column(db.Integer)
|
||||||
|
|
||||||
# 关系定义
|
# 关系定义
|
||||||
material = db.relationship('MaterialBase', back_populates='stock_buys')
|
material = db.relationship('MaterialBase', back_populates='stock_buys')
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
|
# 辅助解析函数:将数据库存储的 JSON 字符串转为 List
|
||||||
|
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 []
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'id': self.id,
|
'id': self.id,
|
||||||
'base_id': self.base_id,
|
'base_id': self.base_id,
|
||||||
@ -87,11 +101,12 @@ class StockBuy(db.Model):
|
|||||||
'purchaser_email': self.buyer_email,
|
'purchaser_email': self.buyer_email,
|
||||||
'source_link': self.original_link,
|
'source_link': self.original_link,
|
||||||
'detail_link': self.detail_link,
|
'detail_link': self.detail_link,
|
||||||
'arrival_photo': self.arrival_photo,
|
|
||||||
|
|
||||||
# [新增] 返回检测报告字段
|
# [修改] 解析为数组返回给前端
|
||||||
'inspection_report': self.inspection_report,
|
'arrival_photo': parse_img_list(self.arrival_photo),
|
||||||
|
'inspection_report': parse_img_list(self.inspection_report),
|
||||||
|
|
||||||
|
# [新增] 返回全局打印ID及其格式化字符串
|
||||||
'global_print_id': self.global_print_id,
|
'global_print_id': self.global_print_id,
|
||||||
'global_print_id_str': f"{self.global_print_id:010d}" if self.global_print_id else ""
|
'global_print_id_str': f"{self.global_print_id:010d}" if self.global_print_id else ""
|
||||||
}
|
}
|
||||||
@ -5,6 +5,7 @@ from app.models.base import MaterialBase
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from sqlalchemy import or_, func, text
|
from sqlalchemy import or_, func, text
|
||||||
import traceback
|
import traceback
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
class BuyInboundService:
|
class BuyInboundService:
|
||||||
@ -46,6 +47,9 @@ class BuyInboundService:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def handle_inbound(data):
|
def handle_inbound(data):
|
||||||
|
"""
|
||||||
|
处理入库逻辑
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
base_id = data.get('base_id')
|
base_id = data.get('base_id')
|
||||||
if not base_id:
|
if not base_id:
|
||||||
@ -83,17 +87,26 @@ class BuyInboundService:
|
|||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# 3. 条码逻辑处理
|
# 3. 条码逻辑处理
|
||||||
# 如果前端没传条码(barcode),则默认使用系统生成的 SKU 作为条码
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
final_barcode = data.get('barcode')
|
final_barcode = data.get('barcode')
|
||||||
if not final_barcode:
|
if not final_barcode:
|
||||||
final_barcode = generated_sku
|
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(
|
new_stock = StockBuy(
|
||||||
base_id=material.id,
|
base_id=material.id,
|
||||||
global_print_id=next_global_id,
|
global_print_id=next_global_id,
|
||||||
sku=generated_sku,
|
sku=generated_sku, # 自动生成的SKU
|
||||||
barcode=final_barcode,
|
barcode=final_barcode, # 如果未输入,则存入SKU值
|
||||||
|
|
||||||
in_date=in_date_val,
|
in_date=in_date_val,
|
||||||
serial_number=data.get('serial_number'),
|
serial_number=data.get('serial_number'),
|
||||||
@ -113,15 +126,16 @@ class BuyInboundService:
|
|||||||
buyer_email=data.get('purchaser_email'),
|
buyer_email=data.get('purchaser_email'),
|
||||||
original_link=data.get('source_link'),
|
original_link=data.get('source_link'),
|
||||||
detail_link=data.get('detail_link'),
|
detail_link=data.get('detail_link'),
|
||||||
arrival_photo=data.get('arrival_photo'),
|
|
||||||
|
|
||||||
# [新增] 保存检测报告字段
|
# [核心修改] 将列表转为 JSON 字符串存储
|
||||||
inspection_report=data.get('inspection_report')
|
arrival_photo=json.dumps(arrival_list),
|
||||||
|
inspection_report=json.dumps(report_list)
|
||||||
)
|
)
|
||||||
|
|
||||||
db.session.add(new_stock)
|
db.session.add(new_stock)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
# 返回创建的对象实例
|
||||||
return new_stock
|
return new_stock
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -130,6 +144,9 @@ class BuyInboundService:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_inbound(stock_id, data):
|
def update_inbound(stock_id, data):
|
||||||
|
"""
|
||||||
|
更新入库记录
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
print(f"----- UPDATE DEBUG: ID={stock_id} -----")
|
print(f"----- UPDATE DEBUG: ID={stock_id} -----")
|
||||||
|
|
||||||
@ -137,6 +154,7 @@ class BuyInboundService:
|
|||||||
if not stock:
|
if not stock:
|
||||||
raise ValueError("记录不存在")
|
raise ValueError("记录不存在")
|
||||||
|
|
||||||
|
# 基础字段映射
|
||||||
field_mapping = {
|
field_mapping = {
|
||||||
'sku': 'sku',
|
'sku': 'sku',
|
||||||
'barcode': 'barcode',
|
'barcode': 'barcode',
|
||||||
@ -147,21 +165,29 @@ class BuyInboundService:
|
|||||||
'inspection_status': 'inspection_status',
|
'inspection_status': 'inspection_status',
|
||||||
'supplier_name': 'supplier_name',
|
'supplier_name': 'supplier_name',
|
||||||
'detail_link': 'detail_link',
|
'detail_link': 'detail_link',
|
||||||
'arrival_photo': 'arrival_photo',
|
|
||||||
'currency': 'currency',
|
'currency': 'currency',
|
||||||
'exchange_rate': 'exchange_rate',
|
'exchange_rate': 'exchange_rate',
|
||||||
'purchaser': 'buyer_name',
|
'purchaser': 'buyer_name',
|
||||||
'purchaser_email': 'buyer_email',
|
'purchaser_email': 'buyer_email',
|
||||||
'source_link': 'original_link',
|
'source_link': 'original_link'
|
||||||
|
|
||||||
# [新增] 允许更新检测报告
|
|
||||||
'inspection_report': 'inspection_report'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for frontend_key, db_attr in field_mapping.items():
|
for frontend_key, db_attr in field_mapping.items():
|
||||||
if frontend_key in data:
|
if frontend_key in data:
|
||||||
setattr(stock, db_attr, data[frontend_key])
|
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
|
qty_changed = False
|
||||||
price_changed = False
|
price_changed = False
|
||||||
|
|
||||||
@ -197,6 +223,9 @@ class BuyInboundService:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_inbound(stock_id):
|
def delete_inbound(stock_id):
|
||||||
|
"""
|
||||||
|
删除入库记录
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
stock = StockBuy.query.get(stock_id)
|
stock = StockBuy.query.get(stock_id)
|
||||||
if not stock:
|
if not stock:
|
||||||
@ -210,7 +239,11 @@ class BuyInboundService:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_list(page, limit, keyword=None):
|
def get_list(page, limit, keyword=None):
|
||||||
|
"""
|
||||||
|
获取分页列表
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
|
# 1. 查询分页数据
|
||||||
query = db.session.query(StockBuy).outerjoin(MaterialBase, StockBuy.base_id == MaterialBase.id)
|
query = db.session.query(StockBuy).outerjoin(MaterialBase, StockBuy.base_id == MaterialBase.id)
|
||||||
|
|
||||||
if keyword:
|
if keyword:
|
||||||
@ -226,6 +259,9 @@ class BuyInboundService:
|
|||||||
|
|
||||||
pagination = query.order_by(StockBuy.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
pagination = query.order_by(StockBuy.id.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------
|
||||||
|
# 计算总库存 (聚合)
|
||||||
|
# ---------------------------------------------------------------------
|
||||||
current_items = pagination.items
|
current_items = pagination.items
|
||||||
base_ids = list(set([item.base_id for item in current_items if item.base_id]))
|
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)
|
'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 = []
|
items = []
|
||||||
for item in current_items:
|
for item in current_items:
|
||||||
mat_name = item.material.name if item.material else '未知物料'
|
mat_name = item.material.name if item.material else '未知物料'
|
||||||
@ -287,10 +335,10 @@ class BuyInboundService:
|
|||||||
'purchaser_email': item.buyer_email,
|
'purchaser_email': item.buyer_email,
|
||||||
'source_link': item.original_link,
|
'source_link': item.original_link,
|
||||||
'detail_link': item.detail_link,
|
'detail_link': item.detail_link,
|
||||||
'arrival_photo': item.arrival_photo,
|
|
||||||
|
|
||||||
# [新增] 返回检测报告
|
# [核心修改] 解析 JSON 字符串为数组返回给前端
|
||||||
'inspection_report': item.inspection_report,
|
'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': item.global_print_id,
|
||||||
'global_print_id_str': f"{item.global_print_id:08d}" if item.global_print_id else ""
|
'global_print_id_str': f"{item.global_print_id:08d}" if item.global_print_id else ""
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user