126 lines
3.6 KiB
JavaScript
126 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
const Service = require('egg').Service;
|
|
|
|
class StockTransactionService extends Service {
|
|
|
|
async list(pageParams = {}, sortParams = {}, groupByBatch = false) {
|
|
const { ctx, app } = this;
|
|
const { Op, fn, col, literal } = app.Sequelize;
|
|
|
|
const warehouseId = ctx.user.store_id;
|
|
const { page = 1, pageSize = 20 } = pageParams;
|
|
const { sortField = 'created_at', sortOrder = 'DESC' } = sortParams;
|
|
|
|
// 基础条件:当前仓库
|
|
const baseWhere = {
|
|
[Op.and]: [
|
|
{ warehouse_id: warehouseId },
|
|
],
|
|
};
|
|
|
|
// 动态 where 条件
|
|
const parsed = ctx.whereParsed;
|
|
if (parsed && parsed.dynamicConditions && parsed.dynamicConditions.length > 0) {
|
|
baseWhere[Op.and].push(...parsed.dynamicConditions);
|
|
}
|
|
|
|
const offset = (page - 1) * pageSize;
|
|
const limit = pageSize;
|
|
|
|
if (groupByBatch) {
|
|
// 按批次明细:保持原有逻辑
|
|
const { count: total, rows } = await ctx.model.StockTransaction.findAndCountAll({
|
|
where: baseWhere,
|
|
order: [[ sortField, sortOrder ]],
|
|
offset,
|
|
limit,
|
|
include: [
|
|
{
|
|
model: ctx.model.StockWarehouse,
|
|
as: 'stockItem',
|
|
attributes: [ 'id', 'item_code', 'item_name', 'sku_property', 'stock_unit' ],
|
|
required: false,
|
|
},
|
|
],
|
|
});
|
|
|
|
const list = rows.map(item => {
|
|
const row = item.get({ plain: true });
|
|
row.created_at = row.created_at ? app.formatDate(row.created_at) : '';
|
|
return row;
|
|
});
|
|
|
|
return {
|
|
list,
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
total,
|
|
totalPages: Math.ceil(total / pageSize),
|
|
},
|
|
};
|
|
}
|
|
|
|
// 不按批次:按 stock_id + space_id + transaction_type + inventory_type 合并
|
|
const groupFields = [ 'stock_id', 'space_id', 'transaction_type', 'inventory_type', 'warehouse_id', 'store_id', 'order_type' ];
|
|
|
|
// 先查总数(分组后的组数)
|
|
const countResult = await ctx.model.StockTransaction.count({
|
|
where: baseWhere,
|
|
group: groupFields,
|
|
});
|
|
const total = Array.isArray(countResult) ? countResult.length : countResult;
|
|
|
|
// 查分组数据
|
|
const rows = await ctx.model.StockTransaction.findAll({
|
|
where: baseWhere,
|
|
attributes: [
|
|
'stock_id',
|
|
'space_id',
|
|
'transaction_type',
|
|
'inventory_type',
|
|
'warehouse_id',
|
|
'store_id',
|
|
'order_type',
|
|
[ fn('SUM', col('stock_transaction.quantity')), 'quantity' ],
|
|
[ fn('MIN', col('stock_transaction.created_at')), 'first_created_at' ],
|
|
[ fn('MAX', col('stock_transaction.created_at')), 'last_created_at' ],
|
|
[ fn('COUNT', col('stock_transaction.id')), 'record_count' ],
|
|
],
|
|
group: groupFields,
|
|
order: [[ literal('last_created_at'), sortOrder ]],
|
|
offset,
|
|
limit,
|
|
include: [
|
|
{
|
|
model: ctx.model.StockWarehouse,
|
|
as: 'stockItem',
|
|
attributes: [ 'id', 'item_code', 'item_name', 'sku_property', 'stock_unit' ],
|
|
required: false,
|
|
},
|
|
],
|
|
raw: true,
|
|
nest: true,
|
|
});
|
|
|
|
const list = rows.map(row => {
|
|
row.first_created_at = row.first_created_at ? app.formatDate(row.first_created_at) : '';
|
|
row.last_created_at = row.last_created_at ? app.formatDate(row.last_created_at) : '';
|
|
return row;
|
|
});
|
|
|
|
return {
|
|
list,
|
|
pagination: {
|
|
page,
|
|
pageSize,
|
|
total,
|
|
totalPages: Math.ceil(total / pageSize),
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = StockTransactionService;
|