using EPLAN.Harness.Common.Extensions; using Sinvo.EplanHpD.Plugin.WPFUI.Models; using Sinvo.EplanHpD.Plugin.WPFUI.Utils; using System; using System.Collections.Generic; using System.Linq; namespace Sinvo.EplanHpD.Plugin.WPFUI.ViewModel { public partial class MainViewModel { public void CheckAll(IEnumerable datas) { datas.AsParallel().WithDegreeOfParallelism(4).ForAll(item => { if (item == null) return; item.IsError = false; item.ErrorCount = 0; item.CheckedMsg = ""; ValidateItem(item); item.IsChecked = true; }); OnPropertyChanged(nameof(StuffedData)); } private void ValidateItem(StuffedDataModel item) { CheckRequiredFields(item); if (IsUseDiscoloration) { CheckImprint(item); } if (IsRequireCe) { CheckCeCompliance(item); } CheckNumberTube(item); CheckInsulation(item); CheckTerminals(item); CheckDuplicateModel(item); } private void CheckNumberTube(StuffedDataModel item) { if (string.IsNullOrEmpty(item.NumberTubeSpec)) { SetItemError(item, "未匹配到正确的号码管!\r\n"); } } private void CheckRequiredFields(StuffedDataModel item) { if (string.IsNullOrEmpty(item.RearTerminalMaterialCode)) { SetItemError(item, "后端子物料编码为空\r\n"); } if (string.IsNullOrEmpty(item.FrontTerminalMaterialCode)) { SetItemError(item, "前端子物料编码为空\r\n"); } if (string.IsNullOrEmpty(item.WireColor)) { SetItemError(item, "线色不符合规范\r\n"); } if (string.IsNullOrEmpty(item.WireCode)) { SetItemError(item, "线材编码为空\r\n"); } } private void CheckCeCompliance(StuffedDataModel item) { if (item.WireModel.IndexOf("-CE", StringComparison.OrdinalIgnoreCase) < 0) { SetItemError(item, "未使用CE认证物料!\r\n"); } if (item.FrontTerminalModel.IndexOf("-CE", StringComparison.OrdinalIgnoreCase) < 0) { SetItemError(item, "前端子未使用CE认证端子!\r\n"); } if (item.RearTerminalModel.IndexOf("-CE", StringComparison.OrdinalIgnoreCase) < 0) { SetItemError(item, "后端子未使用CE认证端子!\r\n"); } } public void CheckInsulation(StuffedDataModel item) { if (item.Insulation != null && item.Insulation.Specification != null) { var match = Consts.regex.Match(item.WireModel); if (!string.IsNullOrEmpty(match?.Value) && match != null) { var itemWireDiameter = double.Parse(match.Value.Replace("RV", "")); var insMatch = Consts.dRegex.Match(item.Insulation.Specification); if (!string.IsNullOrEmpty(insMatch.Value)) { if (itemWireDiameter < 1.5d) { if (insMatch.Value != "V-1.5") { SetItemError(item, "变色套(绝缘软套)大小与线径不匹配!"); } } else { var insDiameter = double.Parse(insMatch.Value.Replace("V-", "")); if (Math.Abs(insDiameter - itemWireDiameter) > 0) { SetItemError(item, "变色套(绝缘软套)大小与线径不匹配!"); } } } else { SetItemError(item, "变色套(绝缘软套)未匹配到大小!"); } } else { SetItemError(item, "未匹配到线材大小!"); } } } /// /// 检查引用的端子 /// /// public void CheckTerminals(StuffedDataModel item) { var canUsedTerminals = new List(); //if(item.) if (item.Imprint.StartsWith("D-PE")) { canUsedTerminals = GetD_PECanUsedTerminal(item.WireModel); } else { canUsedTerminals = GetCanUsedTerminal(item.WireModel); } if (canUsedTerminals != null && canUsedTerminals.Any()) { var terminals = canUsedTerminals.Where(it => !string.IsNullOrEmpty(it.TerminalSpec) && !string.IsNullOrEmpty(it.TerminalMaterialCode)); if (terminals.All(it => it.TerminalSpec != item.FrontTerminalModel)) { SetItemError(item, "前端子不在可选列表中!\r\n"); } if (terminals.All(it => it.TerminalMaterialCode != item.FrontTerminalMaterialCode)) { SetItemError(item, "前端子料号不正确,不在可选列表中!\r\n"); } if (terminals.All(it => it.TerminalSpec != item.RearTerminalModel)) { SetItemError(item, "后端子不在可选列表中!\r\n"); } if (terminals.All(it => it.TerminalMaterialCode != item.RearTerminalMaterialCode)) { SetItemError(item, "后端子料号不正确,不在可选列表中!\r\n"); } } else if (!string.IsNullOrEmpty(item.FrontTerminalModel) || !string.IsNullOrEmpty(item.RearTerminalModel)) { SetItemError(item, "该型号无可用端子!\r\n"); } } private void CheckDuplicateModel(StuffedDataModel item) { if (StuffedData?.Count(it => it != null && it.Model == item.Model) > 1) { if (string.IsNullOrEmpty(item.WireLength)) { SetItemError(item, $"线长度获取异常!\r\n"); } else { var newModel = $"RV-{item.WireColor}-1x{item.CrossSection}-{item.WireLength + 5}-16×N2-{item.WireNumber}"; if (StuffedData.Any(it => it.Model == newModel)) { var newModel2 = $"RV-{item.WireColor}-1x{item.CrossSection}-{item.WireLength + 10}-16×N2-{item.WireNumber}"; if (StuffedData.Any(it => it.Model == newModel2)) SetItemError(item, $"{item.Model} 该型号存在重复!\r\n"); else { item.WireLength += 10; item.Model = newModel2; } } else { item.WireLength += 5; item.Model = newModel; } } } } private void SetItemError(StuffedDataModel item, string errorMsg) { item.IsError = true; item.ErrorCount += 1; item.CheckedMsg += errorMsg; } private List GetCanUsedTerminal(string wireModel) { var result = ExcelHelper.GetWireTerminalMappingTable(wireModel); var terminals = new List(); var excelModels = result as ExcelModel[] ?? result.ToArray(); if (!excelModels.Any()) return terminals; foreach (var item in excelModels) { if (!string.IsNullOrEmpty(item.ForkTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.ForkTerminalMaterialCode, TerminalSpec = item.ForkTerminal }); if (!string.IsNullOrEmpty(item.FlatTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.FlatTerminalMaterialCode, TerminalSpec = item.FlatTerminal }); if (!string.IsNullOrEmpty(item.PinTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.PinTerminalMaterialCode, TerminalSpec = item.PinTerminal }); if (!string.IsNullOrEmpty(item.OTypeTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.OTypeTerminalMaterialCode, TerminalSpec = item.OTypeTerminal }); if (!string.IsNullOrEmpty(item.YTypeTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.YTypeTerminalMaterialCode, TerminalSpec = item.YTypeTerminal }); } return terminals; } /// /// 检查地线可以用的端子 /// /// /// private List GetD_PECanUsedTerminal(string wireModel) { // 获取D-PE前缀的可用端子 var resultWithPrefix = ExcelHelper.GetWireTerminalMappingTable(wireModel, "D-PE"); // 获取不包含前缀的端子 var resultWithoutPrefix = ExcelHelper.GetWireTerminalMappingTable(wireModel); var terminals = new List(); // 转换 var withPrefixExcelModels = resultWithPrefix as List ?? resultWithPrefix.ToList(); var withoutPrefixExcelModels = resultWithoutPrefix as List ?? resultWithoutPrefix.ToList(); // 合并 var excelModels = withPrefixExcelModels.Concat(withoutPrefixExcelModels); if (!excelModels.Any()) return terminals; foreach (var item in excelModels) { if (!string.IsNullOrEmpty(item.ForkTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.ForkTerminalMaterialCode, TerminalSpec = item.ForkTerminal }); if (!string.IsNullOrEmpty(item.FlatTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.FlatTerminalMaterialCode, TerminalSpec = item.FlatTerminal }); if (!string.IsNullOrEmpty(item.PinTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.PinTerminalMaterialCode, TerminalSpec = item.PinTerminal }); if (!string.IsNullOrEmpty(item.OTypeTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.OTypeTerminalMaterialCode, TerminalSpec = item.OTypeTerminal }); if (!string.IsNullOrEmpty(item.YTypeTerminal)) terminals.Add(new TerminalModel { TerminalMaterialCode = item.YTypeTerminalMaterialCode, TerminalSpec = item.YTypeTerminal }); } return terminals; } private void CheckImprint(StuffedDataModel item) { var imprintConditions = new List<(string Prefix, WireFlagType? FlagType, string Color)> { ("L1", WireFlagType.Dual, "褐色"), ("R", WireFlagType.Single, "褐色"), ("L1", WireFlagType.Mix, "褐色"), ("R", WireFlagType.Mix, "褐色"), ("L2", WireFlagType.Dual, "黑色"), ("S", WireFlagType.Single, "黑色"), ("L2", WireFlagType.Mix, "黑色"), ("S", WireFlagType.Mix, "黑色"), ("L3", WireFlagType.Dual, "灰色"), ("T", WireFlagType.Single, "灰色"), ("L3", WireFlagType.Mix, "灰色"), ("T", WireFlagType.Mix, "灰色"), ("PE", null, "绿色") }; foreach (var condition in imprintConditions) { CheckImprintCondition(item, condition.Prefix, condition.FlagType, condition.Color); } } private void CheckImprintCondition(StuffedDataModel item, string imprintPrefix, WireFlagType? flagType, string expectedColor) { if (item.Imprint.StartsWith(imprintPrefix) && (flagType == null || FlagType == flagType)) { if (string.IsNullOrEmpty(item.DiscolorationDesc)) { SetItemError(item, "变色管信息为空\r\n"); } else if (!item.DiscolorationDesc.Contains(expectedColor)) { SetItemError(item, $"变色管颜色有误,应该使用{expectedColor}\r\n"); } } } } }