ExamPatriarchQuestionnaireProgressSync.cs 2.3 KB

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