using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Linq; /// /// 物料状态枚举 /// public enum MaterialCondition { Normal, // 正常 Damaged, // 破损 Unknown // 无法确定 } /// /// 物料品类一致性枚举 /// public enum MaterialCategoryConsistency { Consistent, // 一致 Inconsistent, // 不一致 Unknown // 不适用/未知 } public class MaterialCheckHelper { /// /// 查询JSON中"物料X检查外观"的详细信息 /// public static List GetMaterialAppearanceCheckDetails(string jsonContent) { var result = new List(); try { // 解析JSON JArray jsonArray = JArray.Parse(jsonContent); // 遍历所有步骤 foreach (JObject step in jsonArray) { string stepDescription = step["StepDescription"]?.ToString(); // 获取Actions数组 JArray actions = step["Actions"] as JArray; if (actions == null) continue; // 遍历所有Actions foreach (JObject action in actions) { string actionTitle = action["Title"]?.ToString(); // 获取JudgmentQuestions数组 JArray judgmentQuestions = action["JudgmentQuestions"] as JArray; if (judgmentQuestions == null) continue; // 遍历所有判断题 foreach (JObject question in judgmentQuestions) { string questionText = question["Question"]?.ToString(); // 检查是否包含"物料X检查外观"模式 if (!string.IsNullOrEmpty(questionText) && questionText.Contains("物料") && questionText.Contains("检查外观")) { // 提取物料编号和品类 string materialNumber = ExtractMaterialNumber(questionText); string materialCategory = ExtractMaterialCategory(questionText); // 解析答案选项 var answerOptions = ParseAnswerOptions(question); string correctAnswer = question["CorrectAnswer"]?.ToString(); string condition = DetermineCondition(answerOptions, correctAnswer); MaterialCondition conditionEnum = DetermineConditionEnum(answerOptions, correctAnswer); MaterialCategoryConsistency categoryConsistency = MaterialCategoryConsistency.Unknown; // 外观检查不涉及品类一致性 result.Add(new MaterialCheckInfo { MaterialNumber = materialNumber, Question = questionText, StepDescription = stepDescription, ActionTitle = actionTitle, CorrectAnswer = correctAnswer, AnswerOptions = answerOptions, Condition = condition, ConditionEnum = conditionEnum, MaterialCategory = materialCategory, CategoryConsistency = categoryConsistency }); } // 检查是否包含"物料X物资品类是否一致"模式 else if (!string.IsNullOrEmpty(questionText) && questionText.Contains("物料") && questionText.Contains("物资品类是否一致")) { // 提取物料编号 string materialNumber = ExtractMaterialNumber(questionText); string materialCategory = "品类检查"; // 这是品类一致性检查 // 解析答案选项 var answerOptions = ParseAnswerOptions(question); string correctAnswer = question["CorrectAnswer"]?.ToString(); string condition = "不适用"; // 品类检查不涉及外观状态 MaterialCondition conditionEnum = MaterialCondition.Unknown; // 不适用 MaterialCategoryConsistency categoryConsistency = DetermineCategoryConsistency(correctAnswer); result.Add(new MaterialCheckInfo { MaterialNumber = materialNumber, Question = questionText, StepDescription = stepDescription, ActionTitle = actionTitle, CorrectAnswer = correctAnswer, AnswerOptions = answerOptions, Condition = condition, ConditionEnum = conditionEnum, MaterialCategory = materialCategory, CategoryConsistency = categoryConsistency }); } // 检查是否包含"物料X厂家是否一致"模式 else if (!string.IsNullOrEmpty(questionText) && questionText.Contains("物料") && questionText.Contains("厂家是否一致")) { // 提取物料编号 string materialNumber = ExtractMaterialNumber(questionText); string materialCategory = "厂家检查"; // 这是厂家一致性检查 // 解析答案选项 var answerOptions = ParseAnswerOptions(question); string correctAnswer = question["CorrectAnswer"]?.ToString(); string condition = "不适用"; // 厂家检查不涉及外观状态 MaterialCondition conditionEnum = MaterialCondition.Unknown; // 不适用 MaterialCategoryConsistency categoryConsistency = MaterialCategoryConsistency.Unknown; // 不适用 result.Add(new MaterialCheckInfo { MaterialNumber = materialNumber, Question = questionText, StepDescription = stepDescription, ActionTitle = actionTitle, CorrectAnswer = correctAnswer, AnswerOptions = answerOptions, Condition = condition, ConditionEnum = conditionEnum, MaterialCategory = materialCategory, CategoryConsistency = categoryConsistency }); } } } } return result; } catch (Exception ex) { Console.WriteLine($"解析JSON时发生错误: {ex.Message}"); return result; } } /// /// 解析答案选项 /// private static List ParseAnswerOptions(JObject question) { var options = new List(); try { // 尝试获取Options数组 JArray optionsArray = question["Options"] as JArray; if (optionsArray != null) { foreach (var option in optionsArray) { options.Add(option?.ToString()); } } else { // 根据问题类型设置默认选项 string questionText = question["Question"]?.ToString(); if (questionText?.Contains("检查外观") == true) { options.Add("正常"); options.Add("缺陷"); } else if (questionText?.Contains("是否一致") == true) { options.Add("一致"); options.Add("不一致"); } } } catch (Exception ex) { Console.WriteLine($"解析答案选项时发生错误: {ex.Message}"); // 使用默认选项 options.Add("正常"); options.Add("缺陷"); } return options; } /// /// 判断物料状态(正常/缺陷) /// private static string DetermineCondition(List answerOptions, string correctAnswer) { if (string.IsNullOrEmpty(correctAnswer)) return "无法确定"; // 检查正确答案 if (correctAnswer.Contains("正常") || correctAnswer.Contains("完好")) return "正常"; if (correctAnswer.Contains("缺陷") || correctAnswer.Contains("破损") || correctAnswer.Contains("损坏")) return "缺陷"; // 检查答案选项索引 if (int.TryParse(correctAnswer, out int answerIndex) && answerIndex < answerOptions.Count) { string selectedOption = answerOptions[answerIndex]; if (selectedOption.Contains("正常") || selectedOption.Contains("完好")) return "正常"; if (selectedOption.Contains("缺陷") || selectedOption.Contains("破损") || selectedOption.Contains("损坏")) return "缺陷"; } return "无法确定"; } /// /// 判断物料状态枚举值 /// private static MaterialCondition DetermineConditionEnum(List answerOptions, string correctAnswer) { string condition = DetermineCondition(answerOptions, correctAnswer); switch (condition) { case "正常": return MaterialCondition.Normal; case "缺陷": return MaterialCondition.Damaged; default: return MaterialCondition.Unknown; } } /// /// 从问题文本中提取物料编号 /// private static string ExtractMaterialNumber(string questionText) { // 使用正则表达式提取物料编号 var match = System.Text.RegularExpressions.Regex.Match(questionText, @"物料(\d+)"); return match.Success ? match.Groups[1].Value : "未知"; } /// /// 从问题文本中提取物料品类 /// private static string ExtractMaterialCategory(string questionText) { // 常见的物料品类关键词 var categories = new[] { "电缆", "终端", "接头", "绝缘子", "避雷器", "开关", "变压器", "配电箱", "导线", "金具" }; foreach (var category in categories) { if (questionText.Contains(category)) { return category; } } return "未知品类"; } /// /// 判断物料品类一致性 /// private static MaterialCategoryConsistency DetermineCategoryConsistency(string correctAnswer) { if (string.IsNullOrEmpty(correctAnswer)) return MaterialCategoryConsistency.Inconsistent; // 直接判断CorrectAnswer是否为"一致" if (correctAnswer == "一致") return MaterialCategoryConsistency.Consistent; else return MaterialCategoryConsistency.Inconsistent; } } /// /// 物料检查信息类 /// public class MaterialCheckInfo { public string MaterialNumber { get; set; } public string Question { get; set; } public string StepDescription { get; set; } public string ActionTitle { get; set; } public string CorrectAnswer { get; set; } public List AnswerOptions { get; set; } = new List(); public string Condition { get; set; } public MaterialCondition ConditionEnum { get; set; } public string MaterialCategory { get; set; } // 物料品类 public MaterialCategoryConsistency CategoryConsistency { get; set; } // 品类一致性 public bool IsNormal => ConditionEnum == MaterialCondition.Normal; public bool IsDamaged => ConditionEnum == MaterialCondition.Damaged; public bool IsUnknown => ConditionEnum == MaterialCondition.Unknown; public bool IsCategoryConsistent => CategoryConsistency == MaterialCategoryConsistency.Consistent; public bool IsCategoryInconsistent => CategoryConsistency == MaterialCategoryConsistency.Inconsistent; }