IESHttpTrigger.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs.Models;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.Azure.Cosmos.Table;
  5. using Microsoft.Azure.Functions.Worker;
  6. using Microsoft.Azure.Functions.Worker.Http;
  7. using StackExchange.Redis;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Text.Json;
  16. using System.Threading.Tasks;
  17. using TEAMModelOS.SDK;
  18. using TEAMModelOS.SDK.DI;
  19. using TEAMModelOS.SDK.Extension;
  20. using TEAMModelOS.SDK.Models;
  21. using TEAMModelOS.SDK.Models.Cosmos.Teacher;
  22. using TEAMModelOS.SDK.Models.Table;
  23. using static TEAMModelOS.SDK.Models.Teacher;
  24. namespace TEAMModelOS.FunctionV4.HttpTrigger
  25. {
  26. public class IESHttpTrigger
  27. {
  28. private readonly AzureCosmosFactory _azureCosmos;
  29. private readonly DingDing _dingDing;
  30. private readonly AzureStorageFactory _azureStorage;
  31. private readonly AzureRedisFactory _azureRedis;
  32. public IESHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  33. , AzureRedisFactory azureRedis)
  34. {
  35. _azureCosmos = azureCosmos;
  36. _dingDing = dingDing;
  37. _azureStorage = azureStorage;
  38. _azureRedis = azureRedis;
  39. }
  40. /// <summary>
  41. /// </summary>
  42. /// <param name="req"></param>
  43. /// <param name="log"></param>
  44. /// <returns></returns>
  45. [Function("system-info-function")]
  46. public async Task<HttpResponseData> SystemInfo([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestData req)
  47. {
  48. var response = req.CreateResponse(HttpStatusCode.OK);
  49. Type attr = this.GetType();
  50. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  51. Assembly assembly = Assembly.LoadFrom($"{currentDirectory}\\TEAMModelOS.FunctionV4.dll");
  52. var description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  53. //var v1 = Assembly.GetEntryAssembly().GetName().Version;
  54. //var v2 = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  55. // var description = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  56. var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  57. long nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  58. //Console.WriteLine($"Assembly.GetEntryAssembly().GetName().Version: " +
  59. // $"{Assembly.GetEntryAssembly().GetName().Version}");5.2107.12.1
  60. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version:" +
  61. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}");5.2107.12.1
  62. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion:" +
  63. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion}");5.2107.12
  64. await response.WriteAsJsonAsync(new { version, description, nowtime });
  65. return response;
  66. }
  67. /// <summary>
  68. /// 数据推送接口
  69. /// </summary>
  70. /// <param name="req"></param>
  71. /// <param name="log"></param>
  72. /// <returns></returns>
  73. [Function("lesson-tag-change")]
  74. public async Task<HttpResponseData> LessonTagChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req) {
  75. var response = req.CreateResponse(HttpStatusCode.OK);
  76. string data = await new StreamReader(req.Body).ReadToEndAsync();
  77. var json = JsonDocument.Parse(data).RootElement;
  78. List<TagOldNew> old_new = null;
  79. string school = null;
  80. if (json.TryGetProperty("school", out JsonElement _school))
  81. {
  82. school = _school.GetString();
  83. }
  84. if (json.TryGetProperty("old_new", out JsonElement _old_new))
  85. {
  86. old_new = _old_new.ToObject<List<TagOldNew>>();
  87. }
  88. if (old_new.IsNotEmpty() && !string.IsNullOrWhiteSpace(school))
  89. {
  90. foreach (var on in old_new)
  91. {
  92. List<LessonRecord> lessonRecords = new List<LessonRecord>();
  93. string sql = $"select value(c) from c where array_contains(c.category,'{on._old}') ";
  94. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>
  95. (queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{_school}") }))
  96. {
  97. lessonRecords.Add(item);
  98. }
  99. lessonRecords.ForEach(item =>
  100. {
  101. //修改标签
  102. if (!string.IsNullOrWhiteSpace(on._new))
  103. {
  104. for (int i = 0; i < item.category.Count; i++)
  105. {
  106. if (item.category[i].Equals(on._old))
  107. {
  108. item.category[i] = on._new;
  109. }
  110. }
  111. }
  112. else
  113. {
  114. //表示删除标签
  115. item.category.RemoveAll(x => x.Equals(on._old));
  116. }
  117. });
  118. foreach (var item in lessonRecords)
  119. {
  120. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(item, item.id, new PartitionKey(item.code));
  121. }
  122. }
  123. }
  124. await response.WriteAsJsonAsync(new { data = json });
  125. return response;
  126. }
  127. /// <summary>
  128. /// 数据推送接口
  129. /// </summary>
  130. /// <param name="req"></param>
  131. /// <param name="log"></param>
  132. /// <returns></returns>
  133. [Function("knowledge-change")]
  134. public async Task<HttpResponseData> KnowledgeChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req)
  135. {
  136. var response = req.CreateResponse(HttpStatusCode.OK);
  137. string data = await new StreamReader(req.Body).ReadToEndAsync();
  138. var json = JsonDocument.Parse(data).RootElement;
  139. List<TagOldNew> old_new = null;
  140. string school = null;
  141. if (json.TryGetProperty("school", out JsonElement _school))
  142. {
  143. school = _school.GetString();
  144. }
  145. if (json.TryGetProperty("old_new", out JsonElement _old_new))
  146. {
  147. old_new = _old_new.ToObject<List<TagOldNew>>();
  148. }
  149. if (old_new.IsNotEmpty() && !string.IsNullOrWhiteSpace(school))
  150. {
  151. foreach (var on in old_new)
  152. {
  153. List<ItemInfo> items = new List<ItemInfo>();
  154. string sql = $"select value(c) from c where array_contains(c.knowledge,'{on._old}') ";
  155. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>
  156. (queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{_school}") }))
  157. {
  158. items.Add(item);
  159. }
  160. items.ForEach(item =>
  161. {
  162. //修改知识点
  163. if (!string.IsNullOrEmpty(on._new))
  164. {
  165. for (int i = 0; i < item.knowledge.Count; i++)
  166. {
  167. if (item.knowledge[i].Equals(on._old))
  168. {
  169. item.knowledge[i] = on._new;
  170. }
  171. }
  172. }
  173. else
  174. {
  175. //表示删除知识点
  176. item.knowledge.RemoveAll(x => x.Equals(on._old));
  177. }
  178. });
  179. foreach (var item in items)
  180. {
  181. ItemBlob itemBlob = null;
  182. try
  183. {
  184. BlobDownloadInfo blobDownloadResult = await _azureStorage.GetBlobContainerClient($"{school}").GetBlobClient($"/item/{item.id}/{item.id}.json").DownloadAsync();
  185. if (blobDownloadResult != null)
  186. {
  187. var blob = JsonDocument.Parse(blobDownloadResult.Content);
  188. itemBlob = blob.RootElement.ToObject<ItemBlob>();
  189. itemBlob.exercise.knowledge = item.knowledge;
  190. await _azureStorage.GetBlobContainerClient($"{school}").UploadFileByContainer(itemBlob.ToJsonString(), "item", $"{item.id}/{item.id}.json", true);
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. itemBlob = null;
  196. }
  197. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(item, item.id, new PartitionKey(item.code));
  198. }
  199. }
  200. }
  201. await response.WriteAsJsonAsync(new { data = json });
  202. return response;
  203. }
  204. /// <summary>
  205. /// 在线人数记录
  206. /// </summary>
  207. /// <param name="msg"></param>
  208. /// <returns></returns>
  209. [Function("online-record")]
  210. public async Task<HttpResponseData> OnlineRecord([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestData req)
  211. {
  212. var response = req.CreateResponse(HttpStatusCode.OK);
  213. string data = await new StreamReader(req.Body).ReadToEndAsync();
  214. var json = JsonDocument.Parse(data).RootElement;
  215. try
  216. {
  217. string school = null;
  218. string scope = null;
  219. string id = null;
  220. string ip = null;
  221. int expire = 1;
  222. if (json.TryGetProperty("school", out JsonElement _school))
  223. school = _school.GetString();
  224. if (json.TryGetProperty("scope", out JsonElement _scope))
  225. scope = _scope.GetString();
  226. if (json.TryGetProperty("id", out JsonElement _id))
  227. id = _id.GetString();
  228. if (json.TryGetProperty("ip", out JsonElement _ip))
  229. ip = _ip.GetString();
  230. if (json.TryGetProperty("expire", out JsonElement _expire))
  231. expire = _expire.GetInt32();
  232. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
  233. var cosmosClient = _azureCosmos.GetCosmosClient();
  234. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  235. var dateHour = dateTime.ToString("yyyyMMddHH"); //获取当天的小时
  236. var dateDay = dateTime.ToString("yyyyMMdd"); //获取当天的日期
  237. var dateMonth = dateTime.ToString("yyyyMM");//获取当月的日期
  238. var currentHour = dateTime.Hour; //当前小时
  239. var currentDay = dateTime.Day; //当前天
  240. long Expire = dateTime.AddHours(expire).ToUnixTimeMilliseconds(); //token到期时间
  241. long now = dateTime.ToUnixTimeMilliseconds(); //当前时间戳
  242. DateTime hour = DateTime.UtcNow.AddHours(25); //25小时到期
  243. DateTime month = DateTime.UtcNow.AddDays(32); //一个月到期
  244. var delTbHour = dateTime.AddHours(-168).ToString("yyyyMMddHH"); //168小时前
  245. var delTbDay = dateTime.AddDays(-180).ToString("yyyyMMdd"); //180天前
  246. switch (scope)
  247. {
  248. case "teacher":
  249. try
  250. {
  251. Teacher teacher = await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<Teacher>(id, new PartitionKey("Base"));
  252. teacher.loginInfos = new List<LoginInfo>() { new LoginInfo { expire = Expire, ip = ip, time = now } };
  253. await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  254. }
  255. catch { }
  256. break;
  257. case "student":
  258. try
  259. {
  260. Student student = await cosmosClient.GetContainer("TEAMModelOS", Constant.Student).ReadItemAsync<Student>(id, new PartitionKey($"Base-{school}"));
  261. student.loginInfos = new List<LoginInfo>() { new LoginInfo { expire = Expire, ip = ip, time = now } };
  262. await cosmosClient.GetContainer("TEAMModelOS", Constant.Student).ReplaceItemAsync<Student>(student, student.id, new PartitionKey($"Base-{school}"));
  263. }
  264. catch { }
  265. break;
  266. case "tmduser":
  267. try
  268. {
  269. TmdUser tmdUser = await cosmosClient.GetContainer("TEAMModelOS", Constant.Student).ReadItemAsync<TmdUser>(id, new PartitionKey("Base"));
  270. tmdUser.loginInfos = new List<LoginInfo>() { new LoginInfo { expire = Expire, ip = ip, time = now } };
  271. await cosmosClient.GetContainer("TEAMModelOS", Constant.Student).ReplaceItemAsync<TmdUser>(tmdUser, tmdUser.id, new PartitionKey("Base"));
  272. }
  273. catch { }
  274. break;
  275. }
  276. //天
  277. SortedSetEntry[] dayCnt = null;
  278. //月
  279. SortedSetEntry[] monthCnt = null;
  280. try
  281. {
  282. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:IES:{scope}:{dateDay}", $"{currentHour}", 1);//一天24小时 小时为单位
  283. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:IES:{scope}:{dateMonth}", $"{currentDay}", 1); //一天的累计 天为单位
  284. var resDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:IES:{scope}:{dateDay}");
  285. if (resDay == null)
  286. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:IES:{scope}:{dateDay}", hour); //设置到期时间
  287. var rspMonth = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:IES:{scope}:{dateMonth}");
  288. if (rspMonth == null)
  289. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:IES:{scope}:{dateMonth}", month); //设置到期时间
  290. //保存当前小时统计
  291. dayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateDay}");
  292. //保存当前的统计数据
  293. monthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateMonth}");
  294. }
  295. catch { }
  296. if (dayCnt != null && dayCnt.Length > 0)
  297. {
  298. List<HourLogin> hourLogins = new();
  299. foreach (var dCnt in dayCnt)
  300. {
  301. if (((int)dCnt.Element) == currentHour)
  302. {
  303. var tphourLogins = await table.QueryWhereString<HourLogin>($"PartitionKey eq 'HourLogin' and RowKey eq '{dateHour}'");
  304. if (tphourLogins.Count > 0)
  305. {
  306. foreach (var hourLogin in tphourLogins)
  307. {
  308. if (scope.Equals("teacher"))
  309. hourLogin.Teacher = (int)dCnt.Score;
  310. else if (scope.Equals("student"))
  311. hourLogin.Student = (int)dCnt.Score;
  312. else
  313. hourLogin.TmdUser = (int)dCnt.Score;
  314. hourLogins.Add(hourLogin);
  315. }
  316. }
  317. else
  318. {
  319. HourLogin hourLogin = new() { PartitionKey = $"HourLogin", RowKey = dateHour, Hour = currentHour };
  320. if (scope.Equals("teacher"))
  321. {
  322. hourLogin.Teacher = 1;
  323. hourLogin.Student = 0;
  324. hourLogin.TmdUser = 0;
  325. }
  326. else if (scope.Equals("student"))
  327. {
  328. hourLogin.Teacher = 0;
  329. hourLogin.Student = 1;
  330. hourLogin.TmdUser = 0;
  331. }
  332. else
  333. {
  334. hourLogin.Teacher = 0;
  335. hourLogin.Student = 0;
  336. hourLogin.TmdUser = 1;
  337. }
  338. hourLogins.Add(hourLogin);
  339. }
  340. }
  341. }
  342. await table.SaveOrUpdateAll(hourLogins); //保存和更新保存当前小时登录次数
  343. }
  344. if (monthCnt != null && monthCnt.Length > 0)
  345. {
  346. List<DayLogin> dayLogins = new();
  347. foreach (var mCnt in monthCnt)
  348. {
  349. if (((int)mCnt.Element) == currentDay)
  350. {
  351. //保存当天的峰值
  352. var tbDays = await table.QueryWhereString<DayLogin>($"PartitionKey eq 'DayLogin' and RowKey eq '{dateDay}'");
  353. if (tbDays.Count > 0)
  354. {
  355. foreach (var dayLogin in tbDays)
  356. {
  357. if (scope.Equals("teacher"))
  358. dayLogin.Teacher = (int)mCnt.Score;
  359. else if (scope.Equals("student"))
  360. dayLogin.Student = (int)mCnt.Score;
  361. else
  362. dayLogin.TmdUser = (int)mCnt.Score;
  363. dayLogins.Add(dayLogin);
  364. }
  365. }
  366. else
  367. {
  368. //保存当月每天的峰值
  369. DayLogin dayLogin = new() { PartitionKey = $"DayLogin", RowKey = dateDay, Day = currentDay };
  370. if (scope.Equals("teacher"))
  371. {
  372. dayLogin.Teacher = 1;
  373. dayLogin.Student = 0;
  374. dayLogin.TmdUser = 0;
  375. }
  376. else if (scope.Equals("student"))
  377. {
  378. dayLogin.Teacher = 0;
  379. dayLogin.Student = 1;
  380. dayLogin.TmdUser = 0;
  381. }
  382. else
  383. {
  384. dayLogin.Teacher = 0;
  385. dayLogin.Student = 0;
  386. dayLogin.TmdUser = 1;
  387. }
  388. dayLogins.Add(dayLogin);
  389. }
  390. }
  391. }
  392. await table.SaveOrUpdateAll(dayLogins);// 保存当月每天在线数据
  393. }
  394. string tbHourSql = $"PartitionKey eq 'HourLogin' and RowKey le '{delTbHour}'";
  395. string tbDaySql = $"PartitionKey eq 'DayLogin' and RowKey le '{delTbDay}'";
  396. try
  397. {
  398. await table.DeleteStringWhere<HourLogin>(rowKey: tbHourSql); //删除168小时前的数据
  399. await table.DeleteStringWhere<DayLogin>(rowKey: tbDaySql); //删除180天前的数据
  400. }
  401. catch { }
  402. if (!string.IsNullOrWhiteSpace(school))
  403. {
  404. //天
  405. SortedSetEntry[] scDayCnt = null;
  406. //月
  407. SortedSetEntry[] scMonthCnt = null;
  408. try
  409. {
  410. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:{scope}:{dateDay}", $"{currentHour}", 1);//当天当前小时在线人加1
  411. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:{scope}:{dateMonth}", $"{currentDay}", 1); //当天的在线加1
  412. var reScDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:School:{school}:{scope}:{dateDay}");
  413. if (reScDay == null)
  414. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:School:{school}:{scope}:{dateDay}", hour); //设置到期时间
  415. var reScMonth = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:School:{school}:{scope}:{dateMonth}");
  416. if (reScMonth == null)
  417. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:School:{school}:{scope}:{dateMonth}", month); //设置到期时间
  418. //保存学校当天每小时的
  419. scDayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateDay}");
  420. //学校天峰值
  421. scMonthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateMonth}");
  422. }
  423. catch { }
  424. if (scDayCnt != null && scDayCnt.Length > 0)
  425. {
  426. List<HourLoginSchool> hourLoginSchools = new();
  427. foreach (var scDCnt in scDayCnt)
  428. {
  429. if (((int)scDCnt.Element) == currentHour)
  430. {
  431. var tmpHour = await table.QueryWhereString<HourLoginSchool>($"PartitionKey eq 'HourLogin-{school}' and RowKey eq '{dateHour}'");
  432. if (tmpHour.Count > 0)
  433. {
  434. foreach (var hLoginSc in tmpHour)
  435. {
  436. if (scope.Equals("teacher"))
  437. hLoginSc.Teacher = (int)scDCnt.Score;
  438. else if (scope.Equals("student"))
  439. hLoginSc.Student = (int)scDCnt.Score;
  440. else
  441. hLoginSc.TmdUser = (int)scDCnt.Score;
  442. hourLoginSchools.Add(hLoginSc);
  443. }
  444. }
  445. else
  446. {
  447. //学校小时峰值
  448. HourLoginSchool hourLoginSc = new() { PartitionKey = $"HourLogin-{school}", RowKey = dateHour, Hour = currentHour, School = school };
  449. if (scope.Equals("teacher"))
  450. {
  451. hourLoginSc.Teacher = 1;
  452. hourLoginSc.Student = 0;
  453. hourLoginSc.TmdUser = 0;
  454. }
  455. else if (scope.Equals("student"))
  456. {
  457. hourLoginSc.Teacher = 0;
  458. hourLoginSc.Student = 1;
  459. hourLoginSc.TmdUser = 0;
  460. }
  461. else
  462. {
  463. hourLoginSc.Teacher = 0;
  464. hourLoginSc.Student = 0;
  465. hourLoginSc.TmdUser = 1;
  466. }
  467. hourLoginSchools.Add(hourLoginSc);
  468. }
  469. }
  470. }
  471. await table.SaveOrUpdateAll(hourLoginSchools);
  472. }
  473. if (scMonthCnt != null && scMonthCnt.Length > 0)
  474. {
  475. List<DayLoginSchool> DayLoginSchools = new();
  476. foreach (var scMCnt in scMonthCnt)
  477. {
  478. if (((int)scMCnt.Element) == currentDay)
  479. {
  480. var tempDays = await table.QueryWhereString<DayLoginSchool>($"PartitionKey eq 'DayLogin-{school}' and RowKey eq '{dateDay}'");
  481. if (tempDays.Count > 0)
  482. {
  483. foreach (var dLoginSc in tempDays)
  484. {
  485. if (scope.Equals("teacher"))
  486. dLoginSc.Teacher = (int)scMCnt.Score;
  487. else if (scope.Equals("student"))
  488. dLoginSc.Student = (int)scMCnt.Score;
  489. else
  490. dLoginSc.TmdUser = (int)scMCnt.Score;
  491. DayLoginSchools.Add(dLoginSc);
  492. }
  493. }
  494. else
  495. {
  496. //学校天峰值
  497. DayLoginSchool dayLoginSc = new() { PartitionKey = $"DayLogin-{school}", RowKey = dateDay, Day = currentDay, School = school };
  498. if (scope.Equals("teacher"))
  499. {
  500. dayLoginSc.Teacher = 1;
  501. dayLoginSc.Student = 0;
  502. dayLoginSc.TmdUser = 0;
  503. }
  504. else if (scope.Equals("student"))
  505. {
  506. dayLoginSc.Teacher = 0;
  507. dayLoginSc.Student = 1;
  508. dayLoginSc.TmdUser = 0;
  509. }
  510. else
  511. {
  512. dayLoginSc.Teacher = 0;
  513. dayLoginSc.Student = 0;
  514. dayLoginSc.TmdUser = 1;
  515. }
  516. DayLoginSchools.Add(dayLoginSc);
  517. }
  518. }
  519. }
  520. await table.SaveOrUpdateAll(DayLoginSchools);//保存学校当月在线数据
  521. }
  522. string tbScHourSql = $"PartitionKey eq 'HourLogin-{school}' and RowKey le '{delTbHour}'";
  523. List<HourLogin> scHourLog = await table.QueryWhereString<HourLogin>(tbScHourSql);
  524. if (scHourLog.Count > 0)
  525. try
  526. {
  527. //await table.DeleteStringWhere<HourLogin>(tbScHourSql); //删除学校168小时前的数据
  528. await table.DeleteAll(scHourLog);
  529. }
  530. catch { }
  531. string tbScDaySql = $"PartitionKey eq 'DayLogin-{school}' and RowKey le '{delTbDay}'";
  532. List<DayLogin> scDayLog = await table.QueryWhereString<DayLogin>(tbScDaySql);
  533. if (scDayLog.Count > 0)
  534. try
  535. {
  536. //await table.DeleteStringWhere<DayLogin>(tbScDaySql); //删除学校180天前的数据
  537. await table.DeleteAll(scDayLog);
  538. }
  539. catch { }
  540. }
  541. await response.WriteAsJsonAsync(new { data = json });
  542. return response;
  543. }
  544. catch (Exception ex)
  545. {
  546. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-online-record 人数记录异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  547. await response.WriteAsJsonAsync(new { data = json });
  548. return response;
  549. }
  550. }
  551. }
  552. }