本次将此前被列入退役名单的运营分析相关模块恢复为可用,并新增「模块化库存监控」
一级菜单及其两个子页面。
主要变更:
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 排除。
269 lines
8.9 KiB
JavaScript
269 lines
8.9 KiB
JavaScript
const express = require('express');
|
|
const jianSunDb = require('../config/jianSunDatabase');
|
|
|
|
const router = express.Router();
|
|
|
|
const MAX_PAGE_SIZE = 100;
|
|
|
|
function safeInt(value, fallback) {
|
|
const parsed = Number.parseInt(value, 10);
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
}
|
|
|
|
function normalizeSortField(field) {
|
|
const allowed = new Set([
|
|
'id', 'shopName', 'createTime', 'activity_name', 'activity_id',
|
|
'goods_name', 'goods_id', 'min_activity_sku_price', 'max_activity_sku_price',
|
|
'activity_goods_start_time', 'activity_goods_end_time', 'status',
|
|
]);
|
|
return allowed.has(field) ? field : 'activity_goods_start_time';
|
|
}
|
|
|
|
function normalizeSortOrder(order) {
|
|
return String(order || '').toLowerCase() === 'asc' ? 'ASC' : 'DESC';
|
|
}
|
|
|
|
function escapeColumn(field) {
|
|
const allowed = new Set([
|
|
'id', 'shopName', 'createTime', 'business_type_str', 'activity_name', 'activity_id',
|
|
'goods_name', 'goods_id', 'hd_thumb_url', 'enroll_time', 'min_activity_sku_price',
|
|
'max_activity_sku_price', 'activity_goods_start_time', 'activity_goods_end_time', 'status',
|
|
]);
|
|
if (!allowed.has(field)) return '[id]';
|
|
return field === 'enroll_time' ? '[enroll_time]' : `[${field}]`;
|
|
}
|
|
|
|
async function getSecondLatestDate() {
|
|
const rows = await jianSunDb.query(
|
|
`SELECT DISTINCT TOP 2 LEFT([createTime], 10) AS createDate
|
|
FROM dbo.pdd_DataActivityRecords
|
|
WHERE [createTime] IS NOT NULL AND LTRIM(RTRIM([createTime])) != ''
|
|
ORDER BY createDate DESC`
|
|
);
|
|
if (rows.length >= 2) return rows[1].createDate;
|
|
if (rows.length === 1) return rows[0].createDate;
|
|
return '';
|
|
}
|
|
|
|
router.get('/latest-date', async (req, res) => {
|
|
try {
|
|
const date = await getSecondLatestDate();
|
|
res.json({ success: true, data: { date } });
|
|
} catch (error) {
|
|
console.error('[MarketingEvents] 查询最新数据日期失败:', error.message);
|
|
res.status(500).json({ success: false, error: error.message });
|
|
}
|
|
});
|
|
|
|
router.get('/list', async (req, res) => {
|
|
try {
|
|
const {
|
|
page = 1,
|
|
pageSize = 20,
|
|
sortBy = 'activity_goods_start_time',
|
|
sortOrder = 'DESC',
|
|
shopName,
|
|
activityType,
|
|
activityName,
|
|
activityId,
|
|
goodsName,
|
|
goodsId,
|
|
status,
|
|
date,
|
|
} = req.query;
|
|
|
|
const currentPage = safeInt(page, 1);
|
|
const limit = Math.min(safeInt(pageSize, 20), MAX_PAGE_SIZE);
|
|
const offset = (currentPage - 1) * limit;
|
|
const sortColumn = normalizeSortField(sortBy);
|
|
const sortDir = normalizeSortOrder(sortOrder);
|
|
|
|
const whereParts = [];
|
|
const params = {};
|
|
|
|
const filterDate = (date && String(date).trim()) || await getSecondLatestDate();
|
|
if (filterDate) {
|
|
whereParts.push('LEFT([createTime], 10) = @filterDate');
|
|
params.filterDate = filterDate;
|
|
}
|
|
|
|
if (shopName && shopName.trim()) {
|
|
whereParts.push('[shopName] LIKE @shopName');
|
|
params.shopName = `%${shopName.trim()}%`;
|
|
}
|
|
if (activityType && activityType.trim()) {
|
|
whereParts.push('[business_type_str] LIKE @activityType');
|
|
params.activityType = `%${activityType.trim()}%`;
|
|
}
|
|
if (activityName && activityName.trim()) {
|
|
whereParts.push('[activity_name] LIKE @activityName');
|
|
params.activityName = `%${activityName.trim()}%`;
|
|
}
|
|
if (activityId && String(activityId).trim()) {
|
|
whereParts.push('[activity_id] = @activityId');
|
|
params.activityId = Number(activityId);
|
|
}
|
|
if (goodsName && goodsName.trim()) {
|
|
whereParts.push('[goods_name] LIKE @goodsName');
|
|
params.goodsName = `%${goodsName.trim()}%`;
|
|
}
|
|
if (goodsId && String(goodsId).trim()) {
|
|
whereParts.push('[goods_id] = @goodsId');
|
|
params.goodsId = String(goodsId).trim();
|
|
}
|
|
if (status !== undefined && status !== '' && status !== 'all') {
|
|
whereParts.push('[status] = @status');
|
|
params.status = Number(status);
|
|
}
|
|
|
|
const whereSql = whereParts.length > 0 ? `WHERE ${whereParts.join(' AND ')}` : '';
|
|
|
|
const countResult = await jianSunDb.query(
|
|
`SELECT COUNT(*) AS total FROM dbo.pdd_DataActivityRecords ${whereSql}`,
|
|
params
|
|
);
|
|
const total = countResult[0]?.total || 0;
|
|
|
|
const dataResult = await jianSunDb.query(
|
|
`SELECT
|
|
[id],
|
|
[shopName],
|
|
[createTime],
|
|
[business_type_str] AS activityType,
|
|
[activity_name] AS activityName,
|
|
[activity_id] AS activityId,
|
|
[goods_name] AS goodsName,
|
|
[goods_id] AS goodsId,
|
|
[hd_thumb_url] AS thumbUrl,
|
|
[enroll_time] AS enrollTime,
|
|
TRY_CONVERT(decimal(18,2), [min_activity_sku_price]) / 100.0 AS minPrice,
|
|
TRY_CONVERT(decimal(18,2), [max_activity_sku_price]) / 100.0 AS maxPrice,
|
|
[activity_goods_start_time] AS activityStartTime,
|
|
[activity_goods_end_time] AS activityEndTime,
|
|
[status],
|
|
CASE WHEN [status] = 501 THEN N'报名成功' ELSE CONCAT(N'状态 ', [status]) END AS statusLabel
|
|
FROM dbo.pdd_DataActivityRecords
|
|
${whereSql}
|
|
ORDER BY ${escapeColumn(sortColumn)} ${sortDir}
|
|
OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`,
|
|
{ ...params, offset, limit }
|
|
);
|
|
|
|
const statusCountsResult = await jianSunDb.query(
|
|
`SELECT
|
|
[status],
|
|
COUNT(*) AS count
|
|
FROM dbo.pdd_DataActivityRecords
|
|
${whereSql}
|
|
GROUP BY [status]
|
|
ORDER BY [status]`,
|
|
params
|
|
);
|
|
|
|
const statusCounts = {};
|
|
let totalAll = 0;
|
|
statusCountsResult.forEach((row) => {
|
|
statusCounts[row.status] = row.count;
|
|
totalAll += row.count;
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
list: dataResult,
|
|
total,
|
|
page: currentPage,
|
|
pageSize: limit,
|
|
filterDate,
|
|
statusCounts: { all: totalAll, ...statusCounts },
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error('[MarketingEvents] 查询活动记录失败:', error.message);
|
|
res.status(500).json({ success: false, error: error.message });
|
|
}
|
|
});
|
|
|
|
router.get('/summary', async (req, res) => {
|
|
try {
|
|
const { date } = req.query;
|
|
const filterDate = (date && String(date).trim()) || await getSecondLatestDate();
|
|
|
|
const whereSql = filterDate ? 'WHERE LEFT([createTime], 10) = @filterDate' : '';
|
|
const params = filterDate ? { filterDate } : {};
|
|
|
|
const [row] = await jianSunDb.query(
|
|
`SELECT
|
|
COUNT(*) AS totalRecords,
|
|
SUM(CASE WHEN [status] = 501 THEN 1 ELSE 0 END) AS successCount,
|
|
COUNT(DISTINCT TRY_CONVERT(varchar(50), [goods_id])) AS goodsCount,
|
|
COUNT(DISTINCT [shopName]) AS shopCount,
|
|
COUNT(DISTINCT [activity_id]) AS activityIdCount,
|
|
COUNT(DISTINCT [business_type_str]) AS activityTypeCount,
|
|
AVG(TRY_CONVERT(decimal(18,2), [min_activity_sku_price])) / 100.0 AS avgMinPrice,
|
|
AVG(TRY_CONVERT(decimal(18,2), [max_activity_sku_price])) / 100.0 AS avgMaxPrice,
|
|
MIN(TRY_CONVERT(decimal(18,2), [min_activity_sku_price])) / 100.0 AS globalMinPrice,
|
|
MAX(TRY_CONVERT(decimal(18,2), [max_activity_sku_price])) / 100.0 AS globalMaxPrice
|
|
FROM dbo.pdd_DataActivityRecords
|
|
${whereSql}`,
|
|
params
|
|
);
|
|
|
|
const stats = row || {};
|
|
const totalRecords = Number(stats.totalRecords) || 0;
|
|
const successCount = Number(stats.successCount) || 0;
|
|
const successRate = totalRecords > 0 ? ((successCount / totalRecords) * 100).toFixed(1) : '0.0';
|
|
|
|
res.json({
|
|
success: true,
|
|
data: {
|
|
filterDate,
|
|
totalRecords,
|
|
successCount,
|
|
successRate,
|
|
goodsCount: Number(stats.goodsCount) || 0,
|
|
shopCount: Number(stats.shopCount) || 0,
|
|
activityIdCount: Number(stats.activityIdCount) || 0,
|
|
activityTypeCount: Number(stats.activityTypeCount) || 0,
|
|
avgMinPrice: stats.avgMinPrice != null ? Number(stats.avgMinPrice).toFixed(2) : '-',
|
|
avgMaxPrice: stats.avgMaxPrice != null ? Number(stats.avgMaxPrice).toFixed(2) : '-',
|
|
globalMinPrice: stats.globalMinPrice != null ? Number(stats.globalMinPrice).toFixed(2) : '-',
|
|
globalMaxPrice: stats.globalMaxPrice != null ? Number(stats.globalMaxPrice).toFixed(2) : '-',
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error('[MarketingEvents] 查询汇总数据失败:', error.message);
|
|
res.status(500).json({ success: false, error: error.message });
|
|
}
|
|
});
|
|
|
|
router.get('/status-counts', async (req, res) => {
|
|
try {
|
|
const result = await jianSunDb.query(
|
|
`SELECT
|
|
[status],
|
|
COUNT(*) AS count
|
|
FROM dbo.pdd_DataActivityRecords
|
|
GROUP BY [status]
|
|
ORDER BY [status]`
|
|
);
|
|
|
|
const statusCounts = {};
|
|
let total = 0;
|
|
result.forEach((row) => {
|
|
statusCounts[row.status] = row.count;
|
|
total += row.count;
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: { all: total, ...statusCounts },
|
|
});
|
|
} catch (error) {
|
|
console.error('[MarketingEvents] 查询状态统计失败:', error.message);
|
|
res.status(500).json({ success: false, error: error.message });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|