2026-02-03 09:17:28 +08:00
|
|
|
|
# inventory-backend/app/api/v1/inbound/product.py
|
2026-01-29 09:27:56 +08:00
|
|
|
|
from flask import Blueprint, request, jsonify
|
|
|
|
|
|
from app.services.inbound.product_service import ProductInboundService
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
2026-02-06 10:16:37 +08:00
|
|
|
|
inbound_product_bp = Blueprint('stock_product', __name__)
|
2026-01-29 09:27:56 +08:00
|
|
|
|
|
2026-02-03 09:17:28 +08:00
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2026-02-05 15:04:06 +08:00
|
|
|
|
# 0. 基础物料搜索 (关键接口:配合 Service 实现自动回填)
|
2026-02-03 09:17:28 +08:00
|
|
|
|
# ------------------------------------------------------------------
|
2026-01-29 09:27:56 +08:00
|
|
|
|
@inbound_product_bp.route('/search-base', methods=['GET'])
|
|
|
|
|
|
def search_base():
|
2026-02-05 15:04:06 +08:00
|
|
|
|
"""
|
|
|
|
|
|
对应前端 API: /inbound/product/search-base
|
|
|
|
|
|
功能: 模糊搜索基础物料,返回 spec, unit, category, type 等详细信息
|
|
|
|
|
|
"""
|
2026-01-29 09:27:56 +08:00
|
|
|
|
try:
|
2026-02-05 15:04:06 +08:00
|
|
|
|
keyword = request.args.get('keyword', '')
|
|
|
|
|
|
# 调用 Service 层已修复的 search_base_material 方法
|
|
|
|
|
|
data = ProductInboundService.search_base_material(keyword)
|
2026-01-29 09:27:56 +08:00
|
|
|
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
|
|
|
|
|
except Exception as e:
|
2026-02-05 15:04:06 +08:00
|
|
|
|
# 捕获异常并打印堆栈,方便调试
|
|
|
|
|
|
traceback.print_exc()
|
2026-01-29 09:27:56 +08:00
|
|
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
|
2026-02-03 09:17:28 +08:00
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2026-02-05 15:04:06 +08:00
|
|
|
|
# 1. 获取列表 (支持 status 多选筛选)
|
2026-02-03 09:17:28 +08:00
|
|
|
|
# ------------------------------------------------------------------
|
2026-01-29 09:27:56 +08:00
|
|
|
|
@inbound_product_bp.route('/list', methods=['GET'])
|
|
|
|
|
|
def get_list():
|
|
|
|
|
|
try:
|
|
|
|
|
|
page = request.args.get('page', 1, type=int)
|
|
|
|
|
|
limit = request.args.get('pageSize', 15, type=int)
|
|
|
|
|
|
keyword = request.args.get('keyword', '')
|
2026-02-05 15:04:06 +08:00
|
|
|
|
|
|
|
|
|
|
# 接收状态参数 (逗号分隔字符串 -> 列表)
|
2026-02-05 11:58:35 +08:00
|
|
|
|
statuses_str = request.args.get('statuses', '')
|
|
|
|
|
|
statuses = statuses_str.split(',') if statuses_str else []
|
|
|
|
|
|
|
|
|
|
|
|
result = ProductInboundService.get_list(page, limit, keyword, statuses)
|
2026-01-29 09:27:56 +08:00
|
|
|
|
return jsonify({"code": 200, "msg": "success", "data": result})
|
|
|
|
|
|
except Exception as e:
|
2026-02-05 15:04:06 +08:00
|
|
|
|
traceback.print_exc()
|
2026-01-29 09:27:56 +08:00
|
|
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
|
2026-02-03 09:17:28 +08:00
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2026-02-05 11:58:35 +08:00
|
|
|
|
# 2. 新增入库
|
2026-02-03 09:17:28 +08:00
|
|
|
|
# ------------------------------------------------------------------
|
2026-01-29 09:27:56 +08:00
|
|
|
|
@inbound_product_bp.route('/submit', methods=['POST'])
|
|
|
|
|
|
def submit():
|
|
|
|
|
|
try:
|
2026-02-03 09:17:28 +08:00
|
|
|
|
# 调用 Service 处理入库,获取新创建的对象
|
|
|
|
|
|
new_stock = ProductInboundService.handle_inbound(request.get_json())
|
|
|
|
|
|
|
2026-02-05 15:04:06 +08:00
|
|
|
|
# 返回成功信息以及新创建的数据(包含生成的ID和SKU),供前端自动打印使用
|
2026-02-03 09:17:28 +08:00
|
|
|
|
return jsonify({
|
|
|
|
|
|
"code": 200,
|
|
|
|
|
|
"msg": "入库成功",
|
|
|
|
|
|
"data": new_stock.to_dict()
|
|
|
|
|
|
})
|
2026-01-29 09:27:56 +08:00
|
|
|
|
except Exception as e:
|
2026-02-03 09:17:28 +08:00
|
|
|
|
traceback.print_exc()
|
2026-01-29 09:27:56 +08:00
|
|
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
|
2026-02-03 09:17:28 +08:00
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# 3. 更新入库
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2026-01-29 09:27:56 +08:00
|
|
|
|
@inbound_product_bp.route('/<int:id>', methods=['PUT'])
|
|
|
|
|
|
def update(id):
|
|
|
|
|
|
try:
|
|
|
|
|
|
ProductInboundService.update_inbound(id, request.get_json())
|
|
|
|
|
|
return jsonify({"code": 200, "msg": "更新成功"})
|
|
|
|
|
|
except Exception as e:
|
2026-02-05 15:04:06 +08:00
|
|
|
|
traceback.print_exc()
|
2026-01-29 09:27:56 +08:00
|
|
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
|
2026-02-03 09:17:28 +08:00
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# 4. 删除
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2026-01-29 09:27:56 +08:00
|
|
|
|
@inbound_product_bp.route('/<int:id>', methods=['DELETE'])
|
|
|
|
|
|
def delete(id):
|
|
|
|
|
|
try:
|
|
|
|
|
|
ProductInboundService.delete_inbound(id)
|
|
|
|
|
|
return jsonify({"code": 200, "msg": "删除成功"})
|
2026-02-05 13:17:39 +08:00
|
|
|
|
except Exception as e:
|
2026-02-05 15:04:06 +08:00
|
|
|
|
traceback.print_exc()
|
2026-02-05 13:17:39 +08:00
|
|
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
2026-02-05 15:04:06 +08:00
|
|
|
|
# 5. 获取出库历史
|
2026-02-05 13:17:39 +08:00
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
@inbound_product_bp.route('/<int:id>/history', methods=['GET'])
|
|
|
|
|
|
def get_history(id):
|
|
|
|
|
|
try:
|
|
|
|
|
|
data = ProductInboundService.get_outbound_history(id)
|
|
|
|
|
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
2026-01-29 09:27:56 +08:00
|
|
|
|
except Exception as e:
|
2026-02-05 15:04:06 +08:00
|
|
|
|
traceback.print_exc()
|
2026-02-10 13:51:19 +08:00
|
|
|
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
# 6. 系统用户建议
|
|
|
|
|
|
# ------------------------------------------------------------------
|
|
|
|
|
|
@inbound_product_bp.route('/suggestions/users', methods=['GET'])
|
|
|
|
|
|
def get_user_suggestions():
|
|
|
|
|
|
keyword = request.args.get('keyword', '')
|
|
|
|
|
|
data = ProductInboundService.search_system_users(keyword)
|
|
|
|
|
|
return jsonify({"code": 200, "msg": "success", "data": data})
|