'use strict'; const express = require('express'); const { getReplenishExportRows, getReplenishList, } = require('../services/binReplenishService'); const { sendWorkbookDownload } = require('../services/exportWorkbook'); const router = express.Router(); const EXPORT_COLUMNS = [ { key: 'skuId', title: '商品编码' }, { key: 'productName', title: '品名' }, { key: 'supplier', title: '供应商' }, { key: 'statusLabel', title: '状态' }, { key: 'pickQty', title: '拣货位库存' }, { key: 'storageQty', title: '存储位库存' }, { key: 'totalQty', title: '现有库存合计' }, { key: 'orderQty', title: '待发订单' }, { key: 'excess', title: '超出订单数' }, { key: 'suggestQty', title: '建议补货量' }, { key: 'shortfall', title: '存储缺口' }, ]; /** GET /api/bin-replenish/list —— 缺货补货监控列表(订单驱动) */ router.get('/list', async (req, res, next) => { try { const payload = await getReplenishList({ warehouse: req.query.warehouse, keyword: req.query.keyword, status: req.query.status, page: req.query.page, pageSize: req.query.pageSize, refresh: req.query.refresh, }); res.json({ success: true, data: payload }); } catch (error) { next(error); } }); /** GET /api/bin-replenish/export —— 服务端全量导出 */ router.get('/export', async (req, res, next) => { try { const format = String(req.query.format || 'xlsx').toLowerCase() === 'csv' ? 'csv' : 'xlsx'; const payload = await getReplenishExportRows({ warehouse: req.query.warehouse, keyword: req.query.keyword, status: req.query.status, }); const fileName = `${payload.warehouse.shortName}待补明细`; sendWorkbookDownload(res, { fileName, format, sheets: [{ name: '缺货补货监控', header: EXPORT_COLUMNS.map((column) => column.title), rows: payload.rows.map((row) => EXPORT_COLUMNS.map((column) => row[column.key])), }], }); } catch (error) { next(error); } }); /** GET /api/bin-replenish/ping —— 路由存活探针(401 也代表已注册) */ router.get('/ping', (req, res) => { res.json({ success: true, data: { ok: true } }); }); module.exports = router;