fix(stock.check): exportDetail 商品盘点固定两 sheet(汇总和差异 + 库位/数量/条码/商品名称),移除 mode 入参(0.3.261)

This commit is contained in:
liufumingren 2026-10-10 03:58:54 +08:00
parent 78b46a740b
commit fab2d9c0d1
4 changed files with 73 additions and 122 deletions

View File

@ -815,21 +815,18 @@ class StockCheckMethodService extends MainService {
/**
* 提交盘点明细导出任务(单任务,按盘点范围分流)。
* 异步执行:商品范围任务按 mode 导出——mode=location(缺省)Sheet「库位明细」
* (每商品每库位一行:账面快照/盘点数量/差异);mode=summary Sheet1「明细汇总」
* (每商品一行)+ Sheet2「商品差异」(仅差异商品);库位范围任务 Sheet「盘点明细」
* (库位/商品/盘点数量)→ ExcelJS 流式写入 → COS 上传。
* 异步执行:商品范围任务固定两个 Sheet——Sheet1「汇总和差异」(每商品一行:账面快照合计/实盘合计/差异/差异类型盘盈|盘亏)+ Sheet2「库位明细」(库位编码/数量/条码/商品名称);
* 库位范围任务 Sheet「盘点明细」(库位/商品/盘点数量)→ ExcelJS 流式写入 → COS 上传。
* 进度通过 Redis 缓存追踪,立即返回任务ID;进度查询复用 exportTaskProgress。
*
* @param {Object} data 请求体。
* @param {number} data.id 盘点任务ID。
* @param {string} [data.mode] 商品盘点导出模式:location=区分库位明细(缺省)/summary=明细汇总+商品差异两 sheet;库位盘点忽略。
* @return {Promise<Object>} 统一 success 包装,返回任务ID。
*/
async exportDetail(data = {}) {
const { ctx } = this;
const warehouseId = Number(ctx.user && ctx.user.store_id) || 0;
const { id, mode } = data || {};
const { id } = data || {};
const checkId = Number(id);
if (!Number.isInteger(checkId) || checkId <= 0) {
@ -866,7 +863,6 @@ class StockCheckMethodService extends MainService {
const result = await this.service.systems.stockCheck.exportCheckDetail({
warehouseId,
id: checkId,
mode,
onProgress: async progress => {
const safeProgress = this.isPlainObject(progress) ? progress : {};
await this.setExportTaskProgress(taskId, {

View File

@ -4926,13 +4926,11 @@ class StockCheckService extends Service {
/**
* 盘点明细导出(单任务,按盘点范围分流):
* - 商品范围(scope_type=goods):
* mode=location(缺省):Sheet「库位明细」——每商品每库位一行
* (盘点单号/商品编码/商品名称/规格/库位编码/账面数量(快照)/盘点数量/差异数量,
* 未盘库位盘点数量 0、差异留空);
* mode=summary:Sheet1「明细汇总」(每商品一行:账面快照合计/实盘合计/差异/盘点状态/
* 盘点人/最近盘点时间)+ Sheet2「商品差异」(仅差异商品:账面/实盘/差异/差异类型);
* 未知商品(条码维度,无账面、差异=实盘=盘盈)追加在汇总与差异两个 sheet。
* - 商品范围(scope_type=goods)固定两个 sheet:
* Sheet1「汇总和差异」——每商品一行(盘点单号/商品编码/商品名称/规格/账面数量(快照合计)/
* 盘点数量/差异数量/差异类型(盘盈|盘亏,未盘留空);未知商品(条码维度,无账面、差异=实盘=盘盈)
* 追加在末尾;
* Sheet2「库位明细」——每商品每库位一行(库位编码/数量(实盘,未盘 0)/条码/商品名称)。
* - 库位范围(scope_type=location):Sheet「盘点明细」——库位编码/商品编码/商品名称/盘点数量
* (作业记录口径,未知商品编码=扫描条码,空库位标记商品为空、数量 0)。
* 流式生成 Excel → COS 上传;通过 onProgress 回调实时推送进度。
@ -4940,13 +4938,12 @@ class StockCheckService extends Service {
* @param {Object} params 导出参数。
* @param {number} params.warehouseId 仓库ID(由方法层从上下文注入)。
* @param {number} params.id 盘点任务ID。
* @param {string} [params.mode] 商品盘点导出模式:location=区分库位明细(缺省)/summary=明细汇总+商品差异两 sheet;库位盘点忽略。
* @param {Function} [params.onProgress] 进度回调。
* @return {Promise<Object>} 导出结果,包含文件下载信息与 total_count。
* @throws {Error} 任务不存在或参数非法时抛出业务错误(错误码 400)。
*/
async exportCheckDetail(params = {}) {
const { warehouseId, id, mode, onProgress } = params;
const { warehouseId, id, onProgress } = params;
const { ctx } = this;
const { Op } = ctx.app.Sequelize;
@ -5002,21 +4999,40 @@ class StockCheckService extends Service {
stockMap = new Map(stockRows.map(item => [ Number(item.id), item ]));
}
const normalizedMode = isLocationScope
? 'simple'
: (String(mode || '').trim() === 'summary' ? 'summary' : 'location');
// 商品范围:商品条码(wms_stock_barcodes 按 stock_id 取第一条,库位明细 sheet 的条码列)
const barcodeMap = new Map();
if (!isLocationScope) {
const goodsStockIds = [ ...new Set(goodsRows
.map(row => Number(row.stock_id))
.filter(stockId => Number.isInteger(stockId) && stockId > 0)) ];
if (goodsStockIds.length > 0) {
const barcodeRows = await ctx.model.StockBarcode.findAll({
where: {
warehouse_id: Number(warehouseId),
stock_id: { [Op.in]: goodsStockIds },
},
attributes: [ 'stock_id', 'barcode' ],
order: [[ 'id', 'ASC' ]],
raw: true,
});
for (const barcodeRow of barcodeRows) {
const barcodeStockId = Number(barcodeRow.stock_id);
if (!barcodeMap.has(barcodeStockId)) {
barcodeMap.set(barcodeStockId, String(barcodeRow.barcode || '').trim());
}
}
}
}
const totalCount = isLocationScope
? records.length
: (normalizedMode === 'summary'
? goodsRows.reduce(
(sum, row) => sum + 1 + (Array.isArray(row.unknown_items) ? row.unknown_items.length : 0),
0
)
: goodsRows.reduce(
(sum, row) => sum + (Array.isArray(row.locations) ? row.locations.length : 0),
0
));
: goodsRows.reduce(
(sum, row) => sum + 1 + (Array.isArray(row.unknown_items) ? row.unknown_items.length : 0),
0
) + goodsRows.reduce(
(sum, row) => sum + (Array.isArray(row.locations) ? row.locations.length : 0),
0
);
await reportProgress('running', 10, `正在导出盘点明细(共 ${totalCount} 行)...`, {
totalCount,
@ -5026,10 +5042,10 @@ class StockCheckService extends Service {
const exportResult = await this._generateAndUploadCheckDetailExcel({
check: check.get({ plain: true }),
isLocationScope,
mode: normalizedMode,
goodsRows,
records,
stockMap,
barcodeMap,
totalCount,
onProgress,
});
@ -5048,15 +5064,15 @@ class StockCheckService extends Service {
}
/**
* 生成盘点明细导出 Excel 并上传 COS(Sheet 结构随盘点范围与导出模式变化,见 exportCheckDetail)。
* 生成盘点明细导出 Excel 并上传 COS(Sheet 结构随盘点范围变化,见 exportCheckDetail)。
*
* @param {Object} params 生成参数。
* @param {Object} params.check 盘点主表行。
* @param {boolean} params.isLocationScope 是否库位范围盘点。
* @param {string} params.mode 导出模式:simple(库位范围)/location(商品库位明细)/summary(商品汇总+差异)。
* @param {Array<Object>} params.goodsRows 商品范围聚合行(result 口径)。
* @param {Array<Object>} params.records 作业记录原始行。
* @param {Map<number, Object>} params.stockMap 商品快照映射(库位范围用)。
* @param {Map<number, string>} params.barcodeMap 商品条码映射(商品范围库位明细 sheet 用)。
* @param {number} params.totalCount 总行数(进度计算用)。
* @param {Function} [params.onProgress] 进度回调。
* @return {Promise<Object>} { filename, fileKey, url, cdnUrl, cosUrl }。
@ -5065,10 +5081,10 @@ class StockCheckService extends Service {
const {
check,
isLocationScope,
mode,
goodsRows = [],
records = [],
stockMap = new Map(),
barcodeMap = new Map(),
totalCount = 0,
onProgress,
} = params;
@ -5123,23 +5139,10 @@ class StockCheckService extends Service {
await reportRowProgress();
}
sheet.commit();
} else if (mode === 'summary') {
// ---- 商品范围 mode=summary:明细汇总 + 商品差异 两个 sheet ----
const summarySheet = workbook.addWorksheet('明细汇总');
} else {
// ---- 商品范围:汇总和差异 + 库位明细 两个 sheet ----
const summarySheet = workbook.addWorksheet('汇总和差异');
summarySheet.addRow([
'盘点单号',
'商品编码',
'商品名称',
'规格',
'账面数量',
'盘点数量',
'差异数量',
'盘点状态',
'盘点人',
'最近盘点时间',
]).commit();
const diffSheet = workbook.addWorksheet('商品差异');
diffSheet.addRow([
'盘点单号',
'商品编码',
'商品名称',
@ -5149,9 +5152,12 @@ class StockCheckService extends Service {
'差异数量',
'差异类型',
]).commit();
const locationSheet = workbook.addWorksheet('库位明细');
locationSheet.addRow([ '库位编码', '数量', '条码', '商品名称' ]).commit();
for (const row of goodsRows) {
const hasRecords = Array.isArray(row.records) && row.records.length > 0;
const diffQuantity = hasRecords ? (Number(row.diff_quantity) || 0) : 0;
summarySheet.addRow([
checkNo,
row.item_code || '',
@ -5159,40 +5165,15 @@ class StockCheckService extends Service {
row.spec || '',
Number(row.system_quantity) || 0,
hasRecords ? (Number(row.checked_quantity) || 0) : '',
hasRecords ? (Number(row.diff_quantity) || 0) : '',
hasRecords ? '已盘' : '未盘',
Array.isArray(row.checkers) ? row.checkers.join('、') : '',
row.last_checked_at ? this._formatLocalDateTime(row.last_checked_at) : '',
hasRecords ? diffQuantity : '',
!hasRecords ? '' : (diffQuantity > 0 ? '盘盈' : (diffQuantity < 0 ? '盘亏' : '')),
]).commit();
if (hasRecords && row.is_diff === 1) {
const diffQuantity = Number(row.diff_quantity) || 0;
diffSheet.addRow([
checkNo,
row.item_code || '',
row.item_name || '',
row.spec || '',
Number(row.system_quantity) || 0,
Number(row.checked_quantity) || 0,
diffQuantity,
diffQuantity > 0 ? '盘盈' : '盘亏',
]).commit();
}
await reportRowProgress();
// 未知商品(条码维度,无账面,差异=实盘=盘盈)
for (const unknownItem of (Array.isArray(row.unknown_items) ? row.unknown_items : [])) {
const checkedQuantity = Number(unknownItem.checked_quantity) || 0;
summarySheet.addRow([
checkNo,
unknownItem.barcode || '',
unknownItem.item_name || '',
'',
0,
checkedQuantity,
checkedQuantity,
'已盘',
'',
'',
]).commit();
diffSheet.addRow([
checkNo,
unknownItem.barcode || '',
unknownItem.item_name || '',
@ -5202,43 +5183,24 @@ class StockCheckService extends Service {
checkedQuantity,
'盘盈',
]).commit();
await reportRowProgress();
}
await reportRowProgress();
}
summarySheet.commit();
diffSheet.commit();
} else {
// ---- 商品范围 mode=location:库位明细(每商品每库位一行)----
const sheet = workbook.addWorksheet('库位明细');
sheet.addRow([
'盘点单号',
'商品编码',
'商品名称',
'规格',
'库位编码',
'账面数量',
'盘点数量',
'差异数量',
]).commit();
for (const row of goodsRows) {
const hasRecords = Array.isArray(row.records) && row.records.length > 0;
// 库位明细:库位 / 数量(实盘,未盘 0)/ 条码 / 商品名称
const stockId = Number(row.stock_id);
const barcode = barcodeMap.get(stockId) || '';
for (const location of (Array.isArray(row.locations) ? row.locations : [])) {
const systemQuantity = Number(location.system_quantity) || 0;
const quantity = Number(location.quantity) || 0;
sheet.addRow([
checkNo,
row.item_code || '',
row.item_name || '',
row.spec || '',
locationSheet.addRow([
location.location_code || '',
systemQuantity,
quantity,
hasRecords ? quantity - systemQuantity : '',
Number(location.quantity) || 0,
barcode,
row.item_name || '',
]).commit();
await reportRowProgress();
}
}
sheet.commit();
summarySheet.commit();
locationSheet.commit();
}
await workbook.commit();

View File

@ -3,15 +3,16 @@
"version": "0.3.261",
"date": "2026-10-10",
"type": "added",
"summary": "新增 stock.check.exportDetail 盘点明细导出接口(异步,按盘点范围分流:商品盘点分库位明细/汇总+差异两模式,库位盘点简化)",
"summary": "新增 stock.check.exportDetail 盘点明细导出接口(异步,按盘点范围分流:商品盘点固定两 sheet——汇总和差异 + 库位/数量/条码/商品名称;库位盘点简化)",
"methods": [
"stock.check.exportDetail"
],
"notes": [
"接口:stock.check.exportDetail,入参 id(盘点任务ID,必填)+ mode(可选,仅商品盘点生效:location=区分库位明细(缺省)/summary=明细汇总+商品差异两 sheet;库位盘点忽略);异步执行,立即返回 task_id,进度查询复用 stock.check.exportTaskProgress。",
"商品盘点 mode=location:Sheet「库位明细」每商品每库位一行(盘点单号/商品编码/商品名称/规格/库位编码/账面数量(明细行 system_quantity 快照,提交审核时刷新)/盘点数量/差异数量;未盘库位盘点数量 0、差异留空);mode=summary:Sheet1「明细汇总」(每商品一行:账面快照合计/实盘合计/差异/盘点状态/盘点人/最近盘点时间)+ Sheet2「商品差异」(仅差异商品:账面/实盘/差异/差异类型盘盈|盘亏);未知商品追加在汇总与差异两 sheet(无账面、差异=实盘=盘盈)。",
"接口:stock.check.exportDetail,入参 id(盘点任务ID,必填);异步执行,立即返回 task_id,进度查询复用 stock.check.exportTaskProgress。",
"商品盘点固定两个 Sheet:Sheet1「汇总和差异」每商品一行(盘点单号/商品编码/商品名称/规格/账面数量(明细行 system_quantity 快照合计,提交审核时刷新)/盘点数量/差异数量/差异类型(盘盈|盘亏,无差异或未盘留空);未知商品追加在末尾(无账面、差异=实盘=盘盈);Sheet2「库位明细」每商品每库位一行(库位编码/数量(实盘,未盘 0)/条码(wms_stock_barcodes 第一条)/商品名称)。",
"库位盘点:Sheet「盘点明细」作业记录口径(库位编码/商品编码/商品名称/盘点数量),未知商品编码=扫描条码、空库位标记商品为空数量 0。",
"代码依据:app/service/systems/stock_check.js#exportCheckDetail/#_generateAndUploadCheckDetailExcel(商品范围复用 result 的商品合并聚合)/#_uploadExportExcelFile(原 _generateAndUploadStockCheckExcel 上传段抽为公共方法,两处导出一致复用)+ app/service/systemMethods/stock/check.js#exportDetail(ALL_METHOD_NAMES/_noNeedLog 已加入);回归测试 4 用例(location/summary/库位任务三口径回读 Excel 断言 + 方法层异步任务轮询至 success),stock_check 全量 235 用例通过、ESLint 0 错误。",
"迭代记录:首版按 mode 二选一(location 单 sheet / summary 两 sheet),用户纠正为商品盘点固定两 sheet(汇总和差异 + 库位明细精简列),mode 入参已移除。",
"代码依据:app/service/systems/stock_check.js#exportCheckDetail/#_generateAndUploadCheckDetailExcel(商品范围复用 result 的商品合并聚合;新增 StockBarcode 条码查询)/#_uploadExportExcelFile(原 _generateAndUploadStockCheckExcel 上传段抽为公共方法,两处导出一致复用)+ app/service/systemMethods/stock/check.js#exportDetail(ALL_METHOD_NAMES/_noNeedLog 已加入);回归测试 3 用例(商品两 sheet 回读断言/库位任务/方法层异步轮询至 success),stock_check 全量 235 用例通过、ESLint 0 错误。",
"文档变更范围:methods/system.json 的 module=stock.check 分组新增 stock.check.exportDetail 一条(位于 stock.check.export 之后,分组由 24 条增至 25 条);根版本 0.3.260 → 0.3.261、updatedAt 保持 2026-10-10;methods/changes/system.json 新增本条(插入数组首位);其余 24 条 method 未改动。"
]
},

View File

@ -40453,7 +40453,7 @@
},
{
"method": "stock.check.exportDetail",
"description": "提交盘点明细导出任务(异步,单任务,按盘点范围分流):商品范围任务(scope_type=goods)按 mode 导出——mode=location(缺省)Sheet「库位明细」(每商品每库位一行,列:盘点单号/商品编码/商品名称/规格/库位编码/账面数量(明细行 system_quantity 快照,提交审核时刷新)/盘点数量/差异数量;未盘库位盘点数量 0、差异留空);mode=summary 两个 Sheet——Sheet1「明细汇总」(每商品一行,列:盘点单号/商品编码/商品名称/规格/账面数量(快照合计)/盘点数量/差异数量/盘点状态(已盘|未盘)/盘点人/最近盘点时间)+ Sheet2「商品差异」(仅差异商品,列:盘点单号/商品编码/商品名称/规格/账面数量/盘点数量/差异数量/差异类型(盘盈|盘亏));未知商品(条码维度,无账面、差异=实盘=盘盈)追加在汇总与差异两个 sheet;库位范围任务(scope_type=location)Sheet「盘点明细」(作业记录口径,列:库位编码/商品编码/商品名称/盘点数量;未知商品编码=扫描条码、空库位标记商品编码为空数量 0)。ExcelJS 流式生成后上传 COS;进度通过 Redis 缓存(key stock_check_export_task_*,TTL 1800 秒)追踪,立即返回任务ID;进度查询复用 stock.check.exportTaskProgress。",
"description": "提交盘点明细导出任务(异步,单任务,按盘点范围分流):商品范围任务(scope_type=goods)固定两个 Sheet——Sheet1「汇总和差异」(每商品一行,列:盘点单号/商品编码/商品名称/规格/账面数量(明细行 system_quantity 快照合计,提交审核时刷新)/盘点数量/差异数量/差异类型(盘盈|盘亏,无差异或未盘留空);未知商品(条码维度,无账面、差异=实盘=盘盈)追加在末尾)+ Sheet2「库位明细」(每商品每库位一行,列:库位编码/数量(实盘,未盘 0)/条码(wms_stock_barcodes 第一条)/商品名称);库位范围任务(scope_type=location)Sheet「盘点明细」(作业记录口径,列:库位编码/商品编码/商品名称/盘点数量;未知商品编码=扫描条码、空库位标记商品为空数量 0)。ExcelJS 流式生成后上传 COS;进度通过 Redis 缓存(key stock_check_export_task_*,TTL 1800 秒)追踪,立即返回任务ID;进度查询复用 stock.check.exportTaskProgress。",
"isNeedAuth": false,
"isNeedLogin": true,
"headers": [
@ -40471,13 +40471,6 @@
"required": true,
"source": "body",
"description": "盘点任务ID"
},
{
"name": "mode",
"type": "string",
"required": false,
"source": "body",
"description": "商品盘点导出模式:location=区分库位明细(缺省)/summary=明细汇总+商品差异两 sheet;库位盘点忽略该参数"
}
],
"response": [
@ -40533,7 +40526,7 @@
"name": "total_count",
"type": "number",
"required": true,
"description": "导出总行数(库位范围=作业记录数;商品范围 location 模式=库位明细行数、summary 模式=商品数+未知商品数,导出前即确定)"
"description": "导出总行数(库位范围=作业记录数;商品范围=汇总行数(商品数+未知商品数)+库位明细行数,导出前即确定)"
},
{
"name": "message",
@ -40581,8 +40574,7 @@
"notes": [
"权限口径:随 ALL_METHOD_NAMES 免 API 权限(isNeedAuth=false)、免仓库有效期鉴权、必须登录(isNeedLogin=true);加入免日志清单 _noNeedLog。",
"异步执行:查询任务/明细/作业记录 → 商品范围复用 result 的商品合并聚合(账面=明细行 system_quantity 快照、实盘=作业记录聚合)→ ExcelJS 流式写入 → COS 上传;进度缓存 Redis key stock_check_export_task_{taskId},TTL 1800 秒;进度查询复用 stock.check.exportTaskProgress(task_id 通用)。",
"商品盘点 mode=location:Sheet「库位明细」每商品每库位一行(快照库位按明细 id 升序、仅作业记录存在的库位追加末尾),未盘商品行仍输出全部快照库位(盘点数量 0、差异留空),差异数量仅商品有作业记录时计算(盘点-账面)。",
"商品盘点 mode=summary:Sheet1「明细汇总」每商品一行(账面=各库位快照合计、盘点=实盘合计、差异=盘点-账面,未盘商品盘点/差异留空);Sheet2「商品差异」仅 is_diff=1 的商品(差异类型:正=盘盈、负=盘亏);未知商品(stock_id 为空按条码归组)无账面,追加在汇总与差异两个 sheet(差异=实盘=盘盈)。",
"商品盘点固定两个 Sheet:Sheet1「汇总和差异」每商品一行(账面=各库位快照合计、盘点=实盘合计、差异=盘点-账面,未盘商品盘点/差异留空;差异类型:正=盘盈、负=盘亏、无差异/未盘留空);未知商品(stock_id 为空按条码归组)无账面,追加在汇总末尾(差异=实盘=盘盈)。Sheet2「库位明细」每商品每库位一行(快照库位按明细 id 升序、仅作业记录存在的库位追加末尾),列仅库位编码/数量(实盘,未盘 0)/条码(wms_stock_barcodes 按 stock_id 第一条)/商品名称。",
"库位范围任务:Sheet「盘点明细」作业记录口径(库位编码/商品编码/商品名称/盘点数量),已知商品编码名称从 wms_stock_warehouse 补齐、未知商品编码=扫描条码(barcode)、名称=上报名称快照;空库位标记记录商品编码为空、盘点数量 0;mode 入参忽略。",
"代码依据:app/service/systems/stock_check.js#exportCheckDetail/#_generateAndUploadCheckDetailExcel/#_uploadExportExcelFile(原 _generateAndUploadStockCheckExcel 上传段抽为公共方法复用)+ app/service/systemMethods/stock/check.js#exportDetail。",
"响应统一 data 层封装(2026-10-02 起):成功 { flag:\"success\", code:1, message:\"ok\", data:<业务数据整体>, timestamp };失败 { flag:\"failure\", code:<业务错误码>, message:<错误文案>, data:null, timestamp }。本方法业务数据整体位于 data 内,data 内字段口径与改造前一致(原顶层业务字段全部移入 data.children)。代码依据:app/service/systemMethods/stock/check.js#_call(23 个方法共用统一包装)+ app/lib/api_response.js#toSuccess(识别标准载荷后顶层补充 timestamp)。",