- stock/warehouse: 新增 externalList/externalExport(外仓/货主维度查询) - storeWarehouse/store: 新增货主侧接口(list/get 等) - stock_store: getWarehouseStock 支持 externalWarehouseId/externalStoreId - store_warehouse: 合同关系改为单向维度查询(不再 OR 两边) - storage/report: 库存报表支持 store 维度 + stockIds/keyword 精确筛选 - stock/transaction: storeList/exportStore store_id 从 ctx 获取防越权 - stock_warehouse: getStockDetails 支持显式传 null 跳过仓库过滤 - storage/management: 储位分配待处理排除待审核(step=1)订单
864 lines
27 KiB
JavaScript
864 lines
27 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../../main');
|
||
|
||
const PRINT_QUANTITY_LOG_EXPORT_TASK_CACHE_KEY_PREFIX = 'print_quantity_log_export_task_';
|
||
const PRINT_QUANTITY_LOG_EXPORT_TASK_CACHE_TTL_SECONDS = 1800;
|
||
|
||
|
||
/**
|
||
* 规范仓库配置方法入参,避免把原始引用直接传给下游服务。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Object} 复制后的请求数据。
|
||
*/
|
||
function normalizeStoreWarehousePayload(data) {
|
||
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
||
return {};
|
||
}
|
||
|
||
return { ...data };
|
||
}
|
||
|
||
class StoreWarehouseStoreService extends MainService {
|
||
/**
|
||
* 创建一个仓库配置 store 服务。
|
||
*
|
||
* @param {Object} ctx 上下文对象。
|
||
*/
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = '';
|
||
const _noNeedRight = 'list,sign,confirmSign,unbind,rename,openAccount,getPolicyConfig,getPolicyConfigFields,savePolicyConfig,listApiConfig,saveApiConfig,deleteQimenConfig,queryPrintQuantityLogs,queryStorePrintQuantityLogs,exportStorePrintQuantityLogs,exportStorePrintQuantityLogsTaskProgress,adjustPrintQuantity';
|
||
const _noNeedLog = '*';
|
||
const _noNeedAuth = 'list,sign,confirmSign,unbind,rename,openAccount,getPolicyConfig,getPolicyConfigFields,savePolicyConfig,listApiConfig,saveApiConfig,deleteQimenConfig,queryPrintQuantityLogs,queryStorePrintQuantityLogs,exportPrintQuantityLogs,exportStorePrintQuantityLogs,exportStorePrintQuantityLogsTaskProgress,adjustPrintQuantity';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
|
||
/**
|
||
* 获取签约列表(货主侧)。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async list(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.listForStore !== 'function') {
|
||
return this.fail('获取签约列表失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.listForStore(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '获取签约列表失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 发起签约。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async sign(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.sign !== 'function') {
|
||
return this.fail('签约失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.sign(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '签约失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 确认签约:对方主体确认待签约的申请。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async confirmSign(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.confirmSign !== 'function') {
|
||
return this.fail('确认签约失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.confirmSign(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '确认签约失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改签约名称。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async rename(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.rename !== 'function') {
|
||
return this.fail('修改签约名称失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.rename(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '修改签约名称失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 解除签约。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async unbind(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.unbind !== 'function') {
|
||
return this.fail('解除签约失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.unbind(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '解除签约失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 人工调整面单余额。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async adjustPrintQuantity(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.adjustPrintQuantity !== 'function') {
|
||
return this.fail('人工调整面单余额失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.adjustPrintQuantity(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '人工调整面单余额失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询面单余额变更日志(分页)。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {number|string} data.store_id 货主 ID。
|
||
* @param {Object} [data.where] 筛选条件。
|
||
* @param {Object} [data.pagination] 分页对象。
|
||
* @param {number|string} [data.pagination.page] 页码。
|
||
* @param {number|string} [data.pagination.page_size] 每页条数。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async queryPrintQuantityLogs(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.queryPrintQuantityLogs !== 'function') {
|
||
return this.fail('查询面单余额变更日志失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const requestData = normalizeStoreWarehousePayload(data);
|
||
const { where } = this.prepareWhereContext(requestData);
|
||
const {
|
||
store_id,
|
||
} = requestData;
|
||
const rawPagination = requestData.pagination && typeof requestData.pagination === 'object'
|
||
? requestData.pagination
|
||
: {};
|
||
const {
|
||
page,
|
||
page_size,
|
||
} = rawPagination;
|
||
|
||
const result = await targetService.queryPrintQuantityLogs({
|
||
store_id,
|
||
where,
|
||
pagination: {
|
||
page,
|
||
page_size,
|
||
},
|
||
});
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '查询面单余额变更日志失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取合约策略配置。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getPolicyConfig(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.getPolicyConfig !== 'function') {
|
||
return this.fail('获取合约策略配置失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.getPolicyConfig(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '获取合约策略配置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取合约策略字段信息。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getPolicyConfigFields(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.getPolicyConfigFields !== 'function') {
|
||
return this.fail('获取合约策略字段信息失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.getPolicyConfigFields(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '获取合约策略字段信息失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存合约策略配置。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async savePolicyConfig(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.savePolicyConfig !== 'function') {
|
||
return this.fail('保存合约策略配置失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.savePolicyConfig(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '保存合约策略配置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取接口配置列表。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async listApiConfig(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.listApiConfig !== 'function') {
|
||
return this.fail('获取接口配置列表失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.listApiConfig(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '获取接口配置列表失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存接口配置。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async saveApiConfig(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.saveApiConfig !== 'function') {
|
||
return this.fail('保存接口配置失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.saveApiConfig(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '保存接口配置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除奇门接口配置。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async deleteQimenConfig(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.deleteQimenConfig !== 'function') {
|
||
return this.fail('删除接口配置失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.deleteQimenConfig(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '删除接口配置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 仓库代货主一键开户。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async openAccount(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.openAccount !== 'function') {
|
||
return this.fail('一键开户失败', {
|
||
code: 500,
|
||
});
|
||
}
|
||
|
||
try {
|
||
const payload = normalizeStoreWarehousePayload(data);
|
||
const result = await targetService.openAccount(payload);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '一键开户失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 提交面单余额变更日志导出任务。
|
||
* 入参条件与 queryPrintQuantityLogs 的 where 语义一致,异步执行,立即返回 task_id。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {number|string} data.store_id 货主ID(必填)。
|
||
* @param {Object} [data.where] 动态筛选条件,与 queryPrintQuantityLogs 的 where 语义一致。
|
||
* @return {Promise<Object>} 统一响应结果(包含 task_id)。
|
||
*/
|
||
async exportPrintQuantityLogs(data = {}) {
|
||
const { service } = this;
|
||
const targetService = service && service.systems && service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.exportPrintQuantityLogs !== 'function') {
|
||
return this.fail('导出面单余额变更日志失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const requestData = normalizeStoreWarehousePayload(data);
|
||
const { where } = this.prepareWhereContext(requestData);
|
||
const { store_id } = requestData;
|
||
|
||
const taskId = this.generateExportTaskId();
|
||
const taskPayload = {
|
||
task_id: taskId,
|
||
status: 'submitted',
|
||
progress: 0,
|
||
message: '导出任务已提交',
|
||
result: null,
|
||
error: '',
|
||
};
|
||
|
||
await this.setExportTaskProgress(taskId, taskPayload);
|
||
|
||
setImmediate(async () => {
|
||
try {
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: 'running',
|
||
progress: 10,
|
||
message: '导出任务执行中',
|
||
result: null,
|
||
error: '',
|
||
});
|
||
|
||
const result = await targetService.exportPrintQuantityLogs({
|
||
store_id,
|
||
where,
|
||
});
|
||
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: 'success',
|
||
progress: 100,
|
||
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 (error) {
|
||
return this.fail(error.message || '导出面单余额变更日志失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询面单余额变更日志导出任务进度。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {string} [data.task_id] 任务ID(下划线写法)。
|
||
* @param {string} [data.taskId] 任务ID(驼峰写法)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async exportPrintQuantityLogsTaskProgress(data = {}) {
|
||
const taskId = this.normalizeExportTaskId(data);
|
||
if (!taskId) {
|
||
return this.success('ok', {
|
||
task_id: '',
|
||
status: 'not_found',
|
||
progress: 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,
|
||
message: '任务不存在',
|
||
result: null,
|
||
error: '',
|
||
download_url: '',
|
||
update_time: Date.now(),
|
||
});
|
||
}
|
||
|
||
const safeResult = this.isPlainObject(cachedTask.result) ? cachedTask.result : {};
|
||
const downloadUrl = String(
|
||
safeResult.url
|
||
|| safeResult.cdnUrl
|
||
|| safeResult.cosUrl
|
||
|| safeResult.fileKey
|
||
|| ''
|
||
);
|
||
|
||
return this.success('ok', {
|
||
task_id: taskId,
|
||
...cachedTask,
|
||
download_url: downloadUrl,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 货主侧查询面单余额变更日志(自动从平台激活上下文获取 store_id)。
|
||
* 无需显式传入 store_id,系统自动从当前用户激活的货主平台上下文中获取。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {Object} [data.where] 动态筛选条件。
|
||
* @param {Object} [data.pagination] 分页对象。
|
||
* @param {number|string} [data.pagination.page] 页码。
|
||
* @param {number|string} [data.pagination.page_size] 每页条数。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async queryStorePrintQuantityLogs(data = {}) {
|
||
const { ctx, app, service } = this;
|
||
|
||
// 从平台激活上下文获取 store_id
|
||
const platformRows = await app.model.query(
|
||
'SELECT apa.employee_id, ase.store_id FROM wms_account_platform_active apa ' +
|
||
'INNER JOIN wms_account_store_employee ase ON ase.id = apa.employee_id AND ase.status = 1 ' +
|
||
'WHERE apa.account_id = :accountId AND apa.platform_code = :platformCode LIMIT 1',
|
||
{
|
||
replacements: { accountId: ctx.userId, platformCode: ctx.platformCode || 'web' },
|
||
type: app.Sequelize.QueryTypes.SELECT,
|
||
}
|
||
);
|
||
|
||
if (!platformRows || platformRows.length === 0 || !platformRows[0].store_id) {
|
||
return this.fail('未获取到货主平台激活上下文,请先切换至货主角色');
|
||
}
|
||
|
||
const storeId = platformRows[0].store_id;
|
||
const targetService = service && service.systems && service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.queryPrintQuantityLogs !== 'function') {
|
||
return this.fail('查询面单余额变更日志失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const requestData = normalizeStoreWarehousePayload(data);
|
||
const { where } = this.prepareWhereContext(requestData);
|
||
const rawPagination = requestData.pagination && typeof requestData.pagination === 'object'
|
||
? requestData.pagination
|
||
: {};
|
||
const { page, page_size } = rawPagination;
|
||
|
||
const result = await targetService.queryPrintQuantityLogs({
|
||
store_id: storeId,
|
||
where,
|
||
pagination: { page, page_size },
|
||
});
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '查询面单余额变更日志失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 货主侧提交面单余额变更日志导出任务(自动从平台激活上下文获取 store_id)。
|
||
* 入参条件与 queryStorePrintQuantityLogs 的 where 语义一致,异步执行,立即返回 task_id。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {Object} [data.where] 动态筛选条件。
|
||
* @return {Promise<Object>} 统一响应结果(包含 task_id)。
|
||
*/
|
||
async exportStorePrintQuantityLogs(data = {}) {
|
||
const { ctx, app, service } = this;
|
||
|
||
// 从平台激活上下文获取 store_id
|
||
const platformRows = await app.model.query(
|
||
'SELECT apa.employee_id, ase.store_id FROM wms_account_platform_active apa ' +
|
||
'INNER JOIN wms_account_store_employee ase ON ase.id = apa.employee_id AND ase.status = 1 ' +
|
||
'WHERE apa.account_id = :accountId AND apa.platform_code = :platformCode LIMIT 1',
|
||
{
|
||
replacements: { accountId: ctx.userId, platformCode: ctx.platformCode || 'web' },
|
||
type: app.Sequelize.QueryTypes.SELECT,
|
||
}
|
||
);
|
||
|
||
if (!platformRows || platformRows.length === 0 || !platformRows[0].store_id) {
|
||
return this.fail('未获取到货主平台激活上下文,请先切换至货主角色');
|
||
}
|
||
|
||
const storeId = platformRows[0].store_id;
|
||
const targetService = service && service.systems && service.systems.storeWarehouse;
|
||
|
||
if (!targetService || typeof targetService.exportPrintQuantityLogs !== 'function') {
|
||
return this.fail('导出面单余额变更日志失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const requestData = normalizeStoreWarehousePayload(data);
|
||
const { where } = this.prepareWhereContext(requestData);
|
||
|
||
const taskId = this.generateExportTaskId('store');
|
||
const taskPayload = {
|
||
task_id: taskId,
|
||
status: 'submitted',
|
||
progress: 0,
|
||
message: '货主侧导出任务已提交',
|
||
result: null,
|
||
error: '',
|
||
};
|
||
|
||
await this.setExportTaskProgress(taskId, taskPayload);
|
||
|
||
setImmediate(async () => {
|
||
try {
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: 'running',
|
||
progress: 10,
|
||
message: '货主侧导出任务执行中',
|
||
result: null,
|
||
error: '',
|
||
});
|
||
|
||
const result = await targetService.exportPrintQuantityLogs({
|
||
store_id: storeId,
|
||
where,
|
||
});
|
||
|
||
await this.setExportTaskProgress(taskId, {
|
||
task_id: taskId,
|
||
status: 'success',
|
||
progress: 100,
|
||
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 (error) {
|
||
return this.fail(error.message || '导出面单余额变更日志失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 货主侧查询面单余额变更日志导出任务进度。
|
||
* 复用 exportPrintQuantityLogsTaskProgress 的缓存查询逻辑。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {string} [data.task_id] 任务ID(下划线写法)。
|
||
* @param {string} [data.taskId] 任务ID(驼峰写法)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async exportStorePrintQuantityLogsTaskProgress(data = {}) {
|
||
const taskId = this.normalizeExportTaskId(data);
|
||
if (!taskId) {
|
||
return this.success('ok', {
|
||
task_id: '',
|
||
status: 'not_found',
|
||
progress: 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,
|
||
message: '任务不存在',
|
||
result: null,
|
||
error: '',
|
||
download_url: '',
|
||
update_time: Date.now(),
|
||
});
|
||
}
|
||
|
||
const safeResult = this.isPlainObject(cachedTask.result) ? cachedTask.result : {};
|
||
const downloadUrl = String(
|
||
safeResult.url
|
||
|| safeResult.cdnUrl
|
||
|| safeResult.cosUrl
|
||
|| safeResult.fileKey
|
||
|| ''
|
||
);
|
||
|
||
return this.success('ok', {
|
||
task_id: taskId,
|
||
...cachedTask,
|
||
download_url: downloadUrl,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 生成导出任务ID。
|
||
* @param {string} [prefix=''] 任务类型前缀(如 'store')
|
||
* @return {string} 导出任务ID。
|
||
*/
|
||
generateExportTaskId(prefix = '') {
|
||
const storeId = this.ctx?.user?.store_id || 'unknown';
|
||
const timestamp = Date.now();
|
||
const randomToken = Math.random().toString(36).slice(2, 10);
|
||
const typePrefix = prefix ? `${prefix}_` : '';
|
||
return `print_qty_log_${typePrefix}export_${storeId}_${timestamp}_${randomToken}`;
|
||
}
|
||
|
||
/**
|
||
* 规范导出任务ID。
|
||
* @param {Object} data 请求体。
|
||
* @param {string} [data.task_id] 任务ID(下划线写法)
|
||
* @param {string} [data.taskId] 任务ID(驼峰写法)
|
||
* @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 `${PRINT_QUANTITY_LOG_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 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,
|
||
message: String(safePayload.message || ''),
|
||
result: safePayload.result ?? null,
|
||
error: String(safePayload.error || ''),
|
||
update_time: Date.now(),
|
||
};
|
||
|
||
try {
|
||
await this.app.setCacheAsync(cacheKey, progressPayload, PRINT_QUANTITY_LOG_EXPORT_TASK_CACHE_TTL_SECONDS);
|
||
} catch (error) {
|
||
this.ctx.logger.warn('[storeWarehouse.store.exportPrintQuantityLogs] 写入导出任务缓存失败: %s',
|
||
error && error.message ? error.message : error);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 读取导出任务进度缓存。
|
||
* @param {string} taskId 任务ID。
|
||
* @return {Promise<Object|null>} 任务缓存。
|
||
*/
|
||
async getExportTaskProgress(taskId = '') {
|
||
const cacheKey = this.getExportTaskCacheKey(taskId);
|
||
if (!cacheKey || typeof this.app.getCacheAsync !== 'function') {
|
||
return null;
|
||
}
|
||
|
||
try {
|
||
const cached = await this.app.getCacheAsync(cacheKey);
|
||
if (!this.isPlainObject(cached)) {
|
||
return null;
|
||
}
|
||
return cached;
|
||
} catch (error) {
|
||
this.ctx.logger.warn('[storeWarehouse.store.exportPrintQuantityLogs] 读取导出任务缓存失败: %s',
|
||
error && error.message ? error.message : error);
|
||
return null;
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = StoreWarehouseStoreService;
|