FixDataService.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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, DingDing _dingDing, JsonElement data, Option _option)
  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. try {
  404. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryText: $"SELECT value(c) FROM c where c.pk = 'Exam' and c.publish = '0'" ))
  405. {
  406. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  407. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  408. {
  409. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  410. {
  411. if (obj.TryGetProperty("publish", out JsonElement publish))
  412. {
  413. try
  414. {
  415. if (string.IsNullOrEmpty(publish.GetString()) || publish.GetString().Equals("0"))
  416. {
  417. ExamInfo examInfo = new ExamInfo
  418. {
  419. id = obj.GetProperty("id").GetString(),
  420. owner = obj.TryGetProperty("owner", out JsonElement owner) ? owner.GetString() : "",
  421. code = obj.TryGetProperty("code", out JsonElement code) ? code.GetString() : "",
  422. //examInfo.owner = obj.GetProperty("owner").GetString();
  423. name = obj.GetProperty("name").GetString(),
  424. school = obj.GetProperty("school").GetString(),
  425. creatorId = obj.GetProperty("creatorId").GetString(),
  426. stuCount = obj.TryGetProperty("stuCount", out JsonElement stuCount) ? stuCount.GetInt32() : 0,
  427. createTime = obj.GetProperty("createTime").GetInt64(),
  428. updateTime = obj.TryGetProperty("updateTime", out JsonElement updateTime) ? updateTime.GetInt64() : DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  429. startTime = obj.TryGetProperty("startTime", out JsonElement startTime) ? startTime.GetInt64() : DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  430. endTime = obj.TryGetProperty("endTime", out JsonElement endTime) ? endTime.GetInt64() : DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  431. year = obj.TryGetProperty("year", out JsonElement year) ? year.GetInt32() : 0,
  432. source = obj.TryGetProperty("source", out JsonElement source) ? source.GetString() : "0",
  433. classes = obj.TryGetProperty("classes", out JsonElement classes) ? classes.ToObject<List<string>>() : new List<string>(),
  434. stuLists = obj.TryGetProperty("stuLists", out JsonElement stuLists) ? stuLists.ToObject<List<string>>() : new List<string>(),
  435. //examInfo.stuLists = obj.GetProperty("stuLists").ToObject<List<string>>();
  436. papers = obj.TryGetProperty("papers", out JsonElement papers) ? papers.ToObject<List<PaperSimple>>() : new List<PaperSimple>(),
  437. type = obj.TryGetProperty("type", out JsonElement type) ? type.GetString() : "",
  438. period = obj.TryGetProperty("period", out JsonElement period) ? period.ToObject<PeriodSimple>() : new PeriodSimple(),
  439. grades = obj.TryGetProperty("grades", out JsonElement grades) ? grades.ToObject<List<Grade>>() : new List<Grade>(),
  440. subjects = obj.GetProperty("subjects").ToObject<List<ExamSubject>>(),
  441. progress = obj.TryGetProperty("progress", out JsonElement progress) ? progress.GetString() : "finish",
  442. scope = obj.TryGetProperty("scope", out JsonElement scope) ? scope.GetString() : "school",
  443. examType = obj.TryGetProperty("examType", out JsonElement examType) ? examType.ToObject<Custom>() : new Custom(),
  444. status = obj.TryGetProperty("status", out JsonElement status) ? status.GetInt32() : 0,
  445. average = obj.TryGetProperty("average", out JsonElement average) ? average.GetDouble() : 0,
  446. sRate = obj.TryGetProperty("sRate", out JsonElement sRate) ? sRate.GetDouble() : 0,
  447. lostStu = obj.TryGetProperty("lostStu", out JsonElement lostStu) ? lostStu.ToObject<List<string>>() : new List<string>(),
  448. standard = obj.TryGetProperty("standard", out JsonElement standard) ? standard.GetDouble() : 0,
  449. size = obj.TryGetProperty("size", out JsonElement size) ? size.GetInt64() : 0,
  450. income = obj.TryGetProperty("income", out JsonElement income) ? income.GetInt32() : 0,
  451. touch = obj.TryGetProperty("touch", out JsonElement touch) ? touch.GetInt32() : 0,
  452. publish = 0,
  453. groupLists = obj.TryGetProperty("groupLists", out JsonElement groupLists) ? groupLists.ToObject<List<Dictionary<string, List<string>>>>() : new List<Dictionary<string, List<string>>>(),
  454. targets = obj.TryGetProperty("targets", out JsonElement targets) ? targets.ToObject<List<JsonElement>>() : new List<JsonElement>()
  455. };
  456. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(examInfo, examInfo.id, new PartitionKey(examInfo.code)));
  457. infos.Add(examInfo.id);
  458. };
  459. }
  460. catch (Exception ex)
  461. {
  462. await _dingDing.SendBotMsg($"OS,{_option.Location} /fix-data/fix-publish \n {ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  463. }
  464. }
  465. }
  466. }
  467. }
  468. //await Task.WhenAll(tasks);
  469. int pagesize = 50;
  470. if (tasks.Count <= pagesize)
  471. {
  472. await Task.WhenAll(tasks);
  473. }
  474. else
  475. {
  476. int pages = (tasks.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
  477. for (int i = 0; i < pages; i++)
  478. {
  479. var listssb = tasks.Skip((i) * pagesize).Take(pagesize).ToList();
  480. await Task.WhenAll(listssb);
  481. }
  482. }
  483. } catch (Exception e) {
  484. await _dingDing.SendBotMsg($"OS,{_option.Location} /fix-data/fix-publish \n {e.Message}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  485. }
  486. /*foreach (var item in exams) {
  487. dynamic dyn = JsonConvert.DeserializeObject(Convert.ToString(item)) ;
  488. foreach (var obj in dyn)
  489. {
  490. if (obj.Name == "publish")
  491. {
  492. obj.Value = 0;
  493. break;
  494. }
  495. }
  496. ExamInfo info = new ExamInfo();
  497. try {
  498. string dy = JsonConvert.SerializeObject(dyn);
  499. info = JsonConvert.DeserializeObject<ExamInfo>(dy);
  500. } catch (Exception e ) {
  501. //string dy = JsonConvert.SerializeObject(dyn);
  502. }
  503. //await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(dyn, dyn.id, new PartitionKey(dyn.code));
  504. infos.Add(info.id);
  505. }*/
  506. return infos;
  507. }
  508. public static async Task<List<string>> FixExamClassResult(CosmosClient client, JsonElement data, DingDing _dingDing, CoreAPIHttpService _coreAPIHttpService, Option _option)
  509. {
  510. List<ExamClassResult> results = new List<ExamClassResult>();
  511. List<string> ids = new List<string>();
  512. List<Task<ItemResponse<ExamClassResult>>> tasks = new List<Task<ItemResponse<ExamClassResult>>>();
  513. List<Task<ItemResponse<ExamInfo>>> taskInfo = new List<Task<ItemResponse<ExamInfo>>>();
  514. try
  515. {
  516. /*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"))
  517. {
  518. results.Add(item);
  519. //await Task.WhenAll(tasks);
  520. }
  521. HashSet<string> examIds = new HashSet<string>();
  522. foreach (var item in results)
  523. {
  524. ids.Add(item.id);
  525. examIds.Add(item.examId);
  526. List<int> status = new List<int>();
  527. //List<string> classIds = new List<string>();
  528. //classIds.Add(item.info.id);
  529. if (item.status.Count == 0)
  530. {
  531. if (item.studentAnswers.Count > 0)
  532. {
  533. foreach (var answer in item.studentAnswers)
  534. {
  535. if (answer.Count == 0)
  536. {
  537. item.status.Add(1);
  538. }
  539. else
  540. {
  541. item.status.Add(0);
  542. }
  543. }
  544. }
  545. }
  546. else
  547. {
  548. List<int> vs = new();
  549. foreach (int st in item.status)
  550. {
  551. var ans = (st == -2) ? 1 : 0;
  552. vs.Add(ans);
  553. }
  554. item.status = vs;
  555. }
  556. *//*if (item.progress == true) {
  557. int n = 0;
  558. foreach (string sta in item.studentIds)
  559. {
  560. for (int i = 0; i < item.studentScores[n].Count; i++)
  561. {
  562. if (item.studentScores[n][i] == -1)
  563. {
  564. item.studentScores[n][i] = 0;
  565. }
  566. }
  567. n++;
  568. }
  569. }*//*
  570. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamClassResult>(item, item.id, new PartitionKey(item.code)));
  571. }*/
  572. List<ExamInfo> info = new List<ExamInfo>();
  573. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<ExamInfo>(queryText: $"SELECT value(c) FROM c where c.pk = 'Exam' and c.progress = 'finish' and c._ts >= 1640966400"))
  574. {
  575. info.Add(item);
  576. //await Task.WhenAll(tasks);
  577. }
  578. foreach (var item in info) {
  579. item.updateTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  580. taskInfo.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(item, item.id, new PartitionKey(item.code)));
  581. }
  582. //TODO 数据量过大的时候暂未处理
  583. await Task.WhenAll(taskInfo);
  584. }
  585. catch (Exception ex)
  586. {
  587. await _dingDing.SendBotMsg($"OS,{_option.Location} /fix-data/fix-classIds \n {ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  588. }
  589. return ids;
  590. }
  591. public static async Task<List<string>> FixSchoolType(CosmosClient client, JsonElement data, DingDing _dingDing, CoreAPIHttpService _coreAPIHttpService, Option _option)
  592. {
  593. List<Task<ItemResponse<School>>> tasks = new List<Task<ItemResponse<School>>>();
  594. List<School> info = new List<School>() ;
  595. try
  596. {
  597. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<School>(queryText: $"SELECT value(c) FROM c ",requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  598. {
  599. info.Add(item);
  600. }
  601. foreach (var item in info)
  602. {
  603. foreach (var period in item.period) {
  604. }
  605. tasks.Add(client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<School>(item, item.id, new PartitionKey(item.code)));
  606. }
  607. //TODO 数据量过大的时候暂未处理
  608. await Task.WhenAll(tasks);
  609. }
  610. catch (Exception ex)
  611. {
  612. await _dingDing.SendBotMsg($"OS,{_option.Location} /fix-data/fix-type \n {ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  613. }
  614. var ids = info.Select(x => x.id).ToList();
  615. return ids;
  616. }
  617. }
  618. }