物料-采购件入库页面功能实现
This commit is contained in:
14
inventory-backend/app/api/v1/inbound/__init__.py
Normal file
14
inventory-backend/app/api/v1/inbound/__init__.py
Normal file
@ -0,0 +1,14 @@
|
||||
from flask import Blueprint
|
||||
from .buy import inbound_buy_bp
|
||||
# 后续如果有 semi.py 或 product.py,在这里导入
|
||||
# from .semi import inbound_semi_bp
|
||||
|
||||
# 创建聚合蓝图
|
||||
inbound_bp = Blueprint('inbound', __name__)
|
||||
|
||||
# 挂载子模块。url_prefix 会进行路径拼接
|
||||
# 路径变为:/buy/...
|
||||
inbound_bp.register_blueprint(inbound_buy_bp, url_prefix='/buy')
|
||||
|
||||
# 后续扩展:
|
||||
# inbound_bp.register_blueprint(inbound_semi_bp, url_prefix='/semi')
|
||||
70
inventory-backend/app/api/v1/inbound/buy.py
Normal file
70
inventory-backend/app/api/v1/inbound/buy.py
Normal file
@ -0,0 +1,70 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from app.services.inbound.buy_service import BuyInboundService
|
||||
import traceback
|
||||
|
||||
# 定义蓝图
|
||||
inbound_buy_bp = Blueprint('inbound_buy', __name__)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 1. 获取列表 (GET)
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_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)
|
||||
|
||||
result = BuyInboundService.get_list(page, limit)
|
||||
return jsonify({
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": result
|
||||
})
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 2. 新增入库 (POST)
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/submit', methods=['POST'])
|
||||
def submit():
|
||||
try:
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"code": 400, "msg": "No data provided"}), 400
|
||||
|
||||
BuyInboundService.handle_inbound(data)
|
||||
return jsonify({"code": 200, "msg": "入库成功"})
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 3. 更新入库 (PUT)
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/<int:id>', methods=['PUT'])
|
||||
def update_buy(id):
|
||||
try:
|
||||
data = request.get_json()
|
||||
BuyInboundService.update_inbound(id, data)
|
||||
return jsonify({"code": 200, "msg": "更新成功"})
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 4. 删除入库 (DELETE)
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/<int:id>', methods=['DELETE'])
|
||||
def delete_buy(id):
|
||||
try:
|
||||
BuyInboundService.delete_inbound(id)
|
||||
return jsonify({"code": 200, "msg": "删除成功"})
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
0
inventory-backend/app/api/v1/inbound/product.py
Normal file
0
inventory-backend/app/api/v1/inbound/product.py
Normal file
0
inventory-backend/app/api/v1/inbound/semi.py
Normal file
0
inventory-backend/app/api/v1/inbound/semi.py
Normal file
@ -1,45 +1,87 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from app.services.stock_service import StockService
|
||||
from app.schemas.stock_schema import stock_buy_schema
|
||||
|
||||
# 确保这两个引用路径是存在的,如果报错说明文件没建好
|
||||
try:
|
||||
from app.services.stock_service import StockService
|
||||
from app.schemas.stock_schema import stock_buy_schema
|
||||
except ImportError as e:
|
||||
# 如果服务还没写好,这里会打印错误,防止整个后端起不来
|
||||
print(f"❌ 导入服务出错: {e}")
|
||||
StockService = None
|
||||
stock_buy_schema = None
|
||||
|
||||
stocks_bp = Blueprint('stocks', __name__)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 1. 获取入库列表
|
||||
# URL: /api/v1/stocks/inbound (GET)
|
||||
# ------------------------------------------------------------------
|
||||
@stocks_bp.route('/inbound', methods=['GET'])
|
||||
def get_inbound_list():
|
||||
page = request.args.get('page', 1, type=int)
|
||||
limit = request.args.get('pageSize', 10, type=int)
|
||||
if not StockService:
|
||||
return jsonify({'code': 500, 'msg': '后端服务未初始化'}), 500
|
||||
|
||||
result = StockService.get_list(page, limit)
|
||||
try:
|
||||
page = request.args.get('page', 1, type=int)
|
||||
limit = request.args.get('pageSize', 10, type=int)
|
||||
|
||||
return jsonify({
|
||||
'code': 200,
|
||||
'msg': 'success',
|
||||
'data': result
|
||||
})
|
||||
# 调用 Service 层获取数据
|
||||
result = StockService.get_list(page, limit)
|
||||
|
||||
return jsonify({
|
||||
'code': 200,
|
||||
'msg': 'success',
|
||||
'data': result
|
||||
})
|
||||
except Exception as e:
|
||||
print(f"获取列表报错: {e}")
|
||||
return jsonify({'code': 500, 'msg': '服务器内部错误'}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 2. 新增入库单
|
||||
# URL: /api/v1/stocks/inbound (POST)
|
||||
# ------------------------------------------------------------------
|
||||
@stocks_bp.route('/inbound', methods=['POST'])
|
||||
def create_inbound():
|
||||
if not StockService:
|
||||
return jsonify({'code': 500, 'msg': '后端服务未初始化'}), 500
|
||||
|
||||
json_data = request.get_json()
|
||||
if not json_data:
|
||||
return jsonify({'code': 400, 'msg': '没有接收到数据'}), 400
|
||||
|
||||
try:
|
||||
# 1. 参数校验
|
||||
# 1. 参数校验 (Marshmallow Schema)
|
||||
data = stock_buy_schema.load(json_data)
|
||||
|
||||
# 2. 调用业务逻辑
|
||||
new_stock = StockService.create_inbound(data)
|
||||
|
||||
# 3. 返回成功
|
||||
# 注意:确保 new_stock 对象有 to_dict() 方法,否则这里会报错
|
||||
return jsonify({
|
||||
'code': 200,
|
||||
'msg': '入库成功',
|
||||
'data': new_stock.to_dict()
|
||||
'data': new_stock.to_dict() if hasattr(new_stock, 'to_dict') else str(new_stock)
|
||||
}), 201
|
||||
|
||||
except Exception as e:
|
||||
# 捕获 ValueError 或 SQLAlchemyError
|
||||
# 捕获校验错误或数据库错误
|
||||
print(f"入库报错: {e}")
|
||||
return jsonify({'code': 400, 'msg': str(e)}), 400
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 3. 更新入库单
|
||||
# URL: /api/v1/stocks/inbound/<id> (PUT)
|
||||
# ------------------------------------------------------------------
|
||||
@stocks_bp.route('/inbound/<int:id>', methods=['PUT'])
|
||||
def update_inbound(id):
|
||||
if not StockService:
|
||||
return jsonify({'code': 500, 'msg': '后端服务未初始化'}), 500
|
||||
|
||||
json_data = request.get_json()
|
||||
try:
|
||||
StockService.update_inbound(id, json_data)
|
||||
@ -48,8 +90,15 @@ def update_inbound(id):
|
||||
return jsonify({'code': 400, 'msg': str(e)}), 400
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 4. 删除入库单
|
||||
# URL: /api/v1/stocks/inbound/<id> (DELETE)
|
||||
# ------------------------------------------------------------------
|
||||
@stocks_bp.route('/inbound/<int:id>', methods=['DELETE'])
|
||||
def delete_inbound(id):
|
||||
if not StockService:
|
||||
return jsonify({'code': 500, 'msg': '后端服务未初始化'}), 500
|
||||
|
||||
try:
|
||||
StockService.delete_inbound(id)
|
||||
return jsonify({'code': 200, 'msg': '删除成功'})
|
||||
|
||||
Reference in New Issue
Block a user