using Furion.ClayObject;
using Furion.JsonSerialization;
using Furion.RemoteRequest.Extensions;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 家长问卷填答进度管理服务
///
public class ExamPatriarchQuestionnaireProgressSync : IExamPatriarchQuestionnaireProgressSync, IScoped
{
private readonly IRepository _rep;
public ExamPatriarchQuestionnaireProgressSync(IRepository rep)
{
_rep = rep;
}
public async Task Sync()
{
Log.Information("开始同步学生家长问卷进度");
Dictionary qs = new()
{
{ "9am", "http://wenjuan.cqcet.edu.cn/question/applice/answer/answerData.action?code=9am&tokenId=ce789fc1044dd7b0d4ef2b42e4916b66" },
{ "yyd", "http://wenjuan.cqcet.edu.cn/question/applice/answer/answerData.action?code=yyd&tokenId=d07e946b0d1d36666f68623a38fd26a7" },
};
foreach (var kvp in qs)
{
var maxSubmitTime = await _rep.DetachedEntities.Where(t => t.QuestionnaireCode.ToLower() == kvp.Key.ToLower()).MaxAsync(t => t.SubmitTime);
string url = kvp.Value;
if (maxSubmitTime != null)
{
url = $"{url}&beginTime={maxSubmitTime:yyyy-MM-dd HH:mm:ss}";
}
var strRes = await url.GetAsStringAsync();
var res = (QuestionnaireProgressResponse)Clay.Parse(strRes);
if (res.code != "200")
{
Log.Error($"同步{kvp.Key}问卷错误");
continue;
}
var items = res.data.Where(t => maxSubmitTime == null || t.finishedTime > maxSubmitTime);
var dt = DateTime.Now;
List newItems = new();
foreach (var item in items)
{
newItems.Add(new()
{
QuestionnaireCode = kvp.Key,
ExamStudentId = item.id,
Mobile = item.phone,
SubmitTime = item.finishedTime,
IsCompleted = true,
CreateTime = dt,
});
}
if (items.Any())
{
await _rep.InsertNowAsync(newItems);
}
}
}
}