ExamPatriarchQuestionnaireProgressSync.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 
  2. using Furion.ClayObject;
  3. using Furion.RemoteRequest.Extensions;
  4. using YBEE.EQM.Core;
  5. namespace YBEE.EQM.Application;
  6. /// <summary>
  7. /// 家长问卷填答进度管理服务
  8. /// </summary>
  9. public class ExamPatriarchQuestionnaireProgressSync(IRepository<ExamPatriarchQuestionnaireProgress> rep) : IExamPatriarchQuestionnaireProgressSync, IScoped
  10. {
  11. public async Task Sync()
  12. {
  13. // 目前已废弃
  14. if (DateTime.Now < new DateTime(2023, 1, 1))
  15. {
  16. return;
  17. }
  18. Log.Information("开始同步学生家长问卷进度");
  19. Dictionary<string, string> qs = new()
  20. {
  21. { "9am", "http://wenjuan.cqcet.edu.cn/question/applice/answer/answerData.action?code=9am&tokenId=ce789fc1044dd7b0d4ef2b42e4916b66" },
  22. { "yyd", "http://wenjuan.cqcet.edu.cn/question/applice/answer/answerData.action?code=yyd&tokenId=d07e946b0d1d36666f68623a38fd26a7" },
  23. };
  24. foreach (var kvp in qs)
  25. {
  26. var maxSubmitTime = await rep.DetachedEntities.Where(t => t.QuestionnaireCode.ToLower() == kvp.Key.ToLower()).MaxAsync(t => t.SubmitTime);
  27. string url = kvp.Value;
  28. if (maxSubmitTime != null)
  29. {
  30. url = $"{url}&beginTime={maxSubmitTime:yyyy-MM-dd HH:mm:ss}";
  31. }
  32. var strRes = await url.GetAsStringAsync();
  33. var res = (QuestionnaireProgressResponse)Clay.Parse(strRes);
  34. if (res.code != "200")
  35. {
  36. Log.Error($"同步{kvp.Key}问卷错误");
  37. continue;
  38. }
  39. var items = res.data.Where(t => maxSubmitTime == null || t.finishedTime > maxSubmitTime);
  40. var dt = DateTime.Now;
  41. List<ExamPatriarchQuestionnaireProgress> newItems = [];
  42. foreach (var item in items)
  43. {
  44. newItems.Add(new()
  45. {
  46. QuestionnaireCode = kvp.Key,
  47. ExamStudentId = item.id,
  48. Mobile = item.phone,
  49. SubmitTime = item.finishedTime,
  50. IsCompleted = true,
  51. CreateTime = dt,
  52. });
  53. }
  54. if (items.Any())
  55. {
  56. await rep.InsertNowAsync(newItems);
  57. }
  58. }
  59. }
  60. }