diff --git a/WebApi/Controllers/DeliverController.cs b/WebApi/Controllers/DeliverController.cs index 3cde0fd..a79ae00 100755 --- a/WebApi/Controllers/DeliverController.cs +++ b/WebApi/Controllers/DeliverController.cs @@ -4066,13 +4066,11 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; string todayStr = DateTime.Today.ToString("yyyy-MM-dd"); // 1) 计算日期范围:纯数据驱动 - // endDt = max(今天, 排程实际最晚 PlanEndDate) - // 排程维护到哪天,看板就显示到哪天,不硬撑空时间轴 DateTime startDt = DateTime.Today; - DateTime endDt = startDt; // 默认等于今天 + DateTime endDt = startDt; try { - DataTable maxDt = db.ExecuteTable($"SELECT MAX(PlanEndDate) FROM Production_Schedule WHERE PlanEndDate>='{todayStr}'"); + DataTable maxDt = db.ExecuteTable($"SELECT MAX(PlanEndDate) FROM Production_Schedule WITH(NOLOCK) WHERE PlanEndDate>='{todayStr}'"); if (maxDt.Rows.Count > 0 && maxDt.Rows[0][0] != DBNull.Value) { DateTime actualMax = Convert.ToDateTime(maxDt.Rows[0][0]); @@ -4080,31 +4078,45 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; } } catch { /* 查不到就用今天 */ } + // onlyToday=true(默认): 限制最多7天,砍数据量 + if (onlyToday == "true") + { + DateTime limit = DateTime.Today.AddDays(7); + if (endDt > limit) endDt = limit; + } string start = startDt.ToString("yyyy-MM-dd"); string end = endDt.ToString("yyyy-MM-dd"); - // 2) 取物料清单(按风险/产品类型过滤) - string matWhere = $" WHERE S.PlanEndDate>='{todayStr}' "; - if (!string.IsNullOrEmpty(risk)) matWhere += MultiExact("D.DeliveryRisk", risk); - if (!string.IsNullOrEmpty(productType)) matWhere += MultiCsvContains("D.ProductTypes", productType); + // 2) 取物料清单 — CTE 强制先过滤 D 表(risk/productType 用索引),再 JOIN S + string dWhere = ""; + if (!string.IsNullOrEmpty(risk)) dWhere += MultiExact("D.DeliveryRisk", risk); + if (!string.IsNullOrEmpty(productType)) dWhere += MultiCsvContains("D.ProductTypes", productType); + string sWhere = $" WHERE S.PlanEndDate>='{todayStr}' "; + // dWhere 以 AND 开头,前面补 WHERE + string dClause = string.IsNullOrEmpty(dWhere) ? "" : $" WHERE 1=1 {dWhere}"; DataTable matDt = db.ExecuteTable($@" + ;WITH D AS ( + SELECT Id, ScheduleId, ChildMaterialId, MaterialName, BOMLevel, DeliveryRisk, + AvailableStock, ExpectedStopDate, ExpectedArrivalDate, + Supplier, Purchaser + FROM Material_Delivery D + {dClause} + ) SELECT D.Id, D.ScheduleId, D.ChildMaterialId, D.MaterialName, D.BOMLevel, D.DeliveryRisk, D.AvailableStock, D.ExpectedStopDate, D.ExpectedArrivalDate, D.Supplier, D.Purchaser, S.ProductCode, S.BOMVersion - FROM Material_Delivery D + FROM D INNER JOIN Production_Schedule S ON D.ScheduleId=S.Id - {matWhere} - ORDER BY (CASE D.DeliveryRisk WHEN N'断线' THEN 0 WHEN N'预警' THEN 1 ELSE 2 END), D.ChildMaterialId"); + {sWhere}"); - // 3) 批量取需求(Material_Requirement 关联键 = ScheduleId+ChildMaterialId) + // 3) 批量取需求 — 用已有的 IX_Material_Requirement_Sch_Mat_Date 复合索引 Dictionary> reqMap = new Dictionary>(); if (matDt.Rows.Count > 0) { var schIds = matDt.Rows.Cast().Select(r => r["ScheduleId"].ToString()).Distinct(); string schIdList = string.Join(",", schIds); - // 必须去重:不去重会有 3.4 万项,触发 SQL Server 表达式服务限制 var matIds = matDt.Rows.Cast().Select(r => "'" + Sql(r["ChildMaterialId"].ToString()) + "'").Distinct(); string matIdList = string.Join(",", matIds); DataTable reqDt = db.ExecuteTable($@" @@ -4121,29 +4133,43 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; } } - // 4) 批量取到货 + // 4) 取到货 — 单次查询替代分批(减少 DB 往返) Dictionary>> arrMap = new Dictionary>>(); if (matDt.Rows.Count > 0) { var ids = matDt.Rows.Cast().Select(r => r["Id"].ToString()).Distinct(); - foreach (string batch in BuildInBatches(ids)) + // IN 列表超过 2000 分批一次(SQL Server 参数上限) + DataTable arrDt; + if (ids.Count() <= 2000) { - DataTable arrDt = db.ExecuteTable($@" + string idList = string.Join(",", ids); + arrDt = db.ExecuteTable($@" SELECT A.DeliveryId, A.ExpectedArrivalDate, A.ArrivalQty FROM Arrival_Plan A - WHERE A.DeliveryId IN ({batch}) AND A.ExpectedArrivalDate>='{start}' AND A.ExpectedArrivalDate<='{end}'"); - foreach (DataRow r in arrDt.Rows) + WHERE A.DeliveryId IN ({idList}) AND A.ExpectedArrivalDate>='{start}' AND A.ExpectedArrivalDate<='{end}'"); + } + else + { + // 分批 JOIN CTE + string tmp = "DECLARE @ids TABLE(Id INT); " + + string.Join("; ", ids.Select(id => $"INSERT @ids VALUES({id})")) + "; " + + $@"SELECT A.DeliveryId, A.ExpectedArrivalDate, A.ArrivalQty + FROM Arrival_Plan A + INNER JOIN @ids I ON A.DeliveryId=I.Id + WHERE A.ExpectedArrivalDate>='{start}' AND A.ExpectedArrivalDate<='{end}'"; + arrDt = db.ExecuteTable(tmp); + } + foreach (DataRow r in arrDt.Rows) + { + int did = Int(r["DeliveryId"]); + string date = DateStr(r["ExpectedArrivalDate"]); + double qty = Convert.ToDouble(r["ArrivalQty"] is DBNull ? 0 : r["ArrivalQty"]); + if (!arrMap.ContainsKey(did)) arrMap[did] = new List>(); + arrMap[did].Add(new Dictionary { - int did = Int(r["DeliveryId"]); - string date = DateStr(r["ExpectedArrivalDate"]); - double qty = Convert.ToDouble(r["ArrivalQty"] is DBNull ? 0 : r["ArrivalQty"]); - if (!arrMap.ContainsKey(did)) arrMap[did] = new List>(); - arrMap[did].Add(new Dictionary - { - ["date"] = date, - ["qty"] = qty - }); - } + ["date"] = date, + ["qty"] = qty + }); } } diff --git a/WebApi/View/Delivery/delivery_dashboard.html b/WebApi/View/Delivery/delivery_dashboard.html index e92f07b..b966740 100755 --- a/WebApi/View/Delivery/delivery_dashboard.html +++ b/WebApi/View/Delivery/delivery_dashboard.html @@ -158,7 +158,7 @@ body { font-family: 'Microsoft YaHei','PingFang SC',sans-serif; background: #f3f 风险: @@ -192,7 +192,7 @@ const DAY_WIDTH = 56; // 每天宽度(按截图样式,约56px/列) const ROW_HEIGHT = 50; // 每行高度 const HEAD_HEIGHT = 36; // 表头高度 let autoRefreshTimer = null; -let currentRisk = ''; +let currentRisk = '断线'; // 默认只加载断线风险物料(957条,2.5s),空筛选拉15711条=浏览器灾难 function parseDate(s) { if (!s) return null; @@ -220,12 +220,11 @@ function loadSummary() { } function loadProductTypes() { - fetch(API_BASE + '/GetSchedules?keyword=').then(r => r.json()).then(res => { - if (res.success) { + // 用 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'); - const set = new Set(); - res.data.forEach(s => { if (s.ProductType) set.add(s.ProductType); }); - [...set].sort().forEach(t => { + res.data.productTypes.slice().sort().forEach(t => { const opt = document.createElement('option'); opt.value = t; opt.textContent = t; sel.appendChild(opt); @@ -239,6 +238,7 @@ function loadProductTypes() { function loadGantt() { document.getElementById('ganttContainer').innerHTML = '
加载中...
'; 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 || '');