243 lines
8.9 KiB
JavaScript
243 lines
8.9 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
|
|
const { Op } = require('sequelize');
|
|
const WaveService = require('../../../../../app/service/systemMethods/order/wave');
|
|
|
|
/**
|
|
* 创建自动加团相关测试上下文。
|
|
*
|
|
* @param {Object} options 初始化选项。
|
|
* @param {Array<Object>} options.orders 订单初始数据。
|
|
* @param {Array<Object>} options.waves 波次初始数据。
|
|
* @param {number} [options.storeId=100000] 仓库ID。
|
|
* @param {number} [options.maxWaveQuantity=500] 最大波次订单数。
|
|
* @return {{ service: WaveService, txState: Object, waves: Array<Object> }} 测试上下文。
|
|
*/
|
|
function createHarness({ orders = [], waves = [], storeId = 100000, maxWaveQuantity = 500 } = {}) {
|
|
const orderStore = orders.map(row => ({ ...row }));
|
|
const waveStore = waves.map(row => ({ ...row }));
|
|
const txState = {
|
|
committed: false,
|
|
rolledBack: false,
|
|
sqlCalls: [],
|
|
};
|
|
|
|
const transaction = {
|
|
async commit() {
|
|
txState.committed = true;
|
|
},
|
|
async rollback() {
|
|
txState.rolledBack = true;
|
|
},
|
|
};
|
|
|
|
const ctx = {
|
|
app: {
|
|
config: {},
|
|
},
|
|
user: {
|
|
store_id: storeId,
|
|
},
|
|
model: {
|
|
QueryTypes: {
|
|
SELECT: 'SELECT',
|
|
},
|
|
async transaction() {
|
|
return transaction;
|
|
},
|
|
async query(sql, options = {}) {
|
|
txState.sqlCalls.push({ sql, options });
|
|
|
|
if (sql.includes('SELECT wave_id, wave_status, total_orders')) {
|
|
const targetIds = Array.isArray(options.replacements && options.replacements.ids)
|
|
? options.replacements.ids
|
|
: [];
|
|
return waveStore
|
|
.filter(item => Number(item.warehouse_id) === Number(options.replacements.warehouseId))
|
|
.filter(item => targetIds.includes(item.wave_id))
|
|
.map(item => ({
|
|
wave_id: item.wave_id,
|
|
wave_status: item.wave_status,
|
|
total_orders: item.total_orders,
|
|
}));
|
|
}
|
|
|
|
if (sql.includes('SELECT wave_id, wave_status') && !sql.includes('total_orders')) {
|
|
const targetIds = Array.isArray(options.replacements && options.replacements.ids)
|
|
? options.replacements.ids
|
|
: [];
|
|
return waveStore
|
|
.filter(item => Number(item.warehouse_id) === Number(options.replacements.warehouseId))
|
|
.filter(item => targetIds.includes(item.wave_id))
|
|
.map(item => ({
|
|
wave_id: item.wave_id,
|
|
wave_status: item.wave_status,
|
|
}));
|
|
}
|
|
|
|
if (sql.includes('SET wave_status = 0')) {
|
|
const targetWaveIds = Array.isArray(options.replacements && options.replacements.waveIds)
|
|
? options.replacements.waveIds
|
|
: [];
|
|
let affectedRows = 0;
|
|
for (const wave of waveStore) {
|
|
if (targetWaveIds.includes(wave.wave_id) && Number(wave.warehouse_id) === Number(options.replacements.warehouseId)) {
|
|
wave.wave_status = 0;
|
|
affectedRows += 1;
|
|
}
|
|
}
|
|
return [[], { affectedRows }];
|
|
}
|
|
|
|
if (sql.includes('SET wave_status = 1')) {
|
|
const targetWaveIds = Array.isArray(options.replacements && options.replacements.waveIds)
|
|
? options.replacements.waveIds
|
|
: [];
|
|
let affectedRows = 0;
|
|
for (const wave of waveStore) {
|
|
if (
|
|
targetWaveIds.includes(wave.wave_id)
|
|
&& Number(wave.warehouse_id) === Number(options.replacements.warehouseId)
|
|
&& Number(wave.wave_status) === 0
|
|
) {
|
|
wave.wave_status = 1;
|
|
affectedRows += 1;
|
|
}
|
|
}
|
|
return [[], { affectedRows }];
|
|
}
|
|
|
|
if (sql.includes('DELETE FROM wms_order_wave_index')) {
|
|
const targetWaveIds = Array.isArray(options.replacements && options.replacements.waveIds)
|
|
? options.replacements.waveIds
|
|
: [];
|
|
return [[], { affectedRows: targetWaveIds.length }];
|
|
}
|
|
|
|
if (sql.includes('INSERT INTO wms_order_wave_index')) {
|
|
const replacements = options.replacements || {};
|
|
let affectedRows = 0;
|
|
for (const key of Object.keys(replacements)) {
|
|
if (key.startsWith('orderId_')) {
|
|
affectedRows += 1;
|
|
}
|
|
}
|
|
return [[], { affectedRows }];
|
|
}
|
|
|
|
return [[], { affectedRows: 0 }];
|
|
},
|
|
},
|
|
};
|
|
|
|
const service = new WaveService(ctx);
|
|
service.getDefaultWaveQuantity = async () => maxWaveQuantity;
|
|
service.getOrderModel = async () => ({
|
|
async findAll(options = {}) {
|
|
const where = options.where || {};
|
|
const waveIn = (where.wave_id && where.wave_id[Op.in]) || [];
|
|
const waveGt = Number((where.wave_id && where.wave_id[Op.gt]) || 0);
|
|
|
|
const rows = orderStore
|
|
.filter(item => waveIn.includes(item.wave_id))
|
|
.filter(item => Number(item.wave_id) > waveGt)
|
|
.sort((a, b) => {
|
|
if (Number(a.wave_id) !== Number(b.wave_id)) {
|
|
return Number(a.wave_id) - Number(b.wave_id);
|
|
}
|
|
return Number(a.id) - Number(b.id);
|
|
});
|
|
|
|
return rows.map(item => ({
|
|
id: item.id,
|
|
wave_id: item.wave_id,
|
|
sys_platform_code: item.sys_platform_code,
|
|
}));
|
|
},
|
|
});
|
|
|
|
return {
|
|
service,
|
|
txState,
|
|
waves: waveStore,
|
|
};
|
|
}
|
|
|
|
describe('test/app/service/systemMethods/order/wave.batchAutoGrouping.test.js', () => {
|
|
it('batchAutoGrouping should rebuild wave indexes for updatable waves', async () => {
|
|
const { service, txState, waves } = createHarness({
|
|
maxWaveQuantity: 3,
|
|
orders: [
|
|
{ id: 1001, wave_id: 501, sys_platform_code: 'pdd' },
|
|
{ id: 1002, wave_id: 501, sys_platform_code: 'cainiao' },
|
|
{ id: 1003, wave_id: 502, sys_platform_code: 'pdd' },
|
|
],
|
|
waves: [
|
|
{ warehouse_id: 100000, wave_id: 501, wave_status: 1, total_orders: 2 },
|
|
{ warehouse_id: 100000, wave_id: 502, wave_status: 2, total_orders: 1 },
|
|
{ warehouse_id: 100000, wave_id: 503, wave_status: 1, total_orders: 3 },
|
|
],
|
|
});
|
|
|
|
const result = await service.batchAutoGrouping({ ids: [ 501, 502, 503 ] });
|
|
|
|
assert.equal(txState.committed, true);
|
|
assert.equal(txState.rolledBack, false);
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.affected_wave_ids, [ 501, 502, 503 ]);
|
|
assert.equal(result.updated_wave_count, 1);
|
|
|
|
const targetWave = waves.find(item => item.wave_id === 501);
|
|
assert.equal(targetWave.wave_status, 0);
|
|
|
|
const deleteIndexCall = txState.sqlCalls.find(call => call.sql.includes('DELETE FROM wms_order_wave_index'));
|
|
assert.ok(deleteIndexCall, '应按可更新波次整批删除索引');
|
|
assert.deepEqual(deleteIndexCall.options.replacements.waveIds, [ 501 ]);
|
|
|
|
const insertIndexCall = txState.sqlCalls.find(call => call.sql.includes('INSERT INTO wms_order_wave_index'));
|
|
assert.ok(insertIndexCall, '应按可更新波次整批重建索引');
|
|
assert.equal(insertIndexCall.options.replacements.waveId_0, 501);
|
|
assert.equal(insertIndexCall.options.replacements.orderId_0, 1001);
|
|
assert.equal(insertIndexCall.options.replacements.orderId_1, 1002);
|
|
assert.equal(insertIndexCall.options.replacements.platformsIndex_0, 2);
|
|
assert.equal(insertIndexCall.options.replacements.platformsIndex_1, 1);
|
|
});
|
|
|
|
it('batchFinishAutoGrouping should rebuild wave indexes for updated waves', async () => {
|
|
const { service, txState, waves } = createHarness({
|
|
orders: [
|
|
{ id: 2001, wave_id: 601 },
|
|
{ id: 2002, wave_id: 601 },
|
|
{ id: 2003, wave_id: 602 },
|
|
],
|
|
waves: [
|
|
{ warehouse_id: 100000, wave_id: 601, wave_status: 0, total_orders: 2 },
|
|
{ warehouse_id: 100000, wave_id: 602, wave_status: 1, total_orders: 1 },
|
|
],
|
|
});
|
|
|
|
const result = await service.batchFinishAutoGrouping({ ids: [ 601, 602 ] });
|
|
|
|
assert.equal(txState.committed, true);
|
|
assert.equal(txState.rolledBack, false);
|
|
assert.equal(result.flag, 'success');
|
|
assert.deepEqual(result.updated_wave_ids, [ 601 ]);
|
|
assert.equal(result.updated_wave_count, 1);
|
|
|
|
const updatedWave = waves.find(item => item.wave_id === 601);
|
|
assert.equal(updatedWave.wave_status, 1);
|
|
|
|
const deleteIndexCall = txState.sqlCalls.find(call => call.sql.includes('DELETE FROM wms_order_wave_index'));
|
|
assert.ok(deleteIndexCall, '应按完成加团波次整批删除索引');
|
|
assert.deepEqual(deleteIndexCall.options.replacements.waveIds, [ 601 ]);
|
|
|
|
const insertIndexCall = txState.sqlCalls.find(call => call.sql.includes('INSERT INTO wms_order_wave_index'));
|
|
assert.ok(insertIndexCall, '应按完成加团波次整批重建索引');
|
|
assert.equal(insertIndexCall.options.replacements.waveId_0, 601);
|
|
assert.equal(insertIndexCall.options.replacements.orderId_0, 2001);
|
|
assert.equal(insertIndexCall.options.replacements.orderId_1, 2002);
|
|
});
|
|
});
|