LessonService.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.Extensions.Configuration;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.SDK.Helper.Common.DateTimeHelper;
  14. using TEAMModelOS.SDK.Models.Cosmos.Common;
  15. namespace TEAMModelOS.SDK.Models.Service
  16. {
  17. public class LessonService
  18. {
  19. public static async void DoLessonStudentRecord(DingDing _dingding, SnowflakeId snowflakeId, LessonRecord lessonRecord, string scope, CosmosClient client, string school, string tmdid,
  20. Teacher teacher, NotificationService _notificationService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration, LessonBase lessonBase)
  21. {
  22. try
  23. {
  24. int year = DateTimeOffset.UtcNow.Year;
  25. var clientSummaryList = lessonBase.report.clientSummaryList.Where(x => x.groupTaskCompleteCount != 0 || x.groupScore != 0 || x.score != 0 || x.tnteractScore != 0 || x.taskCompleteCount != 0);
  26. IEnumerable<LessonStudent> students = new List<LessonStudent>();
  27. if (clientSummaryList.Any())
  28. {
  29. students = lessonBase.student.Where(x => clientSummaryList.Select(x => x.seatID).Contains(x.seatID));
  30. }
  31. var stuids = students.Where(x => x.type == 2);
  32. if (stuids.Any())
  33. {
  34. stuids.ToList().ForEach(x => {
  35. x.school = string.IsNullOrWhiteSpace(x.school) ? school : x.school;
  36. });
  37. }
  38. var groups = stuids.Where(z => !string.IsNullOrWhiteSpace(z.school)).GroupBy(x => x.school).Select(y => new { code = y.Key, list = y.ToList() });
  39. List<StudentScoreRecord> lessonStudentRecords = new List<StudentScoreRecord>();
  40. foreach (var group in groups)
  41. {
  42. string stusql = $"select value(c) from c where c.stuid in({string.Join(",", group.list.Select(x => $"'{x.id}'"))}) and c.school='{group.code}' and c.year={year}";
  43. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryIterator<StudentScoreRecord>(queryText: stusql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StudentScoreRecord") }))
  44. {
  45. lessonStudentRecords.Add(item);
  46. }
  47. }
  48. var tmdids = students.Where(x => x.type == 1);
  49. if (tmdids.Any())
  50. {
  51. string tmdsql = $"select value(c) from c where c.tmdid in({string.Join(",", tmdids.Select(x => $"'{x}'"))}) and c.year={year}";
  52. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Student).GetItemQueryIterator<StudentScoreRecord>(queryText: tmdsql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StudentScoreRecord") }))
  53. {
  54. lessonStudentRecords.Add(item);
  55. }
  56. }
  57. List<Task<ItemResponse<StudentScoreRecord>>> records = new List<Task<ItemResponse<StudentScoreRecord>>>();
  58. stuids.ToList().ForEach(x => {
  59. var record = lessonStudentRecords.Find(l => l.stuid.Equals(x.id) && l.code.Equals($"StudentScoreRecord") && l.school.Equals(x.school));
  60. ClientSummaryList clientSummaryList = lessonBase.report.clientSummaryList.Find(c => c.seatID == x.seatID);
  61. if (record != null)
  62. {
  63. if (clientSummaryList != null)
  64. {
  65. var hasrecord= record.lessonRecords.Find(x => x.lessonId.Equals(lessonRecord.id));
  66. if (hasrecord != null)
  67. {
  68. hasrecord.gscore = clientSummaryList.groupScore;
  69. hasrecord.pscore = clientSummaryList.score;
  70. hasrecord.tscore = clientSummaryList.tnteractScore;
  71. hasrecord.tmdid = teacher.id;
  72. hasrecord.school = school;
  73. hasrecord.scope = lessonRecord.scope;
  74. hasrecord.lessonId = lessonRecord.id;
  75. hasrecord.courseId = lessonRecord.courseId;
  76. hasrecord.periodId = lessonRecord.periodId;
  77. hasrecord.subjectId = lessonRecord.subjectId;
  78. hasrecord.time = lessonRecord.startTime;
  79. }
  80. else {
  81. record.lessonRecords.Add(
  82. new StudentLessonRecord
  83. {
  84. gscore = clientSummaryList.groupScore,
  85. pscore = clientSummaryList.score,
  86. tscore = clientSummaryList.tnteractScore,
  87. tmdid = teacher.id,
  88. school = school,
  89. scope = lessonRecord.scope,
  90. lessonId = lessonRecord.id,
  91. courseId = lessonRecord.courseId,
  92. periodId = lessonRecord.periodId,
  93. subjectId = lessonRecord.subjectId,
  94. time = lessonRecord.startTime
  95. }
  96. );
  97. }
  98. }
  99. }
  100. else
  101. {
  102. record = new StudentScoreRecord
  103. {
  104. userType = Constant.ScopeStudent,
  105. id = $"{snowflakeId.NextId()}",
  106. year = year,
  107. stuid = x.id,
  108. school = x.school,
  109. code = $"StudentScoreRecord",
  110. pk = "StudentScoreRecord",
  111. ttl = -1,
  112. lessonRecords = new List<StudentLessonRecord> { new StudentLessonRecord
  113. {
  114. gscore = clientSummaryList.groupScore,
  115. pscore = clientSummaryList.score,
  116. tscore = clientSummaryList.tnteractScore,
  117. tmdid = teacher.id,
  118. school = school,
  119. scope = lessonRecord.scope,
  120. lessonId = lessonRecord.id,
  121. courseId = lessonRecord.courseId,
  122. periodId = lessonRecord.periodId,
  123. subjectId = lessonRecord.subjectId,
  124. time= lessonRecord.startTime
  125. }}
  126. };
  127. }
  128. record.userType = Constant.ScopeStudent;
  129. record.gscore = record.lessonRecords.Select(x => x.gscore).Sum();
  130. record.pscore = record.lessonRecords.Select(x => x.pscore).Sum();
  131. record.tscore = record.lessonRecords.Select(x => x.tscore).Sum();
  132. records.Add(client.GetContainer(Constant.TEAMModelOS, Constant.Student).UpsertItemAsync(record, partitionKey: new PartitionKey(record.code)));
  133. });
  134. tmdids.ToList().ForEach(x => {
  135. var record = lessonStudentRecords.Find(l => l.tmdid.Equals(x.id) && l.code.Equals($"StudentScoreRecord"));
  136. ClientSummaryList clientSummaryList = lessonBase.report.clientSummaryList.Find(c => c.seatID == x.seatID);
  137. if (record != null)
  138. {
  139. if (clientSummaryList != null)
  140. {
  141. var hasrecord = record.lessonRecords.Find(x => x.lessonId.Equals(lessonRecord.id));
  142. if (hasrecord != null)
  143. {
  144. hasrecord.gscore = clientSummaryList.groupScore;
  145. hasrecord.pscore = clientSummaryList.score;
  146. hasrecord.tscore = clientSummaryList.tnteractScore;
  147. hasrecord.tmdid = teacher.id;
  148. hasrecord.school = school;
  149. hasrecord.scope = lessonRecord.scope;
  150. hasrecord.lessonId = lessonRecord.id;
  151. hasrecord.courseId = lessonRecord.courseId;
  152. hasrecord.periodId = lessonRecord.periodId;
  153. hasrecord.subjectId = lessonRecord.subjectId;
  154. hasrecord.time = lessonRecord.startTime;
  155. }
  156. else
  157. {
  158. record.lessonRecords.Add(
  159. new StudentLessonRecord
  160. {
  161. gscore = clientSummaryList.groupScore,
  162. pscore = clientSummaryList.score,
  163. tscore = clientSummaryList.tnteractScore,
  164. tmdid = teacher.id,
  165. school = school,
  166. scope = lessonRecord.scope,
  167. lessonId = lessonRecord.id,
  168. courseId = lessonRecord.courseId,
  169. periodId = lessonRecord.periodId,
  170. subjectId = lessonRecord.subjectId,
  171. time = lessonRecord.startTime
  172. }
  173. );
  174. }
  175. }
  176. }
  177. else
  178. {
  179. record = new StudentScoreRecord
  180. {
  181. userType = Constant.ScopeTmdUser,
  182. id = $"{snowflakeId.NextId()}",
  183. code = $"StudentScoreRecord",
  184. pk = "StudentScoreRecord",
  185. ttl = -1,
  186. year = year,
  187. tmdid = x.id,
  188. lessonRecords = new List<StudentLessonRecord>
  189. {
  190. new StudentLessonRecord
  191. {
  192. gscore = clientSummaryList.groupScore,
  193. pscore = clientSummaryList.score,
  194. tscore = clientSummaryList.tnteractScore,
  195. tmdid = teacher.id,
  196. school = school,
  197. scope = lessonRecord.scope,
  198. lessonId = lessonRecord.id,
  199. courseId = lessonRecord.courseId,
  200. periodId = lessonRecord.periodId,
  201. subjectId = lessonRecord.subjectId,
  202. time=lessonRecord.startTime
  203. }
  204. }
  205. };
  206. }
  207. record.userType = Constant.ScopeStudent;
  208. record.gscore = record.lessonRecords.Select(x => x.gscore).Sum();
  209. record.pscore = record.lessonRecords.Select(x => x.pscore).Sum();
  210. record.tscore = record.lessonRecords.Select(x => x.tscore).Sum();
  211. records.Add(client.GetContainer(Constant.TEAMModelOS, Constant.Student).UpsertItemAsync(record, partitionKey: new PartitionKey(record.code)));
  212. });
  213. if (records.Any())
  214. {
  215. await Task.WhenAll(records);
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. await _dingding.SendBotMsg($"学生个人课例统计信息异常,{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  221. }
  222. }
  223. public static async void DoAutoDeleteSchoolLessonRecord(LessonRecord lessonRecord, string scope, CosmosClient client, string school, string tmdid,
  224. Teacher teacher, NotificationService _notificationService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration)
  225. {
  226. if (lessonRecord.scope.Equals("school"))
  227. {
  228. SchoolSetting setting = null;
  229. Azure.Response schoolSetting = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(school, new PartitionKey("SchoolSetting"));
  230. School schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
  231. if (schoolSetting.Status == 200)
  232. {
  233. setting = JsonDocument.Parse(schoolSetting.Content).RootElement.Deserialize<SchoolSetting>();
  234. if (setting.lessonSetting != null)
  235. {
  236. if (setting.lessonSetting.openAutoClean != 0 && setting.lessonSetting.openAutoClean != 1)
  237. {
  238. setting.lessonSetting.openAutoClean = 0;
  239. setting.lessonSetting.expireDays = Constant.school_lesson_expire;
  240. }
  241. }
  242. else
  243. {
  244. setting.lessonSetting = new LessonSetting() { openAutoClean = 0, expireDays = Constant.school_lesson_expire };
  245. }
  246. }
  247. else
  248. {
  249. setting = new SchoolSetting() { lessonSetting = new LessonSetting { openAutoClean = 0, expireDays = Constant.school_lesson_expire } };
  250. }
  251. int school_lesson_expire = 0;
  252. bool save = true;
  253. List<string> msg = new List<string>();
  254. if (setting.lessonSetting.openAutoClean == 1)
  255. {
  256. if (setting.lessonSetting.conds.IsEmpty())
  257. {
  258. save = false;
  259. }
  260. else
  261. {
  262. school_lesson_expire = setting.lessonSetting.expireDays;
  263. foreach (var item in setting.lessonSetting.conds)
  264. {
  265. switch (item.type)
  266. {
  267. case ">=":
  268. switch (item.key)
  269. {
  270. case "attendRate":
  271. if (!(lessonRecord.attendRate >= item.val))
  272. {
  273. save = false;
  274. msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
  275. }
  276. break;
  277. case "groupCount":
  278. if (!(lessonRecord.groupCount >= item.val))
  279. {
  280. save = false;
  281. msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
  282. }
  283. break;
  284. case "totalPoint":
  285. if (!(lessonRecord.totalPoint >= item.val))
  286. {
  287. save = false;
  288. msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
  289. }
  290. break;
  291. case "collateTaskCount":
  292. if (!(lessonRecord.collateTaskCount >= item.val))
  293. {
  294. save = false;
  295. msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
  296. }
  297. break;
  298. case "collateCount":
  299. if (!(lessonRecord.collateCount >= item.val))
  300. {
  301. save = false;
  302. msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
  303. }
  304. break;
  305. case "pushCount":
  306. if (!(lessonRecord.pushCount >= item.val))
  307. {
  308. save = false;
  309. msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
  310. }
  311. break;
  312. case "totalInteractPoint":
  313. if (!(lessonRecord.totalInteractPoint >= item.val))
  314. {
  315. save = false;
  316. msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
  317. }
  318. break;
  319. case "interactionCount":
  320. if (!(lessonRecord.interactionCount >= item.val))
  321. {
  322. save = false;
  323. msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
  324. }
  325. break;
  326. case "clientInteractionCount":
  327. if (!(lessonRecord.clientInteractionCount >= item.val))
  328. {
  329. save = false;
  330. msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
  331. }
  332. break;
  333. case "examQuizCount":
  334. if (!(lessonRecord.examQuizCount >= item.val))
  335. {
  336. save = false;
  337. msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
  338. }
  339. break;
  340. case "examPointRate":
  341. if (!(lessonRecord.examPointRate >= item.val))
  342. {
  343. save = false;
  344. msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
  345. }
  346. break;
  347. }
  348. break;
  349. case "<=":
  350. switch (item.key)
  351. {
  352. case "attendRate":
  353. if (!(lessonRecord.attendRate <= item.val))
  354. {
  355. save = false;
  356. msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
  357. }
  358. break;
  359. case "groupCount":
  360. if (!(lessonRecord.groupCount <= item.val))
  361. {
  362. save = false;
  363. msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
  364. }
  365. break;
  366. case "totalPoint":
  367. if (!(lessonRecord.totalPoint <= item.val))
  368. {
  369. save = false;
  370. msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
  371. }
  372. break;
  373. case "collateTaskCount":
  374. if (!(lessonRecord.collateTaskCount <= item.val))
  375. {
  376. save = false;
  377. msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
  378. }
  379. break;
  380. case "collateCount":
  381. if (!(lessonRecord.collateCount <= item.val))
  382. {
  383. save = false;
  384. msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
  385. }
  386. break;
  387. case "pushCount":
  388. if (!(lessonRecord.pushCount <= item.val))
  389. {
  390. save = false;
  391. msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
  392. }
  393. break;
  394. case "totalInteractPoint":
  395. if (!(lessonRecord.totalInteractPoint <= item.val))
  396. {
  397. save = false;
  398. msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
  399. }
  400. break;
  401. case "interactionCount":
  402. if (!(lessonRecord.interactionCount <= item.val))
  403. {
  404. save = false;
  405. msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
  406. }
  407. break;
  408. case "clientInteractionCount":
  409. if (!(lessonRecord.clientInteractionCount <= item.val))
  410. {
  411. save = false;
  412. msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
  413. }
  414. break;
  415. case "examQuizCount":
  416. if (!(lessonRecord.examQuizCount <= item.val))
  417. {
  418. save = false;
  419. msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
  420. }
  421. break;
  422. case "examPointRate":
  423. if (!(lessonRecord.examPointRate <= item.val))
  424. {
  425. save = false;
  426. msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
  427. }
  428. break;
  429. }
  430. break;
  431. }
  432. }
  433. }
  434. }
  435. else
  436. {
  437. save = false;
  438. school_lesson_expire = Constant.school_lesson_expire;
  439. }
  440. if (!save && school_lesson_expire > 0)
  441. {
  442. // 1-时间戳,7-时间戳
  443. Dictionary<int, ExpireTag> result = new Dictionary<int, ExpireTag>();
  444. //暂定7天
  445. var now = DateTimeOffset.UtcNow;
  446. //剩余3天的通知
  447. //var day3= now.AddDays(school_lesson_expire - 3).ToUnixTimeMilliseconds();
  448. //result.Add(3, day3);
  449. //剩余1天的通知
  450. var day1 = now.AddDays(school_lesson_expire - (school_lesson_expire - 1)).ToUnixTimeMilliseconds();
  451. result.Add(1, new ExpireTag { expire = day1, tag = "notification" });
  452. //到期通知
  453. //不到五点上传的课例,七天之后直接删除。
  454. int addSecond = 0;
  455. if (now.Hour > 5)
  456. {
  457. // 到凌晨00点还差 (24 - now.Hour) *60 * 60 分钟,再加天数;
  458. addSecond = school_lesson_expire * 86400 + (24 - now.Hour) * 3600 - (now.Hour * 3600);
  459. //再加 00到05小时内的 随机秒数
  460. Random rand = new Random();
  461. int randInt = rand.Next(0, 18000);
  462. addSecond += randInt;
  463. }
  464. else
  465. {
  466. addSecond = school_lesson_expire * 24 * 60 * 60;
  467. }
  468. lessonRecord.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
  469. result.Add(school_lesson_expire, new ExpireTag { expire = lessonRecord.expire, tag = "delete" });
  470. // result.Add(school_lesson_expire, lessonRecord.expire);
  471. string biz = "expire";
  472. Notification notification = new Notification
  473. {
  474. hubName = "hita",
  475. type = "msg",
  476. from = $"ies5:{Environment.GetEnvironmentVariable("Option:Location")}:private",
  477. to = new List<string> { tmdid },
  478. label = $"{biz}_lessonRecord",
  479. body = new
  480. {
  481. location = $"{Environment.GetEnvironmentVariable("Option:Location")}",
  482. biz = biz,
  483. tmdid = tmdid,
  484. tmdname = teacher.name,
  485. scope = scope,
  486. school = school,
  487. schoolName = schoolBase.name,
  488. sid = lessonRecord.id,
  489. sname = lessonRecord.name,
  490. stime = lessonRecord.startTime,
  491. expire = lessonRecord.expire,
  492. status = 1,
  493. //day = school_lesson_expire,
  494. time = now
  495. }.ToJsonString(),
  496. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  497. };
  498. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  499. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  500. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  501. var location = $"{Environment.GetEnvironmentVariable("Option:Location")}";
  502. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  503. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  504. List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
  505. if (records.Count <= 0)
  506. {
  507. foreach (var item in result)
  508. {
  509. string PartitionKey = string.Format("{0}{1}{2}", lessonRecord.code, "-", $"expire-{item.Key}");
  510. //课堂的id ,
  511. //课堂的通知时间类型progress, 默认就会发送一条,到期前一天发送一条,最后已到期发送一条。
  512. var message = new ServiceBusMessage(new
  513. {
  514. id = lessonRecord.id,
  515. progress = item.Key,
  516. code = lessonRecord.code,
  517. scope = lessonRecord.scope,
  518. school = lessonRecord.school,
  519. opt = "delete",
  520. expire = lessonRecord.expire,
  521. tmdid = tmdid,
  522. tmdname = teacher.name,
  523. name = lessonRecord.name,
  524. startTime = lessonRecord.startTime,
  525. tag = item.Value.tag
  526. }.ToJsonString());
  527. message.ApplicationProperties.Add("name", "LessonRecordExpire");
  528. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value.expire));
  529. ChangeRecord changeRecord = new ChangeRecord
  530. {
  531. RowKey = lessonRecord.id,
  532. PartitionKey = PartitionKey,
  533. sequenceNumber = start,
  534. msgId = message.MessageId
  535. };
  536. await table.Save<ChangeRecord>(changeRecord);
  537. }
  538. }
  539. }
  540. else
  541. {
  542. if (lessonRecord.expire > 0)
  543. {
  544. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  545. List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
  546. foreach (var record in records)
  547. {
  548. try
  549. {
  550. await table.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  551. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  552. }
  553. catch (Exception)
  554. {
  555. continue;
  556. }
  557. }
  558. }
  559. lessonRecord.save = 1;
  560. lessonRecord.expire = -1;
  561. }
  562. }
  563. }
  564. public record ExpireTag
  565. {
  566. public long expire { get; set; }
  567. public string tag { get; set; }
  568. }
  569. /// <summary>
  570. ///
  571. /// </summary>
  572. /// <param name="client"></param>
  573. /// <param name="_dingDing"></param>
  574. /// <param name="data"></param>
  575. /// <returns></returns>
  576. public static LessonDis DisLessonCount(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
  577. {
  578. //创建课堂的情况
  579. if (oldRecord == null && newRecord != null)
  580. {
  581. lessonDis.record = 1;
  582. }
  583. //删除数据的情况
  584. //不再对LessonCount进行减
  585. else if (oldRecord != null && newRecord == null)
  586. {
  587. /*lessonDis.record = -1;
  588. //P分数量加减
  589. if (oldRecord.pScore >= 70)
  590. {
  591. lessonDis.disPCount = -1;
  592. }
  593. //T分数量加减
  594. if (oldRecord.tScore >= 70)
  595. {
  596. lessonDis.disTCount = -1;
  597. }
  598. if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
  599. {
  600. lessonDis.disTCount = -1;
  601. }*/
  602. }
  603. //无效操作
  604. else if (oldRecord == null && newRecord == null)
  605. {
  606. }
  607. //前后操作都有值,则表示更新
  608. else
  609. {
  610. //P分数量加减
  611. if (oldRecord.pScore >= 70)
  612. {
  613. if (newRecord.pScore < 70)
  614. {
  615. lessonDis.disPCount = -1;
  616. }
  617. }
  618. else
  619. {
  620. if (newRecord.pScore >= 70)
  621. {
  622. lessonDis.disPCount = 1;
  623. }
  624. }
  625. //T分数量加减
  626. if (oldRecord.tScore >= 70)
  627. {
  628. if (newRecord.tScore < 70)
  629. {
  630. lessonDis.disTCount = -1;
  631. }
  632. }
  633. else
  634. {
  635. if (newRecord.tScore >= 70)
  636. {
  637. lessonDis.disTCount = 1;
  638. }
  639. }
  640. //双绿灯数量
  641. if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
  642. {
  643. if (newRecord.tScore < 70 || newRecord.pScore < 70)
  644. {
  645. lessonDis.disDCount = -1;
  646. }
  647. }
  648. else
  649. {
  650. if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
  651. {
  652. lessonDis.disDCount = 1;
  653. }
  654. }
  655. }
  656. return lessonDis;
  657. }
  658. public static LessonDis DisLessonCount_2(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
  659. {
  660. //创建课堂的情况
  661. if (oldRecord == null && newRecord != null)
  662. {
  663. lessonDis.record = 1;
  664. //P分数量加减
  665. if (newRecord.pScore >= 70)
  666. {
  667. lessonDis.disPCount = 1;
  668. }
  669. //T分数量加减
  670. if (newRecord.tScore >= 70)
  671. {
  672. lessonDis.disTCount = 1;
  673. }
  674. //双绿灯数量
  675. if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
  676. {
  677. lessonDis.disDCount = 1;
  678. }
  679. }
  680. return lessonDis;
  681. }
  682. public static async Task FixLessonCount(CosmosClient client, DingDing _dingDing, LessonRecord record, LessonRecord oldRecord, LessonDis lessonDis)
  683. {
  684. LessonRecord data = null;
  685. try
  686. {
  687. if (record != null && oldRecord == null)
  688. {
  689. data = record;
  690. }
  691. if (record == null && oldRecord != null)
  692. {
  693. data = oldRecord;
  694. }
  695. if (record != null && oldRecord != null)
  696. {
  697. data = record;
  698. }
  699. int day = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).DayOfYear;
  700. int year = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).Year;
  701. int days = DateTimeHelper.getDays(year);
  702. //int years = DateTimeOffset.UtcNow.DayOfYear;
  703. string tbname = string.Empty;
  704. string code = string.Empty;
  705. if (data.scope != null && data.scope.Equals("school"))
  706. {
  707. if (string.IsNullOrEmpty(data.periodId))
  708. {
  709. code = $"LessonCount-{data.school}-{year}";
  710. tbname = "School";
  711. }
  712. else
  713. {
  714. code = $"LessonCount-{data.school}-{year}-{data.periodId}";
  715. tbname = "School";
  716. }
  717. }
  718. else
  719. {
  720. code = $"LessonCount-{year}";
  721. tbname = "Teacher";
  722. }
  723. var response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(data.tmdid.ToString(), new PartitionKey(code));
  724. if (response.Status == 200)
  725. {
  726. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  727. LessonCount count = json.ToObject<LessonCount>();
  728. count.tCount[day - 1] += lessonDis.disTCount;
  729. count.pCount[day - 1] += lessonDis.disPCount;
  730. count.ptCount[day - 1] += lessonDis.disDCount;
  731. count.beginCount[day - 1] += lessonDis.record;
  732. await client.GetContainer("TEAMModelOS", tbname).ReplaceItemAsync(count, count.id, new PartitionKey(code));
  733. }
  734. else
  735. {
  736. LessonCount count = new()
  737. {
  738. id = data.tmdid,
  739. code = code,
  740. ttl = -1
  741. };
  742. double[] da = new double[days];
  743. List<double> list = new(da);
  744. List<double> listT = new(da);
  745. List<double> listP = new(da);
  746. List<double> listPT = new(da);
  747. list[day - 1] += lessonDis.record;
  748. listT[day - 1] += lessonDis.disTCount;
  749. listP[day - 1] += lessonDis.disPCount;
  750. listPT[day - 1] += lessonDis.disDCount;
  751. count.beginCount.AddRange(list);
  752. count.tCount.AddRange(listT);
  753. count.pCount.AddRange(listP);
  754. count.ptCount.AddRange(listPT);
  755. //count.courseIds.Add(data.courseId);
  756. await client.GetContainer("TEAMModelOS", tbname).CreateItemAsync(count, new PartitionKey(code));
  757. }
  758. }
  759. catch (Exception ex)
  760. {
  761. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-LessonCount-FixLessonCount\n{ex.Message}\n{ex.StackTrace}{data.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  762. }
  763. }
  764. }
  765. }