258 lines
7.3 KiB
JavaScript
258 lines
7.3 KiB
JavaScript
'use strict';
|
||
|
||
const { Service } = require('egg');
|
||
|
||
// 仓库设置字段元信息(对应 wms_store.subject_config)
|
||
const WAREHOUSE_SETTING_FIELDS = Object.freeze([
|
||
Object.freeze({
|
||
key: 'max_notice_webhook_count',
|
||
name: '最大通知 Webhook 次数',
|
||
type: 'integer',
|
||
default: 5,
|
||
required: false,
|
||
description: '单次事件最多触发的 Webhook 通知次数(0-5)',
|
||
min: 0,
|
||
max: 5,
|
||
}),
|
||
Object.freeze({
|
||
key: 'max_wave_quantity',
|
||
name: '仓库波次最大数量限制',
|
||
type: 'integer',
|
||
default: 500,
|
||
required: false,
|
||
description: '控制单个波次允许的最大订单数量,默认 500',
|
||
min: 1,
|
||
}),
|
||
Object.freeze({
|
||
key: 'min_group_order_fetch_quantity',
|
||
name: '最小团购订单抓取数量',
|
||
type: 'integer',
|
||
default: 13,
|
||
required: false,
|
||
description: '控制团购波次最小抓取订单数量,默认 13',
|
||
min: 1,
|
||
}),
|
||
Object.freeze({
|
||
key: 'default_express_code',
|
||
name: '默认快递编码',
|
||
type: 'string',
|
||
default: 'STO',
|
||
required: false,
|
||
description: '仓库默认使用的快递编码',
|
||
maxLength: 50,
|
||
}),
|
||
Object.freeze({
|
||
key: 'default_platform_code',
|
||
name: '默认平台编码',
|
||
type: 'string',
|
||
default: 'TB',
|
||
required: false,
|
||
description: '仓库默认使用的平台编码',
|
||
maxLength: 50,
|
||
}),
|
||
Object.freeze({
|
||
key: 'group_wave_prefix',
|
||
name: '团购波次前缀',
|
||
type: 'string',
|
||
default: 'B',
|
||
required: false,
|
||
description: '团购波次编号前缀,最多 5 个字符,默认 B',
|
||
maxLength: 5,
|
||
}),
|
||
Object.freeze({
|
||
key: 'delete_order_wave_prefix',
|
||
name: '删单波次前缀',
|
||
type: 'string',
|
||
default: 'A',
|
||
required: false,
|
||
description: '删单波次编号前缀,最多 5 个字符,默认 A',
|
||
maxLength: 5,
|
||
}),
|
||
Object.freeze({
|
||
key: 'webhook_enabled',
|
||
name: '是否启用 Webhook',
|
||
type: 'flag',
|
||
default: false,
|
||
required: false,
|
||
description: '控制仓库是否启用 Webhook 事件推送',
|
||
}),
|
||
Object.freeze({
|
||
key: 'callback_strategy',
|
||
name: '回调策略',
|
||
type: 'enum',
|
||
default: 'async',
|
||
required: false,
|
||
description: '仓库 Webhook 事件的回调执行策略',
|
||
options: [
|
||
Object.freeze({ value: 'sync', label: '同步回调' }),
|
||
Object.freeze({ value: 'async', label: '异步回调' }),
|
||
],
|
||
}),
|
||
]);
|
||
|
||
class SettingWarehouseService extends Service {
|
||
createError(message, code = 500) {
|
||
const error = new Error(message);
|
||
error.code = code;
|
||
return error;
|
||
}
|
||
|
||
/**
|
||
* 从 ctx.user.store_id 获取当前仓库 ID。
|
||
*
|
||
* @return {number} 仓库 ID。
|
||
* @throws {Error} 未识别到仓库时抛出,状态码 400。
|
||
*/
|
||
getCurrentWarehouseId() {
|
||
const warehouseId = Number(this.ctx.user && this.ctx.user.store_id);
|
||
if (!Number.isFinite(warehouseId) || warehouseId <= 0) {
|
||
throw this.createError('未识别到当前仓库,无法操作仓库设置', 400);
|
||
}
|
||
return warehouseId;
|
||
}
|
||
|
||
/**
|
||
* 删除指定缓存键。
|
||
*
|
||
* @param {string} cacheKey 缓存键。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async deleteCacheByKey(cacheKey) {
|
||
if (!cacheKey) {
|
||
return;
|
||
}
|
||
|
||
if (this.app && typeof this.app.delCacheAsync === 'function') {
|
||
await this.app.delCacheAsync(cacheKey);
|
||
return;
|
||
}
|
||
|
||
if (this.app && typeof this.app.setCacheAsync === 'function') {
|
||
await this.app.setCacheAsync(cacheKey, null, 0);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 清理指定仓库设置相关缓存。
|
||
*
|
||
* @param {number|string} warehouseId 仓库 ID。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async clearWarehouseSettingCache(warehouseId) {
|
||
const normalizedWarehouseId = Number(warehouseId || 0);
|
||
if (!Number.isFinite(normalizedWarehouseId) || normalizedWarehouseId <= 0) {
|
||
return;
|
||
}
|
||
|
||
const keys = [
|
||
`delivery_order:store:${normalizedWarehouseId}:subject_config`,
|
||
`warehouse:${normalizedWarehouseId}store:${normalizedWarehouseId}:info`,
|
||
];
|
||
|
||
await Promise.all(keys.map(key => this.deleteCacheByKey(key).catch(() => null)));
|
||
}
|
||
|
||
/**
|
||
* 获取仓库设置字段元信息列表。
|
||
*
|
||
* @return {Array} 字段元信息数组(浅拷贝,含 options 拷贝)。
|
||
*/
|
||
getFieldMeta() {
|
||
return WAREHOUSE_SETTING_FIELDS.map(field => ({
|
||
key: field.key,
|
||
name: field.name,
|
||
type: field.type,
|
||
default: field.default,
|
||
required: field.required,
|
||
description: field.description,
|
||
...(Array.isArray(field.options) ? { options: field.options.map(o => ({ ...o })) } : {}),
|
||
...(field.min !== undefined ? { min: field.min } : {}),
|
||
...(field.max !== undefined ? { max: field.max } : {}),
|
||
...(field.maxLength !== undefined ? { maxLength: field.maxLength } : {}),
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* 读取当前仓库的设置(wms_store.subject_config)。
|
||
*
|
||
* @return {Promise<Object>} 包含 warehouse_id 与 subject_config 的对象。
|
||
* @throws {Error} 仓库不存在时抛出,状态码 404。
|
||
*/
|
||
async getSettings() {
|
||
const warehouseId = this.getCurrentWarehouseId();
|
||
|
||
const store = await this.ctx.model.Store.findByPk(warehouseId, {
|
||
attributes: [ 'id', 'subject_config' ],
|
||
});
|
||
|
||
if (!store) {
|
||
throw this.createError('仓库不存在', 404);
|
||
}
|
||
|
||
return {
|
||
warehouse_id: warehouseId,
|
||
subject_config: store.subject_config,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 修改当前仓库设置并清理当前仓库缓存。
|
||
*
|
||
* @param {Object} [data={}] 请求参数。
|
||
* @param {Object} [data.subject_config] 仓库设置对象,支持部分字段更新。
|
||
* @return {Promise<Object>} 更新后的仓库设置。
|
||
*/
|
||
async saveSettings(data = {}) {
|
||
const warehouseId = this.getCurrentWarehouseId();
|
||
const storeModel = this.ctx.model && this.ctx.model.Store;
|
||
|
||
if (!storeModel || typeof storeModel.findByPk !== 'function') {
|
||
throw this.createError('Store model 不可用', 500);
|
||
}
|
||
|
||
const store = await storeModel.findByPk(warehouseId, {
|
||
attributes: [ 'id', 'subject_config' ],
|
||
});
|
||
|
||
if (!store) {
|
||
throw this.createError('仓库不存在', 404);
|
||
}
|
||
|
||
const hasSubjectConfig = Object.prototype.hasOwnProperty.call(data || {}, 'subject_config');
|
||
const nextPatch = hasSubjectConfig ? data.subject_config : null;
|
||
if (!nextPatch || typeof nextPatch !== 'object' || Array.isArray(nextPatch)) {
|
||
throw this.createError('subject_config 参数不能为空且必须是对象', 400);
|
||
}
|
||
|
||
const mergedConfig = {
|
||
...(store.subject_config && typeof store.subject_config === 'object' ? store.subject_config : {}),
|
||
...nextPatch,
|
||
};
|
||
|
||
let normalizedConfig;
|
||
try {
|
||
normalizedConfig = typeof storeModel.normalizeSubjectConfig === 'function'
|
||
? storeModel.normalizeSubjectConfig(mergedConfig)
|
||
: mergedConfig;
|
||
} catch (error) {
|
||
throw this.createError(error.message || 'subject_config 参数不合法', Number(error.code) || 400);
|
||
}
|
||
|
||
await store.update({
|
||
subject_config: normalizedConfig,
|
||
});
|
||
|
||
const cacheWarehouseId = warehouseId;
|
||
|
||
await this.clearWarehouseSettingCache(cacheWarehouseId);
|
||
|
||
return {
|
||
warehouse_id: warehouseId,
|
||
subject_config: store.subject_config,
|
||
cache_cleared_warehouse_id: cacheWarehouseId,
|
||
};
|
||
}
|
||
}
|
||
|
||
module.exports = SettingWarehouseService;
|