feat: add batch courier update for pending orders

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-03 20:34:47 +08:00
parent a15c91f5a1
commit 581fa926df
6 changed files with 177 additions and 2 deletions

View File

@ -2334,6 +2334,20 @@ module.exports = {
ids: { type: 'array', itemType: 'integer', required: true, description: '订单ID数组(待审核/异常/待推送)' },
warehouse_id: { type: 'integer', required: true, description: '目标仓库ID(需属于当前主体,同步刷新 warehouse_code 快照)' },
},
/** 批量修改待审核订单快递公司 */
changeOrderStatusLogisticsRequest: {
ids: { type: 'array', itemType: 'integer', required: true, description: '待审核订单ID数组' },
logistics_code: { type: 'string', required: true, description: '启用的快递公司编码' },
},
orderStatusChangeLogisticsData: {
updated: { type: 'integer', description: '更新条数' },
target: { type: 'object', description: '目标快递公司(code + name)' },
},
orderStatusChangeLogisticsResponse: {
code: { type: 'integer', description: '业务状态码,0 成功' },
message: { type: 'string', description: '提示信息' },
data: { type: 'orderStatusChangeLogisticsData', description: '批量修改结果' },
},
orderStatusChangeOwnerData: {
updated: { type: 'integer', description: '更新条数' },
target: { type: 'object', description: '目标仓库(id + code + 名称)' },

View File

@ -427,6 +427,28 @@ class OrderStatusController extends Controller {
}
}
/**
* @summary 批量修改待审核订单快递
* @description 仅待审核订单可修改;快递公司须为启用的快递字典项,并同步更新编码与名称快照。
* @router post /api/order-status/change-logistics
* @apikey
* @request body changeOrderStatusLogisticsRequest
* @response 200 orderStatusChangeLogisticsResponse success
*/
async changeLogistics() {
const { ctx } = this;
const body = ctx.request.body || {};
try {
const data = await ctx.service.orderStatus.changeLogistics({
ids: this._pickIds(),
logistics_code: body.logistics_code,
});
ctx.body = { code: 0, message: 'success', data };
} catch (err) {
this._fail(ctx, err, '批量修改快递失败');
}
}
/**
* @summary 智能识别收件人文本
* @description 把「姓名 电话 地址」等自由文本解析为收件人字段,供新增/编辑表单一键填充。

View File

@ -80,8 +80,9 @@ module.exports = app => {
router.post('/api/order-status/restore', jwtAuth, principalCheck(), controller.orderStatus.restore);
// 推送仓库(WMS deliveryorder.create):单条 / 批量
router.post('/api/order-status/push-wms', jwtAuth, principalCheck(), controller.orderStatus.pushWmsBatch);
// 批量切换仓库 / 设置所属店铺(同步刷新快照)
// 批量切换仓库 / 修改待审核订单快递(同步刷新快照)
router.post('/api/order-status/change-warehouse', jwtAuth, principalCheck(), controller.orderStatus.changeWarehouse);
router.post('/api/order-status/change-logistics', jwtAuth, principalCheck(), controller.orderStatus.changeLogistics);
// 订单详情 / 编辑 / 删除(删除仅限待审核、异常、已取消)/ 推送仓库(单条)
router.get('/api/order-status/:id', jwtAuth, principalCheck(), controller.orderStatus.show);
router.put('/api/order-status/:id', jwtAuth, principalCheck(), controller.orderStatus.update);

View File

@ -1559,6 +1559,66 @@ class OrderStatusService extends Service {
});
}
/**
* 批量修改待审核订单快递公司(同步刷新物流编码与名称快照)
* @param {Object} p { ids, logistics_code }
* @return {Promise<Object>} { updated, target }
*/
async changeLogistics(p = {}) {
const { ctx } = this;
const Op = ctx.app.Sequelize.Op;
const principalId = ctx.state.principalId;
const idList = this._normalizeIds(p.ids);
const code = String(p.logistics_code == null ? '' : p.logistics_code).trim();
if (!code) {
const err = new Error('logistics_code 为必填项');
err.status = 400;
throw err;
}
const indexRows = await this._loadIndexRows(idList, principalId);
const invalid = indexRows.filter(row => row.current_status !== ORDER_STATUS.PENDING);
if (invalid.length) {
const err = new Error(
`「修改快递」仅适用于待审核的订单,当前有 ${invalid.length} 条状态不符(如 ${invalid[0].order_no})`
);
err.status = 400;
throw err;
}
const company = await ctx.model.ExpressCompany.findOne({
where: { code, status: 1 },
attributes: [ 'code', 'name' ],
});
if (!company) {
const err = new Error('快递公司不存在或未启用');
err.status = 400;
throw err;
}
const ids = indexRows.map(row => Number(row.id));
const transaction = await ctx.model.transaction();
let updated = 0;
try {
const [ count ] = await this._model(ORDER_STATUS.PENDING).update(
{ logistics_code: company.code, logistics_name: company.name },
{ where: { principal_id: principalId, id: { [Op.in]: ids } }, transaction }
);
updated = Number(count) || 0;
if (updated !== ids.length) {
const err = new Error('待审核订单更新数量与所选数量不一致,请刷新后重试');
err.status = 400;
throw err;
}
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
return { updated, target: { code: company.code, name: company.name } };
}
/**
* 判断订单是否由外部打单平台拉取(其店铺由平台决定,不允许人工改店铺)
* @param {Object} row 状态表记录(含 extend_props)

View File

@ -394,3 +394,11 @@ export function changeOrdersWarehouse(ids: number[], warehouseId: number) {
{ ids, warehouse_id: warehouseId },
)
}
/** 批量修改待审核订单的快递公司 */
export function changeOrdersLogistics(ids: number[], logisticsCode: string) {
return post<{ updated: number; target: { code: string; name: string } }>(
'/order-status/change-logistics',
{ ids, logistics_code: logisticsCode },
)
}

View File

@ -6,6 +6,7 @@ import ExportButton from '@/components/ExportButton.vue'
import ListPager from '@/components/ListPager.vue'
import RecordPickerDialog from '@/components/RecordPickerDialog.vue'
import OrderFormDialog from './OrderFormDialog.vue'
import { getExpressCompanyOptions, type ExpressCompanyOption } from '@/api/expressCompany'
import { DEFAULT_PAGE_SIZE, useListPage } from '@/composables/useListPage'
import { formatDateTime } from '@/utils/datetime'
import { platformNameOf } from '@/constants/platform'
@ -14,6 +15,7 @@ import {
archiveOrders,
auditOrders,
cancelOrders,
changeOrdersLogistics,
changeOrdersWarehouse,
getOrderStatusDetail,
getOrderStatusFilters,
@ -37,7 +39,7 @@ import {
*
* 八个视图(待审核 / 待推送 / 异常 / 委外中 / 配送 / 已作废 / 已取消 / 历史)共用本面板,数据源为对应状态表;
* 批量与行内动作按场景分布:
* - 待审核:审核(锁定库存 → 待推送)/ 切换仓库 / 取消 / 作废 / 编辑
* - 待审核:审核(锁定库存 → 待推送)/ 修改快递 / 切换仓库 / 取消 / 作废 / 编辑
* - 待推送:推送仓库(WMS deliveryorder.create)/ 驳回(换号+释放库存)/ 取消 / 作废 / 切换仓库 / 手工标记推送结果
* - 异常:转回待审核 / 直接审核 / 取消 / 作废 / 编辑
* - 委外中(已推送成功):只读流转(取消)——**不再提供推送仓库操作**
@ -170,11 +172,13 @@ type BatchKey =
| 'void'
| 'restore'
| 'change_warehouse'
| 'change_logistics'
/** 批量操作按场景分布在各视图(店铺在创建时确定,不再提供批量设置店铺) */
const BATCH_ACTIONS: Record<OrderStage, { key: BatchKey; label: string; type: string }[]> = {
pending: [
{ key: 'audit', label: '批量审核(→ 待推送)', type: 'primary' },
{ key: 'change_logistics', label: '修改快递', type: 'default' },
{ key: 'change_warehouse', label: '切换仓库', type: 'default' },
{ key: 'cancel', label: '取消', type: 'warning' },
{ key: 'void', label: '作废', type: 'danger' },
@ -218,6 +222,7 @@ function confirmText(key: BatchKey, count: number): string {
void: `确认作废选中的 ${count} 条订单?作废记录人与原因、释放锁定库存,不可恢复也禁止删除。`,
restore: '确认将选中的订单恢复到「待审核」?曾推送过的单据会自动换号。',
change_warehouse: '确认切换选中订单的发货仓库?',
change_logistics: `确认将选中的 ${count} 条待审核订单修改快递公司?`,
}
return map[key]
}
@ -274,6 +279,43 @@ async function pickWarehouse(): Promise<number | null> {
return row ? Number(row.id) : null
}
/** 批量修改快递使用的目标选择弹窗 */
const expressPicker = reactive({
visible: false,
options: [] as ExpressCompanyOption[],
})
const expressPickerOptions = computed(() => expressPicker.options.map(item => ({ ...item })))
const expressPickerColumns = [
{ prop: 'code', label: '快递编码', width: 180 },
{ prop: 'name', label: '快递名称', minWidth: 200 },
]
let expressPickerResolve: ((row: Record<string, unknown> | null) => void) | null = null
async function pickExpressCompany(): Promise<Record<string, unknown> | null> {
expressPicker.options = await getExpressCompanyOptions()
if (!expressPicker.options.length) {
ElMessage.warning('暂无启用的快递公司,请先到「快递公司」维护')
return null
}
expressPicker.visible = true
return new Promise(resolve => {
expressPickerResolve = resolve
})
}
function onExpressPickerConfirm(row: Record<string, unknown>) {
if (expressPickerResolve) expressPickerResolve(row)
expressPickerResolve = null
}
function onExpressPickerClose(visible: boolean) {
expressPicker.visible = visible
if (!visible && expressPickerResolve) {
expressPickerResolve(null)
expressPickerResolve = null
}
}
/** 批量操作入口 */
async function handleBatch(key: BatchKey) {
const ids = [ ...selectedIds.value ]
@ -292,6 +334,20 @@ async function handleBatch(key: BatchKey) {
await fetchList()
return
}
if (key === 'change_logistics') {
const company = await pickExpressCompany()
if (!company) return
await ElMessageBox.confirm(
`确认将选中的 ${ids.length} 条待审核订单快递公司修改为「${String(company.name)}」?`,
'批量修改快递',
{ type: 'warning', confirmButtonText: '确定', cancelButtonText: '取消' },
)
const result = await changeOrdersLogistics(ids, String(company.code))
ElMessage.success(`已修改快递:${result.updated} 条,${result.target.name}`)
selectedIds.value = []
await fetchList()
return
}
let tip = ''
if (key === 'push_failed') {
@ -897,6 +953,20 @@ onMounted(async () => {
@update:model-value="onPickerClose"
@confirm="onPickerConfirm"
/>
<!-- 批量修改快递:仅待审核订单可用 -->
<RecordPickerDialog
:model-value="expressPicker.visible"
title="选择快递公司"
:options="expressPickerOptions"
:columns="expressPickerColumns"
value-key="code"
:search-props="['code', 'name']"
confirm-text="修改所选订单"
empty-text="暂无启用的快递公司"
@update:model-value="onExpressPickerClose"
@confirm="onExpressPickerConfirm"
/>
</div>
</template>