# inventory-backend/app/api/v1/inbound/semi.py from flask import Blueprint, request, jsonify from app.services.inbound.semi_service import SemiInboundService import traceback # 定义蓝图 inbound_semi_bp = Blueprint('inbound_semi', __name__) # ------------------------------------------------------------------ # 0. 基础物料搜索 (复用逻辑) # ------------------------------------------------------------------ @inbound_semi_bp.route('/search-base', methods=['GET']) def search_base(): """ 供前端下拉框远程搜索使用 (搜索半成品类型的基础物料) Query Param: keyword (名称或规格) """ try: keyword = request.args.get('keyword', '') # 这里复用 Service 中的搜索逻辑 data = SemiInboundService.search_base_material(keyword) return jsonify({ "code": 200, "msg": "success", "data": data }) except Exception as e: traceback.print_exc() return jsonify({"code": 500, "msg": str(e)}), 500 # ------------------------------------------------------------------ # 1. 获取半成品列表 # ------------------------------------------------------------------ @inbound_semi_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) # 支持按关键字搜索:BOM号、工单号、SN、批号等 keyword = request.args.get('keyword', '') # [修改] 获取状态列表参数 statuses_str = request.args.get('statuses', '') statuses = statuses_str.split(',') if statuses_str else [] result = SemiInboundService.get_list(page, limit, keyword, statuses) return jsonify({"code": 200, "msg": "success", "data": result}) except Exception as e: traceback.print_exc() return jsonify({"code": 500, "msg": str(e)}), 500 # ------------------------------------------------------------------ # 2. 新增半成品入库 (修改:返回创建的对象数据) # ------------------------------------------------------------------ @inbound_semi_bp.route('/submit', methods=['POST']) def submit(): try: data = request.get_json() if not data: return jsonify({"code": 400, "msg": "No data"}), 400 # 修改:调用 Service 处理入库,获取新创建的对象 new_stock = SemiInboundService.handle_inbound(data) # 修改:返回成功信息以及新创建的数据(包含生成的ID和SKU),供前端打印使用 return jsonify({ "code": 200, "msg": "入库成功", "data": new_stock.to_dict() }) except Exception as e: traceback.print_exc() return jsonify({"code": 500, "msg": str(e)}), 500 # ------------------------------------------------------------------ # 3. 更新半成品入库信息 # ------------------------------------------------------------------ @inbound_semi_bp.route('/', methods=['PUT']) def update_semi(id): try: data = request.get_json() SemiInboundService.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. 删除半成品入库记录 # ------------------------------------------------------------------ @inbound_semi_bp.route('/', methods=['DELETE']) def delete_semi(id): try: SemiInboundService.delete_inbound(id) return jsonify({"code": 200, "msg": "删除成功"}) except Exception as e: traceback.print_exc() return jsonify({"code": 500, "msg": str(e)}), 500 # ------------------------------------------------------------------ # 5. [新增] 获取关联出库历史 # ------------------------------------------------------------------ @inbound_semi_bp.route('//history', methods=['GET']) def get_history(id): try: data = SemiInboundService.get_outbound_history(id) return jsonify({ "code": 200, "msg": "success", "data": data }) except Exception as e: traceback.print_exc() return jsonify({"code": 500, "msg": str(e)}), 500