CourseServiceBus.cs 18 KB

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