MonitorServicesBus.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.Json;
  5. using System.Threading.Tasks;
  6. using Azure.Cosmos;
  7. using Azure.Messaging.ServiceBus;
  8. using Microsoft.Azure.WebJobs;
  9. using Microsoft.Azure.WebJobs.Host;
  10. using Microsoft.Extensions.Logging;
  11. using StackExchange.Redis;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK;
  15. using TEAMModelOS.SDK.Models;
  16. using TEAMModelOS.SDK.Models.Cosmos;
  17. using TEAMModelOS.SDK.Models.Cosmos.Common;
  18. using TEAMModelOS.Services.Common;
  19. using TEAMModelOS.SDK.Models.Service;
  20. namespace TEAMModelFunction
  21. {
  22. public class MonitorServicesBus
  23. {
  24. private readonly AzureCosmosFactory _azureCosmos;
  25. private readonly DingDing _dingDing;
  26. private readonly AzureStorageFactory _azureStorage;
  27. private readonly AzureRedisFactory _azureRedis;
  28. private readonly AzureServiceBusFactory _serviceBus;
  29. public MonitorServicesBus(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis, AzureServiceBusFactory serviceBus)
  30. {
  31. _azureCosmos = azureCosmos;
  32. _dingDing = dingDing;
  33. _azureStorage = azureStorage;
  34. _azureRedis = azureRedis;
  35. _serviceBus = serviceBus;
  36. }
  37. [FunctionName("Exam")]
  38. public async Task Exam([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "exam", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  39. {
  40. try
  41. {
  42. var json = JsonDocument.Parse(msg);
  43. json.RootElement.TryGetProperty("id", out JsonElement id);
  44. json.RootElement.TryGetProperty("progress", out JsonElement progress);
  45. json.RootElement.TryGetProperty("code", out JsonElement code);
  46. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  47. var client = _azureCosmos.GetCosmosClient();
  48. ExamInfo exam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  49. exam.progress = progress.ToString();
  50. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  51. }
  52. catch (CosmosException)
  53. {
  54. }
  55. catch (Exception ex)
  56. {
  57. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamBus()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  58. }
  59. }
  60. [FunctionName("Vote")]
  61. public async Task Vote([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "vote", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  62. {
  63. try
  64. {
  65. var jsonMsg = JsonDocument.Parse(msg);
  66. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  67. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  68. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  69. var client = _azureCosmos.GetCosmosClient();
  70. Vote vote = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Vote>(id.ToString(), new PartitionKey($"{code}"));
  71. vote.progress = progress.ToString();
  72. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  73. }
  74. catch (CosmosException)
  75. {
  76. }
  77. catch (Exception ex)
  78. {
  79. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,VoteBus()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  80. }
  81. }
  82. [FunctionName("Correct")]
  83. public async Task Correct([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "correct", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  84. {
  85. try
  86. {
  87. var jsonMsg = JsonDocument.Parse(msg);
  88. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  89. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  90. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  91. var client = _azureCosmos.GetCosmosClient();
  92. Correct correct = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Correct>(id.ToString(), new PartitionKey($"{code}"));
  93. correct.progress = progress.ToString();
  94. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(correct, id.ToString(), new PartitionKey($"{code}"));
  95. }
  96. catch (CosmosException)
  97. {
  98. }
  99. catch (Exception ex)
  100. {
  101. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Correct()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  102. }
  103. }
  104. [FunctionName("Survey")]
  105. public async Task Survey([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "survey", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  106. {
  107. try
  108. {
  109. var jsonMsg = JsonDocument.Parse(msg);
  110. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  111. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  112. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  113. //Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  114. var client = _azureCosmos.GetCosmosClient();
  115. Survey survey = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  116. survey.progress = progress.ToString();
  117. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  118. }
  119. catch (CosmosException)
  120. {
  121. }
  122. catch (Exception ex)
  123. {
  124. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,SurveyBus()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  125. }
  126. }
  127. [FunctionName("Homework")]
  128. public async Task Homework([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "homework", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  129. {
  130. try
  131. {
  132. var jsonMsg = JsonDocument.Parse(msg);
  133. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  134. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  135. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  136. var client = _azureCosmos.GetCosmosClient();
  137. Homework homework = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Homework>(id.ToString(), new PartitionKey($"{code}"));
  138. homework.progress = progress.ToString();
  139. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(homework, id.ToString(), new PartitionKey($"{code}"));
  140. }
  141. catch (CosmosException ) {
  142. }
  143. catch (Exception ex)
  144. {
  145. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Homework()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  146. }
  147. }
  148. [FunctionName("Study")]
  149. public async Task Study([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "study", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  150. {
  151. try
  152. {
  153. var jsonMsg = JsonDocument.Parse(msg);
  154. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  155. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  156. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  157. var client = _azureCosmos.GetCosmosClient();
  158. Study study = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Study>(id.ToString(), new PartitionKey($"{code}"));
  159. study.progress = progress.ToString();
  160. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(study, id.ToString(), new PartitionKey($"{code}"));
  161. }
  162. catch (CosmosException)
  163. {
  164. }
  165. catch (Exception ex)
  166. {
  167. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Study()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  168. }
  169. }
  170. [FunctionName("ExamLite")]
  171. public async Task ExamLite([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "examlite", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  172. {
  173. try
  174. {
  175. var jsonMsg = JsonDocument.Parse(msg);
  176. jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
  177. jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
  178. jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
  179. var client = _azureCosmos.GetCosmosClient();
  180. ExamLite lite = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamLite>(id.ToString(), new PartitionKey($"{code}"));
  181. lite.progress = progress.ToString();
  182. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(lite, id.ToString(), new PartitionKey($"{code}"));
  183. }
  184. catch (CosmosException)
  185. {
  186. }
  187. catch (Exception ex)
  188. {
  189. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamLite()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  190. }
  191. }
  192. [FunctionName("Blob")]
  193. public async Task Blob([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "blob", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
  194. try
  195. {
  196. //11 await _dingDing.SendBotMsg($"ServiceBus,Blob(){msg}", GroupNames.醍摩豆服務運維群組);
  197. var jsonMsg = JsonDocument.Parse(msg);
  198. if(jsonMsg.RootElement.TryGetProperty("name", out JsonElement name)&& name.ValueKind==JsonValueKind.String)
  199. {
  200. var client = _azureStorage.GetBlobContainerClient($"{name}");
  201. var size = await client.GetBlobsCatalogSize();
  202. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{long.Parse($"{size.Item1}")}"));
  203. foreach (var key in size.Item2.Keys)
  204. {
  205. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
  206. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  207. }
  208. //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob() 容器:{name}使用:{size.Item1},文件分类:{size.Item2.ToJsonString()}",
  209. // GroupNames.成都开发測試群組);
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  215. }
  216. }
  217. /// <summary>
  218. /// 根据容器的根目录刷新redis并获取redis的最新使用情况
  219. /// </summary>
  220. /// <param name="msg"></param>
  221. /// <returns></returns>
  222. [FunctionName("BlobRoot")]
  223. public async Task BlobRoot([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "blobroot", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  224. {
  225. try
  226. {
  227. var jsonMsg = JsonDocument.Parse(msg);
  228. if (jsonMsg.RootElement.TryGetProperty("name", out JsonElement _name) && _name.ValueKind == JsonValueKind.String
  229. && jsonMsg.RootElement.TryGetProperty("root", out JsonElement root) && root.ValueKind == JsonValueKind.String)
  230. {
  231. //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob() 容器:触发变更,{jsonMsg.ToJsonString()}",
  232. // GroupNames.成都开发測試群組);
  233. List<Dictionary<string, double?>> list = new List<Dictionary<string, double?>>();
  234. string[] uls = System.Web.HttpUtility.UrlDecode($"{root}", Encoding.UTF8).Split("/");
  235. string u = !string.IsNullOrEmpty(uls[0]) ? uls[0] : uls[1];
  236. string name = $"{_name}";
  237. string lockKey = $"Blob:Lock:{name}:{u}";
  238. bool exist= await _azureRedis.GetRedisClient(8).KeyExistsAsync(lockKey);
  239. if (!exist)
  240. { ///key不存在则正常进行计算
  241. bool condition = false;
  242. TimeSpan timeSpan = new TimeSpan(DateTimeOffset.UtcNow.AddMinutes(5).Ticks);
  243. timeSpan = timeSpan - new TimeSpan(DateTimeOffset.UtcNow.Ticks);
  244. //准备处理Blob刷新时间
  245. long action = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  246. await _azureRedis.GetRedisClient(8).StringSetAsync(lockKey, action, expiry: timeSpan);
  247. await RefreshBlob(name, u);
  248. //将action 与Redis最新的时间进行比较,如果
  249. var rds = await CheckLockKey(lockKey, action);
  250. condition = rds.condition;
  251. exist = rds.exist;
  252. if (condition || !exist) {
  253. await RefreshBlob(name, u);
  254. }
  255. //使用 CancellationToken
  256. //while (condition || !exist)
  257. //{
  258. //}
  259. }
  260. else {
  261. ///key存在则,则刷新key对应的值
  262. TimeSpan timeSpan = new TimeSpan(DateTimeOffset.UtcNow.AddMinutes(5).Ticks);
  263. timeSpan = timeSpan - new TimeSpan(DateTimeOffset.UtcNow.Ticks);
  264. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  265. await _azureRedis.GetRedisClient(8).StringSetAsync(lockKey, now, expiry: timeSpan);
  266. }
  267. //await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob() 容器:{name}使用:{u},文件分类:{list.ToJsonString()}",
  268. // GroupNames.成都开发測試群組);
  269. }
  270. }
  271. catch (Exception ex)
  272. {
  273. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  274. }
  275. }
  276. private async Task<(bool condition,bool exist)> CheckLockKey(string lockKey,long nowTime) {
  277. //Redis的最新时间
  278. long newestTime = 0;
  279. RedisValue value = await _azureRedis.GetRedisClient(8).StringGetAsync(lockKey);
  280. if (value != default && !value.IsNullOrEmpty)
  281. {
  282. JsonElement record = value.ToString().ToObject<JsonElement>();
  283. if (record.TryGetInt64(out newestTime))
  284. {
  285. }
  286. }
  287. //说明key已经不存在
  288. if (newestTime == 0)
  289. {
  290. return (false, true);
  291. }
  292. //说明key存在
  293. else {
  294. //说明Redis记录了最新的时间戳
  295. if (nowTime != newestTime)
  296. {
  297. return (true, false);
  298. }
  299. //时间相同,没有被再次记录最新的时间戳
  300. else
  301. {
  302. await _azureRedis.GetRedisClient(8).KeyDeleteAsync(lockKey);
  303. return (false, true);
  304. }
  305. }
  306. }
  307. private async Task RefreshBlob(string name ,string u) {
  308. long statr = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  309. var client = _azureStorage.GetBlobContainerClient(name);
  310. var size = await client.GetBlobsSize(u);
  311. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", u);
  312. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", u, size.HasValue ? size.Value : 0);
  313. var scores = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{name}");
  314. double blobsize = 0;
  315. if (scores != default && scores != null)
  316. {
  317. foreach (var score in scores)
  318. {
  319. blobsize = blobsize + score.Score;
  320. }
  321. }
  322. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue(name), new RedisValue($"{blobsize}"));
  323. long end = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  324. long dis = (end - statr)/1000;
  325. long timeout = 10;
  326. if (dis> timeout) {
  327. await _dingDing.SendBotMsg($"ServiceBus,RefreshBlob:空间计算已经超过{timeout}秒\n容器名:{name}\n文件夹:{u}\n计算时长:{dis}", GroupNames.醍摩豆服務運維群組);
  328. }
  329. }
  330. /// <summary>
  331. /// 完善课程变更,StuListChange, originCode是学校编码 则表示名单是学校自定义名单,如果是tmdid则表示醍摩豆的私有名单,scope=school,private。
  332. /// </summary>
  333. /// <data msg>
  334. /// CourseChange
  335. ///// </data>
  336. /// <param name="msg"></param>
  337. /// <returns></returns>
  338. [FunctionName("GroupChange")]
  339. public async Task GroupChange([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "group-change", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  340. {
  341. var client = _azureCosmos.GetCosmosClient();
  342. try
  343. {
  344. var jsonMsg = JsonDocument.Parse(msg);
  345. GroupChange groupChange = msg.ToObject<GroupChange>();
  346. //名单变动修改学生课程关联信息
  347. //await StuListService.FixStuCourse(client, stuListChange);
  348. //Vote投票 Survey问卷 Exam评测 Learn学习活动 Homework作业活动
  349. //名单变动修改学生问卷关联信息
  350. await ActivityService.FixActivity(client, _dingDing, groupChange, "Survey");
  351. //名单变动修改学生投票关联信息
  352. await ActivityService.FixActivity(client, _dingDing, groupChange, "Vote");
  353. //名单变动修改学生评测关联信息
  354. await ActivityService.FixActivity(client, _dingDing, groupChange, "Exam");
  355. //TODO学习活动
  356. //await FixActivity(client, stuListChange, "Learn");
  357. //名单变动修改学生作业活动信息
  358. await ActivityService.FixActivity(client, _dingDing, groupChange, "Homework");
  359. if (groupChange.type == null || !groupChange.type.Equals("research"))
  360. {
  361. //课程名单变动修改学生课程关联信息
  362. await ActivityService.FixStuCourse(client, _dingDing, groupChange);
  363. }
  364. }
  365. catch (Exception ex)
  366. {
  367. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-GroupChange-GroupChange\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.成都开发測試群組);
  368. }
  369. }
  370. [FunctionName("ItemCond")]
  371. public async Task ItemCond([ServiceBusTrigger("%Azure:ServiceBus:ItemCondQueue%", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  372. {
  373. try
  374. {
  375. var client = _azureCosmos.GetCosmosClient();
  376. var jsonMsg = JsonDocument.Parse(msg);
  377. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ItemCond()\n题库数据变更:{msg}", GroupNames.醍摩豆服務運維群組);
  378. List<ItemCondDto> itemCondDtos = msg.ToObject<List<ItemCondDto>>();
  379. foreach (var itemCondDto in itemCondDtos)
  380. {
  381. if (itemCondDto.scope.Equals("school")) {
  382. ItemCond itemCond = null;
  383. List<ItemInfo> items = new List<ItemInfo>();
  384. var queryslt = $"SELECT c.gradeIds,c.subjectId,c.periodId,c.type,c.level,c.field ,c.scope FROM c where c.periodId='{itemCondDto.filed}' and c.pid= null ";
  385. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{itemCondDto.key}") }))
  386. {
  387. items.Add(item);
  388. }
  389. itemCond = new ItemCond()
  390. {
  391. id = $"{itemCondDto.filed}",
  392. code = $"ItemCond-{itemCondDto.key}",
  393. pk = "ItemCond",
  394. ttl = -1,
  395. count = items.Count,
  396. grades = new List<GradeCount>(),
  397. subjects = new List<SubjectCount>()
  398. };
  399. items.ForEach(z =>
  400. {
  401. ItemService.CountItemCond(z, null, itemCond);
  402. });
  403. await _azureRedis.GetRedisClient(8).HashSetAsync($"ItemCond:{itemCondDto.key}", $"{itemCondDto.filed}", itemCond.ToJsonString());
  404. }
  405. else
  406. {
  407. ItemCond itemCond = null;
  408. List<ItemInfo> items = new List<ItemInfo>();
  409. var queryslt = $"SELECT c.gradeIds,c.subjectId,c.periodId,c.type,c.level,c.field ,c.scope FROM c where c.pid= null ";
  410. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{itemCondDto.filed}") }))
  411. {
  412. items.Add(item);
  413. }
  414. itemCond = new ItemCond() { id = $"{itemCondDto.filed}", code = $"ItemCond", pk = "ItemCond", ttl = -1, count = items.Count };
  415. items.ForEach(z =>
  416. {
  417. ItemService.CountItemCond(z, null, itemCond);
  418. });
  419. await _azureRedis.GetRedisClient(8).HashSetAsync($"ItemCond:ItemCond", $"{itemCondDto.filed}", itemCond.ToJsonString());
  420. }
  421. }
  422. }
  423. catch (CosmosException ex )
  424. {
  425. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ItemCond()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  426. }
  427. catch (Exception ex)
  428. {
  429. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ItemCond()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
  430. }
  431. }
  432. }
  433. }