本次将此前被列入退役名单的运营分析相关模块恢复为可用,并新增「模块化库存监控」
一级菜单及其两个子页面。
主要变更:
1. 恢复启用:商品监控、激励看板、营销活动、推广模块、商品分析
- retiredFeatures 移出对应退役名单与清表语句,改为正常保留
- 补齐后端路由、前端页面与 API 封装、侧边栏配置、桌面客户端能力项
- 同步更新 retiredFeatures / retiredRoutes / retiredNavigation / desktopSessionService 测试
2. 新增「模块化库存监控」一级菜单
- 配件库存监控:仅取「简尚-崔桥工厂半成品包装项目仓」SKU 的实时库存、
当日入库/出库、近 7 日日均发货量、库存可用天数与预警(库存可用天数 ≤ 7 预警)
- MKH-BOM资料:按商品编码展示配件明细、数量、仓位、库存,支持 A4 纵向打印
3. 新增产品资料系统数据库(PostgreSQL)连接模块:读取系统库 database_configs 中
name = product_information_database 的配置,凭据只从配置读取,不落代码
4. 新增系统设置页配置项:商品标签配置、部门商品标签配置、部门SKU负责人配置
5. 旧 /api/dashboard 接口迁移至 /api/productAnalysis,仪表盘预览页改用新接口,
相应移除 dashboard 路由模块及其测试
6. 修复 retiredRoutes 测试在 Windows 下挂起超时的问题:该测试用
parent.filename.endsWith('/api/planningUpload.js') 判断是否注入 mock,
而 Windows 的 filename 为反斜杠路径,导致判断恒为 false、mock 全部失效、
真实模块去连中央系统库而挂起(Linux CI 正常)。改为平台无关的路径匹配。
7. .gitignore 忽略本地验证产物(outputs/、.git-status.txt)
验证:
- 前端:lint / test(16 项)/ build(含 planning entry 校验)全部通过
- 后端:语法检查 149 个文件通过;测试 38/39 通过
- 已知未通过项:resilientSecretStore.test.js 断言密钥备份文件权限为 0o600,
Windows 下 chmod 无法表达该权限(实际为 0o666),属既有的平台相关失败,
与本次改动无关,本次未修改该文件
- 说明:本机 frontend/ 下存在未纳入版本库的旧依赖备份目录 node_modules_old_*,
会污染 ESLint 扫描(十余万条第三方报错),故 lint 步骤以
--ignore-pattern node_modules_old_*/** 排除后执行,其余步骤与仓库 check 一致
未提交内容:本地验证截图 outputs/ 与 .git-status.txt 已被 .gitignore 排除。
170 lines
4.7 KiB
JavaScript
170 lines
4.7 KiB
JavaScript
/**
|
||
* 产品资料系统数据库连接模块(PostgreSQL)
|
||
*
|
||
* 数据来源:系统库 database_configs 表中 name = 'product_information_database' 的配置。
|
||
* 典型配置:postgres / 218.93.9.38:25432 / furniture3d_prod / external_full_reader / ssl=1
|
||
*
|
||
* 该库用于 MKH-BOM 资料页面的商品资料匹配:
|
||
* external.intake_sku_items_ready_v1
|
||
* —— 真实可用的 SKU 明细视图(约 4.6 万行)
|
||
* —— source_sku_id 为聚水潭原始 SKU 编码(即业务库 BOM 的配件编码)
|
||
* —— sku_code 为商品编码,barcode 为条码
|
||
* external.warehouse_locations_v1 —— 库位主数据(当前仅 1 行:A-01 成品区)
|
||
*
|
||
* 说明:external.product_bom_v1 / product_skus_v1 当前为空视图,不可用。
|
||
*/
|
||
|
||
const { Pool } = require('pg');
|
||
const database = require('./database');
|
||
|
||
const CONFIG_NAME = 'product_information_database';
|
||
const READY_VIEW = 'external.intake_sku_items_ready_v1';
|
||
const LOCATION_VIEW = 'external.warehouse_locations_v1';
|
||
|
||
const parsePositiveInt = (value, fallback) => {
|
||
const parsed = Number.parseInt(value, 10);
|
||
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
||
};
|
||
|
||
const POOL_MAX = parsePositiveInt(process.env.PRODUCT_DB_POOL_MAX, 4);
|
||
|
||
let pool = null;
|
||
let cachedSignature = '';
|
||
|
||
const truthy = (value) => (
|
||
value === true || value === 1 || String(value || '').toLowerCase() === 'true' || String(value || '') === '1'
|
||
);
|
||
|
||
async function loadStoredConfig() {
|
||
const rows = await database.local.query(
|
||
`SELECT id, host, port, database_name, username, password, \`ssl\`, driver, status
|
||
FROM database_configs WHERE name = ? LIMIT 1`,
|
||
[CONFIG_NAME]
|
||
);
|
||
|
||
const config = rows[0];
|
||
if (!config) {
|
||
const error = new Error('产品资料系统数据库尚未配置,请在「系统设置 - 数据库配置」中完成配置');
|
||
error.statusCode = 503;
|
||
error.code = 'PRODUCT_DB_NOT_CONFIGURED';
|
||
throw error;
|
||
}
|
||
|
||
if (String(config.driver || '').toLowerCase() !== 'postgres') {
|
||
const error = new Error('产品资料系统数据库需要选择 PostgreSQL 类型');
|
||
error.statusCode = 503;
|
||
error.code = 'PRODUCT_DB_DRIVER_MISMATCH';
|
||
throw error;
|
||
}
|
||
|
||
return {
|
||
id: config.id,
|
||
host: String(config.host || '').trim(),
|
||
port: Number(config.port) || 5432,
|
||
database: String(config.database_name || '').trim(),
|
||
user: String(config.username || '').trim(),
|
||
password: config.password || '',
|
||
ssl: truthy(config.ssl),
|
||
};
|
||
}
|
||
|
||
const buildSignature = (config) => [
|
||
config.id,
|
||
config.host,
|
||
config.port,
|
||
config.database,
|
||
config.user,
|
||
config.ssl ? 'ssl' : 'nossl',
|
||
].join('::');
|
||
|
||
async function getPool() {
|
||
const config = await loadStoredConfig();
|
||
const signature = buildSignature(config);
|
||
|
||
if (pool && signature === cachedSignature) {
|
||
return pool;
|
||
}
|
||
|
||
if (pool) {
|
||
await pool.end().catch(() => {});
|
||
pool = null;
|
||
}
|
||
|
||
pool = new Pool({
|
||
host: config.host,
|
||
port: config.port,
|
||
database: config.database,
|
||
user: config.user,
|
||
password: config.password,
|
||
ssl: config.ssl ? { rejectUnauthorized: false } : false,
|
||
max: POOL_MAX,
|
||
idleTimeoutMillis: 30000,
|
||
connectionTimeoutMillis: 20000,
|
||
query_timeout: 180000,
|
||
statement_timeout: 180000,
|
||
});
|
||
|
||
pool.on('error', (error) => {
|
||
console.warn('[ProductDB] 连接池异常:', error.message);
|
||
});
|
||
|
||
cachedSignature = signature;
|
||
return pool;
|
||
}
|
||
|
||
/**
|
||
* 执行查询,返回行数组。
|
||
* @param {string} sqlText PostgreSQL 语句(使用 $1/$2 占位)
|
||
* @param {Array} params 参数数组
|
||
*/
|
||
async function query(sqlText, params = []) {
|
||
const maxAttempts = 2;
|
||
let lastError = null;
|
||
|
||
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||
try {
|
||
const activePool = await getPool();
|
||
const result = await activePool.query(sqlText, params);
|
||
return result.rows;
|
||
} catch (error) {
|
||
lastError = error;
|
||
if (error.code === 'PRODUCT_DB_NOT_CONFIGURED' || error.code === 'PRODUCT_DB_DRIVER_MISMATCH') {
|
||
throw error;
|
||
}
|
||
if (attempt < maxAttempts) {
|
||
console.warn(`[ProductDB] 查询失败,准备重连后重试 (${attempt}/${maxAttempts}): ${error.message}`);
|
||
await reset();
|
||
}
|
||
}
|
||
}
|
||
|
||
throw lastError;
|
||
}
|
||
|
||
async function reset() {
|
||
if (pool) {
|
||
await pool.end().catch(() => {});
|
||
}
|
||
pool = null;
|
||
cachedSignature = '';
|
||
}
|
||
|
||
async function test() {
|
||
try {
|
||
const rows = await query('SELECT 1 AS test');
|
||
return { success: true, message: '产品资料系统数据库连接成功', data: rows };
|
||
} catch (error) {
|
||
return { success: false, message: `连接失败: ${error.message}` };
|
||
}
|
||
}
|
||
|
||
module.exports = {
|
||
query,
|
||
test,
|
||
reset,
|
||
getPool,
|
||
CONFIG_NAME,
|
||
READY_VIEW,
|
||
LOCATION_VIEW,
|
||
};
|