LessonService.cs 39 KB

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