using System.Xml.Serialization; using TechHelper.Client.AI; using TechHelper.Services; using Entities.DTO; namespace TechHelper.Client.Exam { public class ExamService : IExamService { private IAIService aIService; public ExamService(IAIService aIService) { this.aIService = aIService; } public ApiResponse ConvertToXML(string xmlContent) { string cleanedXml = xmlContent.Trim(); XmlSerializer serializer = new XmlSerializer(typeof(T)); using (StringReader reader = new StringReader(cleanedXml)) { try { T deserializedObject = (T)serializer.Deserialize(reader); // 成功时返回 ApiResponse return new ApiResponse { Status = true, Result = deserializedObject, Message = "XML 反序列化成功。" }; } catch (InvalidOperationException ex) { return new ApiResponse { Status = false, Result = null, Message = $"XML 反序列化操作错误: {ex.Message}. 内部异常: {ex.InnerException?.Message ?? "无"}" }; } catch (Exception ex) { return new ApiResponse { Status = false, Result = null, Message = $"处理 XML 反序列化时发生未知错误: {ex.Message}" }; } } } public async Task DividExam(string examContent) { try { string respon = await aIService.CallGLM(examContent, AIConfiguration.BreakQuestions); if (respon != null) { return new ApiResponse { Status = true, Result = respon, Message = "试题分割成功。" }; } else { return new ApiResponse { Status = false, Result = null, Message = "AI 服务未能返回有效内容,或返回内容为空。" }; } } catch (Exception ex) { return new ApiResponse { Status = false, Result = null, Message = $"处理试题分割时发生内部错误: {ex.Message}" }; } } public async Task FormatExam(string examContent) { try { string respon = await aIService.CallGLM(examContent, AIConfiguration.Format); if (respon != null) { return new ApiResponse { Status = true, Result = respon, Message = "试题格式化成功。" }; } else { return new ApiResponse { Status = false, Result = null, Message = "AI 服务未能返回有效内容,或返回内容为空。" }; } } catch (Exception ex) { return new ApiResponse { Status = false, Result = null, Message = $"处理试题格式化时发生内部错误: {ex.Message}" }; } } public async Task ParseSingleQuestionGroup(string examContent) { try { string respon = await aIService.CallGLM(examContent, AIConfiguration.ParseSignelQuestion2); if (respon != null) { return new ApiResponse { Status = true, Result = respon, Message = "试题解析成功。" }; } else { return new ApiResponse { Status = false, Result = null, Message = "AI 服务未能返回有效内容,或返回内容为空。" }; } } catch (Exception ex) { return new ApiResponse { Status = false, Result = null, Message = $"处理试题解析时发生内部错误: {ex.Message}" }; } } public Task SaveParsedExam(ExamDto examDto) { throw new NotImplementedException(); } } }