CourseServiceBus.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.Helper.Common.CollectionHelper;
  11. using TEAMModelOS.SDK.Models.Cosmos.Common;
  12. namespace TEAMModelFunction
  13. {
  14. public class CourseServiceBus
  15. {
  16. private readonly AzureCosmosFactory _azureCosmos;
  17. private readonly DingDing _dingDing;
  18. public CourseServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing)
  19. {
  20. _azureCosmos = azureCosmos;
  21. _dingDing = dingDing;
  22. }
  23. /// <summary>
  24. /// 完善课程变更
  25. /// </summary>
  26. /// <data msg>
  27. /// CourseChange
  28. ///// </data>
  29. /// <param name="msg"></param>
  30. /// <returns></returns>
  31. [FunctionName("Course")]
  32. public async Task Course([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "course", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  33. {
  34. var client = _azureCosmos.GetCosmosClient();
  35. try
  36. {
  37. await _dingDing.SendBotMsg($"ServiceBus,CourseChange:{msg}", GroupNames.醍摩豆服務運維群組);
  38. var jsonMsg = JsonDocument.Parse(msg);
  39. CourseChange courseChange = msg.ToObject<CourseChange>();
  40. if (courseChange == null) {
  41. return;
  42. }
  43. foreach (var cls in courseChange.addClass) {
  44. (List<string> addTmdidsCls, List<Students> addStudentsCls) = await TriggerStuActivity.GetStuList(client, _dingDing,new List<string> { cls}, courseChange.school);
  45. foreach (var stu in addStudentsCls) {
  46. try
  47. {
  48. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  49. if (!stuCourse.Value.classId.Contains(cls)) {
  50. stuCourse.Value.classId.Add(cls);
  51. }
  52. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  53. }
  54. catch (CosmosException ex)
  55. {
  56. if (ex.Response.Status == 404)
  57. {
  58. var course = new StuCourse
  59. {
  60. id = courseChange.id,
  61. scode = courseChange.code,
  62. name = courseChange.name,
  63. code = $"StuCourse-{courseChange.school}-{stu.id}",
  64. scope = courseChange.scope,
  65. school = courseChange.school,
  66. creatorId = courseChange.creatorId,
  67. classId= new List<string> { cls},
  68. pk = "StuCourse"
  69. };
  70. await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(course, new PartitionKey(course.code));
  71. }
  72. }
  73. }
  74. foreach (var tmd in addTmdidsCls)
  75. {
  76. try
  77. {
  78. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  79. if (!stuCourse.Value.classId.Contains(cls))
  80. {
  81. stuCourse.Value.classId.Add(cls);
  82. }
  83. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  84. }
  85. catch (CosmosException ex)
  86. {
  87. if (ex.Response.Status == 404)
  88. {
  89. var course = new StuCourse
  90. {
  91. id = courseChange.id,
  92. scode = courseChange.code,
  93. name = courseChange.name,
  94. code = $"StuCourse-{tmd}",
  95. scope = courseChange.scope,
  96. school = courseChange.school,
  97. creatorId = courseChange.creatorId,
  98. classId = new List<string> { cls },
  99. pk = "StuCourse"
  100. };
  101. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(course, new PartitionKey(course.code));
  102. }
  103. }
  104. }
  105. }
  106. foreach (var list in courseChange.addList)
  107. {
  108. (List<string> addTmdidsCls, List<Students> addStudentsCls) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { list }, courseChange.school);
  109. foreach (var stu in addStudentsCls)
  110. {
  111. try
  112. {
  113. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{stu.schoolId}-{stu.id}"));
  114. if (!stuCourse.Value.stulist.Contains(list))
  115. {
  116. stuCourse.Value.stulist.Add(list);
  117. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{stu.schoolId}-{stu.id}"));
  118. }
  119. }
  120. catch (CosmosException ex)
  121. {
  122. if (ex.Response.Status == 404)
  123. {
  124. var course = new StuCourse
  125. {
  126. id = courseChange.id,
  127. scode = courseChange.code,
  128. name = courseChange.name,
  129. code = $"StuCourse-{courseChange.school}-{stu.id}",
  130. scope = courseChange.scope,
  131. school = courseChange.school,
  132. creatorId = courseChange.creatorId,
  133. stulist = new List<string> { list },
  134. pk = "StuCourse"
  135. };
  136. await client.GetContainer("TEAMModelOS", "Student").CreateItemAsync(course, new PartitionKey(course.code));
  137. }
  138. }
  139. }
  140. foreach (var tmd in addTmdidsCls)
  141. {
  142. try
  143. {
  144. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  145. if (!stuCourse.Value.stulist.Contains(list))
  146. {
  147. stuCourse.Value.stulist.Add(list);
  148. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  149. }
  150. }
  151. catch (CosmosException ex)
  152. {
  153. if (ex.Response.Status == 404)
  154. {
  155. var course = new StuCourse
  156. {
  157. id = courseChange.id,
  158. scode = courseChange.code,
  159. name = courseChange.name,
  160. code = $"StuCourse-{tmd}",
  161. scope = courseChange.scope,
  162. school = courseChange.school,
  163. creatorId = courseChange.creatorId,
  164. stulist = new List<string> { list },
  165. pk = "StuCourse"
  166. };
  167. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(course, new PartitionKey(course.code));
  168. }
  169. }
  170. }
  171. }
  172. foreach (var delCls in courseChange.delClass) {
  173. (List<string> delTmdidsCls, List<Students> delStudentsCls) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { delCls }, courseChange.school);
  174. foreach (var stu in delStudentsCls)
  175. {
  176. try
  177. {
  178. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  179. if (stuCourse.Value.classId.Contains(delCls))
  180. {
  181. stuCourse.Value.classId.Remove(delCls);
  182. }
  183. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  184. {
  185. //当两个列表都不存在时则直接删除
  186. await client.GetContainer("TEAMModelOS", "Student").DeleteItemAsync<StuCourse>( courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  187. }
  188. else {
  189. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  190. }
  191. }
  192. catch (CosmosException ex)
  193. {
  194. }
  195. }
  196. foreach (var tmd in delTmdidsCls)
  197. {
  198. try
  199. {
  200. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  201. if (stuCourse.Value.classId.Contains(delCls))
  202. {
  203. stuCourse.Value.classId.Remove(delCls);
  204. }
  205. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  206. {
  207. //当两个列表都不存在时则直接删除
  208. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  209. }
  210. else
  211. {
  212. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  213. }
  214. }
  215. catch (CosmosException ex)
  216. {
  217. }
  218. }
  219. }
  220. foreach (var delList in courseChange.delList)
  221. {
  222. (List<string> delTmdidsCls, List<Students> delStudentsCls) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { delList }, courseChange.school);
  223. foreach (var stu in delStudentsCls)
  224. {
  225. try
  226. {
  227. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  228. if (stuCourse.Value.stulist.Contains(delList))
  229. {
  230. stuCourse.Value.stulist.Remove(delList);
  231. }
  232. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  233. {
  234. //当两个列表都不存在时则直接删除
  235. await client.GetContainer("TEAMModelOS", "Student").DeleteItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  236. }
  237. else
  238. {
  239. await client.GetContainer("TEAMModelOS", "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  240. }
  241. }
  242. catch (CosmosException ex)
  243. {
  244. }
  245. }
  246. foreach (var tmd in delTmdidsCls)
  247. {
  248. try
  249. {
  250. ItemResponse<StuCourse> stuCourse = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  251. if (stuCourse.Value.stulist.Contains(delList))
  252. {
  253. stuCourse.Value.stulist.Remove(delList);
  254. }
  255. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  256. {
  257. //当两个列表都不存在时则直接删除
  258. await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  259. }
  260. else
  261. {
  262. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd}"));
  263. }
  264. }
  265. catch (CosmosException ex)
  266. {
  267. }
  268. }
  269. }
  270. ////根据新增名单获取 新增的学生id 及timdid
  271. //(List<string> addTmdids, List<Students> addStudents) = await TriggerStuActivity.GetStuList(client, _dingDing, courseChange.addList, courseChange.school);
  272. ////根据删除名单获取 新增的学生id 及timdid
  273. //(List<string> delTmdids, List<Students> delStudents) = await TriggerStuActivity.GetStuList(client, _dingDing, courseChange.delList, courseChange.school);
  274. //foreach (var addStu in addStudents)
  275. //{
  276. // var course = new StuCourse
  277. // {
  278. // id = courseChange.id,
  279. // scode = courseChange.code,
  280. // name = courseChange.name,
  281. // code = $"StuCourse-{courseChange.school}-{addStu.id}",
  282. // scope = courseChange.scope,
  283. // school = courseChange.school,
  284. // creatorId = courseChange.creatorId,
  285. // pk = "StuCourse"
  286. // };
  287. // await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(course, new PartitionKey(course.code));
  288. //}
  289. //foreach (var addTmd in addTmdids)
  290. //{
  291. // var course = new StuCourse
  292. // {
  293. // id = courseChange.id,
  294. // scode = courseChange.code,
  295. // name = courseChange.name,
  296. // code = $"StuCourse-{addTmd}",
  297. // scope = courseChange.scope,
  298. // //school = courseChange.school,
  299. // creatorId = courseChange.creatorId,
  300. // pk = "StuCourse"
  301. // };
  302. // await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(course, new PartitionKey(course.code));
  303. //}
  304. //foreach (var delStu in delStudents)
  305. //{
  306. // await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{courseChange.school}-{delStu.id}"));
  307. //}
  308. //foreach (var delTmd in delTmdids)
  309. //{
  310. // await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{delTmd}"));
  311. //}
  312. }
  313. catch (Exception ex)
  314. {
  315. await _dingDing.SendBotMsg($"CourseServiceBus-Course\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  316. }
  317. }
  318. }
  319. }