LessonService.cs 38 KB

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