fix(storage): 修复库存/商品列表按数量等字段排序不生效
- stock.warehouse.list: 透传 sort 参数给 getWarehouseStock(此前漏传,后端始终按 id DESC 排序,quantity/available_quantity 等排序字段均无效) - systems/storage/inventory getInventoryList/exportInventoryList: - 汇总模式(按 stock_id 合并)order 原写死 last_inbound_time,现支持按聚合别名排序:quantity/locked_quantity/available_quantity/batch_count/first_inbound_time/last_inbound_time/production_date/expiry_date,其余回退 last_inbound_time - 明细模式新增真实列白名单,非法字段回退默认列,避免 SQL 报错/注入 - sortOrder 统一归一化为 ASC/DESC,分页兼容 currentPage
This commit is contained in:
parent
0dc7e1cd09
commit
6d33210624
@ -51,11 +51,19 @@ class WarehouseService extends MainService {
|
|||||||
* @param {Object} data 请求体
|
* @param {Object} data 请求体
|
||||||
* @param {Object} [data.pagination] 分页参数
|
* @param {Object} [data.pagination] 分页参数
|
||||||
* @param {Object} [data.where] 筛选条件
|
* @param {Object} [data.where] 筛选条件
|
||||||
|
* @param {Object} [data.sort] 排序参数
|
||||||
|
* @param {string} [data.sort.sortField] 排序字段(兼容:sort_field)
|
||||||
|
* @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order)
|
||||||
* @return {Promise<Object>} 统一成功包装响应
|
* @return {Promise<Object>} 统一成功包装响应
|
||||||
*/
|
*/
|
||||||
async list(data = {}) {
|
async list(data = {}) {
|
||||||
const { service } = this;
|
const { service } = this;
|
||||||
const { pagination } = data;
|
const { pagination } = data;
|
||||||
|
const sort = data.sort || {};
|
||||||
|
const sortParams = {
|
||||||
|
sortField: sort.sortField || sort.sort_field || 'id',
|
||||||
|
sortOrder: sort.sortOrder || sort.sort_order || 'DESC',
|
||||||
|
};
|
||||||
if (this.isPlainObject(data)) {
|
if (this.isPlainObject(data)) {
|
||||||
const rawWhere = this.isPlainObject(data.where) ? data.where : {};
|
const rawWhere = this.isPlainObject(data.where) ? data.where : {};
|
||||||
data.where = this.normalizeWarehouseRecycleWhere(rawWhere);
|
data.where = this.normalizeWarehouseRecycleWhere(rawWhere);
|
||||||
@ -75,7 +83,7 @@ class WarehouseService extends MainService {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const result = await service.systems.stockStore.getWarehouseStock(where, pagination);
|
const result = await service.systems.stockStore.getWarehouseStock(where, pagination, sortParams);
|
||||||
return this.success('ok', result);
|
return this.success('ok', result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,33 @@
|
|||||||
const Service = require('egg').Service;
|
const Service = require('egg').Service;
|
||||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 库存明细(逐条)模式允许的排序字段白名单(wms_storage 真实列),
|
||||||
|
* 防止非法字段进入 ORDER BY 导致 SQL 报错或注入。
|
||||||
|
*/
|
||||||
|
const STORAGE_BATCH_SORT_FIELD_WHITELIST = new Set([
|
||||||
|
'id', 'stock_id', 'space_id', 'batch_no', 'inventory_type',
|
||||||
|
'quantity', 'locked_quantity', 'available_quantity',
|
||||||
|
'production_date', 'expiry_date', 'inbound_time',
|
||||||
|
'created_at', 'updated_at',
|
||||||
|
]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 库存汇总(按 stock_id 合并)模式允许的排序字段 → 聚合别名映射。
|
||||||
|
* 汇总查询中 quantity/locked_quantity/available_quantity 等为 SUM/MIN/MAX 别名,
|
||||||
|
* MySQL 支持 ORDER BY 别名,因此直接按别名排序。
|
||||||
|
*/
|
||||||
|
const STORAGE_AGGREGATE_ORDER_FIELD_MAP = {
|
||||||
|
quantity: 'quantity',
|
||||||
|
locked_quantity: 'locked_quantity',
|
||||||
|
available_quantity: 'available_quantity',
|
||||||
|
batch_count: 'batch_count',
|
||||||
|
first_inbound_time: 'first_inbound_time',
|
||||||
|
last_inbound_time: 'last_inbound_time',
|
||||||
|
production_date: 'production_date',
|
||||||
|
expiry_date: 'expiry_date',
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算库存批次的保质期预警状态。
|
* 计算库存批次的保质期预警状态。
|
||||||
*
|
*
|
||||||
@ -1135,7 +1162,7 @@ class InventoryService extends Service {
|
|||||||
* @param {number} [pageParams.page=1] 页码
|
* @param {number} [pageParams.page=1] 页码
|
||||||
* @param {number} [pageParams.pageSize=20] 每页数量
|
* @param {number} [pageParams.pageSize=20] 每页数量
|
||||||
* @param {Object} [sortParams={}] 排序参数
|
* @param {Object} [sortParams={}] 排序参数
|
||||||
* @param {string} [sortParams.sortField='created_at'] 排序字段
|
* @param {string} [sortParams.sortField='created_at'] 排序字段(明细模式支持 wms_storage 真实列;汇总模式仅支持 quantity/locked_quantity/available_quantity/batch_count/first_inbound_time/last_inbound_time/production_date/expiry_date,其余回退 last_inbound_time)
|
||||||
* @param {string} [sortParams.sortOrder='DESC'] 排序方向
|
* @param {string} [sortParams.sortOrder='DESC'] 排序方向
|
||||||
* @param {boolean} [groupByBatch=false] 是否按批次明细返回
|
* @param {boolean} [groupByBatch=false] 是否按批次明细返回
|
||||||
* @return {Promise<{list: Object[], pagination: {page: number, pageSize: number, total: number, totalPages: number}}>} 库存列表结果
|
* @return {Promise<{list: Object[], pagination: {page: number, pageSize: number, total: number, totalPages: number}}>} 库存列表结果
|
||||||
@ -1146,8 +1173,14 @@ class InventoryService extends Service {
|
|||||||
const nowMs = Date.now();
|
const nowMs = Date.now();
|
||||||
|
|
||||||
const warehouseId = ctx.user.store_id;
|
const warehouseId = ctx.user.store_id;
|
||||||
const { page = 1, pageSize = 20 } = pageParams;
|
const page = Math.max(Number(pageParams.page || pageParams.currentPage || 1) || 1, 1);
|
||||||
const { sortField = 'created_at', sortOrder = 'DESC' } = sortParams;
|
const pageSize = Math.max(Number(pageParams.pageSize || 20) || 20, 1);
|
||||||
|
const sortField = sortParams.sortField || sortParams.sort_field || 'created_at';
|
||||||
|
const sortOrder = sortParams.sortOrder || sortParams.sort_order || 'DESC';
|
||||||
|
const safeSortOrder = String(sortOrder).toUpperCase() === 'ASC' ? 'ASC' : 'DESC';
|
||||||
|
// 明细模式按真实列排序;汇总模式按聚合别名排序(默认 last_inbound_time,与历史行为一致)
|
||||||
|
const batchSortField = STORAGE_BATCH_SORT_FIELD_WHITELIST.has(sortField) ? sortField : 'created_at';
|
||||||
|
const aggregateOrderField = STORAGE_AGGREGATE_ORDER_FIELD_MAP[sortField] || 'last_inbound_time';
|
||||||
|
|
||||||
// 基础条件
|
// 基础条件
|
||||||
const baseWhere = {
|
const baseWhere = {
|
||||||
@ -1188,7 +1221,7 @@ class InventoryService extends Service {
|
|||||||
// 按批次明细:逐条返回
|
// 按批次明细:逐条返回
|
||||||
const { count: total, rows } = await ctx.model.Storage.findAndCountAll({
|
const { count: total, rows } = await ctx.model.Storage.findAndCountAll({
|
||||||
where: baseWhere,
|
where: baseWhere,
|
||||||
order: [[ sortField, sortOrder ]],
|
order: [[ batchSortField, safeSortOrder ]],
|
||||||
offset,
|
offset,
|
||||||
limit,
|
limit,
|
||||||
include: [
|
include: [
|
||||||
@ -1291,7 +1324,7 @@ class InventoryService extends Service {
|
|||||||
[ fn('COUNT', col('wms_storage.id')), 'batch_count' ],
|
[ fn('COUNT', col('wms_storage.id')), 'batch_count' ],
|
||||||
],
|
],
|
||||||
group: groupFields,
|
group: groupFields,
|
||||||
order: [[ literal('last_inbound_time'), sortOrder ]],
|
order: [[ literal(aggregateOrderField), safeSortOrder ]],
|
||||||
offset,
|
offset,
|
||||||
limit,
|
limit,
|
||||||
include: [
|
include: [
|
||||||
@ -1660,6 +1693,9 @@ class InventoryService extends Service {
|
|||||||
const { Op, fn, col, literal } = app.Sequelize;
|
const { Op, fn, col, literal } = app.Sequelize;
|
||||||
const warehouseId = ctx.user.store_id;
|
const warehouseId = ctx.user.store_id;
|
||||||
const { sortField = 'id', sortOrder = 'DESC' } = sortParams;
|
const { sortField = 'id', sortOrder = 'DESC' } = sortParams;
|
||||||
|
const safeSortOrder = String(sortOrder).toUpperCase() === 'ASC' ? 'ASC' : 'DESC';
|
||||||
|
const batchSortField = STORAGE_BATCH_SORT_FIELD_WHITELIST.has(sortField) ? sortField : 'id';
|
||||||
|
const aggregateOrderField = STORAGE_AGGREGATE_ORDER_FIELD_MAP[sortField] || 'last_inbound_time';
|
||||||
const pageSize = 500;
|
const pageSize = 500;
|
||||||
let page = 1;
|
let page = 1;
|
||||||
let hasNext = true;
|
let hasNext = true;
|
||||||
@ -1692,7 +1728,7 @@ class InventoryService extends Service {
|
|||||||
'supplier_batch_no', 'store_id', 'warehouse_id',
|
'supplier_batch_no', 'store_id', 'warehouse_id',
|
||||||
'created_at', 'updated_at',
|
'created_at', 'updated_at',
|
||||||
],
|
],
|
||||||
order: [[ sortField, sortOrder ]],
|
order: [[ batchSortField, safeSortOrder ]],
|
||||||
offset: (page - 1) * pageSize,
|
offset: (page - 1) * pageSize,
|
||||||
limit: pageSize,
|
limit: pageSize,
|
||||||
include: [
|
include: [
|
||||||
@ -1783,7 +1819,7 @@ class InventoryService extends Service {
|
|||||||
[ fn('MAX', col('wms_storage.updated_at')), 'last_updated_at' ],
|
[ fn('MAX', col('wms_storage.updated_at')), 'last_updated_at' ],
|
||||||
],
|
],
|
||||||
group: groupFields,
|
group: groupFields,
|
||||||
order: [[ literal('last_inbound_time'), sortOrder ]],
|
order: [[ literal(aggregateOrderField), safeSortOrder ]],
|
||||||
offset: (page - 1) * pageSize,
|
offset: (page - 1) * pageSize,
|
||||||
limit: pageSize,
|
limit: pageSize,
|
||||||
include: [
|
include: [
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user