From 2b0e335790877b3bdeeda9f0ca54a539af1ff96d Mon Sep 17 00:00:00 2001 From: yueli Date: Tue, 8 Sep 2026 18:16:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(inbound/buy):=20=E6=89=B9=E5=8F=B7=E8=87=AA?= =?UTF-8?q?=E5=A2=9E=E6=94=B9=E4=B8=BA=E6=8C=89=E7=89=A9=E6=96=99=E7=B2=BE?= =?UTF-8?q?=E5=87=86=E5=8F=96=E6=9C=80=E8=BF=91=E4=B8=80=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 get_latest_batch_record_by_base_id:SQL 直接 filter(base_id)+order by in_date/id desc first(),O(1) 不分页 - 新增 GET /inbound/buy/latest-record - 修复原“拉全表前1000条再前端过滤”导致的历史被截断→批号退回000001/重复入库被拦(如 CCAB0029) --- inventory-backend/app/api/v1/inbound/buy.py | 15 +++++++++++++ .../app/services/inbound/buy_service.py | 22 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/inventory-backend/app/api/v1/inbound/buy.py b/inventory-backend/app/api/v1/inbound/buy.py index 4dae9d5..486001e 100644 --- a/inventory-backend/app/api/v1/inbound/buy.py +++ b/inventory-backend/app/api/v1/inbound/buy.py @@ -353,3 +353,18 @@ def get_last_location(): location = BuyInboundService.get_last_location_by_base_id(base_id) return jsonify({"code": 200, "msg": "success", "data": {"location": location}}) + + +@inbound_buy_bp.route('/latest-record', methods=['GET']) +@permission_required('inbound_buy') +def get_latest_batch_record(): + """ + 按物料精准取最近一次采购入库记录(批号自增用)。 + 返回 data: {'has_history': bool, 'mode': 'serial'|'batch', 'latest_batch': str} + """ + base_id = request.args.get('base_id', type=int) + if not base_id: + return jsonify({"code": 400, "msg": "base_id required"}), 400 + + data = BuyInboundService.get_latest_batch_record_by_base_id(base_id) + return jsonify({"code": 200, "msg": "success", "data": data}) diff --git a/inventory-backend/app/services/inbound/buy_service.py b/inventory-backend/app/services/inbound/buy_service.py index 1a12ebb..e3a886b 100644 --- a/inventory-backend/app/services/inbound/buy_service.py +++ b/inventory-backend/app/services/inbound/buy_service.py @@ -601,6 +601,28 @@ class BuyInboundService: return [r[0] for r in db.session.query(StockBuy.warehouse_location).filter(StockBuy.base_id == base_id).distinct().all()] + @staticmethod + def get_latest_batch_record_by_base_id(base_id): + """ + 按物料精准取“最近一次采购入库记录”(SQL 直接 filter base_id + limit,O(1)、不分页)。 + 用于采购入库表单的批号自增:避免以前端拉 getBuyList 前 1000 条再过滤造成的 + “历史被截断 → 误判无历史 → 批号退回 000001/重复入库被拦”问题。 + 返回: {'has_history': bool, 'mode': 'serial'|'batch', 'latest_batch': str} + """ + if not base_id: + return {'has_history': False, 'mode': 'batch', 'latest_batch': ''} + latest = StockBuy.query.filter( + StockBuy.base_id == base_id + ).order_by( + StockBuy.in_date.desc().nullslast(), + StockBuy.id.desc() + ).first() + if latest is None: + return {'has_history': False, 'mode': 'batch', 'latest_batch': ''} + if latest.serial_number: + return {'has_history': True, 'mode': 'serial', 'latest_batch': latest.batch_number or ''} + return {'has_history': True, 'mode': 'batch', 'latest_batch': latest.batch_number or ''} + @staticmethod def get_last_location_by_base_id(base_id): """