CourseServiceBus.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Azure.Cosmos;
  2. using Microsoft.Azure.WebJobs;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Extension;
  10. using TEAMModelOS.SDK.Models.Cosmos.Common;
  11. namespace TEAMModelFunction
  12. {
  13. public class CourseServiceBus
  14. {
  15. private readonly AzureCosmosFactory _azureCosmos;
  16. private readonly DingDing _dingDing;
  17. public CourseServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing)
  18. {
  19. _azureCosmos = azureCosmos;
  20. _dingDing = dingDing;
  21. }
  22. /// <summary>
  23. /// 完善课程变更
  24. /// </summary>
  25. /// <data msg>
  26. /// CourseChange
  27. ///// </data>
  28. /// <param name="msg"></param>
  29. /// <returns></returns>
  30. [FunctionName("Course")]
  31. public async Task Course([ServiceBusTrigger("active-task", "course", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  32. {
  33. var client = _azureCosmos.GetCosmosClient();
  34. try
  35. {
  36. await _dingDing.SendBotMsg($"ServiceBus,CourseChange:{msg}", GroupNames.醍摩豆服務運維群組);
  37. var jsonMsg = JsonDocument.Parse(msg);
  38. CourseChange courseChange = msg.ToObject<CourseChange>();
  39. //根据新增名单获取 新增的学生id 及timdid
  40. (List<string> addTmdids, List<Students> addStudents) = await TriggerStuActivity.GetStuList(client, courseChange.addList, courseChange.school);
  41. //根据删除名单获取 新增的学生id 及timdid
  42. (List<string> delTmdids, List<Students> delStudents) = await TriggerStuActivity.GetStuList(client, courseChange.delList, courseChange.school);
  43. foreach (var addStu in addStudents)
  44. {
  45. var course = new StuCourse
  46. {
  47. id = courseChange.id,
  48. scode = courseChange.code,
  49. name = courseChange.name,
  50. code = $"Course-{courseChange.school}-{addStu.id}",
  51. scope = courseChange.scope,
  52. school = courseChange.school,
  53. creatorId = courseChange.creatorId,
  54. pk = "Course"
  55. };
  56. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(course, new PartitionKey(course.code));
  57. }
  58. foreach (var addTmd in addTmdids)
  59. {
  60. var course = new StuCourse
  61. {
  62. id = courseChange.id,
  63. scode = courseChange.code,
  64. name = courseChange.name,
  65. code = $"Course-{addTmd}",
  66. scope = courseChange.scope,
  67. //school = courseChange.school,
  68. creatorId = courseChange.creatorId,
  69. pk = "Course"
  70. };
  71. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(course, new PartitionKey(course.code));
  72. }
  73. foreach (var delStu in delStudents)
  74. {
  75. await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{courseChange.school}-{delStu.id}"));
  76. }
  77. foreach (var delTmd in delTmdids)
  78. {
  79. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{delTmd}"));
  80. }
  81. }
  82. catch (Exception ex)
  83. {
  84. await _dingDing.SendBotMsg($"CourseServiceBus-Course\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  85. }
  86. }
  87. }
  88. }