FixDataService.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. using Azure;
  2. using Azure.Cosmos;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK;
  13. using HTEXLib.COMM.Helpers;
  14. using TEAMModelOS.SDK.Models.Cosmos.Common;
  15. using System.Dynamic;
  16. using Newtonsoft.Json;
  17. using TEAMModelOS.Models;
  18. namespace TEAMModelOS.SDK.Models.Service
  19. {
  20. public static class FixDataService
  21. {
  22. /// <summary>
  23. /// 修复学生数据
  24. /// </summary>
  25. /// <param name="client"></param>
  26. /// <param name="_dingDing"></param>
  27. /// <param name="_azureStorage"></param>
  28. /// <param name="data"></param>
  29. /// <returns></returns>
  30. public static async Task<List<Student>> FixStudentInfo(CosmosClient client, DingDing _dingDing, AzureStorageFactory _azureStorage, JsonElement data)
  31. {
  32. var code = data.GetProperty("code").GetString();
  33. var ids = data.GetProperty("ids").ToObject<List<string>>();
  34. var dict = data.GetProperty("dict").ToObject<Dictionary<string, object>>();
  35. string queryText = $"SELECT VALUE c FROM c WHERE c.id IN ({string.Join(",", ids.Select(o => $"'{o}'"))})";
  36. List<Student> students = new List<Student>();
  37. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student")
  38. .GetItemQueryIterator<Student>(
  39. queryText: queryText,
  40. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{code}") }))
  41. {
  42. foreach (var key in dict.Keys)
  43. {
  44. switch (key)
  45. {
  46. case "classId":
  47. item.classId = $"{ dict[key]}";
  48. break;
  49. case "periodId":
  50. item.periodId = $"{ dict[key]}";
  51. break;
  52. case "schoolId":
  53. item.schoolId = $"{ dict[key]}";
  54. break;
  55. case "year":
  56. int year = DateTime.Now.Year;
  57. int.TryParse($"dict[key]", out year);
  58. item.year = year;
  59. break;
  60. default:
  61. break;
  62. }
  63. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<Student>(item, item.id, new PartitionKey(item.code));
  64. students.Add(item);
  65. }
  66. }
  67. return students;
  68. }
  69. /// <summary>
  70. /// 修复内容模块数据
  71. /// </summary>
  72. /// <param name="client"></param>
  73. /// <param name="_dingDing"></param>
  74. /// <param name="_azureStorage"></param>
  75. /// <param name="data"></param>
  76. /// <returns></returns>
  77. public static async Task FixBlobContent(CosmosClient client, DingDing _dingDing, AzureStorageFactory _azureStorage, JsonElement data)
  78. {
  79. if (data.TryGetProperty("doPrivate", out JsonElement _doPrivate) && $"{_doPrivate}".Equals("yes", StringComparison.OrdinalIgnoreCase))
  80. {
  81. foreach (var cnt in _azureStorage.GetBlobServiceClient().GetBlobContainers())
  82. {
  83. if (cnt.Name.Length == 10 && int.TryParse(cnt.Name, out _))
  84. {
  85. await doFixBlob(client, _azureStorage, cnt.Name, "private");
  86. }
  87. }
  88. }
  89. List<School> schools = new List<School>();
  90. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: "select value(c) from c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  91. {
  92. schools.Add(item);
  93. }
  94. foreach (var school in schools)
  95. {
  96. await doFixBlob(client, _azureStorage, school.id, "school");
  97. }
  98. }
  99. private static async Task doFixBlob(CosmosClient client, AzureStorageFactory _azureStorage, string name, string scope)
  100. {
  101. List<string> prefixs = new List<string>() { "audio", "doc", "image", "other", "res", "video" };
  102. var ContainerClient = _azureStorage.GetBlobContainerClient($"{name}");
  103. var tb = "Teacher";
  104. if (scope != "private")
  105. {
  106. tb = "School";
  107. }
  108. List<string> ids = new List<string>();
  109. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, tb).GetItemQueryIterator<Bloblog>(queryDefinition: new QueryDefinition("select c.id from c "), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{name}") }))
  110. {
  111. ids.Add(item.id);
  112. }
  113. await client.GetContainer(Constant.TEAMModelOS, tb).DeleteItemsStreamAsync(ids, $"Bloblog-{name}");
  114. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  115. foreach (var prefix in prefixs)
  116. {
  117. if (prefix.Equals("res"))
  118. {
  119. List<string> itemres = await ContainerClient.List(prefix);
  120. if (itemres.IsNotEmpty())
  121. {
  122. HashSet<string> set = new HashSet<string>();
  123. itemres.ForEach(x =>
  124. {
  125. var uri = x.Split("/");
  126. set.Add($"res/{uri[1]}");
  127. });
  128. foreach (var item in set)
  129. {
  130. var urlsSize = await ContainerClient.GetBlobsSize(item);
  131. var url = item;
  132. if (!item.EndsWith(".hte", StringComparison.OrdinalIgnoreCase) && !item.EndsWith(".HTEX", StringComparison.OrdinalIgnoreCase))
  133. {
  134. url += ".HTEX";
  135. }
  136. Bloblog bloblog = new Bloblog { id = Guid.NewGuid().ToString(), code = $"Bloblog-{name}", pk = "Bloblog", time = now, url = url, size = urlsSize != null && urlsSize.HasValue ? urlsSize.Value : 0, type = prefix };
  137. await client.GetContainer(Constant.TEAMModelOS, tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  138. }
  139. }
  140. }
  141. else
  142. {
  143. List<string> items = await ContainerClient.List(prefix);
  144. if (items.IsNotEmpty())
  145. {
  146. foreach (var item in items)
  147. {
  148. var urlsSize = await ContainerClient.GetBlobsSize(item);
  149. Bloblog bloblog = new Bloblog { id = Guid.NewGuid().ToString(), code = $"Bloblog-{name}", pk = "Bloblog", time = now, url = item, size = urlsSize != null && urlsSize.HasValue ? urlsSize.Value : 0, type = prefix };
  150. await client.GetContainer(Constant.TEAMModelOS, tb).UpsertItemAsync(bloblog, new Azure.Cosmos.PartitionKey(bloblog.code));
  151. }
  152. }
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// 修復學校基本資料
  158. /// </summary>
  159. /// <param name="client"></param>
  160. /// <param name="schoolCode"></param>
  161. /// <returns></returns>
  162. public static async Task<List<string>> FixSchoolPeriodId(CosmosClient client, string schoolCode)
  163. {
  164. List<string> periodIdList = new List<string>();
  165. await foreach (School schinfo in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: $"SELECT value(c) FROM c WHERE c.id = '{schoolCode}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  166. {
  167. int periodIndex = 0;
  168. foreach (Period periodNow in schinfo.period)
  169. {
  170. if (periodNow.id.Equals("上學期") || periodNow.id.Equals("上学期") || periodNow.id.Equals("First semester"))
  171. {
  172. string periodId = Guid.NewGuid().ToString();
  173. schinfo.period[periodIndex].id = periodId;
  174. periodIdList.Add(periodId);
  175. }
  176. periodIndex++;
  177. }
  178. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(schinfo, schinfo.id, new PartitionKey("Base"));
  179. }
  180. return periodIdList;
  181. }
  182. /// <summary>
  183. /// 修復學校班級資料
  184. /// </summary>
  185. /// <param name="client"></param>
  186. /// <param name="schoolCode"></param>
  187. /// <param name="dataDic"></param>
  188. /// <returns></returns>
  189. public static async Task<List<string>> FixClassInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  190. {
  191. List<string> classIdList = new List<string>();
  192. await foreach (Class classinfo in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Class>(queryText: $"SELECT value(c) FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{schoolCode}") }))
  193. {
  194. foreach (KeyValuePair<string, string> item in dataDic)
  195. {
  196. switch (item.Key)
  197. {
  198. case "periodId":
  199. classinfo.periodId = item.Value;
  200. break;
  201. }
  202. }
  203. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Class>(classinfo, classinfo.id, new PartitionKey($"Class-{schoolCode}"));
  204. classIdList.Add(classinfo.id);
  205. }
  206. return classIdList;
  207. }
  208. /// <summary>
  209. /// 修復學校課程資料
  210. /// </summary>
  211. /// <param name="client"></param>
  212. /// <param name="schoolCode"></param>
  213. /// <param name="dataDic"></param>
  214. /// <returns></returns>
  215. public static async Task<List<string>> FixCourseInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  216. {
  217. List<string> courseIdList = new List<string>();
  218. await foreach (Course courseinfo in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Course>(queryText: $"SELECT value(c) FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{schoolCode}") }))
  219. {
  220. foreach (KeyValuePair<string, string> item in dataDic)
  221. {
  222. switch (item.Key)
  223. {
  224. case "periodId":
  225. courseinfo.period.id = item.Value;
  226. break;
  227. }
  228. }
  229. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Course>(courseinfo, courseinfo.id, new PartitionKey($"Course-{schoolCode}"));
  230. courseIdList.Add(courseinfo.id);
  231. }
  232. return courseIdList;
  233. }
  234. /// <summary>
  235. /// 修復學校知識點資料
  236. /// </summary>
  237. /// <param name="client"></param>
  238. /// <param name="schoolCode"></param>
  239. /// <param name="dataDic"></param>
  240. /// <returns></returns>
  241. public static async Task<List<string>> FixKnowledgeInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  242. {
  243. List<string> knowledgeIdList = new List<string>();
  244. await foreach (Knowledge knowledgeinfo in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Knowledge>(queryText: $"SELECT value(c) FROM c WHERE c.pk = 'Knowledge' AND c.owner = '{schoolCode}'", requestOptions: new QueryRequestOptions() { }))
  245. {
  246. foreach (KeyValuePair<string, string> item in dataDic)
  247. {
  248. switch (item.Key)
  249. {
  250. case "periodId":
  251. knowledgeinfo.periodId = item.Value;
  252. break;
  253. }
  254. }
  255. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Knowledge>(knowledgeinfo, knowledgeinfo.id, new PartitionKey($"{knowledgeinfo.code}"));
  256. knowledgeIdList.Add(knowledgeinfo.id);
  257. }
  258. return knowledgeIdList;
  259. }
  260. /// <summary>
  261. /// 修復學校試卷資料
  262. /// </summary>
  263. /// <param name="client"></param>
  264. /// <param name="schoolCode"></param>
  265. /// <param name="dataDic"></param>
  266. /// <returns></returns>
  267. public static async Task<List<string>> FixPaperInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  268. {
  269. List<string> paperIdList = new List<string>();
  270. await foreach (Paper paperinfo in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Paper>(queryText: $"SELECT value(c) FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{schoolCode}") }))
  271. {
  272. foreach (KeyValuePair<string, string> item in dataDic)
  273. {
  274. switch (item.Key)
  275. {
  276. case "periodId":
  277. paperinfo.periodId = item.Value;
  278. break;
  279. }
  280. }
  281. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Paper>(paperinfo, paperinfo.id, new PartitionKey($"Paper-{schoolCode}"));
  282. paperIdList.Add(paperinfo.id);
  283. }
  284. return paperIdList;
  285. }
  286. /// <summary>
  287. /// 修復學校課綱資料
  288. /// </summary>
  289. /// <param name="client"></param>
  290. /// <param name="schoolCode"></param>
  291. /// <param name="dataDic"></param>
  292. /// <returns></returns>
  293. public static async Task<List<string>> FixVolumeInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  294. {
  295. List<string> volumeIdList = new List<string>();
  296. await foreach (Volume volumeinfo in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Volume>(queryText: $"SELECT value(c) FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Volume-{schoolCode}") }))
  297. {
  298. foreach (KeyValuePair<string, string> item in dataDic)
  299. {
  300. switch (item.Key)
  301. {
  302. case "periodId":
  303. volumeinfo.periodId = item.Value;
  304. break;
  305. }
  306. }
  307. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<Volume>(volumeinfo, volumeinfo.id, new PartitionKey($"Volume-{schoolCode}"));
  308. volumeIdList.Add(volumeinfo.id);
  309. }
  310. return volumeIdList;
  311. }
  312. /// <summary>
  313. /// 修復學校試題資料
  314. /// </summary>
  315. /// <param name="client"></param>
  316. /// <param name="schoolCode"></param>
  317. /// <param name="dataDic"></param>
  318. /// <returns></returns>
  319. public static async Task<List<string>> FixItemInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  320. {
  321. List<string> itemIdList = new List<string>();
  322. await foreach (ItemInfo iteminfo in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemInfo>(queryText: $"SELECT value(c) FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{schoolCode}") }))
  323. {
  324. foreach (KeyValuePair<string, string> item in dataDic)
  325. {
  326. switch (item.Key)
  327. {
  328. case "periodId":
  329. iteminfo.periodId = item.Value;
  330. break;
  331. }
  332. }
  333. await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<ItemInfo>(iteminfo, iteminfo.id, new PartitionKey($"Item-{schoolCode}"));
  334. itemIdList.Add(iteminfo.id);
  335. }
  336. return itemIdList;
  337. }
  338. /// <summary>
  339. /// 修復學校評測資料
  340. /// </summary>
  341. /// <param name="client"></param>
  342. /// <param name="schoolCode"></param>
  343. /// <param name="dataDic"></param>
  344. /// <returns></returns>
  345. public static async Task<List<string>> FixExamInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  346. {
  347. List<string> examIdList = new List<string>();
  348. await foreach (ExamInfo examinfo in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamInfo>(queryText: $"SELECT value(c) FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{schoolCode}") }))
  349. {
  350. foreach (KeyValuePair<string, string> item in dataDic)
  351. {
  352. switch (item.Key)
  353. {
  354. case "periodId":
  355. examinfo.period.id = item.Value;
  356. break;
  357. }
  358. }
  359. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(examinfo, examinfo.id, new PartitionKey($"Exam-{schoolCode}"));
  360. examIdList.Add(examinfo.id);
  361. }
  362. return examIdList;
  363. }
  364. /// <summary>
  365. /// 修復學生資料
  366. /// </summary>
  367. /// <param name="client"></param>
  368. /// <param name="schoolCode"></param>
  369. /// <param name="dataDic"></param>
  370. /// <returns></returns>
  371. public static async Task<List<string>> FixStudentInfo(CosmosClient client, string schoolCode, Dictionary<string, string> dataDic)
  372. {
  373. List<string> studentIdList = new List<string>();
  374. await foreach (Student studentinfo in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"SELECT value(c) FROM c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{schoolCode}") }))
  375. {
  376. foreach (KeyValuePair<string, string> item in dataDic)
  377. {
  378. switch (item.Key)
  379. {
  380. case "periodId":
  381. studentinfo.periodId = item.Value;
  382. break;
  383. }
  384. }
  385. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<Student>(studentinfo, studentinfo.id, new PartitionKey($"Base-{schoolCode}"));
  386. studentIdList.Add(studentinfo.id);
  387. }
  388. return studentIdList;
  389. }
  390. /// <summary>
  391. /// 修復评测内容
  392. /// </summary>
  393. /// <param name="client"></param>
  394. /// <param name="data"></param>
  395. /// <returns></returns>
  396. public static async Task<List<string>> FixExamPublish(CosmosClient client, JsonElement data)
  397. {
  398. List<string> infos = new List<string>();
  399. var dict = data.GetProperty("publish").GetInt32();
  400. //List<(string id, string code)> ps = new List<(string id, string code)>();
  401. List<object> exams = new List<object>();
  402. List<Task<ItemResponse<ExamInfo>>> tasks = new List<Task<ItemResponse<ExamInfo>>>();
  403. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT value(c) FROM c where c.pk = 'Exam'"))
  404. {
  405. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  406. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  407. {
  408. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  409. {
  410. if (obj.TryGetProperty("publish", out JsonElement publish))
  411. {
  412. try
  413. {
  414. if (!string.IsNullOrEmpty(publish.GetString()) && publish.GetString().Equals("0"))
  415. {
  416. ExamInfo examInfo = new ExamInfo
  417. {
  418. id = obj.GetProperty("id").GetString(),
  419. owner = obj.TryGetProperty("owner", out JsonElement owner) ? owner.GetString() : "",
  420. //examInfo.owner = obj.GetProperty("owner").GetString();
  421. name = obj.GetProperty("name").GetString(),
  422. school = obj.GetProperty("school").GetString(),
  423. creatorId = obj.GetProperty("creatorId").GetString(),
  424. stuCount = obj.TryGetProperty("stuCount", out JsonElement stuCount) ? stuCount.GetInt32() : 0,
  425. createTime = obj.GetProperty("createTime").GetInt64(),
  426. updateTime = obj.TryGetProperty("updateTime", out JsonElement updateTime) ? updateTime.GetInt64() : DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  427. startTime = obj.GetProperty("startTime").GetInt64(),
  428. endTime = obj.GetProperty("endTime").GetInt64(),
  429. year = obj.TryGetProperty("year", out JsonElement year) ? year.GetInt32() : 0,
  430. source = obj.TryGetProperty("source", out JsonElement source) ? source.GetString() : "0",
  431. classes = obj.TryGetProperty("classes", out JsonElement classes) ? classes.ToObject<List<string>>() : new List<string>(),
  432. stuLists = obj.TryGetProperty("stuLists", out JsonElement stuLists) ? stuLists.ToObject<List<string>>() : new List<string>(),
  433. //examInfo.stuLists = obj.GetProperty("stuLists").ToObject<List<string>>();
  434. papers = obj.TryGetProperty("papers", out JsonElement papers) ? papers.ToObject<List<PaperSimple>>() : new List<PaperSimple>(),
  435. type = obj.TryGetProperty("type", out JsonElement type) ? type.GetString() : "",
  436. period = obj.TryGetProperty("period", out JsonElement period) ? period.ToObject<PeriodSimple>() : new PeriodSimple(),
  437. grades = obj.TryGetProperty("grades", out JsonElement grades) ? grades.ToObject<List<Grade>>() : new List<Grade>(),
  438. subjects = obj.GetProperty("subjects").ToObject<List<ExamSubject>>(),
  439. progress = obj.TryGetProperty("progress", out JsonElement progress) ? progress.GetString() : "finish",
  440. scope = obj.TryGetProperty("scope", out JsonElement scope) ? scope.GetString() : "school",
  441. examType = obj.TryGetProperty("examType", out JsonElement examType) ? examType.ToObject<Custom>() : new Custom(),
  442. status = obj.TryGetProperty("status", out JsonElement status) ? status.GetInt32() : 0,
  443. average = obj.TryGetProperty("average", out JsonElement average) ? average.GetDouble() : 0,
  444. sRate = obj.TryGetProperty("sRate", out JsonElement sRate) ? sRate.GetDouble() : 0,
  445. lostStu = obj.TryGetProperty("lostStu", out JsonElement lostStu) ? lostStu.ToObject<List<string>>() : new List<string>(),
  446. standard = obj.TryGetProperty("standard", out JsonElement standard) ? standard.GetDouble() : 0,
  447. size = obj.TryGetProperty("size", out JsonElement size) ? standard.GetInt64() : 0,
  448. income = obj.TryGetProperty("income", out JsonElement income) ? income.GetInt32() : 0,
  449. touch = obj.TryGetProperty("touch", out JsonElement touch) ? touch.GetInt32() : 0,
  450. publish = 0,
  451. groupLists = obj.TryGetProperty("groupLists", out JsonElement groupLists) ? groupLists.ToObject<List<Dictionary<string, List<string>>>>() : new List<Dictionary<string, List<string>>>(),
  452. targets = obj.TryGetProperty("targets", out JsonElement targets) ? targets.ToObject<List<JsonElement>>() : new List<JsonElement>()
  453. };
  454. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(examInfo, examInfo.id, new PartitionKey(examInfo.code)));
  455. infos.Add(examInfo.id);
  456. };
  457. }
  458. catch (Exception e)
  459. {
  460. var aa = obj.GetProperty("id").GetString();
  461. }
  462. }
  463. }
  464. }
  465. await Task.WhenAll(tasks);
  466. /* info.publish = dict;
  467. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(info, info.id, new PartitionKey(info.code));
  468. infos.Add(info.id);*/
  469. }
  470. /*foreach (var item in exams) {
  471. dynamic dyn = JsonConvert.DeserializeObject(Convert.ToString(item)) ;
  472. foreach (var obj in dyn)
  473. {
  474. if (obj.Name == "publish")
  475. {
  476. obj.Value = 0;
  477. break;
  478. }
  479. }
  480. ExamInfo info = new ExamInfo();
  481. try {
  482. string dy = JsonConvert.SerializeObject(dyn);
  483. info = JsonConvert.DeserializeObject<ExamInfo>(dy);
  484. } catch (Exception e ) {
  485. //string dy = JsonConvert.SerializeObject(dyn);
  486. }
  487. //await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(dyn, dyn.id, new PartitionKey(dyn.code));
  488. infos.Add(info.id);
  489. }*/
  490. return infos;
  491. }
  492. public static async Task<List<string>> FixExamClassResult(CosmosClient client, JsonElement data, DingDing _dingDing, CoreAPIHttpService _coreAPIHttpService, Option _option)
  493. {
  494. List<ExamClassResult> results = new List<ExamClassResult>();
  495. List<string> ids = new List<string>();
  496. List<Task<ItemResponse<ExamClassResult>>> tasks = new List<Task<ItemResponse<ExamClassResult>>>();
  497. try
  498. {
  499. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamClassResult>(queryText: $"SELECT value(c) FROM c where c.pk = 'ExamClassResult' and c._ts > 1640966400"))
  500. {
  501. results.Add(item);
  502. //await Task.WhenAll(tasks);
  503. }
  504. foreach (var item in results)
  505. {
  506. ids.Add(item.id);
  507. List<int> status = new List<int>();
  508. List<string> classIds = new List<string>();
  509. classIds.Add(item.info.id);
  510. if (item.status.Count == 0)
  511. {
  512. if (item.studentAnswers.Count > 0)
  513. {
  514. foreach (var answer in item.studentAnswers)
  515. {
  516. if (answer.Count == 0)
  517. {
  518. item.status.Add(-2);
  519. }
  520. else
  521. {
  522. item.status.Add(0);
  523. }
  524. }
  525. }
  526. }
  527. if (!string.IsNullOrEmpty(item.info.id) && item.scIds.Count == 0)
  528. {
  529. string school = string.Empty;
  530. if (item.scope.Equals("school"))
  531. {
  532. school = item.school;
  533. }
  534. (List<RMember> rs, List<RGroupList> groupLists) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, classIds, school, null);
  535. foreach (string id in item.studentIds)
  536. {
  537. if (rs.Count == 0)
  538. {
  539. item.scIds.Add("");
  540. }
  541. else
  542. {
  543. var code = rs.FirstOrDefault(c => c.id == id)?.code;
  544. //var code = rs.Where(x => x.id.Equals(id))?.FirstOrDefault().code;
  545. if (code != null)
  546. {
  547. item.scIds.Add(code.ToString());
  548. }
  549. else
  550. {
  551. item.scIds.Add("");
  552. }
  553. }
  554. }
  555. }
  556. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamClassResult>(item, item.id, new PartitionKey(item.code)));
  557. }
  558. await Task.WhenAll(tasks);
  559. }
  560. catch (Exception ex)
  561. {
  562. await _dingDing.SendBotMsg($"OS,{_option.Location} /fix-data/fix-classIds \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  563. }
  564. return ids;
  565. }
  566. }
  567. }