feat(shop): 数据库店铺商品表/订单表支持拉取状态过滤(可填多个状态值)
- shop_database_configs 增加 product_filter_column/values、order_filter_column/values - 配置弹窗在商品表、订单表下新增「状态字段 + 状态值」:状态字段选源表列,状态值可填多个(回车追加) - 拉取时按状态列 IN (多个状态值) 过滤,订单表与时间窗条件同时生效;未配置状态列表示不过滤 - 保存时校验状态列存在、填了状态列必须至少填一个状态值(最多 100 个) - 修复 DATASETS 数据集缺失 key 导致过滤校验窜到订单明细表的问题 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
873527d556
commit
797bbbcd82
@ -1230,6 +1230,10 @@ module.exports = {
|
||||
product_fields: { type: 'shopDatabaseFieldMapping', description: '商品表字段映射' },
|
||||
order_fields: { type: 'shopDatabaseFieldMapping', description: '订单表字段映射' },
|
||||
order_item_fields: { type: 'shopDatabaseFieldMapping', description: '订单明细表字段映射' },
|
||||
product_filter_column: { type: 'string', description: '商品表拉取过滤:状态列名(空表示不过滤)' },
|
||||
product_filter_values: { type: 'array', itemType: 'string', description: '商品表拉取过滤:允许的状态值(可多个)' },
|
||||
order_filter_column: { type: 'string', description: '订单表拉取过滤:状态列名(空表示不过滤)' },
|
||||
order_filter_values: { type: 'array', itemType: 'string', description: '订单表拉取过滤:允许的状态值(可多个)' },
|
||||
callback_mode: { type: 'integer', description: '回传单号方式:0不回传 1写回字段 2写回行' },
|
||||
callback_table: { type: 'string', description: '回传单号写回的目标表名' },
|
||||
callback_field: { type: 'string', description: '写回字段方式:运单号写入的列名' },
|
||||
@ -1266,6 +1270,10 @@ module.exports = {
|
||||
label: { type: 'string', description: '数据集名称(商品表 / 订单表 / 订单明细表)' },
|
||||
table_column: { type: 'string', description: '保存表名的配置列' },
|
||||
field_column: { type: 'string', description: '保存字段映射的配置列' },
|
||||
filter_column: { type: 'string', description: '保存拉取过滤状态列的配置列(仅商品表/订单表)' },
|
||||
filter_values: { type: 'string', description: '保存拉取过滤状态值的配置列(仅商品表/订单表)' },
|
||||
filter_column_label: { type: 'string', description: '状态列标题' },
|
||||
filter_values_label: { type: 'string', description: '状态值标题' },
|
||||
fields: { type: 'array', itemType: 'shopDatabaseDatasetField', description: 'OMS 侧字段定义' },
|
||||
},
|
||||
shopDatabaseDatasetField: {
|
||||
@ -1287,6 +1295,10 @@ module.exports = {
|
||||
product_fields: { type: 'shopDatabaseFieldMapping', description: '商品表字段映射' },
|
||||
order_fields: { type: 'shopDatabaseFieldMapping', description: '订单表字段映射' },
|
||||
order_item_fields: { type: 'shopDatabaseFieldMapping', description: '订单明细表字段映射' },
|
||||
product_filter_column: { type: 'string', description: '商品表拉取过滤:状态列名(空表示不过滤)' },
|
||||
product_filter_values: { type: 'array', itemType: 'string', description: '商品表拉取过滤:允许的状态值(可多个)' },
|
||||
order_filter_column: { type: 'string', description: '订单表拉取过滤:状态列名(空表示不过滤)' },
|
||||
order_filter_values: { type: 'array', itemType: 'string', description: '订单表拉取过滤:允许的状态值(可多个)' },
|
||||
callback_mode: { type: 'integer', description: '回传单号方式:0不回传 1写回字段 2写回行' },
|
||||
callback_table: { type: 'string', description: '回传单号写回的目标表名' },
|
||||
callback_field: { type: 'string', description: '写回字段方式:运单号写入的列名' },
|
||||
|
||||
@ -129,6 +129,10 @@ class ShopDatabaseController extends Controller {
|
||||
product_fields: plain.product_fields || {},
|
||||
order_fields: plain.order_fields || {},
|
||||
order_item_fields: plain.order_item_fields || {},
|
||||
product_filter_column: plain.product_filter_column || '',
|
||||
product_filter_values: Array.isArray(plain.product_filter_values) ? plain.product_filter_values : [],
|
||||
order_filter_column: plain.order_filter_column || '',
|
||||
order_filter_values: Array.isArray(plain.order_filter_values) ? plain.order_filter_values : [],
|
||||
callback_mode: plain.callback_mode == null ? CALLBACK_MODE_OFF : Number(plain.callback_mode),
|
||||
callback_table: plain.callback_table || '',
|
||||
callback_field: plain.callback_field || '',
|
||||
@ -208,6 +212,28 @@ class ShopDatabaseController extends Controller {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并整理拉取过滤条件(状态列 + 允许多个状态值)
|
||||
* @param {Object} body 请求体
|
||||
* @param {Object} dataset DATASETS 中的定义
|
||||
* @return {{error?: string, value?: Object}} 校验结果
|
||||
*/
|
||||
resolveFilter(body, dataset) {
|
||||
if (!dataset.filter) return { value: {} };
|
||||
const input = body || {};
|
||||
const column = String(input[dataset.filter.columnField] || '').trim();
|
||||
const empty = { [dataset.filter.columnField]: '', [dataset.filter.valuesField]: [] };
|
||||
if (!column) return { value: empty };
|
||||
if (!SAFE_IDENTIFIER.test(column)) return { error: `${dataset.label}:状态字段名不合法` };
|
||||
|
||||
const raw = input[dataset.filter.valuesField];
|
||||
const list = Array.isArray(raw) ? raw : String(raw == null ? '' : raw).split(/[,,\s]+/);
|
||||
const values = [ ...new Set(list.map(value => String(value == null ? '' : value).trim()).filter(Boolean)) ];
|
||||
if (!values.length) return { error: `${dataset.label}:请填写状态值(可填多个)` };
|
||||
if (values.length > 100) return { error: `${dataset.label}:状态值最多 100 个` };
|
||||
return { value: { [dataset.filter.columnField]: column, [dataset.filter.valuesField]: values } };
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并整理「回传单号」设置
|
||||
* @param {Object} body 请求体
|
||||
@ -269,9 +295,10 @@ class ShopDatabaseController extends Controller {
|
||||
* @param {Object} tables 三张数据集选表结果 { product_table: 表名 ... }
|
||||
* @param {Object} mappings 三张数据集映射 { product_fields: {...} ... }
|
||||
* @param {Object} [callback] 回传单号设置
|
||||
* @param {Object} [filters] 拉取过滤:{ 数据集key: 状态列名 }
|
||||
* @return {Promise<{ok: boolean, connected: boolean, message?: string}>} 校验结果(connected 表示是否连上了库)
|
||||
*/
|
||||
async verifyAgainstDatabase(params, tables, mappings, callback) {
|
||||
async verifyAgainstDatabase(params, tables, mappings, callback, filters) {
|
||||
let conn = null;
|
||||
try {
|
||||
conn = await openConnection(params);
|
||||
@ -302,6 +329,15 @@ class ShopDatabaseController extends Controller {
|
||||
};
|
||||
}
|
||||
}
|
||||
// 拉取过滤的状态列同样要存在于该表
|
||||
const filterColumn = filters && filters[dataset.key];
|
||||
if (filterColumn && !columns.has(filterColumn)) {
|
||||
return {
|
||||
ok: false,
|
||||
connected: true,
|
||||
message: `${dataset.label}:状态字段 ${filterColumn} 不存在`,
|
||||
};
|
||||
}
|
||||
}
|
||||
// 回传单号:目标表与列同样要校验存在
|
||||
if (callback && Number(callback.callback_mode) && callback.callback_table) {
|
||||
@ -362,6 +398,8 @@ class ShopDatabaseController extends Controller {
|
||||
|
||||
const tables = {};
|
||||
const mappings = {};
|
||||
const filters = {};
|
||||
const filterFields = {};
|
||||
for (const dataset of Object.values(DATASETS)) {
|
||||
const table = String(body[dataset.tableColumn] || '').trim();
|
||||
if (!table) {
|
||||
@ -383,6 +421,18 @@ class ShopDatabaseController extends Controller {
|
||||
return;
|
||||
}
|
||||
mappings[dataset.fieldColumn] = this.cleanMapping(body[dataset.fieldColumn]);
|
||||
|
||||
// 拉取过滤(商品表 / 订单表可配状态列 + 多个状态值,订单明细表不参与)
|
||||
const filter = this.resolveFilter(body, dataset);
|
||||
if (filter.error) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: filter.error };
|
||||
return;
|
||||
}
|
||||
Object.assign(filterFields, filter.value);
|
||||
if (dataset.filter && filter.value[dataset.filter.columnField]) {
|
||||
filters[dataset.key] = filter.value[dataset.filter.columnField];
|
||||
}
|
||||
}
|
||||
|
||||
// 回传单号设置(发货确认后把运单号写回客户库)
|
||||
@ -395,7 +445,7 @@ class ShopDatabaseController extends Controller {
|
||||
|
||||
// 能连上就校验表/列是否真实存在:连上但校验不过直接拒绝(避免存下跑不通的映射);
|
||||
// 连不上时仍保存连接信息,便于先存配置再排查网络
|
||||
const verified = await this.verifyAgainstDatabase(params, tables, mappings, callback.value);
|
||||
const verified = await this.verifyAgainstDatabase(params, tables, mappings, callback.value, filters);
|
||||
if (verified.connected && !verified.ok) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: verified.message };
|
||||
@ -412,6 +462,7 @@ class ShopDatabaseController extends Controller {
|
||||
password: params.password,
|
||||
...tables,
|
||||
...mappings,
|
||||
...filterFields,
|
||||
...callback.value,
|
||||
last_test_at: new Date(),
|
||||
last_test_success: verified.ok ? 1 : 0,
|
||||
|
||||
@ -19,12 +19,20 @@ const DEFAULT_PORT = 3306;
|
||||
* 三张数据集的定义:key 与表 config 列一一对应
|
||||
* - tableColumn:保存表名的列(shop_database_configs 内)
|
||||
* - fieldColumn:保存字段映射的列(JSON:{ oms字段: 源列名 })
|
||||
* - filter:拉取过滤条件(状态字段 + 多个状态值),未配置表示不过滤
|
||||
*/
|
||||
const DATASETS = {
|
||||
product: {
|
||||
key: 'product',
|
||||
label: '商品表',
|
||||
tableColumn: 'product_table',
|
||||
fieldColumn: 'product_fields',
|
||||
filter: {
|
||||
columnField: 'product_filter_column',
|
||||
valuesField: 'product_filter_values',
|
||||
columnLabel: '状态字段',
|
||||
valuesLabel: '状态值',
|
||||
},
|
||||
fields: [
|
||||
{ key: 'item_code', label: '商品编码', required: true },
|
||||
{ key: 'item_name', label: '商品名称', required: true },
|
||||
@ -35,9 +43,16 @@ const DATASETS = {
|
||||
],
|
||||
},
|
||||
order: {
|
||||
key: 'order',
|
||||
label: '订单表',
|
||||
tableColumn: 'order_table',
|
||||
fieldColumn: 'order_fields',
|
||||
filter: {
|
||||
columnField: 'order_filter_column',
|
||||
valuesField: 'order_filter_values',
|
||||
columnLabel: '状态字段',
|
||||
valuesLabel: '状态值',
|
||||
},
|
||||
fields: [
|
||||
{ key: 'order_no', label: '订单号', required: true },
|
||||
{ key: 'place_order_time', label: '下单时间', required: false },
|
||||
@ -57,6 +72,7 @@ const DATASETS = {
|
||||
],
|
||||
},
|
||||
order_item: {
|
||||
key: 'order_item',
|
||||
label: '订单明细表',
|
||||
tableColumn: 'order_item_table',
|
||||
fieldColumn: 'order_item_fields',
|
||||
@ -104,11 +120,15 @@ const CALLBACK_FIELDS = [
|
||||
* @return {Array} 三组字段定义
|
||||
*/
|
||||
function datasetDefs() {
|
||||
return Object.entries(DATASETS).map(([ key, def ]) => ({
|
||||
key,
|
||||
return Object.values(DATASETS).map(def => ({
|
||||
key: def.key,
|
||||
label: def.label,
|
||||
table_column: def.tableColumn,
|
||||
field_column: def.fieldColumn,
|
||||
filter_column: def.filter ? def.filter.columnField : '',
|
||||
filter_values: def.filter ? def.filter.valuesField : '',
|
||||
filter_column_label: def.filter ? def.filter.columnLabel : '',
|
||||
filter_values_label: def.filter ? def.filter.valuesLabel : '',
|
||||
fields: def.fields.map(field => ({ key: field.key, label: field.label, required: field.required })),
|
||||
}));
|
||||
}
|
||||
|
||||
@ -28,6 +28,10 @@ module.exports = app => {
|
||||
product_fields: { type: JSONTYPE, comment: '商品表字段映射:{ oms字段: 源列名 }' },
|
||||
order_fields: { type: JSONTYPE, comment: '订单表字段映射:{ oms字段: 源列名 }' },
|
||||
order_item_fields: { type: JSONTYPE, comment: '订单明细表字段映射:{ oms字段: 源列名 }' },
|
||||
product_filter_column: { type: STRING(100), comment: '商品表拉取过滤:状态列名(空表示不过滤)' },
|
||||
product_filter_values: { type: JSONTYPE, comment: '商品表拉取过滤:允许的状态值(数组)' },
|
||||
order_filter_column: { type: STRING(100), comment: '订单表拉取过滤:状态列名(空表示不过滤)' },
|
||||
order_filter_values: { type: JSONTYPE, comment: '订单表拉取过滤:允许的状态值(数组)' },
|
||||
callback_mode: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '回传单号方式:0不回传 1写回字段 2写回行' },
|
||||
callback_table: { type: STRING(100), comment: '回传单号的目标表名' },
|
||||
callback_field: { type: STRING(100), comment: '写回字段方式:运单号写入的列名' },
|
||||
|
||||
@ -131,10 +131,34 @@ class ShopDatabaseSyncService extends Service {
|
||||
return rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取过滤条件转 WHERE 片段(状态列 + 多个状态值)
|
||||
* @param {Object} conn 连接
|
||||
* @param {Object} config 配置实例
|
||||
* @param {Object} dataset DATASETS 中的定义
|
||||
* @param {Array<string>} [wheres] 已有条件(会被追加)
|
||||
* @param {Array} [values] 已有参数
|
||||
* @return {{wheres: Array<string>, values: Array}} 过滤条件与参数
|
||||
*/
|
||||
buildFilter(conn, config, dataset, wheres, values) {
|
||||
const list = (wheres || []).slice();
|
||||
const params = (values || []).slice();
|
||||
if (!dataset.filter) return { wheres: list, values: params };
|
||||
const column = config[dataset.filter.columnField];
|
||||
const raw = config[dataset.filter.valuesField];
|
||||
const filterValues = (Array.isArray(raw) ? raw : [])
|
||||
.map(value => String(value == null ? '' : value).trim())
|
||||
.filter(Boolean);
|
||||
if (!column || !filterValues.length) return { wheres: list, values: params };
|
||||
list.push(`${conn.escapeId(column)} IN (${filterValues.map(() => '?').join(', ')})`);
|
||||
params.push(...filterValues);
|
||||
return { wheres: list, values: params };
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取店铺商品:按商品表映射全量读取,写入 / 更新店铺商品
|
||||
* @param {Object} shop 店铺实例
|
||||
* @return {Promise<{total: number, created: number, updated: number}>} 拉取统计
|
||||
* @return {Promise<{total: number, created: number, updated: number, filtered: boolean}>} 拉取统计
|
||||
*/
|
||||
async pullProducts(shop) {
|
||||
const { ctx } = this;
|
||||
@ -144,7 +168,15 @@ class ShopDatabaseSyncService extends Service {
|
||||
try {
|
||||
conn = await openConnection(params);
|
||||
const dataset = DATASETS.product;
|
||||
const rows = await this.selectMapped(conn, config.product_table, config.product_fields, dataset);
|
||||
const filter = this.buildFilter(conn, config, dataset);
|
||||
const rows = await this.selectMapped(
|
||||
conn,
|
||||
config.product_table,
|
||||
config.product_fields,
|
||||
dataset,
|
||||
filter.wheres.length ? filter.wheres.join(' AND ') : '',
|
||||
filter.values
|
||||
);
|
||||
|
||||
const existRows = await ctx.model.ShopProduct.findAll({
|
||||
where: { principal_id: shop.principal_id, shop_id: shop.id },
|
||||
@ -188,7 +220,7 @@ class ShopDatabaseSyncService extends Service {
|
||||
}
|
||||
}
|
||||
await ctx.service.cache.invalidateShop(shop.id);
|
||||
return { total: rows.length, created, updated };
|
||||
return { total: rows.length, created, updated, filtered: filter.wheres.length > 0 };
|
||||
} finally {
|
||||
await closeQuietly(conn);
|
||||
}
|
||||
@ -211,21 +243,22 @@ class ShopDatabaseSyncService extends Service {
|
||||
try {
|
||||
conn = await openConnection(params);
|
||||
|
||||
// 下单时间可映射时按时间窗过滤,减少读取量
|
||||
// 下单时间可映射时按时间窗过滤,减少读取量;订单状态按配置的状态列过滤(可多个值)
|
||||
const timeColumn = (config.order_fields || {}).place_order_time;
|
||||
let where = '';
|
||||
const wheres = [];
|
||||
const values = [];
|
||||
if (timeColumn && options.startTime && options.endTime) {
|
||||
where = `${conn.escapeId(timeColumn)} >= ? AND ${conn.escapeId(timeColumn)} <= ?`;
|
||||
wheres.push(`${conn.escapeId(timeColumn)} >= ? AND ${conn.escapeId(timeColumn)} <= ?`);
|
||||
values.push(options.startTime, options.endTime);
|
||||
}
|
||||
const filter = this.buildFilter(conn, config, orderDataset, wheres, values);
|
||||
const orderRows = await this.selectMapped(
|
||||
conn,
|
||||
config.order_table,
|
||||
config.order_fields,
|
||||
orderDataset,
|
||||
where,
|
||||
values
|
||||
filter.wheres.length ? filter.wheres.join(' AND ') : '',
|
||||
filter.values
|
||||
);
|
||||
|
||||
const itemRows = await this.selectMapped(
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 数据库店铺(shops.type=3)商品表 / 订单表增加「拉取过滤」设置
|
||||
*
|
||||
* 客户库的商品表、订单表通常混有无效数据(下架商品、取消/关闭的订单),
|
||||
* 拉取时需要按状态列过滤,且状态值允许多个:
|
||||
* - product_filter_column / product_filter_values:商品表状态列 + 状态值(数组)
|
||||
* - order_filter_column / order_filter_values:订单表状态列 + 状态值(数组)
|
||||
* 未配置状态列表示不过滤。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('shop_database_configs', 'product_filter_column', {
|
||||
type: Sequelize.STRING(100),
|
||||
allowNull: true,
|
||||
comment: '商品表拉取过滤:状态列名(空表示不过滤)',
|
||||
});
|
||||
await queryInterface.addColumn('shop_database_configs', 'product_filter_values', {
|
||||
type: Sequelize.JSON,
|
||||
allowNull: true,
|
||||
comment: '商品表拉取过滤:允许的状态值(数组,可多个)',
|
||||
});
|
||||
await queryInterface.addColumn('shop_database_configs', 'order_filter_column', {
|
||||
type: Sequelize.STRING(100),
|
||||
allowNull: true,
|
||||
comment: '订单表拉取过滤:状态列名(空表示不过滤)',
|
||||
});
|
||||
await queryInterface.addColumn('shop_database_configs', 'order_filter_values', {
|
||||
type: Sequelize.JSON,
|
||||
allowNull: true,
|
||||
comment: '订单表拉取过滤:允许的状态值(数组,可多个)',
|
||||
});
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
for (const column of [
|
||||
'product_filter_column',
|
||||
'product_filter_values',
|
||||
'order_filter_column',
|
||||
'order_filter_values',
|
||||
]) {
|
||||
await queryInterface.removeColumn('shop_database_configs', column);
|
||||
}
|
||||
},
|
||||
};
|
||||
@ -22,6 +22,14 @@ export interface ShopDatabaseDataset {
|
||||
table_column: string
|
||||
/** 保存字段映射的配置字段名 */
|
||||
field_column: string
|
||||
/** 保存拉取过滤状态列的配置字段名(仅商品表/订单表) */
|
||||
filter_column: string
|
||||
/** 保存拉取过滤状态值的配置字段名(仅商品表/订单表) */
|
||||
filter_values: string
|
||||
/** 状态列标题 */
|
||||
filter_column_label: string
|
||||
/** 状态值标题 */
|
||||
filter_values_label: string
|
||||
fields: ShopDatabaseDatasetField[]
|
||||
}
|
||||
|
||||
@ -50,6 +58,14 @@ export interface ShopDatabaseConfig {
|
||||
product_fields: ShopDatabaseFieldMapping
|
||||
order_fields: ShopDatabaseFieldMapping
|
||||
order_item_fields: ShopDatabaseFieldMapping
|
||||
/** 商品表拉取过滤:状态列名(空表示不过滤) */
|
||||
product_filter_column: string
|
||||
/** 商品表拉取过滤:允许的状态值(可多个) */
|
||||
product_filter_values: string[]
|
||||
/** 订单表拉取过滤:状态列名(空表示不过滤) */
|
||||
order_filter_column: string
|
||||
/** 订单表拉取过滤:允许的状态值(可多个) */
|
||||
order_filter_values: string[]
|
||||
/** 回传单号方式:0不回传 1写回字段 2写回行 */
|
||||
callback_mode: number
|
||||
callback_table: string
|
||||
@ -80,6 +96,10 @@ export interface ShopDatabaseSavePayload extends ShopDatabaseConnectionPayload {
|
||||
product_fields?: ShopDatabaseFieldMapping
|
||||
order_fields?: ShopDatabaseFieldMapping
|
||||
order_item_fields?: ShopDatabaseFieldMapping
|
||||
product_filter_column?: string
|
||||
product_filter_values?: string[]
|
||||
order_filter_column?: string
|
||||
order_filter_values?: string[]
|
||||
callback_mode?: number
|
||||
callback_table?: string
|
||||
callback_field?: string
|
||||
|
||||
@ -285,6 +285,8 @@ const dbTables = ref<ShopDatabaseTable[]>([])
|
||||
const dbColumns = ref<Record<string, ShopDatabaseColumn[]>>({})
|
||||
/** 字段映射:数据集 key → { OMS字段: 源列名 } */
|
||||
const dbMappings = reactive<Record<string, ShopDatabaseFieldMapping>>({})
|
||||
/** 拉取过滤:数据集 key → { column: 状态列名, values: 状态值数组 } */
|
||||
const dbFilters = reactive<Record<string, { column: string; values: string[] }>>({})
|
||||
/** 最近一次保存/校验结果 */
|
||||
const dbLastResult = ref<{ success: boolean | null; message: string; at: string }>({
|
||||
success: null,
|
||||
@ -344,6 +346,15 @@ async function openDatabaseConfig(row: { id: number; name: string }) {
|
||||
for (const dataset of datasets) {
|
||||
const saved = (config as unknown as Record<string, ShopDatabaseFieldMapping>)[dataset.field_column] || {}
|
||||
dbMappings[dataset.key] = { ...saved }
|
||||
// 拉取过滤(商品表 / 订单表)
|
||||
if (dataset.filter_column) {
|
||||
const rawColumn = (config as unknown as Record<string, unknown>)[dataset.filter_column]
|
||||
const rawValues = (config as unknown as Record<string, unknown>)[dataset.filter_values]
|
||||
dbFilters[dataset.key] = {
|
||||
column: typeof rawColumn === 'string' ? rawColumn : '',
|
||||
values: Array.isArray(rawValues) ? rawValues.map(v => String(v)) : [],
|
||||
}
|
||||
}
|
||||
}
|
||||
// 回传单号设置
|
||||
dbCallback.mode = Number(config.callback_mode || 0)
|
||||
@ -424,9 +435,37 @@ function setMapping(datasetKey: string, fieldKey: string, value: string | null)
|
||||
/** 选表变化:清空该表已映射的列名并拉取字段 */
|
||||
async function onTableChange(dataset: ShopDatabaseDataset) {
|
||||
dbMappings[dataset.key] = {}
|
||||
if (dataset.filter_column && dbFilters[dataset.key]) {
|
||||
dbFilters[dataset.key].column = ''
|
||||
dbFilters[dataset.key].values = []
|
||||
}
|
||||
await ensureColumns([ tableOf(dataset) ])
|
||||
}
|
||||
|
||||
/** 拉取过滤:当前状态列 */
|
||||
function filterColumnOf(dataset: ShopDatabaseDataset) {
|
||||
return (dbFilters[dataset.key] || {}).column || ''
|
||||
}
|
||||
|
||||
/** 拉取过滤:当前状态值 */
|
||||
function filterValuesOf(dataset: ShopDatabaseDataset) {
|
||||
return (dbFilters[dataset.key] || {}).values || []
|
||||
}
|
||||
|
||||
/** 拉取过滤:选状态列(切换列后清空已填状态值,避免语义混淆) */
|
||||
async function setFilterColumn(dataset: ShopDatabaseDataset, value: string | null) {
|
||||
if (!dbFilters[dataset.key]) dbFilters[dataset.key] = { column: '', values: [] }
|
||||
dbFilters[dataset.key].column = value || ''
|
||||
dbFilters[dataset.key].values = []
|
||||
await ensureColumns([ tableOf(dataset) ])
|
||||
}
|
||||
|
||||
/** 拉取过滤:设置状态值(可多个) */
|
||||
function setFilterValues(dataset: ShopDatabaseDataset, values: string[]) {
|
||||
if (!dbFilters[dataset.key]) dbFilters[dataset.key] = { column: '', values: [] }
|
||||
dbFilters[dataset.key].values = (values || []).map(v => String(v).trim()).filter(Boolean)
|
||||
}
|
||||
|
||||
/** 字段映射下拉选项 */
|
||||
function columnsOf(dataset: ShopDatabaseDataset): ShopDatabaseColumn[] {
|
||||
const table = tableOf(dataset)
|
||||
@ -522,12 +561,28 @@ async function handleSaveDatabaseConfig() {
|
||||
ElMessage.warning(`${dataset.label}:请完成必填字段映射(${missing.join('、')})`)
|
||||
return
|
||||
}
|
||||
// 拉取过滤:填了状态列就必须至少填一个状态值
|
||||
if (dataset.filter_column) {
|
||||
const filter = dbFilters[dataset.key]
|
||||
if (filter?.column && !(filter.values || []).length) {
|
||||
ElMessage.warning(`${dataset.label}:请填写${dataset.filter_values_label || '状态值'}(可填多个)`)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
const callbackError = validateCallbackSetting()
|
||||
if (callbackError) {
|
||||
ElMessage.warning(callbackError)
|
||||
return
|
||||
}
|
||||
// 拉取过滤按数据集定义动态取键(product / order 的 filter_column 不同)
|
||||
const filterPayload: Record<string, unknown> = {}
|
||||
for (const dataset of dbDatasets.value) {
|
||||
if (!dataset.filter_column) continue
|
||||
const filter = dbFilters[dataset.key]
|
||||
filterPayload[dataset.filter_column] = filter?.column || ''
|
||||
filterPayload[dataset.filter_values] = filter?.column ? filter.values : []
|
||||
}
|
||||
dbConfigSaving.value = true
|
||||
try {
|
||||
const res = await saveShopDatabaseConfig(dbConfigShop.value.id, {
|
||||
@ -538,6 +593,7 @@ async function handleSaveDatabaseConfig() {
|
||||
product_fields: dbMappings.product || {},
|
||||
order_fields: dbMappings.order || {},
|
||||
order_item_fields: dbMappings.order_item || {},
|
||||
...filterPayload,
|
||||
callback_mode: dbCallback.mode,
|
||||
callback_table: dbCallback.mode ? dbCallback.table : '',
|
||||
callback_field: dbCallback.mode === 1 ? dbCallback.field : '',
|
||||
@ -1160,6 +1216,40 @@ onMounted(() => {
|
||||
/>
|
||||
</el-select>
|
||||
</div>
|
||||
<div v-if="dataset.filter_column" class="db-dataset-filter">
|
||||
<span class="db-filter-label">{{ dataset.filter_column_label || '状态字段' }}</span>
|
||||
<el-select
|
||||
:model-value="filterColumnOf(dataset)"
|
||||
placeholder="不选表示不过滤"
|
||||
filterable
|
||||
clearable
|
||||
:disabled="!tableOf(dataset)"
|
||||
style="width: 220px"
|
||||
@change="(v: string | null) => setFilterColumn(dataset, v)"
|
||||
>
|
||||
<el-option
|
||||
v-for="col in columnsOf(dataset)"
|
||||
:key="col.name"
|
||||
:label="col.comment ? `${col.name}(${col.comment})` : col.name"
|
||||
:value="col.name"
|
||||
/>
|
||||
</el-select>
|
||||
<span class="db-filter-label">{{ dataset.filter_values_label || '状态值' }}</span>
|
||||
<el-select
|
||||
:model-value="filterValuesOf(dataset)"
|
||||
placeholder="输入后回车,可填多个"
|
||||
multiple
|
||||
filterable
|
||||
allow-create
|
||||
default-first-option
|
||||
:disabled="!filterColumnOf(dataset)"
|
||||
style="width: 320px"
|
||||
@change="(v: string[]) => setFilterValues(dataset, v)"
|
||||
>
|
||||
<el-option v-for="v in filterValuesOf(dataset)" :key="v" :label="v" :value="v" />
|
||||
</el-select>
|
||||
<span class="db-conn-tip">只拉取该字段值命中的数据,留空表示不过滤</span>
|
||||
</div>
|
||||
<el-table :data="dataset.fields" size="small" border class="db-map-table">
|
||||
<el-table-column label="OMS 字段" width="200">
|
||||
<template #default="{ row }">
|
||||
@ -1404,6 +1494,17 @@ onMounted(() => {
|
||||
gap: 10px;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.db-dataset-filter {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
.db-filter-label {
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-regular);
|
||||
}
|
||||
.db-dataset-title {
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user