diff --git a/WebApi/Controllers/DeliverController.cs b/WebApi/Controllers/DeliverController.cs index 5002d37..d11cd40 100755 --- a/WebApi/Controllers/DeliverController.cs +++ b/WebApi/Controllers/DeliverController.cs @@ -117,6 +117,50 @@ namespace GXWebApi.Controllers } } + // ===== 多选筛选辅助(前端下拉传逗号分隔值,后端拆成 OR 条件)===== + + /// + /// 多值 LIKE 模糊匹配(OR 语义)。例:supplier=A,B → (Supplier LIKE '%A%' OR Supplier LIKE '%B%') + /// + private static string MultiLike(string field, string csv, bool useNVarChar = true) + { + if (string.IsNullOrEmpty(csv)) return ""; + var parts = csv.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Where(v => !string.IsNullOrEmpty(v)).Distinct().ToList(); + if (parts.Count == 0) return ""; + string q = useNVarChar ? "N" : ""; + if (parts.Count == 1) return $" AND ISNULL({field},'') LIKE {q}'%{parts[0]}%' "; + return " AND (" + string.Join(" OR ", parts.Select(v => $"ISNULL({field},'') LIKE {q}'%{v}%'")) + ") "; + } + + /// + /// 多值精确匹配(OR 语义)。例:risk=断线,预警 → (Risk=N'断线' OR Risk=N'预警') + /// + private static string MultiExact(string field, string csv) + { + if (string.IsNullOrEmpty(csv)) return ""; + var parts = csv.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Where(v => !string.IsNullOrEmpty(v)).Distinct().ToList(); + if (parts.Count == 0) return ""; + if (parts.Count == 1) return $" AND {field}=N'{parts[0]}' "; + return " AND (" + string.Join(" OR ", parts.Select(v => $"{field}=N'{v}'")) + ") "; + } + + /// + /// 多值检查逗号拼接字段是否"包含"(OR 语义)。 + /// 例:ProductTypes='SI,M4' → productType=SI → (','+ISNULL(ProductTypes,'')+',') LIKE '%,SI,%' + /// productType=SI,M4 → (','+field+',' LIKE '%,SI,%' OR ','+field+',' LIKE '%,M4,%') + /// + private static string MultiCsvContains(string field, string csv) + { + if (string.IsNullOrEmpty(csv)) return ""; + var parts = csv.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + .Where(v => !string.IsNullOrEmpty(v)).Distinct().ToList(); + if (parts.Count == 0) return ""; + if (parts.Count == 1) return $" AND (','+ISNULL({field},'')+',') LIKE '%,{parts[0]},%' "; + return " AND (" + string.Join(" OR ", parts.Select(v => $"(','+ISNULL({field},'')+',') LIKE '%,{v},%'")) + ") "; + } + #endregion #region ===== 产品BOM主数据 ===== @@ -588,7 +632,7 @@ namespace GXWebApi.Controllers string where = " WHERE 1=1 "; if (!string.IsNullOrEmpty(productCode)) where += $" AND S.ProductCode='{Sql(productCode)}' "; if (!string.IsNullOrEmpty(bomVersion)) where += $" AND S.BOMVersion='{Sql(bomVersion)}' "; - if (!string.IsNullOrEmpty(productType)) where += $" AND ISNULL(S.ProductType,N'') LIKE '%{Sql(productType)}%' "; + if (!string.IsNullOrEmpty(productType)) where += MultiLike("S.ProductType", productType, useNVarChar: true); if (!string.IsNullOrEmpty(status)) where += $" AND S.Status=N'{Sql(status)}' "; if (!string.IsNullOrEmpty(keyword)) where += $" AND (S.ProductCode LIKE '%{Sql(keyword)}%' OR M.ProductName LIKE N'%{Sql(keyword)}%') "; @@ -2377,10 +2421,10 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; string todayStr = DateTime.Now.ToString("yyyy-MM-dd"); string where = " WHERE S.PlanEndDate>='" + todayStr + "' "; - if (!string.IsNullOrEmpty(productType)) where += $" AND (','+ISNULL(D.ProductTypes,'')+',') LIKE '%,{Sql(productType)},%' "; - if (!string.IsNullOrEmpty(supplier)) where += $" AND ISNULL(D.Supplier,'') LIKE N'%{Sql(supplier)}%' "; - if (!string.IsNullOrEmpty(purchaser)) where += $" AND ISNULL(D.Purchaser,'') LIKE N'%{Sql(purchaser)}%' "; - if (!string.IsNullOrEmpty(risk)) where += $" AND D.DeliveryRisk=N'{Sql(risk)}' "; + if (!string.IsNullOrEmpty(productType)) where += MultiCsvContains("D.ProductTypes", productType); + if (!string.IsNullOrEmpty(supplier)) where += MultiLike("D.Supplier", supplier); + if (!string.IsNullOrEmpty(purchaser)) where += MultiLike("D.Purchaser", purchaser); + if (!string.IsNullOrEmpty(risk)) where += MultiExact("D.DeliveryRisk", risk); if (!string.IsNullOrEmpty(bomLevel)) where += $" AND D.BOMLevel={Int(bomLevel)} "; if (!string.IsNullOrEmpty(keyword)) { @@ -3498,8 +3542,8 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; try { string where = " WHERE 1=1 "; - if (!string.IsNullOrEmpty(productType)) where += $" AND D.ProductTypes LIKE '%{Sql(productType)}%' "; - if (!string.IsNullOrEmpty(risk)) where += $" AND D.DeliveryRisk=N'{Sql(risk)}' "; + if (!string.IsNullOrEmpty(productType)) where += MultiCsvContains("D.ProductTypes", productType); + if (!string.IsNullOrEmpty(risk)) where += MultiExact("D.DeliveryRisk", risk); DataTable dt = db.ExecuteTable($@" SELECT @@ -3592,8 +3636,8 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; try { string where = " WHERE 1=1 "; - if (!string.IsNullOrEmpty(productType)) where += $" AND D.ProductTypes LIKE '%{Sql(productType)}%' "; - if (!string.IsNullOrEmpty(risk)) where += $" AND D.DeliveryRisk=N'{Sql(risk)}' "; + if (!string.IsNullOrEmpty(productType)) where += MultiCsvContains("D.ProductTypes", productType); + if (!string.IsNullOrEmpty(risk)) where += MultiExact("D.DeliveryRisk", risk); if (!string.IsNullOrEmpty(keyword)) where += $" AND (D.ChildMaterialId LIKE '%{Sql(keyword)}%' OR D.MaterialName LIKE N'%{Sql(keyword)}%') "; DataTable dtCount = db.ExecuteTable($"SELECT COUNT(*) AS CNT FROM Material_Delivery D {where}"); @@ -3667,7 +3711,7 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; string end = dtRange.Rows.Count > 0 ? DateStr(dtRange.Rows[0]["EndD"]) : ""; string where = " WHERE 1=1 "; - if (!string.IsNullOrEmpty(productType)) where += $" AND D.ProductTypes LIKE '%{Sql(productType)}%' "; + if (!string.IsNullOrEmpty(productType)) where += MultiCsvContains("D.ProductTypes", productType); if (!string.IsNullOrEmpty(risk)) { string[] risks = risk.Split(','); @@ -3791,10 +3835,10 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; string todayStr = DateTime.Now.ToString("yyyy-MM-dd"); string where = " WHERE S.PlanEndDate>=@today "; - if (!string.IsNullOrEmpty(risk)) where += $" AND D.DeliveryRisk=N'{Sql(risk)}' "; - if (!string.IsNullOrEmpty(productType)) where += $" AND (','+ISNULL(D.ProductTypes,'')+',') LIKE '%,{Sql(productType)},%' "; - if (!string.IsNullOrEmpty(supplier)) where += $" AND ISNULL(D.Supplier,'') LIKE N'%{Sql(supplier)}%' "; - if (!string.IsNullOrEmpty(purchaser)) where += $" AND ISNULL(D.Purchaser,'') LIKE N'%{Sql(purchaser)}%' "; + if (!string.IsNullOrEmpty(risk)) where += MultiExact("D.DeliveryRisk", risk); + if (!string.IsNullOrEmpty(productType)) where += MultiCsvContains("D.ProductTypes", productType); + if (!string.IsNullOrEmpty(supplier)) where += MultiLike("D.Supplier", supplier); + if (!string.IsNullOrEmpty(purchaser)) where += MultiLike("D.Purchaser", purchaser); if (!string.IsNullOrEmpty(keyword)) { string kw = Sql(keyword); @@ -4041,8 +4085,8 @@ LEFT JOIN #SupByMat SP ON SP.MaterialCode=D.ChildMaterialId; // 2) 取物料清单(按风险/产品类型过滤) string matWhere = $" WHERE S.PlanEndDate>='{todayStr}' "; - if (!string.IsNullOrEmpty(risk)) matWhere += $" AND D.DeliveryRisk=N'{Sql(risk)}' "; - if (!string.IsNullOrEmpty(productType)) matWhere += $" AND (','+ISNULL(D.ProductTypes,'')+',' LIKE '%,{Sql(productType)},%') "; + if (!string.IsNullOrEmpty(risk)) matWhere += MultiExact("D.DeliveryRisk", risk); + if (!string.IsNullOrEmpty(productType)) matWhere += MultiCsvContains("D.ProductTypes", productType); DataTable matDt = db.ExecuteTable($@" SELECT D.Id, D.ScheduleId, D.ChildMaterialId, D.MaterialName, D.BOMLevel, D.DeliveryRisk, diff --git a/WebApi/View/Delivery/delivery_dashboard_risk.html b/WebApi/View/Delivery/delivery_dashboard_risk.html index a3ca099..c6a9cbc 100755 --- a/WebApi/View/Delivery/delivery_dashboard_risk.html +++ b/WebApi/View/Delivery/delivery_dashboard_risk.html @@ -9,6 +9,34 @@