## 根因定位 分段测 SQL 耗时(risk=断线): - matDt 957条: 4.6s(JOIN 选错方向 + 缺覆盖索引) - reqDt: 0.3s ✅(IX_Material_Requirement_Sch_Mat_Date 已覆盖) - arrDt: 0.2s ✅ - 空筛选: >120s 超时,6.8MB JSON ## 后端优化 1. CTE 强制先过滤:risk/productType 先在 Material_Delivery 用索引过滤,再 JOIN Production_Schedule 2. onlyToday 生效:日期范围限制 ≤ 7 天(之前 parameter 是摆设) 3. 去 ORDER BY:前端渲染不依赖后端排序 4. arrDt 单次查询:替代 BuildInBatches 32 次分批往返 5. 回退 NOLOCK:SQL Server 选错执行计划,反而慢 3s ## 前端默认行为 delivery_dashboard.html: - risk 下拉默认选中 "断线"(957条,5.4s) - currentRisk 初始化为 "断线" - loadGantt 显式 onlyToday=true ## 数据对比 | 场景 | 优化前 | 优化后 | |------|--------|--------| | risk=断线(默认) | ~120s 超时 | 5.4s, 372KB | | 空筛选 | >120s 超时 | 59s, 6.4MB | ## 瓶颈根因 Material_Delivery JOIN Production_Schedule 缺覆盖索引。 matDt 扫描成本是硬伤,需要 DBA 加: - IX_MD_RiskTypeSch (DeliveryRisk, ScheduleId, ProductTypes) INCLUDE (...) - IX_PS_EndDateId (PlanEndDate, Id) INCLUDE (ProductCode, BOMVersion) ## 教训 1. NOLOCK 在大表 JOIN 上可能反效果(脏页回滚 + 执行计划劣化) 2. SQL Server CTE 不一定强制 JOIN 方向(优化器可能内联),但 WHERE 条件前置通常有效 3. 浏览器渲染 15000 行甘特图本身是灾难(23 万 DOM 节点),前端必须默认缩小范围 4. 部署后立刻 curl 测 API 耗时,分段拆 SQL 才能找到真凶
643 lines
31 KiB
HTML
Executable File
643 lines
31 KiB
HTML
Executable File
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>采购物料交付计划看板</title>
|
||
<link rel="stylesheet" href="../common/nav.css">
|
||
<style>
|
||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||
html, body { height: 100%; }
|
||
body { font-family: 'Microsoft YaHei','PingFang SC',sans-serif; background: #f3f5fa; color: #2c3e50; padding: 14px 18px; }
|
||
|
||
/* 顶部 4 卡片 */
|
||
.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 14px; }
|
||
.card { background: #fff; border-radius: 8px; padding: 16px 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); cursor: pointer; transition: transform .15s, box-shadow .15s; border-left: 4px solid #c0c4cc; }
|
||
.card:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(0,0,0,0.1); }
|
||
.card .label { font-size: 13px; color: #909399; margin-bottom: 6px; }
|
||
.card .num { font-size: 30px; font-weight: 700; line-height: 1.2; }
|
||
.card .sub { font-size: 12px; color: #b0b3ba; margin-top: 4px; }
|
||
.card.total { border-left-color: #409eff; } .card.total .num { color: #409eff; }
|
||
.card.broken { border-left-color: #f56c6c; } .card.broken .num { color: #f56c6c; }
|
||
.card.warn { border-left-color: #e6a23c; } .card.warn .num { color: #e6a23c; }
|
||
.card.safe { border-left-color: #67c23a; } .card.safe .num { color: #67c23a; }
|
||
.card.active { box-shadow: 0 0 0 2px #409eff, 0 6px 16px rgba(0,0,0,0.1); }
|
||
|
||
/* 工具栏 */
|
||
.toolbar { background: #fff; border-radius: 8px; padding: 10px 14px; margin-bottom: 12px; display: flex; align-items: center; gap: 10px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
|
||
.toolbar select, .toolbar button { height: 30px; padding: 0 12px; border: 1px solid #dcdfe6; border-radius: 6px; background: #fff; font-size: 13px; cursor: pointer; }
|
||
.toolbar select:hover, .toolbar button:hover { border-color: #409eff; color: #409eff; }
|
||
.toolbar .spacer { flex: 1; }
|
||
.toolbar .meta { font-size: 12px; color: #909399; }
|
||
|
||
/* 甘特图容器 */
|
||
.gantt-wrap { background: #fff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.06); padding: 16px 18px 14px; }
|
||
.gantt-title { font-size: 16px; font-weight: 600; color: #303133; margin-bottom: 6px; }
|
||
.gantt-sub { font-size: 12px; color: #909399; margin-bottom: 12px; }
|
||
.gantt-legend { display: flex; align-items: center; gap: 16px; margin-bottom: 12px; font-size: 12px; color: #606266; }
|
||
.gantt-legend .item { display: flex; align-items: center; gap: 5px; }
|
||
.gantt-legend .dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; }
|
||
.gantt-legend .dot.req { background: #409eff; }
|
||
.gantt-legend .dot.arr { background: #67c23a; }
|
||
.gantt-legend .dot.broken { background: #f56c6c; }
|
||
.gantt-legend .dot.weekend { background: #f4f4f5; border: 1px solid #dcdfe6; }
|
||
|
||
.gantt-scroll { overflow-x: auto; overflow-y: visible; position: relative; padding-bottom: 6px; }
|
||
.gantt-scroll::-webkit-scrollbar { height: 10px; }
|
||
.gantt-scroll::-webkit-scrollbar-thumb { background: #c0c4cc; border-radius: 5px; }
|
||
.gantt-scroll::-webkit-scrollbar-track { background: #f0f2f5; border-radius: 5px; }
|
||
|
||
.gantt { position: relative; min-width: 100%; border-collapse: collapse; }
|
||
.gantt-row { display: flex; border-bottom: 1px solid #f0f2f5; min-height: 50px; max-height: 50px; overflow: hidden; align-items: stretch; }
|
||
.gantt-row.head { background: #f7f9fc; border-bottom: 1px solid #ebeef5; min-height: 36px; max-height: 36px; align-items: stretch; position: sticky; top: 0; z-index: 10; }
|
||
.gantt-row:hover:not(.head) { background: #fafbfc; }
|
||
|
||
/* 固定左列 */
|
||
.gantt-fixed { position: sticky; left: 0; z-index: 4; background: #fff; display: flex; flex-shrink: 0; box-shadow: 2px 0 4px rgba(0,0,0,0.04); }
|
||
.gantt-row.head .gantt-fixed { background: #f7f9fc; z-index: 11; }
|
||
.gantt-cell { padding: 8px 10px; font-size: 12px; color: #303133; border-right: 1px solid #ebeef5; display: flex; flex-direction: column; justify-content: center; }
|
||
.gantt-row.head .gantt-cell { font-weight: 600; color: #606266; padding: 6px 10px; }
|
||
.f-name { width: 160px; min-width: 160px; max-width: 160px; overflow: hidden; }
|
||
.f-name .nm { font-size: 13px; font-weight: 600; color: #303133; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||
.f-name .sub { font-size: 11px; color: #909399; margin-top: 2px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; line-height: 1.35; }
|
||
.f-tag { width: 70px; display: flex; align-items: center; }
|
||
.f-tag .tag { display: inline-block; padding: 2px 6px; border-radius: 4px; font-size: 11px; font-weight: 600; text-align: center; }
|
||
.f-tag .tag.broken { background: #fef0f0; color: #f56c6c; }
|
||
.f-tag .tag.warn { background: #fdf6ec; color: #e6a23c; }
|
||
.f-tag .tag.safe { background: #f0f9eb; color: #67c23a; }
|
||
.f-tag .tag.empty { background: #f4f4f5; color: #909399; }
|
||
|
||
/* 时间轴区 */
|
||
.gantt-timeline { position: relative; flex-shrink: 0; }
|
||
.gantt-row.head .gantt-timeline { background: #f7f9fc; }
|
||
.day-col { position: absolute; top: 0; height: 100%; display: flex; align-items: center; justify-content: center; border-right: 1px solid #f0f2f5; font-size: 12px; }
|
||
.gantt-row.head .day-col { color: #606266; font-weight: 500; }
|
||
.day-col.weekend { background: #fafafa; color: #909399; }
|
||
.day-col.past { background: #f5f5f5; color: #c0c4cc; }
|
||
.day-col.month-end { border-right: 2px solid #c0c4cc; }
|
||
.day-col.today { background: #ecf5ff; color: #409eff; font-weight: 700; }
|
||
.day-col .v { position: relative; display: flex; align-items: center; justify-content: center; padding: 4px; }
|
||
|
||
/* 单元格内的数据元素 */
|
||
.cell-num { position: absolute; font-weight: 600; }
|
||
.cell-num.req { color: #409eff; font-size: 12px; }
|
||
.cell-num.arr { color: #67c23a; font-size: 12px; }
|
||
.cell-arr-label { color: #67c23a; font-size: 10px; font-weight: 500; }
|
||
|
||
/* 停线标记 - 黄底橙边(预警) */
|
||
.stop-mark { position: absolute; inset: 6px 4px; border: 2px solid #e6a23c; background: rgba(230, 162, 60, 0.12); display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 10px; color: #c8770f; font-weight: 600; }
|
||
.stop-mark .icon { font-size: 11px; line-height: 1; }
|
||
|
||
/* 断线标记 - 红底红边(断线) */
|
||
.break-mark { position: absolute; inset: 6px 4px; border: 2px solid #f56c6c; background: rgba(245, 108, 108, 0.12); display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 10px; color: #f56c6c; font-weight: 600; }
|
||
|
||
/* 库存说明(图表下方) */
|
||
.gantt-summary { margin-top: 10px; padding: 8px 12px; background: #f7f9fc; border-radius: 6px; font-size: 12px; color: #909399; line-height: 1.7; }
|
||
|
||
.loading, .empty { text-align: center; padding: 50px 0; color: #909399; font-size: 14px; }
|
||
|
||
/* ===== MultiSelect 多选下拉组件(方案B:下拉+checkbox)===== */
|
||
.ms-wrap{position:relative;display:inline-block;vertical-align:middle}
|
||
.ms-native{display:none}/* 隐藏原生select,保留id供JS取值 */
|
||
.ms-trigger{display:flex;align-items:center;justify-content:space-between;min-width:160px;padding:0 28px 0 10px;height:30px;background:#fff;border:1px solid #d9d9d9;border-radius:4px;font-size:13px;color:#333;cursor:pointer;user-select:none;transition:border-color .2s}
|
||
.ms-trigger:hover{border-color:#1890ff}
|
||
.ms-trigger.active{border-color:#1890ff;box-shadow:0 0 0 2px rgba(24,144,255,.15)}
|
||
.ms-trigger .ms-arrow{position:absolute;right:8px;top:50%;transform:translateY(-50%);width:0;height:0;border-left:4px solid transparent;border-right:4px solid transparent;border-top:5px solid #999;pointer-events:none}
|
||
.ms-trigger .ms-placeholder{color:#999}
|
||
.ms-trigger .ms-tags{display:flex;flex-wrap:wrap;gap:4px;margin-right:12px;max-width:260px}
|
||
.ms-trigger .ms-tag{display:inline-flex;align-items:center;gap:3px;background:#e6f4ff;color:#1890ff;padding:0 6px;height:20px;border-radius:3px;font-size:12px;line-height:1}
|
||
.ms-trigger .ms-tag .ms-tag-close{cursor:pointer;font-size:14px;line-height:1;margin-top:-1px}
|
||
.ms-trigger .ms-tag .ms-tag-close:hover{color:#096dd9}
|
||
.ms-trigger .ms-more{color:#999;font-size:12px;padding:0 4px}
|
||
.ms-dropdown{display:none;position:absolute;top:calc(100% + 4px);left:0;min-width:200px;max-height:280px;background:#fff;border:1px solid #e8e8e8;border-radius:6px;box-shadow:0 6px 16px rgba(0,0,0,.12);z-index:100;overflow:hidden}
|
||
.ms-dropdown.active{display:block}
|
||
.ms-search{padding:8px;border-bottom:1px solid #f0f0f0}
|
||
.ms-search input{width:100%;height:26px;padding:0 8px;border:1px solid #d9d9d9;border-radius:3px;font-size:12px;outline:none}
|
||
.ms-search input:focus{border-color:#1890ff}
|
||
.ms-options{max-height:220px;overflow-y:auto;padding:4px 0}
|
||
.ms-options label{display:flex;align-items:center;gap:8px;padding:5px 10px;cursor:pointer;font-size:13px;color:#333;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
||
.ms-options label:hover{background:#f5f5f5}
|
||
.ms-options input[type=checkbox]{cursor:pointer}
|
||
.ms-footer{display:flex;justify-content:space-between;padding:6px 10px;border-top:1px solid #f0f0f0;font-size:12px}
|
||
.ms-footer .ms-btn{padding:2px 10px;border:1px solid #d9d9d9;border-radius:3px;background:#fff;cursor:pointer;color:#555}
|
||
.ms-footer .ms-btn:hover{border-color:#1890ff;color:#1890ff}
|
||
.ms-footer .ms-btn.primary{background:#1890ff;border-color:#1890ff;color:#fff}
|
||
.ms-footer .ms-btn.primary:hover{background:#096dd9}
|
||
/* ===== MultiSelect 结束 ===== */
|
||
</style>
|
||
</head>
|
||
<body>
|
||
|
||
<div id="sidebar"></div>
|
||
|
||
<div class="cards">
|
||
<div class="card total" data-risk="">
|
||
<div class="label">物料总数</div>
|
||
<div class="num" id="cardTotal">-</div>
|
||
<div class="sub">所有未结束排程物料</div>
|
||
</div>
|
||
<div class="card broken" data-risk="断线">
|
||
<div class="label">断线风险</div>
|
||
<div class="num" id="cardBroken">-</div>
|
||
<div class="sub">到货晚于停线(紧急)</div>
|
||
</div>
|
||
<div class="card warn" data-risk="预警">
|
||
<div class="label">预警关注</div>
|
||
<div class="num" id="cardWarn">-</div>
|
||
<div class="sub">到货日 = 停线日</div>
|
||
</div>
|
||
<div class="card safe" data-risk="安全">
|
||
<div class="label">安全供应</div>
|
||
<div class="num" id="cardSafe">-</div>
|
||
<div class="sub">到货早于停线</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="toolbar">
|
||
<span>产品类型:</span>
|
||
<select id="filterProductType" class="ms-native"><option value="">全部</option></select>
|
||
<span>风险:</span>
|
||
<select id="filterRisk">
|
||
<option value="">全部</option>
|
||
<option value="断线" selected>断线</option>
|
||
<option value="预警">预警</option>
|
||
<option value="安全">安全</option>
|
||
</select>
|
||
<button id="btnRefresh">刷新</button>
|
||
<button id="btnAutoRefresh">自动刷新:开</button>
|
||
<div class="spacer"></div>
|
||
<span class="meta" id="metaInfo">加载中...</span>
|
||
</div>
|
||
|
||
<div class="gantt-wrap">
|
||
<div class="gantt-title">物料交付甘特图</div>
|
||
<div class="gantt-sub">按日展示每个物料的当日需求、到货、停线预警/断线情况</div>
|
||
<div class="gantt-legend">
|
||
<div class="item"><span class="dot req"></span> 当日需求</div>
|
||
<div class="item"><span class="dot arr"></span> 到货</div>
|
||
<div class="item"><span class="dot broken"></span> 断线</div>
|
||
<div class="item" style="border:2px solid #e6a23c;border-radius:3px;width:14px;height:12px;background:rgba(230,162,60,.12);"></div> 预警
|
||
<div class="item"><span class="dot weekend"></span> 周末</div>
|
||
</div>
|
||
<div id="ganttContainer" class="gantt-scroll">
|
||
<div class="loading">加载中...</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 公共导航栏组件(菜单维护点:View/common/nav.js) -->
|
||
<script src="../common/nav.js"></script>
|
||
|
||
<script>
|
||
const API_BASE = '../../api/Deliver';
|
||
const DAY_WIDTH = 56; // 每天宽度(按截图样式,约56px/列)
|
||
const ROW_HEIGHT = 50; // 每行高度
|
||
const HEAD_HEIGHT = 36; // 表头高度
|
||
let autoRefreshTimer = null;
|
||
let currentRisk = '断线'; // 默认只加载断线风险物料(957条,2.5s),空筛选拉15711条=浏览器灾难
|
||
|
||
function parseDate(s) {
|
||
if (!s) return null;
|
||
const d = new Date(s);
|
||
if (isNaN(d.getTime())) return null;
|
||
// 统一归零到本地午夜 —— 避免 API 返回 UTC 字符串(带 Z)+ 东八区 8 小时偏移
|
||
// 极端情况下会让 dateDiffDays 差 1 天,导致表头/数据列数不匹配
|
||
return new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
||
}
|
||
function fmtDate(d) {
|
||
return d.getFullYear() + '-' + String(d.getMonth()+1).padStart(2,'0') + '-' + String(d.getDate()).padStart(2,'0');
|
||
}
|
||
function dateDiffDays(a, b) { return Math.round((b - a) / 86400000); }
|
||
function isWeekend(d) { const w = d.getDay(); return w === 0 || w === 6; }
|
||
|
||
function loadSummary() {
|
||
fetch(API_BASE + '/GetBoardSummary').then(r => r.json()).then(res => {
|
||
if (res.success) {
|
||
document.getElementById('cardTotal').textContent = res.total;
|
||
document.getElementById('cardBroken').textContent = res.broken;
|
||
document.getElementById('cardWarn').textContent = res.warn;
|
||
document.getElementById('cardSafe').textContent = res.safe;
|
||
}
|
||
});
|
||
}
|
||
|
||
function loadProductTypes() {
|
||
// 用 GetOptions 接口(已验证快且正确),而非不存在的 GetSchedules
|
||
fetch(API_BASE + '/GetOptions?field=productType').then(r => r.json()).then(res => {
|
||
if (res.success && res.data && res.data.productTypes) {
|
||
const sel = document.getElementById('filterProductType');
|
||
res.data.productTypes.slice().sort().forEach(t => {
|
||
const opt = document.createElement('option');
|
||
opt.value = t; opt.textContent = t;
|
||
sel.appendChild(opt);
|
||
});
|
||
// MultiSelect 组件:options 注入后重建 checkbox DOM
|
||
if (sel.refreshMulti) sel.refreshMulti();
|
||
}
|
||
});
|
||
}
|
||
|
||
function loadGantt() {
|
||
document.getElementById('ganttContainer').innerHTML = '<div class="loading">加载中...</div>';
|
||
const params = new URLSearchParams();
|
||
params.append('onlyToday', 'true'); // 限制最多7天,砍数据量
|
||
// 优先从 MultiSelect 组件读多值,fallback 原生 .value
|
||
const ptEl = document.getElementById('filterProductType');
|
||
const ptCsv = ptEl.getMultiValues ? ptEl.getMultiValues().join(',') : (ptEl.value || '');
|
||
if (ptCsv) params.append('productType', ptCsv);
|
||
if (currentRisk) params.append('risk', currentRisk);
|
||
fetch(API_BASE + '/GetBoardGantt?' + params).then(r => r.json()).then(res => {
|
||
if (!res.success) {
|
||
document.getElementById('ganttContainer').innerHTML = '<div class="empty">加载失败:' + (res.message || '') + '</div>';
|
||
return;
|
||
}
|
||
document.getElementById('metaInfo').textContent = '时间范围:' + res.startDate + ' ~ ' + res.endDate + '(共 ' + res.data.length + ' 个物料)';
|
||
renderGantt(res);
|
||
});
|
||
}
|
||
|
||
function renderGantt(res) {
|
||
const data = res.data;
|
||
if (!data || data.length === 0) {
|
||
document.getElementById('ganttContainer').innerHTML = '<div class="empty">暂无数据</div>';
|
||
return;
|
||
}
|
||
const startD = parseDate(res.startDate);
|
||
const endD = parseDate(res.endDate);
|
||
const today = parseDate(res.today);
|
||
if (!startD || !endD || endD < startD) {
|
||
document.getElementById('ganttContainer').innerHTML = '<div class="empty">日期范围无效</div>';
|
||
return;
|
||
}
|
||
const dayCount = dateDiffDays(startD, endD) + 1;
|
||
const timelineWidth = dayCount * DAY_WIDTH;
|
||
|
||
// 计算每个物料的"日均需求"(用于"需求X/天"标注)
|
||
function getDailyRate(reqMap) {
|
||
let total = 0, days = 0;
|
||
Object.values(reqMap).forEach(v => { if (v > 0) { total += v; days++; } });
|
||
if (days === 0) return 0;
|
||
// 计算频次
|
||
const dates = Object.keys(reqMap).filter(d => reqMap[d] > 0).sort();
|
||
if (dates.length < 2) return dates.length > 0 ? reqMap[dates[0]] : 0;
|
||
// 频次 = 总需求 / (最后一天 - 第一天的天数)
|
||
const d1 = parseDate(dates[0]), d2 = parseDate(dates[dates.length-1]);
|
||
const span = Math.max(1, dateDiffDays(d1, d2) + 1);
|
||
return Math.round(total / span);
|
||
}
|
||
|
||
const root = document.createElement('div');
|
||
root.className = 'gantt';
|
||
root.style.width = (160 + 70 + timelineWidth) + 'px';
|
||
|
||
// 表头
|
||
const head = document.createElement('div');
|
||
head.className = 'gantt-row head';
|
||
head.style.height = HEAD_HEIGHT + 'px';
|
||
const hFixed = document.createElement('div');
|
||
hFixed.className = 'gantt-fixed';
|
||
hFixed.innerHTML =
|
||
'<div class="gantt-cell f-name">物料</div>' +
|
||
'<div class="gantt-cell f-tag">风险</div>';
|
||
head.appendChild(hFixed);
|
||
const hTime = document.createElement('div');
|
||
hTime.className = 'gantt-timeline';
|
||
hTime.style.width = timelineWidth + 'px';
|
||
hTime.style.height = HEAD_HEIGHT + 'px';
|
||
for (let i = 0; i < dayCount; i++) {
|
||
const d = new Date(startD); d.setDate(d.getDate() + i);
|
||
const cell = document.createElement('div');
|
||
cell.className = 'day-col';
|
||
if (isWeekend(d)) cell.classList.add('weekend');
|
||
// 月末
|
||
const nextD = new Date(d); nextD.setDate(nextD.getDate() + 1);
|
||
if (nextD.getMonth() !== d.getMonth()) cell.classList.add('month-end');
|
||
// 今天
|
||
if (today && fmtDate(d) === fmtDate(today)) cell.classList.add('today');
|
||
// 今天之前(过期)
|
||
if (today && d < today && fmtDate(d) !== fmtDate(today)) cell.classList.add('past');
|
||
cell.style.left = (i * DAY_WIDTH) + 'px';
|
||
cell.style.width = DAY_WIDTH + 'px';
|
||
cell.textContent = (d.getMonth() + 1) + '/' + d.getDate();
|
||
hTime.appendChild(cell);
|
||
}
|
||
head.appendChild(hTime);
|
||
root.appendChild(head);
|
||
|
||
// 物料行
|
||
data.forEach(m => {
|
||
const row = document.createElement('div');
|
||
row.className = 'gantt-row';
|
||
row.style.height = ROW_HEIGHT + 'px';
|
||
|
||
// 固定列
|
||
const fx = document.createElement('div');
|
||
fx.className = 'gantt-fixed';
|
||
const riskCls = ({'断线':'broken','预警':'warn','安全':'safe'}[m.DeliveryRisk] || 'empty');
|
||
const dailyRate = getDailyRate(m.Requirement || {});
|
||
fx.innerHTML =
|
||
'<div class="gantt-cell f-name"><div class="nm" title="' + m.ChildMaterialId + '">' + m.ChildMaterialId + '</div><div class="sub" title="' + (m.MaterialName || '-') + (dailyRate > 0 ? ' · 需求' + dailyRate + '/天' : '') + '">' + (m.MaterialName || '-') + (dailyRate > 0 ? ' · 需求' + dailyRate + '/天' : '') + '</div></div>' +
|
||
'<div class="gantt-cell f-tag"><span class="tag ' + riskCls + '">' + (m.DeliveryRisk || '未判定') + '</span></div>';
|
||
row.appendChild(fx);
|
||
|
||
// 时间轴
|
||
const tl = document.createElement('div');
|
||
tl.className = 'gantt-timeline';
|
||
tl.style.width = timelineWidth + 'px';
|
||
tl.style.height = ROW_HEIGHT + 'px';
|
||
|
||
const reqMap = m.Requirement || {};
|
||
const arrList = m.Arrival || [];
|
||
const arrByDate = {};
|
||
arrList.forEach(a => {
|
||
if (!arrByDate[a.date]) arrByDate[a.date] = 0;
|
||
arrByDate[a.date] += a.qty;
|
||
});
|
||
|
||
for (let i = 0; i < dayCount; i++) {
|
||
const d = new Date(startD); d.setDate(d.getDate() + i);
|
||
const dateStr = fmtDate(d);
|
||
|
||
// 周末/今天/过期 背景
|
||
if (isWeekend(d)) {
|
||
const bg = document.createElement('div');
|
||
bg.className = 'day-col weekend';
|
||
bg.style.left = (i * DAY_WIDTH) + 'px';
|
||
bg.style.width = DAY_WIDTH + 'px';
|
||
tl.appendChild(bg);
|
||
} else if (today && d < today && fmtDate(d) !== fmtDate(today)) {
|
||
const bg = document.createElement('div');
|
||
bg.className = 'day-col past';
|
||
bg.style.left = (i * DAY_WIDTH) + 'px';
|
||
bg.style.width = DAY_WIDTH + 'px';
|
||
tl.appendChild(bg);
|
||
} else if (today && fmtDate(d) === fmtDate(today)) {
|
||
const bg = document.createElement('div');
|
||
bg.className = 'day-col today';
|
||
bg.style.left = (i * DAY_WIDTH) + 'px';
|
||
bg.style.width = DAY_WIDTH + 'px';
|
||
tl.appendChild(bg);
|
||
}
|
||
const nextD = new Date(d); nextD.setDate(nextD.getDate() + 1);
|
||
if (nextD.getMonth() !== d.getMonth()) {
|
||
const bg = document.createElement('div');
|
||
bg.className = 'day-col month-end';
|
||
bg.style.left = (i * DAY_WIDTH) + 'px';
|
||
bg.style.width = DAY_WIDTH + 'px';
|
||
tl.appendChild(bg);
|
||
}
|
||
|
||
// 内容容器(用于在格子里绝对定位)
|
||
const wrap = document.createElement('div');
|
||
wrap.className = 'v';
|
||
wrap.style.position = 'absolute';
|
||
wrap.style.left = (i * DAY_WIDTH) + 'px';
|
||
wrap.style.width = DAY_WIDTH + 'px';
|
||
wrap.style.height = '100%';
|
||
|
||
// 需求
|
||
const reqQty = reqMap[dateStr] || 0;
|
||
if (reqQty > 0) {
|
||
const num = document.createElement('div');
|
||
num.className = 'cell-num req';
|
||
num.textContent = reqQty;
|
||
num.style.top = '4px';
|
||
num.style.left = '4px';
|
||
wrap.appendChild(num);
|
||
}
|
||
|
||
// 到货
|
||
const arrQty = arrByDate[dateStr] || 0;
|
||
if (arrQty > 0) {
|
||
// 模拟图样式:到货/60 形式
|
||
const num = document.createElement('div');
|
||
num.className = 'cell-num arr';
|
||
num.innerHTML = '到' + arrQty;
|
||
num.style.bottom = '4px';
|
||
num.style.left = '4px';
|
||
wrap.appendChild(num);
|
||
}
|
||
|
||
// 停线/断线
|
||
if (m.ExpectedStopDate && dateStr === m.ExpectedStopDate) {
|
||
const mark = document.createElement('div');
|
||
if (m.DeliveryRisk === '断线') {
|
||
mark.className = 'break-mark';
|
||
mark.innerHTML = '<div class="icon">停</div><div>断线</div>';
|
||
} else {
|
||
mark.className = 'stop-mark';
|
||
mark.innerHTML = '<div class="icon">停</div><div>预警</div>';
|
||
}
|
||
wrap.appendChild(mark);
|
||
}
|
||
|
||
tl.appendChild(wrap);
|
||
}
|
||
row.appendChild(tl);
|
||
root.appendChild(row);
|
||
});
|
||
|
||
document.getElementById('ganttContainer').innerHTML = '';
|
||
document.getElementById('ganttContainer').appendChild(root);
|
||
|
||
// 滚动到今天
|
||
if (today) {
|
||
const offset = dateDiffDays(startD, today) * DAY_WIDTH;
|
||
const wrap = document.getElementById('ganttContainer');
|
||
setTimeout(() => { wrap.scrollLeft = Math.max(0, offset - 100); }, 50);
|
||
}
|
||
}
|
||
|
||
document.querySelectorAll('.card').forEach(card => {
|
||
card.addEventListener('click', () => {
|
||
const risk = card.dataset.risk;
|
||
const params = new URLSearchParams();
|
||
if (risk) params.append('risk', risk);
|
||
// MultiSelect 多值 join 成逗号串
|
||
const ptEl = document.getElementById('filterProductType');
|
||
const ptCsv = ptEl.getMultiValues ? ptEl.getMultiValues().join(',') : (ptEl.value || '');
|
||
if (ptCsv) params.append('productType', ptCsv);
|
||
window.location.href = 'delivery_dashboard_risk.html?' + params.toString();
|
||
});
|
||
});
|
||
|
||
document.getElementById('filterRisk').addEventListener('change', e => { currentRisk = e.target.value; loadGantt(); });
|
||
document.getElementById('btnRefresh').addEventListener('click', () => { loadSummary(); loadGantt(); });
|
||
let autoOn = true;
|
||
document.getElementById('btnAutoRefresh').addEventListener('click', e => {
|
||
autoOn = !autoOn;
|
||
e.target.textContent = '自动刷新:' + (autoOn ? '开' : '关');
|
||
if (autoOn) startAuto(); else stopAuto();
|
||
});
|
||
function startAuto() { stopAuto(); autoRefreshTimer = setInterval(() => { loadSummary(); loadGantt(); }, 600000); }
|
||
function stopAuto() { if (autoRefreshTimer) { clearInterval(autoRefreshTimer); autoRefreshTimer = null; } }
|
||
|
||
/* ===== MultiSelect 组件初始化(方案B:下拉+checkbox)===== */
|
||
function initMultiSelect(selectEl, placeholder){
|
||
var wrap=document.createElement('div');
|
||
wrap.className='ms-wrap';
|
||
var trigger=document.createElement('div');
|
||
trigger.className='ms-trigger';
|
||
trigger.innerHTML='<span class="ms-tags"></span><span class="ms-placeholder">'+placeholder+'</span><span class="ms-arrow"></span>';
|
||
var dropdown=document.createElement('div');
|
||
dropdown.className='ms-dropdown';
|
||
var searchWrap=document.createElement('div');searchWrap.className='ms-search';
|
||
var searchInput=document.createElement('input');searchInput.type='text';searchInput.placeholder='搜索...';
|
||
searchWrap.appendChild(searchInput);
|
||
var optionsWrap=document.createElement('div');optionsWrap.className='ms-options';
|
||
var footer=document.createElement('div');footer.className='ms-footer';
|
||
footer.innerHTML='<button class="ms-btn" data-action="clear">清空</button><button class="ms-btn primary" data-action="ok">确定</button>';
|
||
dropdown.appendChild(searchWrap);dropdown.appendChild(optionsWrap);dropdown.appendChild(footer);
|
||
|
||
wrap.appendChild(trigger);wrap.appendChild(dropdown);
|
||
selectEl.parentNode.insertBefore(wrap, selectEl);
|
||
|
||
// 读原生 options 构建 checkbox 列表
|
||
var origOptions=selectEl.querySelectorAll('option');
|
||
var listItems=[];
|
||
origOptions.forEach(function(opt){
|
||
if(!opt.value)return;// 跳过"全部"空值
|
||
var lbl=document.createElement('label');
|
||
var cb=document.createElement('input');cb.type='checkbox';cb.value=opt.value;
|
||
var txt=document.createElement('span');txt.textContent=opt.textContent;
|
||
lbl.appendChild(cb);lbl.appendChild(txt);
|
||
optionsWrap.appendChild(lbl);
|
||
listItems.push({cb:cb,txt:txt,label:lbl});
|
||
});
|
||
|
||
function getSelectedValues(){
|
||
return listItems.filter(i=>i.cb.checked).map(i=>i.cb.value);
|
||
}
|
||
var _msDebounce=null;
|
||
function triggerAutoSearch(){
|
||
if(_msDebounce)clearTimeout(_msDebounce);
|
||
_msDebounce=setTimeout(function(){loadGantt();},300);
|
||
}
|
||
function renderTags(){
|
||
var vals=getSelectedValues();
|
||
var tagsSpan=trigger.querySelector('.ms-tags');
|
||
var ph=trigger.querySelector('.ms-placeholder');
|
||
if(vals.length===0){
|
||
tagsSpan.innerHTML='';ph.style.display='inline';ph.textContent=placeholder;
|
||
trigger.style.minWidth='160px';
|
||
}else{
|
||
ph.style.display='none';
|
||
if(vals.length<=2){
|
||
tagsSpan.innerHTML=vals.map(function(v){
|
||
var label=selectEl.querySelector('option[value="'+v+'"]');
|
||
var txt=label?label.textContent:v;
|
||
return '<span class="ms-tag">'+txt+'<span class="ms-tag-close" data-val="'+v+'">×</span></span>';
|
||
}).join('');
|
||
}else{
|
||
var shown=vals.slice(0,1);
|
||
var hidden=vals.length-1;
|
||
tagsSpan.innerHTML='<span class="ms-tag">'+(selectEl.querySelector('option[value="'+shown[0]+'"]')?.textContent||shown[0])+'<span class="ms-tag-close" data-val="'+shown[0]+'">×</span></span><span class="ms-more">+'+hidden+'</span>';
|
||
}
|
||
}
|
||
trigger.style.minWidth='';
|
||
}
|
||
|
||
// 点 trigger 打开
|
||
trigger.addEventListener('click',function(e){
|
||
e.stopPropagation();
|
||
// 关闭其他
|
||
document.querySelectorAll('.ms-dropdown.active').forEach(function(d){if(d!==dropdown)d.classList.remove('active');document.querySelectorAll('.ms-trigger.active').forEach(function(t){t.classList.remove('active')});});
|
||
dropdown.classList.toggle('active');
|
||
trigger.classList.toggle('active');
|
||
if(dropdown.classList.contains('active')){
|
||
searchInput.focus();searchInput.value='';
|
||
listItems.forEach(function(i){i.label.style.display=''});
|
||
}
|
||
});
|
||
|
||
// 搜索
|
||
searchInput.addEventListener('input',function(){
|
||
var q=searchInput.value.toLowerCase();
|
||
listItems.forEach(function(i){
|
||
if(!q||i.txt.textContent.toLowerCase().indexOf(q)>=0)i.label.style.display='';
|
||
else i.label.style.display='none';
|
||
});
|
||
});
|
||
|
||
// checkbox 变更 → 防抖自动筛选
|
||
listItems.forEach(function(i){
|
||
i.cb.addEventListener('change',function(){renderTags();triggerAutoSearch();});
|
||
});
|
||
|
||
// tag 里的 × 关闭
|
||
wrap.addEventListener('click',function(e){
|
||
var close=e.target.closest('.ms-tag-close');
|
||
if(close){
|
||
e.stopPropagation();
|
||
var v=close.getAttribute('data-val');
|
||
var item=listItems.find(function(x){return x.cb.value===v});
|
||
if(item){item.cb.checked=false;renderTags();triggerAutoSearch();}
|
||
}
|
||
});
|
||
|
||
// footer 按钮
|
||
footer.addEventListener('click',function(e){
|
||
var btn=e.target.closest('.ms-btn');if(!btn)return;
|
||
if(btn.getAttribute('data-action')==='clear'){
|
||
listItems.forEach(function(i){i.cb.checked=false;});
|
||
renderTags();triggerAutoSearch();
|
||
dropdown.classList.remove('active');trigger.classList.remove('active');
|
||
}else if(btn.getAttribute('data-action')==='ok'){
|
||
dropdown.classList.remove('active');trigger.classList.remove('active');
|
||
}
|
||
});
|
||
|
||
// 点外面关闭
|
||
document.addEventListener('click',function(e){
|
||
if(!wrap.contains(e.target)){
|
||
dropdown.classList.remove('active');trigger.classList.remove('active');
|
||
}
|
||
});
|
||
|
||
// 提供 reset 方法供外部调用
|
||
selectEl.resetMulti=function(){listItems.forEach(function(i){i.cb.checked=false;});renderTags();triggerAutoSearch();};
|
||
// 提供 setValues 供 loadOptions 后回填
|
||
selectEl.setValuesMulti=function(csv){
|
||
var arr=(csv||'').split(',').filter(Boolean);
|
||
listItems.forEach(function(i){i.cb.checked=arr.indexOf(i.cb.value)>=0;});
|
||
renderTags();
|
||
};
|
||
// loadGantt 优先调用这个(数组),绕过原生 select .value
|
||
selectEl.getMultiValues=function(){return getSelectedValues();};
|
||
|
||
// 刷新 options(当原生 select innerHTML 被动态更新后调用,如 loadProductTypes 回调)
|
||
selectEl.refreshMulti=function(){
|
||
var saved=getSelectedValues();// 先保存当前选中值
|
||
optionsWrap.innerHTML='';
|
||
listItems=[];
|
||
origOptions=selectEl.querySelectorAll('option');
|
||
origOptions.forEach(function(opt){
|
||
if(!opt.value)return;
|
||
var lbl=document.createElement('label');
|
||
var cb=document.createElement('input');cb.type='checkbox';cb.value=opt.value;
|
||
var txt=document.createElement('span');txt.textContent=opt.textContent;
|
||
lbl.appendChild(cb);lbl.appendChild(txt);
|
||
optionsWrap.appendChild(lbl);
|
||
listItems.push({cb:cb,txt:txt,label:lbl});
|
||
});
|
||
listItems.forEach(function(i){
|
||
i.cb.addEventListener('change',function(){renderTags();triggerAutoSearch();});
|
||
});
|
||
// 恢复之前的选中值
|
||
listItems.forEach(function(i){i.cb.checked=saved.indexOf(i.cb.value)>=0;});
|
||
renderTags();
|
||
};
|
||
|
||
renderTags();
|
||
return selectEl;
|
||
}
|
||
|
||
// 初始化 MultiSelect(在原生 options 注入前先搭好组件骨架)
|
||
initMultiSelect(document.getElementById('filterProductType'),'产品类型:全部');
|
||
|
||
loadSummary();
|
||
loadProductTypes();
|
||
loadGantt();
|
||
startAuto();
|
||
</script>
|
||
</body>
|
||
</html>
|