Browse Source

修复首页库存计算,排除下架规格

pull/19/head
徐飞洋 1 month ago
parent
commit
7d03d464a9
  1. 82
      pages/index/index.js
  2. 3
      server-example/server-mysql.js

82
pages/index/index.js

@ -1014,23 +1014,44 @@ Page({
}; };
}); });
// 计算库存总数 - 支持逗号分隔的数字字符串 // 计算库存总数 - 支持逗号分隔的数字字符串,同时考虑规格状态
const calculateTotalStock = (value) => { const calculateTotalStock = (value, specStatusString) => {
if (!value) return 0; if (!value) return 0;
if (typeof value === 'string') { if (typeof value === 'string') {
// 支持逗号分隔的数字字符串,如 "23,34,24" // 支持逗号分隔的数字字符串,如 "23,34,24"
return value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0).reduce((sum, num) => sum + num, 0); const quantityArray = value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0);
// 检查是否有规格状态字符串
if (specStatusString && typeof specStatusString === 'string') {
const specStatusArray = specStatusString.split(/[,,、]/).map(item => item.trim());
// 只计算未下架的规格库存(specStatus !== '1')
// 确保规格状态数组与数量数组长度匹配
return quantityArray.reduce((sum, num, index) => {
// 如果规格状态数组长度不足,默认为未下架状态
const specStatus = index < specStatusArray.length ? specStatusArray[index] : '0';
if (specStatus !== '1') {
return sum + num;
}
return sum;
}, 0);
}
// 如果没有规格状态,计算所有库存
return quantityArray.reduce((sum, num) => sum + num, 0);
} }
return parseInt(value) || 0; return parseInt(value) || 0;
}; };
// 优化库存计算 - 尝试多个字段 // 优化库存计算 - 尝试多个字段,同时考虑规格状态
const minOrder = calculateTotalStock(product.minOrder); // 尝试不同的字段名来获取规格状态
const quantity = calculateTotalStock(product.quantity); const specStatusString = product.specStatusString || product.specStatus || product.statusString || product.spec_status || '';
const stock = calculateTotalStock(product.stock); const minOrder = calculateTotalStock(product.minOrder, specStatusString);
const inventory = calculateTotalStock(product.inventory); const quantity = calculateTotalStock(product.quantity, specStatusString);
const availableStock = calculateTotalStock(product.availableStock); const stock = calculateTotalStock(product.stock, specStatusString);
const totalAvailable = calculateTotalStock(product.totalAvailable); const inventory = calculateTotalStock(product.inventory, specStatusString);
const availableStock = calculateTotalStock(product.availableStock, specStatusString);
const totalAvailable = calculateTotalStock(product.totalAvailable, specStatusString);
// 优先使用最具体的库存字段 // 优先使用最具体的库存字段
const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable;
@ -1199,23 +1220,44 @@ Page({
}; };
}); });
// 计算库存总数 - 支持逗号分隔的数字字符串 // 计算库存总数 - 支持逗号分隔的数字字符串,同时考虑规格状态
const calculateTotalStock = (value) => { const calculateTotalStock = (value, specStatusString) => {
if (!value) return 0; if (!value) return 0;
if (typeof value === 'string') { if (typeof value === 'string') {
// 支持逗号分隔的数字字符串,如 "23,34,24" // 支持逗号分隔的数字字符串,如 "23,34,24"
return value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0).reduce((sum, num) => sum + num, 0); const quantityArray = value.split(/[,,、]/).map(item => parseInt(item.trim()) || 0);
// 检查是否有规格状态字符串
if (specStatusString && typeof specStatusString === 'string') {
const specStatusArray = specStatusString.split(/[,,、]/).map(item => item.trim());
// 只计算未下架的规格库存(specStatus !== '1')
// 确保规格状态数组与数量数组长度匹配
return quantityArray.reduce((sum, num, index) => {
// 如果规格状态数组长度不足,默认为未下架状态
const specStatus = index < specStatusArray.length ? specStatusArray[index] : '0';
if (specStatus !== '1') {
return sum + num;
}
return sum;
}, 0);
}
// 如果没有规格状态,计算所有库存
return quantityArray.reduce((sum, num) => sum + num, 0);
} }
return parseInt(value) || 0; return parseInt(value) || 0;
}; };
// 优化库存计算 - 尝试多个字段 // 优化库存计算 - 尝试多个字段,同时考虑规格状态
const minOrder = calculateTotalStock(product.minOrder); // 尝试不同的字段名来获取规格状态
const quantity = calculateTotalStock(product.quantity); const specStatusString = product.specStatusString || product.specStatus || product.statusString || product.spec_status || '';
const stock = calculateTotalStock(product.stock); const minOrder = calculateTotalStock(product.minOrder, specStatusString);
const inventory = calculateTotalStock(product.inventory); const quantity = calculateTotalStock(product.quantity, specStatusString);
const availableStock = calculateTotalStock(product.availableStock); const stock = calculateTotalStock(product.stock, specStatusString);
const totalAvailable = calculateTotalStock(product.totalAvailable); const inventory = calculateTotalStock(product.inventory, specStatusString);
const availableStock = calculateTotalStock(product.availableStock, specStatusString);
const totalAvailable = calculateTotalStock(product.totalAvailable, specStatusString);
// 优先使用最具体的库存字段 // 优先使用最具体的库存字段
const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable;

3
server-example/server-mysql.js

@ -2261,7 +2261,8 @@ app.post('/api/product/list', async (req, res) => {
'category', 'category',
'producting', 'producting',
'description', 'description',
'frequency' 'frequency',
'spec_status'
], ],
order: [['created_at', 'DESC']], order: [['created_at', 'DESC']],
limit: pageSize, limit: pageSize,

Loading…
Cancel
Save