213 lines
4.6 KiB
C#
213 lines
4.6 KiB
C#
using System.Xml.Serialization;
|
|
using TechHelper.Client.AI;
|
|
using TechHelper.Services;
|
|
using Entities.DTO;
|
|
using System.Net.Http.Json;
|
|
using Newtonsoft.Json;
|
|
using TechHelper.Client.Pages.Exam;
|
|
|
|
|
|
namespace TechHelper.Client.Exam
|
|
{
|
|
public class ExamService : IExamService
|
|
{
|
|
private IAIService aIService;
|
|
private IHttpClientFactory httpClientFactory;
|
|
|
|
public ExamService(IAIService aIService,
|
|
IHttpClientFactory httpClientFactory)
|
|
{
|
|
this.aIService = aIService;
|
|
this.httpClientFactory = httpClientFactory;
|
|
}
|
|
|
|
public ApiResponse ConvertToXML<T>(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<ApiResponse> 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<ApiResponse> 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<ApiResponse> GetAllExam(string user)
|
|
{
|
|
using (var client = httpClientFactory.CreateClient("Default"))
|
|
{
|
|
var response = await client.GetAsync($"exam/getAllPreview?user={user}");
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var content = await response.Content.ReadAsStringAsync();
|
|
var result = JsonConvert.DeserializeObject<List<ExamDto>>(content);
|
|
return ApiResponse.Success(result: result);
|
|
}
|
|
else
|
|
{
|
|
return ApiResponse.Error(await response.Content.ReadAsStringAsync());
|
|
}
|
|
}
|
|
}
|
|
|
|
public async Task<ApiResponse> GetExam(Guid guid)
|
|
{
|
|
return ApiResponse.Success("HELLO");
|
|
}
|
|
|
|
public async Task<ApiResponse> 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 async Task<ApiResponse> SaveParsedExam(ExamDto examDto)
|
|
{
|
|
using (var client = httpClientFactory.CreateClient("Default"))
|
|
{
|
|
var respont = await client.PostAsJsonAsync("exam/add",
|
|
examDto);
|
|
|
|
if (respont.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
return new ApiResponse(true, "ok");
|
|
}
|
|
return new ApiResponse("false");
|
|
}
|
|
}
|
|
}
|
|
}
|