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() { StuffedData.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); } CheckInsulation(item); CheckTerminals(item); CheckDuplicateModel(item); } 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"); } } private void CheckInsulation(StuffedDataModel item) { if (item.Insulation != null && item.Insulation.Specification != null) { var match = Consts.regex.Match(item.WireModel); if (!string.IsNullOrEmpty(match.Value)) { 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 != "RV1.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, "未匹配到线材平方数!"); } } } private void CheckTerminals(StuffedDataModel item) { var canUsedTerminals = GetCanUsedTerminal(item.WireModel); if (canUsedTerminals != null && canUsedTerminals.Any()) { if (canUsedTerminals.All(it => it != item.FrontTerminalModel)) { SetItemError(item, "前端子不在可选列表中!\r\n"); } if (canUsedTerminals.All(it => it != item.RearTerminalModel)) { 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) { SetItemError(item, $"{item.Model} 该型号存在重复!\r\n"); } } 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(item.ForkTerminal); if (!string.IsNullOrEmpty(item.FlatTerminal)) terminals.Add(item.FlatTerminal); if (!string.IsNullOrEmpty(item.PinTerminal)) terminals.Add(item.PinTerminal); if (!string.IsNullOrEmpty(item.OTypeTerminal)) terminals.Add(item.OTypeTerminal); if (!string.IsNullOrEmpty(item.YTypeTerminal)) terminals.Add(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"); } } } } }