diff --git a/inventory-backend/app/api/v1/transactions.py b/inventory-backend/app/api/v1/transactions.py index b75a8b8..1925230 100644 --- a/inventory-backend/app/api/v1/transactions.py +++ b/inventory-backend/app/api/v1/transactions.py @@ -737,6 +737,33 @@ def reject_borrow_transfer(transfer_id): return jsonify({'code': 500, 'msg': f'服务器内部错误: {str(e)}'}), 500 +# --- 待我接收的转交数量(全局待办提醒)--- +@trans_bp.route('/borrow/transfer/pending-count', methods=['GET']) +@jwt_required() +def get_pending_transfer_count(): + """ + 待我接收的转交数量,供前端全局待办强提醒使用。 + + ★ 无 permission_required:接收人可能是普通员工,待办提醒必须人人可见 + (与 accept/reject 同级的理由 —— 这是员工处置自己名下资产,非库管职权)。 + + ★ 路由不与 /borrow//transfer 等冲突: + 「transfer」无法匹配 ,「pending-count」也无法匹配 + ,Werkzeug 会按转换器精确分派。 + """ + try: + count = TransService.count_pending_transfers(get_jwt_identity()) + return jsonify({ + 'code': 200, + 'msg': 'success', + 'count': count, + 'data': {'count': count}, + }), 200 + except Exception as e: + traceback.print_exc() + return jsonify({'code': 500, 'msg': f'服务器内部错误: {str(e)}'}), 500 + + # --- 借出单的流转历史(转交链 + 逐次归还)--- @trans_bp.route('/borrow//history', methods=['GET']) @jwt_required() diff --git a/inventory-backend/app/services/trans_service.py b/inventory-backend/app/services/trans_service.py index 649af5f..20f8767 100644 --- a/inventory-backend/app/services/trans_service.py +++ b/inventory-backend/app/services/trans_service.py @@ -726,6 +726,24 @@ class TransService: raise e return transfer + @staticmethod + def count_pending_transfers(user_id): + """ + 「待我接收」的转交数量 —— 供全局待办提醒在初始化与轮询时调用。 + + ★ 刻意做成极轻量:一次 count,不联表、不解析物料名。 + 轮询接口必须便宜,否则会从「提醒」变成「后台噪音」。 + """ + if user_id is None: + return 0 + try: + return TransBorrowTransfer.query.filter( + TransBorrowTransfer.to_user_id == int(user_id), + TransBorrowTransfer.status == TRANSFER_STATUS_PENDING, + ).count() + except (TypeError, ValueError): + return 0 + @staticmethod def get_transfer_history(borrow_id): """某条借出记录的转交历史(按时间正序,便于还原 A→B→C 链路)"""