feat(order): 电商平台筛选可遍历实际平台,数据库店铺与手工单默认淘宝

- 订单筛选项新增 platforms:系统预设 ∪ 当前主体订单里实际出现过的编码(带数量,
  未收录进预设的标记 preset=false),下拉按用量排序,店管家新增平台类型时不会「筛不到」
- 手工单默认 source_platform_code=TB(淘宝),新建/编辑弹窗新增「电商平台」下拉;
  编辑改平台时按系统预设同步名称;外部打单平台拉取的订单该字段置灰不可改
- 数据库店铺拉单按店铺配置的平台落库(未配置时默认淘宝),待审核订单可见可筛
- 迁移 20261107010000:把「数据库店铺订单(extend_props.source=shop_database)」与
  「手工单(extend_props.labels 含手工单)」里为空的电商平台回填为 TB/淘宝

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-07 01:18:21 +08:00
parent 9a944be886
commit ba0d03414a
6 changed files with 173 additions and 1 deletions

View File

@ -9,6 +9,7 @@ const {
ORDER_ARCHIVE_MONTHS,
ORDER_INDEX_TABLE,
ORDER_ITEM_TABLE,
ORDER_TABLE_BY_STATUS,
normalizeOrderStatus,
orderStatusLabel,
orderModelName,
@ -16,6 +17,7 @@ const {
orderCopyFields,
} = require('../lib/orderStatus');
const { resolveAccountName } = require('../lib/accountProfile');
const { PLATFORM_PRESETS, platformNameOf, resolvePlatform } = require('../lib/platformCode');
const { validateReceiver } = require('../lib/orderReceiver');
const { validateItems } = require('../lib/orderDeliveryItems');
@ -35,6 +37,9 @@ const ARCHIVE_BATCH_LIMIT = 200;
/** 手工创建订单在扩展字段(extend_props)中写入的标记(与老发货单一致) */
const HANDMADE_ORDER_LABEL = '手工单';
/** 手工单默认电商平台(订单页按平台筛选/展示;可用 source_platform_code 覆盖) */
const HANDMADE_DEFAULT_PLATFORM = 'TB';
/** 店铺类型:2 外部打单平台商家(其订单只能由平台拉取,手工建单不可选) */
const SHOP_TYPE_EXTERNAL = 2;
@ -726,6 +731,10 @@ class OrderStatusService extends Service {
// 仓库:可空(审核前补齐即可),但填了必须是当前主体的仓库,并快照 warehouse_code
const fields = this._pickCreateFields(payload);
// 电商平台:手工单默认「淘宝」,可通过 source_platform_code(编码或中文名)显式指定
const manualPlatform = resolvePlatform(trimOrNull(payload.source_platform_code) || HANDMADE_DEFAULT_PLATFORM);
fields.source_platform_code = manualPlatform.code;
fields.source_platform_name = trimOrNull(payload.source_platform_name) || manualPlatform.name;
if (fields.warehouse_id !== undefined && fields.warehouse_id !== null && fields.warehouse_id !== '') {
const warehouseId = Number(fields.warehouse_id);
const warehouse = await ctx.model.Warehouse.findOne({
@ -1633,6 +1642,13 @@ class OrderStatusService extends Service {
delete body.subsidiary_id;
const patch = this._pickCreateFields(body);
// 电商平台变更:名称按系统预设同步(前端只提交编码),避免编码与名称不一致
if (body.source_platform_code !== undefined) {
const platform = resolvePlatform(trimOrNull(body.source_platform_code) || HANDMADE_DEFAULT_PLATFORM);
patch.source_platform_code = platform.code;
patch.source_platform_name = trimOrNull(body.source_platform_name) || platform.name;
}
// 店铺变更:校验归属 + 刷新 shop_nick 快照(推送时靠 shopNick 匹配快递策略)
// 先于仓库 / 明细校验处理,换店铺后订单货主跟随新店铺,后续校验用生效货主
let effectiveSubsidiaryId = row.subsidiary_id == null ? null : Number(row.subsidiary_id);
@ -2117,6 +2133,7 @@ class OrderStatusService extends Service {
archive_months: ORDER_ARCHIVE_MONTHS,
index_table: ORDER_INDEX_TABLE,
item_table: ORDER_ITEM_TABLE,
platforms: await this._platformOptions(principalId),
shops: shops.map(shop => ({
id: Number(shop.id),
code: shop.code,
@ -2132,6 +2149,52 @@ class OrderStatusService extends Service {
})),
};
}
/**
* 电商平台筛选项:系统预设编码 ∪ 当前主体订单里实际出现过的编码
*
* 预设保证「新平台/尚未拉单的平台」也能筛;实际出现的编码保证店管家新增平台类型
* (未收录进预设、落成其它编码)时不会在筛选里「查不到」。
* 只统计活动状态表(不含历史表),避免归档数据把每次筛选都扫一遍。
* @param {Number} principalId 主体ID
* @return {Promise<Array<{code: string, name: string, count: number, preset: boolean}>>} 平台选项
*/
async _platformOptions(principalId) {
const { ctx } = this;
const pid = Number(principalId);
const counts = new Map();
const unions = ORDER_ACTIVE_STATUSES
.map(status => ORDER_TABLE_BY_STATUS[status])
.filter(Boolean)
.map(table => `SELECT source_platform_code FROM ${table} WHERE principal_id = ${pid} AND source_platform_code IS NOT NULL`);
if (unions.length) {
try {
const sql = `SELECT source_platform_code AS code, COUNT(*) AS n FROM (${unions.join(' UNION ALL ')}) t GROUP BY 1`;
const rows = await ctx.model.query(sql, { type: ctx.app.Sequelize.QueryTypes.SELECT });
for (const row of rows || []) {
const code = String(row.code || '').trim().toUpperCase();
if (code) counts.set(code, (counts.get(code) || 0) + (Number(row.n) || 0));
}
} catch (err) {
ctx.logger.warn('[orderStatus] 统计订单平台分布失败:%s', err.message);
}
}
const presetCodes = new Set(PLATFORM_PRESETS.map(item => item.code));
const list = PLATFORM_PRESETS.map(item => ({
code: item.code,
name: item.name,
count: counts.get(item.code) || 0,
preset: true,
}));
for (const [ code, count ] of counts) {
if (presetCodes.has(code)) continue;
list.push({ code, name: platformNameOf(code), count, preset: false });
}
// 用过的平台排前面(同等条件下预设顺序不变)
return list.sort((a, b) => (b.count - a.count) || (Number(b.preset) - Number(a.preset)));
}
}
module.exports = OrderStatusService;

View File

@ -2,6 +2,7 @@
const { Service } = require('egg');
const { ORDER_STATUS } = require('../lib/orderStatus');
const { resolvePlatform } = require('../lib/platformCode');
const { buildSnapshot } = require('../lib/orderItemExpand');
const { DEFAULT_DB_TYPE, DEFAULT_PORT, DATASETS, CALLBACK_MODE_FIELD, CALLBACK_MODE_ROW, DEFAULT_CALLBACK_SEPARATOR } = require('../lib/shopDatabaseFields');
const {
@ -374,6 +375,8 @@ class ShopDatabaseSyncService extends Service {
const defaultWarehouseFields = defaultWarehouse
? { warehouse_id: defaultWarehouse.id, warehouse_code: defaultWarehouse.code }
: {};
// 电商平台:数据库店铺优先用店铺上配置的平台,未配置时按淘宝(数据库店铺通常对应淘宝店铺)
const platform = resolvePlatform(shop.platform || 'TB');
const transaction = await ctx.model.transaction();
try {
@ -412,6 +415,9 @@ class ShopDatabaseSyncService extends Service {
shop_id: shop.id,
subsidiary_id: subsidiaryId,
shop_nick: truncateText(shop.name, 200),
// 电商平台:数据库店铺没有平台回传,按店铺配置(默认淘宝)落库,订单页可直接筛选
source_platform_code: platform.code,
source_platform_name: platform.name,
product_info: buildProductInfo(resolved.map(row => ({
sku_code: row.line.item_code,
outer_item_id: row.line.item_code,

View File

@ -0,0 +1,51 @@
'use strict';
/**
* 回填「数据库店铺拉取的订单」与「手工单」的电商平台(淘宝)。
*
* 背景:订单页新增「电商平台」列与筛选后,这两类订单的 source_platform_code/name 一直是空,
* 列表只能显示 '-'、按平台也筛不出来。口径调整为:
* - 数据库店铺(type=3)拉单:没有平台回传,按店铺配置的平台,未配置时按淘宝;
* - 手工单:默认淘宝。
* 这里只回填「当前为空」的历史行,已有值的(外部打单平台订单)不动。
*/
/** 订单状态表(与 app/lib/orderStatus.js 的 ORDER_STATUS 对齐) */
const STATUS_TABLES = [
'order_status_pending',
'order_status_queued',
'order_status_exception',
'order_status_outsourcing',
'order_status_delivery',
'order_status_void',
'order_status_canceled',
'order_status_history',
];
/** 回填口径:数据库店铺拉取的订单 + 手工建单 */
const BACKFILL_WHERE = "WHERE (source_platform_code IS NULL OR source_platform_code = '') " +
"AND (JSON_UNQUOTE(JSON_EXTRACT(extend_props, '$.source')) = 'shop_database' " +
"OR JSON_UNQUOTE(JSON_EXTRACT(extend_props, '$.labels')) LIKE '%手工单%')";
module.exports = {
async up(queryInterface) {
const transaction = await queryInterface.sequelize.transaction();
try {
for (const table of STATUS_TABLES) {
await queryInterface.sequelize.query(
`UPDATE \`${table}\` SET source_platform_code = 'TB', source_platform_name = '淘宝' ${BACKFILL_WHERE}`,
{ transaction }
);
}
await transaction.commit();
} catch (err) {
await transaction.rollback();
throw err;
}
},
// 回填属数据修正:撤销会重新让这两类订单失去平台信息,故不做反向操作
async down() {
return undefined;
},
};

View File

@ -190,6 +190,13 @@ export interface OrderStatusFilters {
archive_months: number
/** 订单索引表名 */
index_table?: string
/** 电商平台选项:系统预设 ∪ 订单里实际出现过的编码(preset=false 表示未收录进预设) */
platforms?: {
code: string
name: string
count: number
preset: boolean
}[]
shops: { id: number; code?: string; name: string; type?: number; subsidiary_id?: number | null }[]
warehouses: { id: number; code: string; name: string; subsidiary_id?: number | null }[]
}
@ -198,6 +205,8 @@ export interface OrderStatusFilters {
export interface OrderStatusPayload {
/** 来源订单号(上游/平台订单号;与 order_no 至少传一个) */
parent_order_no?: string
/** 电商平台编码(系统预设,如 TB / PDD;手工单默认 TB) */
source_platform_code?: string
order_no?: string
shop_id?: number | null
warehouse_id?: number | null

View File

@ -4,6 +4,7 @@ import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import DataTable from '@/components/DataTable.vue'
import ProductSelect from '@/components/ProductSelect.vue'
import { getExpressCodeOptions, type ExpressCodeOption } from '@/api/customExpressCode'
import { PLATFORM_PRESETS } from '@/constants/platform'
import type { ProductSearchItem } from '@/api/product'
import type { DataItem } from 'element-china-area-data'
import {
@ -56,6 +57,8 @@ const ORDER_TYPES = [
const form = reactive<OrderStatusPayload & { items: OrderItemLine[] }>({
parent_order_no: '',
order_no: '',
/** 电商平台:手工单默认淘宝 */
source_platform_code: 'TB',
warehouse_id: null,
shop_id: null,
order_type: 'JYCK',
@ -166,6 +169,7 @@ function reset() {
Object.assign(form, {
parent_order_no: '',
order_no: '',
source_platform_code: 'TB',
warehouse_id: defaultWarehouseId(),
shop_id: form.shop_id,
order_type: 'JYCK',
@ -212,6 +216,7 @@ async function loadDetail(id: number) {
Object.assign(form, {
parent_order_no: order.parent_order_no || '',
order_no: order.order_no,
source_platform_code: order.source_platform_code || 'TB',
warehouse_id: order.warehouse_id ?? null,
shop_id: order.shop_id ?? null,
order_type: order.order_type || 'JYCK',
@ -498,6 +503,7 @@ function buildPayload(): OrderStatusPayload {
return {
parent_order_no: text(form.parent_order_no),
order_no: isEdit.value ? undefined : text(form.order_no),
source_platform_code: text(form.source_platform_code),
warehouse_id: form.warehouse_id ?? null,
shop_id: form.shop_id ?? null,
order_type: form.order_type || 'JYCK',
@ -639,6 +645,24 @@ async function submit() {
</el-select>
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="电商平台">
<el-select
v-model="form.source_platform_code"
filterable
:disabled="isExternal"
placeholder="默认淘宝"
style="width: 100%"
>
<el-option
v-for="item in PLATFORM_PRESETS"
:key="item.code"
:label="item.name"
:value="item.code"
/>
</el-select>
</el-form-item>
</el-col>
<el-col :span="8">
<el-form-item label="是否紧急">
<el-select v-model="form.is_urgency" style="width: 100%">

View File

@ -203,6 +203,20 @@ async function fetchFilters() {
}
}
/** 电商平台下拉:后端返回「预设 ∪ 订单里实际出现的编码」,缺失时退回本地预设表 */
const platformOptions = computed(() => {
const list = filters.value.platforms
if (Array.isArray(list) && list.length) {
return list.map(item => ({
code: item.code,
label: item.count
? `${item.preset ? item.name : `${item.name}(未收录)`} (${item.count})`
: (item.preset ? item.name : `${item.name}(未收录)`),
}))
}
return PLATFORM_PRESETS.map(item => ({ code: item.code, label: item.name }))
})
/* -------------------- 勾选与批量操作 -------------------- */
const selectedIds = ref<number[]>([])
function onSelectionChange(rows: OrderStatusItem[]) {
@ -846,7 +860,12 @@ onBeforeUnmount(() => {
style="width: 190px"
@change="search"
>
<el-option v-for="opt in PLATFORM_PRESETS" :key="opt.code" :label="opt.name" :value="opt.code" />
<el-option
v-for="opt in platformOptions"
:key="opt.code"
:label="opt.label"
:value="opt.code"
/>
</el-select>
</el-form-item>
<el-form-item v-if="isDelivery" label="外部发货">