fix(inbound/buy): 批号自增改为按物料精准取最近一条

- 新增 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)
This commit is contained in:
yueli
2026-09-08 18:16:04 +08:00
parent b4971f057e
commit 2b0e335790
2 changed files with 37 additions and 0 deletions

View File

@ -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})

View File

@ -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 + limitO(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):
"""