oms/backend/app/lib/shopDatabaseFields.js
liufumingren 23a7715dc9 feat(shop): 数据库店铺新增回传单号设置,发货确认后把运单号写回客户库
- shop_database_configs 增加回传设置:方式(不回传/写回字段/写回行)、目标表、运单号字段、定位字段、间隔符、写回行字段映射
- 配置弹窗新增「回传单号」区块:写回字段选表+字段+定位字段+间隔符;写回行按回传值(订单号/运单号/快递公司编码/快递公司名称/回传时间)逐项选列
- 保存时校验回传表与列在客户库中存在,回传行必填项(订单号、运单号)
- 发货确认(WMS 回推运单号)后自动回传:写回字段按间隔符合并写单值,写回行按订单号先删后插保证幂等;失败只记日志不影响发货确认

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-05 16:33:15 +08:00

129 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 数据库店铺(shops.type=3)可映射的 OMS 字段定义
*
* 数据库店铺只支持「明文订单」:源库里的收件人、省市区等字段都是直接可读的值,
* 不需要解密/解析,因此每个字段都要求人工指定对应的源表列名。
* 商品表 / 订单表 / 订单明细表三组字段分别独立映射。
*/
/** 支持的数据库类型(当前仅 MySQL) */
const DB_TYPES = [
{ value: 'mysql', label: 'MySQL' },
];
const DEFAULT_DB_TYPE = 'mysql';
const DEFAULT_PORT = 3306;
/**
* 三张数据集的定义:key 与表 config 列一一对应
* - tableColumn:保存表名的列(shop_database_configs 内)
* - fieldColumn:保存字段映射的列(JSON:{ oms字段: 源列名 })
*/
const DATASETS = {
product: {
label: '商品表',
tableColumn: 'product_table',
fieldColumn: 'product_fields',
fields: [
{ key: 'item_code', label: '商品编码', required: true },
{ key: 'item_name', label: '商品名称', required: true },
{ key: 'sku_property', label: 'SKU规格', required: false },
{ key: 'barcode', label: '条码', required: false },
{ key: 'stock_unit', label: '库存单位', required: false },
{ key: 'price', label: '单价', required: false },
],
},
order: {
label: '订单表',
tableColumn: 'order_table',
fieldColumn: 'order_fields',
fields: [
{ key: 'order_no', label: '订单号', required: true },
{ key: 'place_order_time', label: '下单时间', required: false },
{ key: 'receiver', label: '收件人', required: true },
{ key: 'mobile', label: '手机号', required: false },
{ key: 'tel', label: '电话', required: false },
{ key: 'province', label: '省', required: true },
{ key: 'city', label: '市', required: true },
{ key: 'area', label: '区/县', required: true },
{ key: 'town', label: '街道/镇', required: false },
{ key: 'detail_address', label: '详细地址', required: true },
{ key: 'zip_code', label: '邮编', required: false },
{ key: 'total_amount', label: '订单金额', required: false },
{ key: 'freight', label: '运费', required: false },
{ key: 'buyer_message', label: '买家留言', required: false },
{ key: 'seller_message', label: '卖家留言', required: false },
],
},
order_item: {
label: '订单明细表',
tableColumn: 'order_item_table',
fieldColumn: 'order_item_fields',
fields: [
{ key: 'order_no', label: '订单号(关联订单表)', required: true },
{ key: 'item_code', label: '商品编码', required: true },
{ key: 'item_name', label: '商品名称', required: false },
{ key: 'sku_property', label: 'SKU规格', required: false },
{ key: 'quantity', label: '数量', required: true },
{ key: 'price', label: '单价', required: false },
],
},
};
/**
* 回传单号(运单号写回客户库)的两种方式
* - field:把运单号写回某个表的一个字段(多包裹用间隔符合并成一个字符串)
* - row:把运单号写回某个表的一行(多包裹一个运单一行)
*/
const CALLBACK_MODES = [
{ value: 0, label: '不回传' },
{ value: 1, label: '写回字段' },
{ value: 2, label: '写回行' },
];
const CALLBACK_MODE_OFF = 0;
const CALLBACK_MODE_FIELD = 1;
const CALLBACK_MODE_ROW = 2;
/** 写回字段默认的多包裹间隔符 */
const DEFAULT_CALLBACK_SEPARATOR = ',';
/**
* 「写回行」可映射的 OMS 回传值:每项都要指定写进源表的哪一列
* - order_no:源订单号(拉单时写入的 external_order_id),同时作为定位已有行的条件
*/
const CALLBACK_FIELDS = [
{ key: 'order_no', label: '订单号(定位行)', required: true },
{ key: 'waybill_no', label: '运单号', required: true },
{ key: 'express_code', label: '快递公司编码', required: false },
{ key: 'express_name', label: '快递公司名称', required: false },
{ key: 'synced_at', label: '回传时间', required: false },
];
/**
* 供接口返回给前端的字段定义(含必填标记与保存列名,前端据此渲染映射表单)
* @return {Array} 三组字段定义
*/
function datasetDefs() {
return Object.entries(DATASETS).map(([ key, def ]) => ({
key,
label: def.label,
table_column: def.tableColumn,
field_column: def.fieldColumn,
fields: def.fields.map(field => ({ key: field.key, label: field.label, required: field.required })),
}));
}
module.exports = {
DB_TYPES,
DEFAULT_DB_TYPE,
DEFAULT_PORT,
DATASETS,
datasetDefs,
CALLBACK_MODES,
CALLBACK_MODE_OFF,
CALLBACK_MODE_FIELD,
CALLBACK_MODE_ROW,
DEFAULT_CALLBACK_SEPARATOR,
CALLBACK_FIELDS,
};