后端能力: - 接入店管家订单接口(/NewOrder/List),明细归一化后按店铺商品匹配系统商品/组合商品 - 新增 service/externalPrintOrder:手动与定时拉单共用入库逻辑,同时写入待审核状态分表 - 新增 schedule/fetchExternalShopOrders:按店铺配置的间隔自动拉单(最小 3 分钟,起点重叠 10 分钟) - 新增 lib/orderItemExpand + service/orderItemResolve:平台行与最小单位行互转; 绑定组合商品时按「数量 = 平台数量 x 组合内数量、单价取子商品零售价」拆成子商品行,解绑可还原 - 明细新增 platform_snapshot,保留平台原样(编码/名称/规格/图片/原行号/原数量/原单价) - 店铺商品列表支持 has_unbound_order 筛选,订单列表支持 exclude_unbound(均已同步导出定义) - 店铺商品只读化:移除新增/编辑/删除动作,仅保留查询与绑定; 同步商品时平台已不存在的商品自动清理(未绑定且未被订单引用直接删除,已绑定或被引用仅下架) - 新增 scripts/rebuild-order-items 存量明细重建脚本,新增 orderItemExpand 单测(8 项) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1078 lines
45 KiB
JavaScript
1078 lines
45 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 导出定义与 SQL 构造(纯函数,便于单测与复用)
|
||
*
|
||
* - EXPORT_TYPES:导出类型注册表,每种类型声明 label(中文名)、filters(可传筛选字段,
|
||
* 前端据此动态渲染表单、后端据此做参数白名单)与 build(构造参数化 SELECT)。
|
||
* - buildExportSql(type, filters, principalId, userId):按类型与筛选条件构造「参数化」SELECT 语句,
|
||
* 返回 { sql, countSql, columns, replacements };所有用户输入经 ? 占位参数传递,避免 SQL 注入。
|
||
* - columns:每列 { key(结果字段), header(XLSX 表头), expr?(SELECT 表达式,缺省用 key), format?(原始值→展示值) }。
|
||
* - 主体隔离:除「主体管理」按登录账号可见主体隔离外,其余类型一律 principal_id = 当前主体。
|
||
*/
|
||
|
||
const {
|
||
ORDER_STATUS,
|
||
ORDER_STATUS_ORDER,
|
||
ORDER_STATUS_LABELS,
|
||
ORDER_TABLE_BY_STATUS,
|
||
} = require('../lib/orderStatus');
|
||
const { BILL_STATUS_LABELS, BILL_STATUS_ORDER } = require('../lib/billStatus');
|
||
const { INBOUND_ORDER_TYPES, OUTBOUND_ORDER_TYPES } = require('../lib/wms/orderTypes');
|
||
const { BIZ_TYPES } = require('./stockRecord');
|
||
|
||
/* ==================== 选项常量(标签一律取自各业务模块的单一来源) ==================== */
|
||
|
||
/** 任务状态中文(与 export_tasks.status 对齐) */
|
||
const STATUS_LABELS = { 0: '待处理', 1: '处理中', 2: '已完成', 3: '失败' };
|
||
|
||
/** 单据状态选项(入库单/出库单通用) */
|
||
const BILL_STATUS_OPTIONS = BILL_STATUS_ORDER.map(value => ({ label: BILL_STATUS_LABELS[value], value }));
|
||
|
||
/** 订单流程状态选项(待审核/待推送/异常/委外中/配送/已作废/已取消/历史) */
|
||
const ORDER_STATUS_OPTIONS = ORDER_STATUS_ORDER.map(value => ({ label: ORDER_STATUS_LABELS[value], value }));
|
||
|
||
/** 库存类型选项 */
|
||
const ITEM_TYPE_OPTIONS = [
|
||
{ label: '正品(ZC)', value: 'ZC' },
|
||
{ label: '残次品(CC)', value: 'CC' },
|
||
];
|
||
|
||
/** 出入方向 / 单据类型选项 */
|
||
const DIRECTION_OPTIONS = [
|
||
{ label: '入库方向', value: 1 },
|
||
{ label: '出库方向', value: 2 },
|
||
];
|
||
const BILL_TYPE_OPTIONS = [
|
||
{ label: '入库单', value: 1 },
|
||
{ label: '出库单', value: 2 },
|
||
{ label: '发货单/订单', value: 3 },
|
||
];
|
||
|
||
/** 二值选项:启用状态 / 数据来源 / 是否已推送 / 是否成功 */
|
||
const ENABLE_OPTIONS = [
|
||
{ label: '启用', value: 1 },
|
||
{ label: '禁用', value: 0 },
|
||
];
|
||
const IS_SYSTEM_OPTIONS = [
|
||
{ label: '系统数据', value: 1 },
|
||
{ label: '自定义', value: 0 },
|
||
];
|
||
const PUSHED_OPTIONS = [
|
||
{ label: '已推送', value: 1 },
|
||
{ label: '未推送', value: 0 },
|
||
];
|
||
const SUCCESS_OPTIONS = [
|
||
{ label: '成功', value: 1 },
|
||
{ label: '失败', value: 0 },
|
||
];
|
||
|
||
/** 业务动作选项(出库推送锁定 / 出库解锁 / 订单审核锁定 …) */
|
||
const BIZ_TYPE_OPTIONS = Object.keys(BIZ_TYPES).map(value => ({ label: BIZ_TYPES[value].label, value }));
|
||
|
||
/** 入库单 / 出库单类型选项 */
|
||
const INBOUND_TYPE_OPTIONS = Object.keys(INBOUND_ORDER_TYPES).map(value => ({ label: `${INBOUND_ORDER_TYPES[value]}(${value})`, value }));
|
||
const OUTBOUND_TYPE_OPTIONS = Object.keys(OUTBOUND_ORDER_TYPES).map(value => ({ label: `${OUTBOUND_ORDER_TYPES[value]}(${value})`, value }));
|
||
|
||
/** 店铺平台 / 主体类型 / 账号角色 / 仓库类型 / 店铺商品绑定类型选项 */
|
||
const PLATFORM_OPTIONS = [
|
||
{ label: '淘宝/天猫', value: 'taobao' },
|
||
{ label: '拼多多', value: 'pdd' },
|
||
{ label: '抖音', value: 'douyin' },
|
||
{ label: '京东', value: 'jd' },
|
||
{ label: '其他', value: 'other' },
|
||
];
|
||
const PRINCIPAL_TYPE_OPTIONS = [
|
||
{ label: '自营', value: 1 },
|
||
{ label: '加盟', value: 2 },
|
||
{ label: '外部', value: 3 },
|
||
];
|
||
const ROLE_OPTIONS = [
|
||
{ label: '所有者', value: 'owner' },
|
||
{ label: '管理员', value: 'admin' },
|
||
{ label: '成员', value: 'member' },
|
||
];
|
||
const WAREHOUSE_TYPE_OPTIONS = [
|
||
{ label: 'WMS', value: 'wms' },
|
||
{ label: 'ERP', value: 'erp' },
|
||
{ label: '其他', value: 'other' },
|
||
];
|
||
const BIND_TYPE_OPTIONS = [
|
||
{ label: '未绑定', value: 0 },
|
||
{ label: '系统商品', value: 1 },
|
||
{ label: '组合商品', value: 2 },
|
||
];
|
||
|
||
/* 选项数组 → 「值 → 中文」映射(value 为数字时同样适用) */
|
||
function mapOf(options) {
|
||
return options.reduce((acc, option) => { acc[option.value] = option.label; return acc; }, {});
|
||
}
|
||
|
||
const ITEM_TYPE_MAP = mapOf(ITEM_TYPE_OPTIONS);
|
||
const DIRECTION_MAP = mapOf(DIRECTION_OPTIONS);
|
||
const BILL_TYPE_MAP = mapOf(BILL_TYPE_OPTIONS);
|
||
const ENABLE_MAP = mapOf(ENABLE_OPTIONS);
|
||
const IS_SYSTEM_MAP = mapOf(IS_SYSTEM_OPTIONS);
|
||
const PUSHED_MAP = mapOf(PUSHED_OPTIONS);
|
||
const SUCCESS_MAP = mapOf(SUCCESS_OPTIONS);
|
||
const PLATFORM_MAP = mapOf(PLATFORM_OPTIONS);
|
||
const PRINCIPAL_TYPE_MAP = mapOf(PRINCIPAL_TYPE_OPTIONS);
|
||
const ROLE_MAP = mapOf(ROLE_OPTIONS);
|
||
const WAREHOUSE_TYPE_MAP = mapOf(WAREHOUSE_TYPE_OPTIONS);
|
||
const BIND_TYPE_MAP = mapOf(BIND_TYPE_OPTIONS);
|
||
const BIZ_LABELS = Object.keys(BIZ_TYPES).reduce((acc, code) => { acc[code] = BIZ_TYPES[code].label; return acc; }, {});
|
||
|
||
/* ==================== 通用工具(纯函数) ==================== */
|
||
|
||
/* 空值判断:undefined / null / 空串视为「未填」 */
|
||
function blank(value) { return value === undefined || value === null || value === ''; }
|
||
|
||
/* 编码归一化(去空格 + 大写,用于编码类筛选) */
|
||
function upper(value) { return blank(value) ? '' : String(value).trim().toUpperCase(); }
|
||
|
||
/* 生成「值 → 中文」格式化器(未知值原样返回) */
|
||
function labels(map) { return value => (map[value] === undefined ? value : map[value]); }
|
||
|
||
/* 1/0 → 是/否(二进制标记列) */
|
||
function yesNo(value) { return value === 1 || value === '1' ? '是' : '否'; }
|
||
|
||
/* JSON 值 → 可读文本(数组用「、」连接,对象转 JSON 字符串) */
|
||
function jsonText(value) {
|
||
if (blank(value)) return '';
|
||
if (Array.isArray(value)) return value.filter(item => !blank(item)).join('、');
|
||
if (typeof value === 'object') {
|
||
try { return JSON.stringify(value); } catch { return String(value); }
|
||
}
|
||
return String(value);
|
||
}
|
||
|
||
/* 日期边界:只写到天(YYYY-MM-DD)时补 00:00:00 / 23:59:59 */
|
||
function dateBound(value, isEnd) {
|
||
const text = String(value).trim();
|
||
if (!/^\d{4}-\d{2}-\d{2}$/.test(text)) return text;
|
||
return text + (isEnd ? ' 23:59:59' : ' 00:00:00');
|
||
}
|
||
|
||
/**
|
||
* 参数化 WHERE 条件构造器(所有用户输入经 ? 占位传递,避免 SQL 注入;空值自动跳过)
|
||
* @return {Object} 链式条件收集器(eq/like/kw/from/to/raw 追加条件,sql() 取 WHERE 子句)
|
||
*/
|
||
function makeWhere() {
|
||
const conds = [];
|
||
const repl = [];
|
||
const api = {
|
||
// 等值条件
|
||
eq(column, value) {
|
||
if (!blank(value)) { conds.push(`${column} = ?`); repl.push(String(value).trim()); }
|
||
return api;
|
||
},
|
||
// 模糊条件
|
||
like(column, value) {
|
||
if (!blank(value)) { conds.push(`${column} LIKE ?`); repl.push('%' + String(value).trim() + '%'); }
|
||
return api;
|
||
},
|
||
// 多列模糊(任一命中)
|
||
kw(columns, value) {
|
||
if (!blank(value)) {
|
||
const text = String(value).trim();
|
||
conds.push('(' + columns.map(column => `${column} LIKE ?`).join(' OR ') + ')');
|
||
columns.forEach(() => repl.push('%' + text + '%'));
|
||
}
|
||
return api;
|
||
},
|
||
// 时间下界
|
||
from(column, value) {
|
||
if (!blank(value)) { conds.push(`${column} >= ?`); repl.push(dateBound(value, false)); }
|
||
return api;
|
||
},
|
||
// 时间上界
|
||
to(column, value) {
|
||
if (!blank(value)) { conds.push(`${column} <= ?`); repl.push(dateBound(value, true)); }
|
||
return api;
|
||
},
|
||
// 自定义条件(占位符顺序与传参一致)
|
||
raw(condition, ...values) {
|
||
conds.push(condition);
|
||
repl.push(...values);
|
||
return api;
|
||
},
|
||
// 条件数组
|
||
get conditions() { return conds; },
|
||
// 参数数组(顺序与 SQL 中的 ? 一致)
|
||
get replacements() { return repl; },
|
||
// WHERE 子句(无条件返回空串)
|
||
sql() { return conds.length ? ' WHERE ' + conds.join(' AND ') : ''; },
|
||
};
|
||
return api;
|
||
}
|
||
|
||
/**
|
||
* 组装导出查询结果
|
||
* @param {Object} options 组装参数
|
||
* @param {string} options.from FROM 子句
|
||
* @param {Object} options.wh makeWhere() 结果
|
||
* @param {Array} options.columns 列定义
|
||
* @param {string} [options.orderBy] 排序(默认 id DESC)
|
||
* @param {Array} [options.leadRepl] 排在 WHERE 参数之前的参数(如 UNION 各分支的主体ID)
|
||
* @return {{sql: string, countSql: string, columns: Array, replacements: Array}} 查询语句与列定义
|
||
*/
|
||
function buildResult({ from, wh, columns, orderBy = 'id DESC', leadRepl = [] }) {
|
||
const select = columns.map(c => (c.expr ? `${c.expr} AS ${c.key}` : c.key)).join(', ');
|
||
const base = `SELECT ${select} FROM ${from}${wh.sql()}`;
|
||
return {
|
||
sql: `${base} ORDER BY ${orderBy}`,
|
||
countSql: `SELECT COUNT(*) AS cnt FROM (${base}) AS cnt_t`,
|
||
columns,
|
||
replacements: [ ...leadRepl, ...wh.replacements ],
|
||
};
|
||
}
|
||
|
||
/* ==================== 各导出类型的列定义 ==================== */
|
||
|
||
/** 订单状态表列(8 张状态表列名一致,多状态导出时 UNION ALL 后再筛选) */
|
||
const ORDER_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'order_no', header: '订单号' },
|
||
{ key: 'parent_order_no', header: '来源订单号' },
|
||
{ key: 'status', header: '状态', format: labels(ORDER_STATUS_LABELS) },
|
||
{ key: 'order_type', header: '订单类型' },
|
||
{ key: 'shop_id', header: '店铺ID' },
|
||
{ key: 'shop_nick', header: '店铺名称' },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'warehouse_code', header: '仓库编码' },
|
||
{ key: 'receiver', header: '收货人' },
|
||
{ key: 'tel', header: '电话' },
|
||
{ key: 'mobile', header: '手机' },
|
||
{ key: 'address', header: '收货地址' },
|
||
{ key: 'item_kind', header: '商品种类' },
|
||
{ key: 'item_total', header: '商品总数' },
|
||
{ key: 'total_amount', header: '订单总金额' },
|
||
{ key: 'pay_amount', header: '实付金额' },
|
||
{ key: 'item_amount', header: '商品总金额' },
|
||
{ key: 'freight', header: '快递费' },
|
||
{ key: 'logistics_code', header: '物流公司编码' },
|
||
{ key: 'logistics_name', header: '物流公司名称' },
|
||
{ key: 'express_code', header: '运单号' },
|
||
{ key: 'express_codes', header: '运单号集合', format: jsonText },
|
||
{ key: 'wms_status', header: 'WMS状态' },
|
||
{ key: 'wms_order_no', header: 'WMS单号' },
|
||
{ key: 'operator_name', header: '操作人' },
|
||
{ key: 'audit_time', header: '审核时间' },
|
||
{ key: 'pushed_at', header: '推送时间' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
{ key: 'remark', header: '备注' },
|
||
];
|
||
|
||
/** 入库单列 */
|
||
const INBOUND_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'bill_no', header: '入库单号' },
|
||
{ key: 'status', header: '状态', format: labels(BILL_STATUS_LABELS) },
|
||
{ key: 'inbound_type', header: '入库类型', format: labels(INBOUND_ORDER_TYPES) },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'shop_id', header: '店铺ID' },
|
||
{ key: 'supplier', header: '供应商' },
|
||
{ key: 'supplier_code', header: '供应商编码' },
|
||
{ key: 'purchase_order_code', header: '采购单号' },
|
||
{ key: 'operator', header: '操作人' },
|
||
{ key: 'item_kind', header: '商品种类' },
|
||
{ key: 'item_total', header: '商品总数' },
|
||
{ key: 'total_amount', header: '单据金额' },
|
||
{ key: 'logistics_code', header: '物流公司编码' },
|
||
{ key: 'logistics_name', header: '物流公司名称' },
|
||
{ key: 'express_code', header: '运单号' },
|
||
{ key: 'operate_time', header: '操作时间' },
|
||
{ key: 'expect_start_time', header: '预计到货' },
|
||
{ key: 'expect_end_time', header: '最迟到货' },
|
||
{ key: 'audit_time', header: '审核时间' },
|
||
{ key: 'pushed_at', header: '推送时间' },
|
||
{ key: 'void_at', header: '作废时间' },
|
||
{ key: 'void_reason', header: '作废原因' },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 出库单列 */
|
||
const OUTBOUND_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'bill_no', header: '出库单号' },
|
||
{ key: 'status', header: '状态', format: labels(BILL_STATUS_LABELS) },
|
||
{ key: 'outbound_type', header: '出库类型', format: labels(OUTBOUND_ORDER_TYPES) },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'shop_id', header: '店铺ID' },
|
||
{ key: 'customer', header: '客户' },
|
||
{ key: 'item_kind', header: '商品种类' },
|
||
{ key: 'item_total', header: '商品总数' },
|
||
{ key: 'total_amount', header: '单据金额' },
|
||
{ key: 'express_code', header: '快递公司编码' },
|
||
{ key: 'express_name', header: '快递公司名称' },
|
||
{ key: 'waybill_codes', header: '运单号', format: jsonText },
|
||
{ key: 'receiver_name', header: '收货人' },
|
||
{ key: 'receiver_tel', header: '收货人电话' },
|
||
{ key: 'receiver_mobile', header: '收货人手机' },
|
||
{
|
||
key: 'receiver_address',
|
||
header: '收货地址',
|
||
expr: "CONCAT_WS('', t.receiver_province, t.receiver_city, t.receiver_area, t.receiver_town, t.receiver_detail_address)",
|
||
},
|
||
{ key: 'stock_locked', header: '库存已锁定', format: yesNo },
|
||
{ key: 'stock_deducted', header: '库存已扣减', format: yesNo },
|
||
{ key: 'operator', header: '操作人' },
|
||
{ key: 'pushed_at', header: '推送时间' },
|
||
{ key: 'void_at', header: '作废时间' },
|
||
{ key: 'void_reason', header: '作废原因' },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 出入库记录(库存流水)列 */
|
||
const STOCK_RECORD_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'created_at', header: '记录时间' },
|
||
{ key: 'biz_type', header: '业务动作', format: labels(BIZ_LABELS) },
|
||
{ key: 'direction', header: '方向', format: labels(DIRECTION_MAP) },
|
||
{ key: 'bill_type', header: '单据类型', format: labels(BILL_TYPE_MAP) },
|
||
{ key: 'bill_no', header: '单据号' },
|
||
{ key: 'item_code', header: '商品编码' },
|
||
{ key: 'item_name', header: '商品名称' },
|
||
{ key: 'item_type', header: '库存类型', format: labels(ITEM_TYPE_MAP) },
|
||
{ key: 'quantity', header: '变动数量' },
|
||
{ key: 'quantity_before', header: '总数量(前)' },
|
||
{ key: 'quantity_after', header: '总数量(后)' },
|
||
{ key: 'lock_quantity', header: '锁定变动' },
|
||
{ key: 'lock_before', header: '锁定(前)' },
|
||
{ key: 'lock_after', header: '锁定(后)' },
|
||
{ key: 'available_before', header: '可用(前)' },
|
||
{ key: 'available_after', header: '可用(后)' },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'remark', header: '备注' },
|
||
];
|
||
|
||
/** 商品列 */
|
||
const PRODUCT_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'item_code', header: '商品编码' },
|
||
{ key: 'item_name', header: '商品名称' },
|
||
{ key: 'goods_code', header: '货物编码' },
|
||
{ key: 'sku_property', header: 'SKU规格' },
|
||
{ key: 'category_name', header: '分类' },
|
||
{ key: 'brand_name', header: '品牌' },
|
||
{ key: 'barcode', header: '条码' },
|
||
{ key: 'stock_unit', header: '单位' },
|
||
{ key: 'retail_price', header: '零售价' },
|
||
{ key: 'cost_price', header: '成本价' },
|
||
{ key: 'tag_price', header: '标签价' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 仓库商品列(商品 × 仓库推送关联,含本地库存数量) */
|
||
const WAREHOUSE_PRODUCT_COLUMNS = [
|
||
{ key: 'product_id', header: '商品ID', expr: 't.id' },
|
||
{ key: 'item_code', header: '商品编码', expr: 't.item_code' },
|
||
{ key: 'item_name', header: '商品名称', expr: 't.item_name' },
|
||
{ key: 'goods_code', header: '货物编码', expr: 't.goods_code' },
|
||
{ key: 'barcode', header: '条码', expr: 't.barcode' },
|
||
{ key: 'sku_property', header: 'SKU规格', expr: 't.sku_property' },
|
||
{ key: 'stock_unit', header: '单位', expr: 't.stock_unit' },
|
||
{ key: 'product_status', header: '商品状态', expr: 't.status', format: labels(ENABLE_MAP) },
|
||
{ key: 'warehouse_id', header: '仓库ID', expr: 'wp.warehouse_id' },
|
||
{ key: 'warehouse_name', header: '仓库名称', expr: 'wh_w.name' },
|
||
{ key: 'wms_item_id', header: 'WMS商品ID', expr: 'wp.wms_item_id' },
|
||
{ key: 'pushed', header: '推送状态', expr: 'wp.pushed', format: labels(PUSHED_MAP) },
|
||
{ key: 'pushed_at', header: '推送时间', expr: 'wp.pushed_at' },
|
||
{
|
||
key: 'good_quantity',
|
||
header: '正品数量',
|
||
expr: "(SELECT ps.quantity FROM product_stocks ps WHERE ps.product_id = t.id AND ps.principal_id = t.principal_id AND ps.item_type = 'ZC' LIMIT 1)",
|
||
},
|
||
{
|
||
key: 'defect_quantity',
|
||
header: '残次品数量',
|
||
expr: "(SELECT ps.quantity FROM product_stocks ps WHERE ps.product_id = t.id AND ps.principal_id = t.principal_id AND ps.item_type = 'CC' LIMIT 1)",
|
||
},
|
||
];
|
||
|
||
/** 商品属性列(分类 / 品牌 / 单位三张同构表共用) */
|
||
const ATTR_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'name', header: '名称' },
|
||
{ key: 'code', header: '编码' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'sort', header: '排序' },
|
||
{ key: 'is_system', header: '数据来源', format: labels(IS_SYSTEM_MAP) },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 组合商品列 */
|
||
const COMBO_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'code', header: '组合编码' },
|
||
{ key: 'name', header: '组合名称' },
|
||
{ key: 'price', header: '组合售价' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{
|
||
key: 'items_text',
|
||
header: '组合明细',
|
||
expr: "(SELECT GROUP_CONCAT(CONCAT(p.item_name, '×', ci.quantity) SEPARATOR ',') FROM combo_product_items ci LEFT JOIN products p ON p.id = ci.product_id WHERE ci.combo_product_id = t.id)",
|
||
},
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 店铺商品列 */
|
||
const SHOP_PRODUCT_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'shop_id', header: '店铺ID' },
|
||
{ key: 'shop_name', header: '店铺名称', expr: '(SELECT s.name FROM shops s WHERE s.id = t.shop_id)' },
|
||
{ key: 'bind_type', header: '绑定类型', format: labels(BIND_TYPE_MAP) },
|
||
{ key: 'product_id', header: '系统商品ID' },
|
||
{ key: 'product_name', header: '系统商品名称', expr: '(SELECT p.item_name FROM products p WHERE p.id = t.product_id)' },
|
||
{ key: 'combo_product_id', header: '组合商品ID' },
|
||
{ key: 'outer_item_id', header: '平台商品ID' },
|
||
{ key: 'outer_sku_id', header: '平台规格ID' },
|
||
{ key: 'outer_code', header: '商家编码' },
|
||
{ key: 'title', header: '平台标题' },
|
||
{ key: 'spec', header: '规格' },
|
||
{ key: 'price', header: '售价' },
|
||
{ key: 'stock', header: '平台库存' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'synced_at', header: '同步时间' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 店铺列 */
|
||
const SHOP_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'code', header: '店铺编码' },
|
||
{ key: 'name', header: '店铺名称' },
|
||
{ key: 'platform', header: '平台', format: labels(PLATFORM_MAP) },
|
||
{ key: 'contact_name', header: '联系人' },
|
||
{ key: 'contact_phone', header: '联系电话' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 主体列 */
|
||
const PRINCIPAL_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'name', header: '主体名称' },
|
||
{ key: 'type', header: '主体类型', format: labels(PRINCIPAL_TYPE_MAP) },
|
||
{ key: 'contact_name', header: '联系人' },
|
||
{ key: 'contact_phone', header: '联系电话' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 账号(主体成员关联)列 */
|
||
const PRINCIPAL_USER_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'user_id', header: '用户ID' },
|
||
{ key: 'name', header: '名称' },
|
||
{
|
||
key: 'username',
|
||
header: '登录账号',
|
||
expr: '(SELECT u.username FROM users u WHERE u.id = t.user_id)',
|
||
},
|
||
{ key: 'nickname', header: '用户昵称', expr: '(SELECT u.nickname FROM users u WHERE u.id = t.user_id)' },
|
||
{ key: 'role', header: '角色', format: labels(ROLE_MAP) },
|
||
{ key: 'is_default', header: '默认主体', format: yesNo },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 外部仓库列 */
|
||
const WAREHOUSE_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'code', header: '仓库编码' },
|
||
{ key: 'name', header: '仓库名称' },
|
||
{ key: 'type', header: '仓库类型', format: labels(WAREHOUSE_TYPE_MAP) },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 接口配置列(不下发 app_secret,避免密钥外泄) */
|
||
const WAREHOUSE_API_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'warehouse_name', header: '仓库名称', expr: '(SELECT w.name FROM warehouses w WHERE w.id = t.warehouse_id)' },
|
||
{ key: 'name', header: '配置名称' },
|
||
{ key: 'type', header: '接口类型' },
|
||
{ key: 'base_url', header: '接口地址' },
|
||
{ key: 'app_key', header: 'app_key' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 仓库快递映射列 */
|
||
const EXPRESS_MAPPING_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'warehouse_name', header: '仓库名称', expr: '(SELECT w.name FROM warehouses w WHERE w.id = t.warehouse_id)' },
|
||
{ key: 'express_code', header: '默认快递编码' },
|
||
{ key: 'express_name', header: '快递名称', expr: '(SELECT ec.name FROM express_companies ec WHERE ec.code = t.express_code)' },
|
||
{ key: 'custom_code', header: '自定义编码' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/** 对外 API 调用日志列(OMS → 外部仓库) */
|
||
const OUTBOUND_LOG_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'created_at', header: '调用时间' },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'warehouse_api_id', header: '接口配置ID' },
|
||
{ key: 'method', header: '接口方法' },
|
||
{ key: 'response_status', header: 'HTTP状态码' },
|
||
{ key: 'is_success', header: '是否成功', format: labels(SUCCESS_MAP) },
|
||
{ key: 'business_code', header: '业务码' },
|
||
{ key: 'business_message', header: '业务提示' },
|
||
{ key: 'duration', header: '耗时(ms)' },
|
||
{ key: 'error', header: '异常信息' },
|
||
{ key: 'url', header: '请求地址' },
|
||
{ key: 'request_body', header: '请求报文' },
|
||
{ key: 'response_body', header: '响应报文' },
|
||
];
|
||
|
||
/** 回调日志列(外部仓库 → OMS 的 /openapi 回调) */
|
||
const CALLBACK_LOG_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'created_at', header: '回调时间' },
|
||
{ key: 'warehouse_id', header: '仓库ID' },
|
||
{ key: 'method', header: '回调方法' },
|
||
{ key: 'app_key', header: 'app_key' },
|
||
{ key: 'ip', header: '来源IP' },
|
||
{ key: 'http_status', header: 'HTTP状态码' },
|
||
{ key: 'flag', header: '结果标记' },
|
||
{ key: 'business_code', header: '业务码' },
|
||
{ key: 'message', header: '处理结果' },
|
||
{ key: 'is_success', header: '是否成功', format: labels(SUCCESS_MAP) },
|
||
{ key: 'duration', header: '耗时(ms)' },
|
||
{ key: 'error', header: '异常信息' },
|
||
{ key: 'request_query', header: '请求参数' },
|
||
{ key: 'request_body', header: '请求报文' },
|
||
{ key: 'response_body', header: '响应报文' },
|
||
];
|
||
|
||
/** 快递公司字典列 */
|
||
const EXPRESS_COMPANY_COLUMNS = [
|
||
{ key: 'id', header: 'ID' },
|
||
{ key: 'code', header: '快递编码' },
|
||
{ key: 'name', header: '快递名称' },
|
||
{ key: 'status', header: '状态', format: labels(ENABLE_MAP) },
|
||
{ key: 'sort', header: '排序' },
|
||
{ key: 'remark', header: '备注' },
|
||
{ key: 'created_at', header: '创建时间' },
|
||
];
|
||
|
||
/* ==================== 导出类型注册表 ==================== */
|
||
|
||
/**
|
||
* 生成商品属性(分类/品牌/单位)三张同构表的导出定义
|
||
* @param {string} label 类型中文名
|
||
* @param {string} table 表名
|
||
* @return {Object} 导出类型定义
|
||
*/
|
||
function attrType(label, table) {
|
||
return {
|
||
label,
|
||
filters: [
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '名称 / 编码' },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
{ key: 'is_system', label: '数据来源', type: 'select', options: IS_SYSTEM_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.kw([ 't.name', 't.code' ], f.keyword)
|
||
.eq('t.status', f.status)
|
||
.eq('t.is_system', f.is_system);
|
||
return buildResult({ columns: ATTR_COLUMNS, from: `${table} t`, wh });
|
||
},
|
||
};
|
||
}
|
||
|
||
/** 导出类型定义:label 中文名 / filters 可传筛选(前端据此渲染表单) / build SQL 构造
|
||
* ⚠️ key 长度需 ≤ 20(落库列 export_tasks.type 为 varchar(20))
|
||
*/
|
||
const EXPORT_TYPES = {
|
||
order: {
|
||
label: '订单',
|
||
filters: [
|
||
{ key: 'order_stage', label: '订单状态', type: 'select', options: ORDER_STATUS_OPTIONS, placeholder: '不选则导出全部状态' },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '订单号 / 来源订单号 / 收货人 / 地址' },
|
||
{ key: 'order_no', label: '订单号', type: 'text', placeholder: '支持模糊匹配' },
|
||
{ key: 'parent_order_no', label: '来源订单号', type: 'text', placeholder: '支持模糊匹配' },
|
||
{ key: 'receiver', label: '收货人', type: 'text' },
|
||
{ key: 'address', label: '收货地址', type: 'text' },
|
||
{ key: 'remark', label: '备注', type: 'text' },
|
||
{ key: 'shop_id', label: '店铺', type: 'shop' },
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'created_from', label: '创建时间起', type: 'date' },
|
||
{ key: 'created_to', label: '创建时间止', type: 'date' },
|
||
{ key: 'archived_from', label: '归档时间起', type: 'date' },
|
||
{ key: 'archived_to', label: '归档时间止', type: 'date' },
|
||
{ key: 'exclude_unbound', label: '排除含未绑定商品的订单', type: 'select', options: [{ value: '1', label: '是' }] },
|
||
],
|
||
build: (f, ctx) => {
|
||
// 订单按状态分表存储:指定状态时只查对应表,未指定时 UNION ALL 各状态表
|
||
const stages = ORDER_TABLE_BY_STATUS[f.order_stage] ? [ f.order_stage ] : ORDER_STATUS_ORDER;
|
||
const select = ORDER_COLUMNS.map(c => c.key).join(', ');
|
||
const inner = stages.map(stage => {
|
||
const table = ORDER_TABLE_BY_STATUS[stage];
|
||
const archived = stage === ORDER_STATUS.HISTORY ? 't.archived_at' : 'NULL AS archived_at';
|
||
return `SELECT ${select}, ${archived} FROM ${table} t WHERE t.principal_id = ?`;
|
||
}).join(' UNION ALL ');
|
||
const wh = makeWhere();
|
||
wh.like('order_no', f.order_no)
|
||
.like('parent_order_no', f.parent_order_no)
|
||
.like('receiver', f.receiver)
|
||
.like('address', f.address)
|
||
.like('remark', f.remark)
|
||
.kw([ 'order_no', 'parent_order_no', 'receiver', 'address' ], f.keyword)
|
||
.eq('shop_id', f.shop_id)
|
||
.eq('warehouse_id', f.warehouse_id)
|
||
.from('created_at', f.created_from)
|
||
.to('created_at', f.created_to)
|
||
.from('archived_at', f.archived_from)
|
||
.to('archived_at', f.archived_to);
|
||
if (String(f.exclude_unbound) === '1') {
|
||
wh.raw(
|
||
'NOT EXISTS (SELECT 1 FROM order_status_items oi WHERE oi.order_id = t.id ' +
|
||
`AND oi.principal_id = ${Number(ctx.principalId)} AND oi.parse_status = 2)`
|
||
);
|
||
}
|
||
return buildResult({
|
||
columns: ORDER_COLUMNS,
|
||
from: `(${inner}) AS t`,
|
||
wh,
|
||
leadRepl: stages.map(() => ctx.principalId),
|
||
});
|
||
},
|
||
},
|
||
|
||
inbound_order: {
|
||
label: '入库单',
|
||
filters: [
|
||
{ key: 'bill_no', label: '入库单号', type: 'text', placeholder: '支持模糊匹配' },
|
||
{ key: 'status', label: '状态', type: 'select', options: BILL_STATUS_OPTIONS },
|
||
{ key: 'inbound_type', label: '入库类型', type: 'select', options: INBOUND_TYPE_OPTIONS },
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '单号 / 供应商 / 运单号' },
|
||
{ key: 'start_time', label: '创建时间起', type: 'date' },
|
||
{ key: 'end_time', label: '创建时间止', type: 'date' },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.like('t.bill_no', f.bill_no)
|
||
.eq('t.status', f.status)
|
||
.eq('t.inbound_type', upper(f.inbound_type))
|
||
.eq('t.warehouse_id', f.warehouse_id)
|
||
.kw([ 't.bill_no', 't.supplier', 't.supplier_code', 't.express_code' ], f.keyword)
|
||
.from('t.created_at', f.start_time)
|
||
.to('t.created_at', f.end_time);
|
||
return buildResult({ columns: INBOUND_COLUMNS, from: 'inbound_orders t', wh });
|
||
},
|
||
},
|
||
|
||
outbound_order: {
|
||
label: '出库单',
|
||
filters: [
|
||
{ key: 'bill_no', label: '出库单号', type: 'text', placeholder: '支持模糊匹配' },
|
||
{ key: 'status', label: '状态', type: 'select', options: BILL_STATUS_OPTIONS },
|
||
{ key: 'outbound_type', label: '出库类型', type: 'select', options: OUTBOUND_TYPE_OPTIONS },
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'shop_id', label: '店铺', type: 'shop' },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '单号 / 客户 / 收货人 / 运单号' },
|
||
{ key: 'start_time', label: '创建时间起', type: 'date' },
|
||
{ key: 'end_time', label: '创建时间止', type: 'date' },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.like('t.bill_no', f.bill_no)
|
||
.eq('t.status', f.status)
|
||
.eq('t.outbound_type', upper(f.outbound_type))
|
||
.eq('t.warehouse_id', f.warehouse_id)
|
||
.eq('t.shop_id', f.shop_id)
|
||
.kw([ 't.bill_no', 't.customer', 't.receiver_name', 't.receiver_mobile', 't.express_code' ], f.keyword)
|
||
.from('t.created_at', f.start_time)
|
||
.to('t.created_at', f.end_time);
|
||
return buildResult({ columns: OUTBOUND_COLUMNS, from: 'outbound_orders t', wh });
|
||
},
|
||
},
|
||
|
||
stock_record: {
|
||
label: '出入库记录',
|
||
filters: [
|
||
{ key: 'product_id', label: '商品', type: 'product' },
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'biz_type', label: '业务动作', type: 'select', options: BIZ_TYPE_OPTIONS },
|
||
{ key: 'direction', label: '方向', type: 'select', options: DIRECTION_OPTIONS },
|
||
{ key: 'bill_type', label: '单据类型', type: 'select', options: BILL_TYPE_OPTIONS },
|
||
{ key: 'item_type', label: '库存类型', type: 'select', options: ITEM_TYPE_OPTIONS },
|
||
{ key: 'bill_no', label: '单据号', type: 'text', placeholder: '支持模糊匹配' },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '单据号 / 商品编码 / 商品名称' },
|
||
{ key: 'start_time', label: '记录时间起', type: 'date' },
|
||
{ key: 'end_time', label: '记录时间止', type: 'date' },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.eq('t.product_id', f.product_id)
|
||
.eq('t.warehouse_id', f.warehouse_id)
|
||
.eq('t.biz_type', f.biz_type)
|
||
.eq('t.direction', f.direction)
|
||
.eq('t.bill_type', f.bill_type)
|
||
.eq('t.item_type', upper(f.item_type))
|
||
.like('t.bill_no', f.bill_no)
|
||
.kw([ 't.bill_no', 't.item_code', 't.item_name' ], f.keyword)
|
||
.from('t.created_at', f.start_time)
|
||
.to('t.created_at', f.end_time);
|
||
return buildResult({ columns: STOCK_RECORD_COLUMNS, from: 'stock_records t', wh });
|
||
},
|
||
},
|
||
|
||
product: {
|
||
label: '商品',
|
||
filters: [
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '商品编码 / 货物编码 / 条码 / 名称' },
|
||
{ key: 'category_name', label: '分类', type: 'text', placeholder: '精确匹配(如 服装)' },
|
||
{ key: 'brand_name', label: '品牌', type: 'text', placeholder: '精确匹配' },
|
||
{ key: 'stock_unit', label: '单位', type: 'text', placeholder: '精确匹配(如 件)' },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.raw('t.deleted_at IS NULL')
|
||
.kw([ 't.item_code', 't.goods_code', 't.barcode', 't.item_name' ], f.keyword)
|
||
.eq('t.category_name', f.category_name)
|
||
.eq('t.brand_name', f.brand_name)
|
||
.eq('t.stock_unit', f.stock_unit)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: PRODUCT_COLUMNS, from: 'products t', wh });
|
||
},
|
||
},
|
||
|
||
warehouse_product: {
|
||
label: '仓库商品',
|
||
filters: [
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'pushed', label: '推送状态', type: 'select', options: PUSHED_OPTIONS },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '商品编码 / 货物编码 / 条码 / 名称' },
|
||
{ key: 'status', label: '商品状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.raw('t.deleted_at IS NULL')
|
||
.eq('wp.warehouse_id', f.warehouse_id)
|
||
.eq('wp.pushed', f.pushed)
|
||
.eq('t.status', f.status)
|
||
.kw([ 't.item_code', 't.goods_code', 't.barcode', 't.item_name' ], f.keyword);
|
||
const from = 'products t'
|
||
+ ' LEFT JOIN warehouse_products wp ON wp.product_id = t.id'
|
||
+ ' LEFT JOIN warehouses wh_w ON wh_w.id = wp.warehouse_id';
|
||
// 多表关联下 id 有歧义,排序显式限定到商品表
|
||
return buildResult({ columns: WAREHOUSE_PRODUCT_COLUMNS, from, wh, orderBy: 't.id DESC' });
|
||
},
|
||
},
|
||
|
||
product_category: attrType('商品分类', 'product_categories'),
|
||
product_brand: attrType('商品品牌', 'product_brands'),
|
||
product_unit: attrType('商品单位', 'product_units'),
|
||
|
||
combo_product: {
|
||
label: '组合商品',
|
||
filters: [
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '组合编码 / 组合名称' },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.kw([ 't.code', 't.name' ], f.keyword)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: COMBO_COLUMNS, from: 'combo_products t', wh });
|
||
},
|
||
},
|
||
|
||
shop_product: {
|
||
label: '店铺商品',
|
||
filters: [
|
||
{ key: 'shop_id', label: '店铺', type: 'shop' },
|
||
{ key: 'bind_type', label: '绑定类型', type: 'select', options: BIND_TYPE_OPTIONS },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
{ key: 'has_unbound_order', label: '只看有订单未绑定', type: 'select', options: [{ value: '1', label: '是' }] },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '标题 / 平台商品ID / 规格ID / 商家编码' },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.eq('t.shop_id', f.shop_id)
|
||
.eq('t.bind_type', f.bind_type)
|
||
.eq('t.status', f.status)
|
||
.kw([ 't.title', 't.outer_item_id', 't.outer_sku_id', 't.outer_code' ], f.keyword);
|
||
// 有订单未绑定:与列表接口同口径(未绑定 + 被未解析的订单明细引用)
|
||
if (String(f.has_unbound_order) === '1') {
|
||
wh.raw('t.bind_type = 0');
|
||
wh.raw(
|
||
`t.id IN (SELECT DISTINCT shop_product_id FROM order_status_items WHERE principal_id = ${Number(ctx.principalId)} ` +
|
||
'AND parse_status = 2 AND shop_product_id IS NOT NULL)'
|
||
);
|
||
}
|
||
return buildResult({ columns: SHOP_PRODUCT_COLUMNS, from: 'shop_products t', wh });
|
||
},
|
||
},
|
||
|
||
shop: {
|
||
label: '店铺',
|
||
filters: [
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '店铺编码 / 名称' },
|
||
{ key: 'platform', label: '平台', type: 'select', options: PLATFORM_OPTIONS },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.kw([ 't.code', 't.name' ], f.keyword)
|
||
.eq('t.platform', f.platform)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: SHOP_COLUMNS, from: 'shops t', wh });
|
||
},
|
||
},
|
||
|
||
principal: {
|
||
label: '主体',
|
||
filters: [
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '主体名称 / 联系人' },
|
||
{ key: 'type', label: '主体类型', type: 'select', options: PRINCIPAL_TYPE_OPTIONS },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
// 主体表无 principal_id:按「当前登录账号已绑定的主体」隔离(与列表页口径一致)
|
||
const wh = makeWhere();
|
||
if (ctx.userId) {
|
||
wh.raw('t.id IN (SELECT principal_id FROM principal_users WHERE user_id = ?)', ctx.userId);
|
||
} else {
|
||
wh.eq('t.id', ctx.principalId);
|
||
}
|
||
wh.kw([ 't.name', 't.contact_name' ], f.keyword)
|
||
.eq('t.type', f.type)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: PRINCIPAL_COLUMNS, from: 'principals t', wh });
|
||
},
|
||
},
|
||
|
||
principal_user: {
|
||
label: '账号',
|
||
filters: [
|
||
{ key: 'role', label: '角色', type: 'select', options: ROLE_OPTIONS },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
{ key: 'user_id', label: '用户ID', type: 'number' },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.eq('t.role', f.role)
|
||
.eq('t.status', f.status)
|
||
.eq('t.user_id', f.user_id);
|
||
return buildResult({ columns: PRINCIPAL_USER_COLUMNS, from: 'principal_users t', wh });
|
||
},
|
||
},
|
||
|
||
warehouse: {
|
||
label: '外部仓库',
|
||
filters: [
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '仓库编码 / 名称' },
|
||
{ key: 'type', label: '仓库类型', type: 'select', options: WAREHOUSE_TYPE_OPTIONS },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.raw('t.deleted_at IS NULL')
|
||
.kw([ 't.code', 't.name' ], f.keyword)
|
||
.eq('t.type', f.type)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: WAREHOUSE_COLUMNS, from: 'warehouses t', wh });
|
||
},
|
||
},
|
||
|
||
warehouse_api: {
|
||
label: '接口配置',
|
||
filters: [
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '配置名称 / app_key / 接口地址' },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.eq('t.warehouse_id', f.warehouse_id)
|
||
.kw([ 't.name', 't.app_key', 't.base_url' ], f.keyword)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: WAREHOUSE_API_COLUMNS, from: 'warehouse_apis t', wh });
|
||
},
|
||
},
|
||
|
||
express_mapping: {
|
||
label: '仓库快递映射',
|
||
filters: [
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '默认快递编码 / 自定义编码' },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.eq('t.warehouse_id', f.warehouse_id)
|
||
.kw([ 't.express_code', 't.custom_code' ], f.keyword)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: EXPRESS_MAPPING_COLUMNS, from: 'warehouse_express_mappings t', wh });
|
||
},
|
||
},
|
||
|
||
api_outbound_log: {
|
||
label: '对外调用日志',
|
||
filters: [
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'warehouse_api_id', label: '接口配置ID', type: 'number' },
|
||
{ key: 'method', label: '接口方法', type: 'text', placeholder: '如 index.ping' },
|
||
{ key: 'is_success', label: '是否成功', type: 'select', options: SUCCESS_OPTIONS },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '请求地址 / 业务提示 / 异常信息' },
|
||
{ key: 'start_time', label: '调用时间起', type: 'date' },
|
||
{ key: 'end_time', label: '调用时间止', type: 'date' },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.eq('t.warehouse_id', f.warehouse_id)
|
||
.eq('t.warehouse_api_id', f.warehouse_api_id)
|
||
.eq('t.method', f.method)
|
||
.eq('t.is_success', f.is_success)
|
||
.kw([ 't.url', 't.business_message', 't.error' ], f.keyword)
|
||
.from('t.created_at', f.start_time)
|
||
.to('t.created_at', f.end_time);
|
||
return buildResult({ columns: OUTBOUND_LOG_COLUMNS, from: 'api_outbound_logs t', wh });
|
||
},
|
||
},
|
||
|
||
callback_log: {
|
||
label: '回调日志',
|
||
filters: [
|
||
{ key: 'warehouse_id', label: '仓库', type: 'warehouse' },
|
||
{ key: 'method', label: '回调方法', type: 'text', placeholder: '如 entryorder.confirm' },
|
||
{ key: 'app_key', label: 'app_key', type: 'text' },
|
||
{ key: 'is_success', label: '是否成功', type: 'select', options: SUCCESS_OPTIONS },
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '处理结果 / 异常信息 / 报文' },
|
||
{ key: 'start_time', label: '回调时间起', type: 'date' },
|
||
{ key: 'end_time', label: '回调时间止', type: 'date' },
|
||
],
|
||
build: (f, ctx) => {
|
||
const wh = makeWhere();
|
||
wh.eq('t.principal_id', ctx.principalId)
|
||
.eq('t.warehouse_id', f.warehouse_id)
|
||
.eq('t.method', f.method)
|
||
.eq('t.app_key', f.app_key)
|
||
.eq('t.is_success', f.is_success)
|
||
.kw([ 't.message', 't.error', 't.request_body' ], f.keyword)
|
||
.from('t.created_at', f.start_time)
|
||
.to('t.created_at', f.end_time);
|
||
return buildResult({ columns: CALLBACK_LOG_COLUMNS, from: 'callback_logs t', wh });
|
||
},
|
||
},
|
||
|
||
express_company: {
|
||
label: '快递公司',
|
||
filters: [
|
||
{ key: 'keyword', label: '关键词', type: 'text', placeholder: '快递编码 / 名称' },
|
||
{ key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS },
|
||
],
|
||
build: f => {
|
||
const wh = makeWhere();
|
||
wh.kw([ 't.code', 't.name' ], f.keyword)
|
||
.eq('t.status', f.status);
|
||
return buildResult({ columns: EXPRESS_COMPANY_COLUMNS, from: 'express_companies t', wh });
|
||
},
|
||
},
|
||
};
|
||
|
||
/** 类型 → 中文名(导出文件 sheet 名与任务列表展示,避免前后端枚举漂移) */
|
||
const TYPE_LABELS = Object.keys(EXPORT_TYPES).reduce((acc, key) => { acc[key] = EXPORT_TYPES[key].label; return acc; }, {});
|
||
|
||
/**
|
||
* 构造导出 SQL
|
||
* @param {string} type 导出类型(见 EXPORT_TYPES)
|
||
* @param {Object} filters 筛选条件(来自前端,键已按类型白名单过滤)
|
||
* @param {number} principalId 主体ID(强制隔离)
|
||
* @param {number} [userId] 提交人ID(「主体」类型按账号可见主体隔离时使用)
|
||
* @return {{sql: string, countSql: string, columns: Array, replacements: Array}} 查询语句与列定义
|
||
*/
|
||
function buildExportSql(type, filters, principalId, userId) {
|
||
const def = EXPORT_TYPES[type];
|
||
if (!def) throw new Error('不支持的导出类型: ' + type);
|
||
return def.build(filters || {}, { principalId, userId });
|
||
}
|
||
|
||
/**
|
||
* 按类型白名单过滤筛选参数(只保留该类型声明的筛选字段,空值丢弃)
|
||
* @param {string} type 导出类型
|
||
* @param {Object} raw 原始筛选参数
|
||
* @return {Object} 只含该类型声明字段的筛选参数
|
||
*/
|
||
function cleanFilters(type, raw) {
|
||
const def = EXPORT_TYPES[type];
|
||
const clean = {};
|
||
if (!def || !raw || typeof raw !== 'object') return clean;
|
||
for (const item of def.filters) {
|
||
const value = raw[item.key];
|
||
if (value === undefined || value === null || value === '') continue;
|
||
clean[item.key] = value;
|
||
}
|
||
return clean;
|
||
}
|
||
|
||
/**
|
||
* 导出类型选项(含各类型的筛选字段定义,供前端动态渲染提交表单)
|
||
* @return {Array<{value: string, label: string, filters: Array}>} 类型清单(含筛选字段定义)
|
||
*/
|
||
function listExportTypeSchema() {
|
||
return Object.keys(EXPORT_TYPES).map(value => ({
|
||
value,
|
||
label: EXPORT_TYPES[value].label,
|
||
filters: EXPORT_TYPES[value].filters,
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* 将 Date 格式化为本地时间字符串 YYYY-MM-DD HH:mm:ss(XLSX 单元格可读)
|
||
* @param {Date|string|number} value 时间
|
||
* @return {string} 本地时间文本(无法解析返回空串)
|
||
*/
|
||
function formatDate(value) {
|
||
const dt = value instanceof Date ? value : new Date(value);
|
||
if (isNaN(dt.getTime())) return '';
|
||
const p = n => String(n).padStart(2, '0');
|
||
return `${dt.getFullYear()}-${p(dt.getMonth() + 1)}-${p(dt.getDate())} ${p(dt.getHours())}:${p(dt.getMinutes())}:${p(dt.getSeconds())}`;
|
||
}
|
||
|
||
module.exports = {
|
||
EXPORT_TYPES,
|
||
STATUS_LABELS,
|
||
TYPE_LABELS,
|
||
buildExportSql,
|
||
cleanFilters,
|
||
listExportTypeSchema,
|
||
formatDate,
|
||
};
|