- 新增 systems/waybill.js 业务层:运单列表查询/详情/导出 - 新增 systemMethods/waybill/warehouse.js 入口层 - 新增 6 个迁移脚本:面单列表索引、store_id 列、物流编码/名称回填、发货单号列、货主ID回填、发货字段回填 - 更新 waybill-module-design.md 设计文档
308 lines
9.5 KiB
JavaScript
308 lines
9.5 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../../main');
|
||
|
||
const WAYBILL_EXPORT_TASK_CACHE_KEY_PREFIX = 'waybill_export_task_';
|
||
|
||
/**
|
||
* 运单号仓库侧入口层。
|
||
* 负责鉴权声明、参数校验,委托 systems/waybill.js 执行业务逻辑。
|
||
*
|
||
* 所有方法均需登录但无需细粒度鉴权(与订单列表保持一致)。
|
||
*/
|
||
class WaybillWarehouseService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = '';
|
||
const _noNeedRight = 'list,detail,export,exportTaskProgress';
|
||
const _noNeedLog = '';
|
||
const _noNeedAuth = 'list,detail,export,exportTaskProgress';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
|
||
/**
|
||
* 运单列表查询(仓库侧)。
|
||
* 支持分页、多条件筛选、排序。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @param {Object} [data.where] 筛选条件。
|
||
* @param {Object} [data.pagination] 分页参数 { page, pageSize }。
|
||
* @param {Object} [data.sort] 排序参数 { field, order }。
|
||
* @return {Promise<Object>} 运单列表与分页信息。
|
||
*/
|
||
async list(data) {
|
||
try {
|
||
const { where, pagination, sort } = data || {};
|
||
const result = await this.service.systems.waybill.list({
|
||
where: where || {},
|
||
pagination: pagination || {},
|
||
sort: sort || {},
|
||
});
|
||
return this.success('获取成功', result);
|
||
} catch (e) {
|
||
return this.fail(e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 运单详情查询(仓库侧)。
|
||
* 支持按运单ID或运单号查询。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @param {number} [data.id] 运单记录ID。
|
||
* @param {string} [data.waybillCode] 运单号(与 id 二选一,waybillCode 优先)。
|
||
* @return {Promise<Object>} 运单详情(含订单/打印/打包信息)。
|
||
*/
|
||
async detail(data) {
|
||
try {
|
||
const { id, waybillCode } = data || {};
|
||
if (!id && !waybillCode) {
|
||
return this.fail('运单ID或运单号不能为空');
|
||
}
|
||
const result = await this.service.systems.waybill.detail({
|
||
id,
|
||
waybillCode,
|
||
});
|
||
return this.success('获取成功', result);
|
||
} catch (e) {
|
||
return this.fail(e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 运单导出(仓库侧)。异步任务模式,提交后通过 exportTaskProgress 查询进度。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @param {Object} [data.where] 筛选条件(同 list)。
|
||
* @param {Object} [data.sort] 排序参数(同 list)。
|
||
* @param {string[]} [data.exportFields] 需要导出的字段列表。
|
||
* @return {Promise<Object>} 任务提交结果,含 task_id。
|
||
*/
|
||
async export(data) {
|
||
try {
|
||
const { where, sort, exportFields } = data || {};
|
||
const taskId = this.generateExportTaskId();
|
||
|
||
const taskPayload = {
|
||
task_id: taskId,
|
||
status: 'submitted',
|
||
progress: 0,
|
||
current_count: 0,
|
||
total_count: 0,
|
||
message: '导出任务已提交',
|
||
result: null,
|
||
error: '',
|
||
};
|
||
await this.setExportTaskProgress(taskId, taskPayload);
|
||
|
||
setImmediate(async () => {
|
||
try {
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: 'running',
|
||
progress: 5,
|
||
current_count: 0,
|
||
total_count: 0,
|
||
message: '导出任务执行中',
|
||
result: null,
|
||
error: '',
|
||
});
|
||
|
||
const result = await this.service.systems.waybill.export({
|
||
where: where || {},
|
||
sort: sort || {},
|
||
exportFields,
|
||
onProgress: async progress => {
|
||
const safeProgress = this.isPlainObject(progress) ? progress : {};
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: safeProgress.status || 'running',
|
||
progress: safeProgress.progress || 0,
|
||
current_count: safeProgress.currentCount || safeProgress.current_count || 0,
|
||
total_count: safeProgress.totalCount || safeProgress.total_count || 0,
|
||
message: safeProgress.message || '导出任务执行中',
|
||
result: safeProgress.result || null,
|
||
error: '',
|
||
});
|
||
},
|
||
});
|
||
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: 'success',
|
||
progress: 100,
|
||
current_count: result.current_count || 0,
|
||
total_count: result.total_count || 0,
|
||
message: '导出任务完成',
|
||
result,
|
||
error: '',
|
||
});
|
||
} catch (error) {
|
||
const errorMessage = error && error.message ? error.message : `${error}`;
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: 'failed',
|
||
progress: 100,
|
||
message: '导出任务失败',
|
||
result: null,
|
||
error: errorMessage,
|
||
});
|
||
}
|
||
});
|
||
|
||
return this.success('ok', {
|
||
...taskPayload,
|
||
update_time: Date.now(),
|
||
});
|
||
} catch (e) {
|
||
return this.fail(e.message);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询运单导出任务进度。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @param {string} [data.task_id] 任务ID(下划线写法)。
|
||
* @param {string} [data.taskId] 任务ID(驼峰写法)。
|
||
* @return {Promise<Object>} 任务进度信息。
|
||
*/
|
||
async exportTaskProgress(data = {}) {
|
||
try {
|
||
const taskId = this.normalizeExportTaskId(data);
|
||
if (!taskId) {
|
||
return this.success('ok', {
|
||
task_id: '',
|
||
status: 'not_found',
|
||
progress: 0,
|
||
current_count: 0,
|
||
total_count: 0,
|
||
message: '任务不存在',
|
||
result: null,
|
||
error: '',
|
||
download_url: '',
|
||
update_time: Date.now(),
|
||
});
|
||
}
|
||
|
||
const cachedTask = await this.getExportTaskProgress(taskId);
|
||
if (!cachedTask) {
|
||
return this.success('ok', {
|
||
task_id: taskId,
|
||
status: 'not_found',
|
||
progress: 0,
|
||
current_count: 0,
|
||
total_count: 0,
|
||
message: '任务不存在',
|
||
result: null,
|
||
error: '',
|
||
download_url: '',
|
||
update_time: Date.now(),
|
||
});
|
||
}
|
||
|
||
const safeResult = this.isPlainObject(cachedTask.result) ? cachedTask.result : {};
|
||
const downloadUrl = String(
|
||
safeResult.url
|
||
|| safeResult.cndUrl
|
||
|| safeResult.cdnUrl
|
||
|| safeResult.cosUrl
|
||
|| safeResult.fileKey
|
||
|| ''
|
||
);
|
||
|
||
return this.success('ok', {
|
||
task_id: taskId,
|
||
...cachedTask,
|
||
download_url: downloadUrl,
|
||
});
|
||
} catch (e) {
|
||
return this.fail(e.message);
|
||
}
|
||
}
|
||
|
||
// ==================== 导出任务辅助方法 ====================
|
||
|
||
/**
|
||
* 生成运单导出任务ID。
|
||
* @return {string} 任务ID。
|
||
*/
|
||
generateExportTaskId() {
|
||
const warehouseId = this.ctx?.user?.store_id || 'unknown';
|
||
const timestamp = Date.now();
|
||
const randomToken = Math.random().toString(36).slice(2, 10);
|
||
return `waybill_export_${warehouseId}_${timestamp}_${randomToken}`;
|
||
}
|
||
|
||
/**
|
||
* 规范导出任务ID。
|
||
* @param {Object} data 请求体。
|
||
* @return {string} 规范后的任务ID。
|
||
*/
|
||
normalizeExportTaskId(data = {}) {
|
||
const normalizedData = this.isPlainObject(data) ? data : {};
|
||
return String(normalizedData.task_id || normalizedData.taskId || '').trim();
|
||
}
|
||
|
||
/**
|
||
* 获取运单导出任务缓存键。
|
||
* @param {string} taskId 任务ID。
|
||
* @return {string} 缓存键。
|
||
*/
|
||
getExportTaskCacheKey(taskId = '') {
|
||
const normalizedTaskId = String(taskId || '').trim();
|
||
if (!normalizedTaskId) {
|
||
return '';
|
||
}
|
||
return `${WAYBILL_EXPORT_TASK_CACHE_KEY_PREFIX}${normalizedTaskId}`;
|
||
}
|
||
|
||
/**
|
||
* 写入运单导出任务进度缓存。
|
||
* @param {string} taskId 任务ID。
|
||
* @param {Object} payload 任务状态。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async setExportTaskProgress(taskId = '', payload = {}) {
|
||
const cacheKey = this.getExportTaskCacheKey(taskId);
|
||
if (!cacheKey || typeof this.app.setCacheAsync !== 'function') {
|
||
return;
|
||
}
|
||
|
||
const safePayload = this.isPlainObject(payload) ? payload : {};
|
||
const normalizeCount = value => {
|
||
const numericValue = Number(value);
|
||
return Number.isFinite(numericValue) && numericValue >= 0 ? Math.round(numericValue) : 0;
|
||
};
|
||
const progressPayload = {
|
||
task_id: String(taskId || '').trim(),
|
||
status: String(safePayload.status || 'submitted').trim() || 'submitted',
|
||
progress: Number.isFinite(Number(safePayload.progress))
|
||
? Math.min(100, Math.max(0, Math.round(Number(safePayload.progress))))
|
||
: 0,
|
||
current_count: normalizeCount(safePayload.current_count),
|
||
total_count: normalizeCount(safePayload.total_count),
|
||
message: String(safePayload.message || ''),
|
||
result: safePayload.result ?? null,
|
||
error: String(safePayload.error || ''),
|
||
};
|
||
|
||
await this.app.setCacheAsync(cacheKey, progressPayload);
|
||
}
|
||
|
||
/**
|
||
* 读取运单导出任务进度缓存。
|
||
* @param {string} taskId 任务ID。
|
||
* @return {Promise<Object|null>} 任务状态对象或 null。
|
||
*/
|
||
async getExportTaskProgress(taskId = '') {
|
||
const cacheKey = this.getExportTaskCacheKey(taskId);
|
||
if (!cacheKey || typeof this.app.getCacheAsync !== 'function') {
|
||
return null;
|
||
}
|
||
return await this.app.getCacheAsync(cacheKey);
|
||
}
|
||
}
|
||
|
||
module.exports = WaybillWarehouseService;
|