'use strict'; const Controller = require('egg').Controller; /** * 外部打单平台「同步发货」相关接口 * * 用于配送视图:查看同步数量(异常数量快速展示)、批量重试同步异常/失败的订单。 * 真正的同步由定时任务 `externalSendSyncWorker` 执行,批量重试会立刻跑一批并返回结果。 * @controller ExternalSendSyncController */ class ExternalSendSyncController extends Controller { /** * @summary 同步状态数量 * @description 返回当前主体下配送订单的外部发货同步数量:待同步 / 待重试 / 异常 / 已同步 / 合计。 * @router get /api/external-send/counts * @apikey * @response 200 externalSendCountsResponse success */ async counts() { const { ctx } = this; try { const data = await ctx.service.externalSendSync.counts(ctx.state.principalId); ctx.body = { code: 0, message: 'success', data }; } catch (err) { const status = Number(err && err.status) || 500; ctx.status = status; ctx.body = { code: status, message: (err && err.message) || '查询同步数量失败' }; } } /** * @summary 批量重试同步外部发货 * @description 把指定订单(或按状态筛选的订单)重置为「待同步」并立刻同步一批。 * 不传 ids 时默认重试「异常 + 待重试」的订单;次数会清零重新计数。 * @router post /api/external-send/retry * @apikey * @request body externalSendRetryRequest retryRequest 重试参数 * @response 200 externalSendRetryResponse success */ async retry() { const { ctx } = this; const body = ctx.request.body || {}; const ids = Array.isArray(body.ids) ? body.ids : []; const statuses = Array.isArray(body.statuses) ? body.statuses.map(Number) : undefined; try { const data = await ctx.service.externalSendSync.retry({ ids, statuses, principalId: ctx.state.principalId, }); ctx.body = { code: 0, message: 'success', data }; } catch (err) { const status = Number(err && err.status) || 500; ctx.status = status; ctx.body = { code: status, message: (err && err.message) || '重试同步失败' }; } } } module.exports = ExternalSendSyncController;