SchoolRepController.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. using System.Net.Http;
  7. using TEAMModelOS.SDK.DI;
  8. using TEAMModelOS.SDK;
  9. using TEAMModelOS.Models;
  10. using System.Threading.Tasks;
  11. using System.Text.Json;
  12. using System.IO;
  13. using TEAMModelBI.Models;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. using TEAMModelOS.SDK.Extension;
  17. using System;
  18. using TEAMModelOS.SDK.Models;
  19. using Azure.Cosmos;
  20. using System.Linq;
  21. using TEAMModelOS.SDK.Context.Constant;
  22. using Pipelines.Sockets.Unofficial.Arenas;
  23. using TEAMModelBI.Tool;
  24. using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
  25. using TEAMModelOS.SDK.Models.Cosmos.BI;
  26. using static TEAMModelBI.Controllers.Census.SchoolController;
  27. using DocumentFormat.OpenXml.Math;
  28. using TEAMModelOS.SDK.Models.Service.BI;
  29. using TEAMModelBI.Tool.CosmosBank;
  30. using TEAMModelOS.SDK.Models.Service.BIStatsWay;
  31. namespace TEAMModelBI.Controllers.RepairApi
  32. {
  33. [Route("sccholrep")]
  34. [ApiController]
  35. public class SchoolRepController : ControllerBase
  36. {
  37. private readonly AzureCosmosFactory _azureCosmos;
  38. private readonly DingDing _dingDing;
  39. private readonly Option _option;
  40. private readonly AzureStorageFactory _azureStorage;
  41. private readonly AzureRedisFactory _azureRedis;
  42. private readonly IConfiguration _configuration;
  43. private readonly CoreAPIHttpService _coreAPIHttpService;
  44. private readonly IWebHostEnvironment _environment; //读取文件
  45. private readonly IHttpClientFactory _httpClient;
  46. public SchoolRepController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, IWebHostEnvironment hostingEnvironment)
  47. {
  48. _azureCosmos = azureCosmos;
  49. _dingDing = dingDing;
  50. _azureStorage = azureStorage;
  51. _option = option?.Value;
  52. _azureRedis = azureRedis;
  53. _configuration = configuration;
  54. _coreAPIHttpService = coreAPIHttpService;
  55. _httpClient = httpClient;
  56. _environment = hostingEnvironment;
  57. }
  58. /// <summary>
  59. /// 添加新学段的学校
  60. /// </summary>
  61. /// <param name="jsonElement"></param>
  62. /// <returns></returns>
  63. [ProducesDefaultResponseType]
  64. [HttpPost("set-allscperiod")]
  65. public async Task<IActionResult> SetAllScPeriod(JsonElement jsonElement)
  66. {
  67. jsonElement.TryGetProperty("Language", out JsonElement Language);
  68. jsonElement.TryGetProperty("scId", out JsonElement scId);
  69. jsonElement.TryGetProperty("periodName", out JsonElement periodName);
  70. var cosmosClient = _azureCosmos.GetCosmosClient();
  71. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  72. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  73. StringBuilder stringBuilder = new();
  74. string text;
  75. while ((text = streamReader.ReadLine()) != null)
  76. {
  77. stringBuilder.Append(text.ToString());
  78. }
  79. streamReader.Close();
  80. string input = stringBuilder.ToString();
  81. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  82. SchoolConfig schoolConfig = null;
  83. string Lang = string.Empty; //語系判斷用字串
  84. if (!string.IsNullOrEmpty($"{Language}"))
  85. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  86. Lang = $"{Language}";
  87. if (schoolConfig == null)
  88. {
  89. //CN站
  90. if(_option.Location.Contains("china", StringComparison.OrdinalIgnoreCase)) {
  91. if ($"{Language}".Contains("en"))
  92. {
  93. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  94. Lang = "en-US";
  95. }
  96. else
  97. {
  98. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  99. Lang = "zh-CN";
  100. }
  101. }
  102. //GL站
  103. else
  104. {
  105. if ($"{Language}".Contains("tw", StringComparison.OrdinalIgnoreCase))
  106. {
  107. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-TW"));
  108. Lang = "zh-TW";
  109. }
  110. else
  111. {
  112. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  113. Lang = "en-US";
  114. }
  115. }
  116. }
  117. string campusId = Guid.NewGuid().ToString();
  118. //默認學段、通識科目、學前、语言 各語系名稱取得
  119. string scName = string.Empty;
  120. string subjectGeneral = string.Empty;
  121. string subjectLanage = string.Empty;
  122. string gradePreschool = string.Empty;
  123. switch (Lang)
  124. {
  125. case "zh-CN":
  126. scName = "默认学段";
  127. subjectGeneral = "通识";
  128. subjectLanage = "语言";
  129. gradePreschool = "学前";
  130. break;
  131. case "zh-TW":
  132. scName = "預設學段";
  133. subjectGeneral = "通識";
  134. subjectLanage = "語言";
  135. gradePreschool = "學前";
  136. break;
  137. case "en-US":
  138. scName = "Default period";
  139. subjectGeneral = "General Studies";
  140. subjectLanage = "Language";
  141. gradePreschool = "Preschool";
  142. break;
  143. }
  144. StringBuilder noPeriodScSql = new("SELECT DISTINCT value(c) FROM c ");
  145. List<School> allSc = new();
  146. List<string> scidList = new(); //更新的學校ID(回傳值)
  147. if (!string.IsNullOrEmpty($"{scId}"))
  148. noPeriodScSql.Append($" where c.id='{scId}'");
  149. else
  150. noPeriodScSql.Append($" join sp in c.period where c.code = 'Base' AND ( NOT IS_DEFINED(sp.grades) OR ARRAY_LENGTH(sp.grades) = 0 OR NOT IS_DEFINED(sp.subjects) OR ARRAY_LENGTH(sp.subjects) = 0 )");
  151. if (!string.IsNullOrEmpty($"{periodName}"))
  152. scName = $"{periodName}";
  153. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: "select value(c) from c where c.code='Base' and c.period=[]", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  154. {
  155. allSc.Add(item);
  156. }
  157. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: noPeriodScSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  158. {
  159. allSc.Add(item);
  160. }
  161. foreach (var school in allSc)
  162. {
  163. if (school.name.Contains("幼儿园") || school.name.Contains("幼稚園") || school.name.Contains("幼兒園") || school.name.Contains("kindergarten", StringComparison.OrdinalIgnoreCase) || school.name.Contains("preschool", StringComparison.OrdinalIgnoreCase))
  164. {
  165. List<Subject> newSubjects = new List<Subject>() {
  166. new Subject { id = Guid.NewGuid().ToString(), name = subjectLanage, type = 1 }
  167. };
  168. if (school.period.Count > 0)
  169. {
  170. List<Period> grades = school.period.FindAll(sg => sg.grades.Count == 0);
  171. foreach (Period grade in grades)
  172. {
  173. grade.grades = new List<string>() { gradePreschool };
  174. grade.gradeCount = grade.grades.Count;
  175. }
  176. List<Period> semesters = school.period.FindAll(sg => sg.semesters.Count == 0);
  177. foreach (Period semester in semesters)
  178. {
  179. semester.semesters = new List<Semester>() {
  180. new Semester { name = schoolConfig.semester[0].term, start = schoolConfig.semester[0].start, month = schoolConfig.semester[0].month, day = schoolConfig.semester[0].day, id = Guid.NewGuid().ToString() },
  181. new Semester { name = schoolConfig.semester[1].term, start = schoolConfig.semester[1].start, month = schoolConfig.semester[1].month, day = schoolConfig.semester[1].day, id = Guid.NewGuid().ToString() }
  182. };
  183. semester.semesterCount = semester.semesters.Count;
  184. }
  185. List<Period> subjects = school.period.FindAll(sg => sg.subjects.Count == 0);
  186. foreach (Period subject in subjects)
  187. {
  188. subject.subjects = newSubjects;
  189. subject.subjectCount = subject.subjects.Count;
  190. }
  191. }
  192. else
  193. {
  194. if (school.campuses.Count > 0)
  195. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  196. school.period.Add(new Period
  197. {
  198. id = Guid.NewGuid().ToString(),
  199. name = scName,
  200. grades = new List<string>() { gradePreschool },
  201. campusId = campusId,
  202. subjects = newSubjects,
  203. semesters = new List<Semester>() {
  204. new Semester { name = schoolConfig.semester[0].term, start = schoolConfig.semester[0].start, month = schoolConfig.semester[0].month, day = schoolConfig.semester[0].day, id = Guid.NewGuid().ToString() },
  205. new Semester { name = schoolConfig.semester[1].term, start = schoolConfig.semester[1].start, month = schoolConfig.semester[1].month, day = schoolConfig.semester[1].day, id = Guid.NewGuid().ToString() }
  206. },
  207. gradeCount = 1,
  208. semesterCount = 2,
  209. subjectCount = newSubjects.Count,
  210. });
  211. if (!school.campuses.Select(x => x.id).Contains(campusId))
  212. school.campuses.Add(new Campus { name = school.name, id = campusId });
  213. }
  214. }
  215. else
  216. {
  217. if (school.period.Count > 0)
  218. {
  219. List<Period> grades = school.period.FindAll(sg => sg.grades.Count == 0);
  220. foreach (Period grade in grades)
  221. {
  222. grade.grades = schoolConfig.grades;
  223. grade.gradeCount = schoolConfig.grades.Count;
  224. }
  225. List<Period> semesters = school.period.FindAll(sg => sg.semesters.Count == 0);
  226. foreach (Period semester in semesters)
  227. {
  228. semester.semesters = new List<Semester>()
  229. {
  230. new Semester { name = schoolConfig.semester[0].term, start = schoolConfig.semester[0].start, month = schoolConfig.semester[0].month, day = schoolConfig.semester[0].day, id = Guid.NewGuid().ToString() },
  231. new Semester { name = schoolConfig.semester[1].term, start = schoolConfig.semester[1].start, month = schoolConfig.semester[1].month, day = schoolConfig.semester[1].day, id = Guid.NewGuid().ToString() }
  232. };
  233. semester.semesterCount = semester.semesters.Count;
  234. }
  235. List<Period> subjects = school.period.FindAll(sg => sg.subjects.Count == 0);
  236. foreach (Period subject in subjects)
  237. {
  238. subject.subjects = (school.type.Equals(2)) ? new List<Subject>() //高教
  239. {
  240. new Subject { id=Guid.NewGuid().ToString(),name=subjectGeneral,type=1 }
  241. } : new List<Subject>() //普教
  242. {
  243. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  244. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  245. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  246. };
  247. subject.subjectCount = subject.subjects.Count;
  248. }
  249. }
  250. else
  251. {
  252. if (school.campuses.Count > 0)
  253. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  254. school.period.Add(new Period
  255. {
  256. id = Guid.NewGuid().ToString(),
  257. name = scName,
  258. campusId = campusId,
  259. semesters = new List<Semester>() {
  260. new Semester { name = schoolConfig.semester[0].term, start = schoolConfig.semester[0].start, month = schoolConfig.semester[0].month, day = schoolConfig.semester[0].day, id = Guid.NewGuid().ToString() },
  261. new Semester { name = schoolConfig.semester[1].term, start = schoolConfig.semester[1].start, month = schoolConfig.semester[1].month, day = schoolConfig.semester[1].day, id = Guid.NewGuid().ToString() }
  262. },
  263. subjects = (school.type.Equals(2)) ? new List<Subject>() { //高教
  264. new Subject { id=Guid.NewGuid().ToString(),name=subjectGeneral,type=1 },
  265. } : new List<Subject>() { //普教
  266. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  267. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  268. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  269. },
  270. grades = schoolConfig.grades,
  271. analysis = new Analysis()
  272. {
  273. type = new List<ExamSimple>() {
  274. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  275. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  276. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  277. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name }
  278. },
  279. income = schoolConfig.PresetExam[0].income,
  280. eugenics = schoolConfig.PresetExam[0].eugenics,
  281. touch = schoolConfig.PresetExam[0].touch
  282. },
  283. gradeCount = schoolConfig.grades.Count,
  284. semesterCount = schoolConfig.semester.Count,
  285. subjectCount = (school.type.Equals(2)) ? 1 : schoolConfig.PresetSubject.Count
  286. });
  287. if (!school.campuses.Select(x => x.id).Contains(campusId))
  288. school.campuses.Add(new Campus { name = school.name, id = campusId });
  289. }
  290. }
  291. School rSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  292. scidList.Add(rSchool.id);
  293. }
  294. return Ok(new { state = RespondCode.Ok, ids = scidList });
  295. }
  296. /// <summary>
  297. /// 新增学校学段(多个)
  298. /// </summary>
  299. /// <param name="jsonElement"></param>
  300. /// <returns></returns>
  301. [ProducesDefaultResponseType]
  302. [HttpPost("set-scperiod")]
  303. public async Task<IActionResult> SetScPeriod(JsonElement jsonElement)
  304. {
  305. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  306. if (!jsonElement.TryGetProperty("periodName", out JsonElement periodName)) return BadRequest();
  307. List<string> periodNames = $"{periodName}".ToObject<List<string>>();
  308. jsonElement.TryGetProperty("Language", out JsonElement Language);
  309. var cosmosClient = _azureCosmos.GetCosmosClient();
  310. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  311. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  312. StringBuilder stringBuilder = new();
  313. string text;
  314. while ((text = streamReader.ReadLine()) != null)
  315. {
  316. stringBuilder.Append(text.ToString());
  317. }
  318. streamReader.Close();
  319. string input = stringBuilder.ToString();
  320. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  321. SchoolConfig schoolConfig = null;
  322. if (!string.IsNullOrEmpty($"{Language}"))
  323. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  324. if (schoolConfig == null)
  325. {
  326. if ($"{Language}".Contains("en"))
  327. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  328. else
  329. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  330. }
  331. string campusId = Guid.NewGuid().ToString();
  332. School school = null;
  333. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{scId}", new PartitionKey("Base"));
  334. if (school != null)
  335. {
  336. foreach (var item in periodNames)
  337. {
  338. if (school.campuses.Count > 0)
  339. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  340. school.period.Add(new Period
  341. {
  342. id = Guid.NewGuid().ToString(),
  343. name = item,
  344. campusId = campusId,
  345. semesters = new List<Semester>() { new Semester { name = schoolConfig.semester[0].term, start = schoolConfig.semester[0].start, month = schoolConfig.semester[0].month, day = schoolConfig.semester[0].day, id = Guid.NewGuid().ToString() },
  346. new Semester { name = schoolConfig.semester[1].term, start = schoolConfig.semester[1].start, month = schoolConfig.semester[1].month, day = schoolConfig.semester[1].day, id = Guid.NewGuid().ToString() } },
  347. subjects = new List<Subject>() {
  348. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  349. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  350. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  351. },
  352. grades = schoolConfig.grades,
  353. analysis = new Analysis()
  354. {
  355. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  356. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  357. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  358. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  359. income = schoolConfig.PresetExam[0].income,
  360. eugenics = schoolConfig.PresetExam[0].eugenics,
  361. touch = schoolConfig.PresetExam[0].touch
  362. }
  363. });
  364. }
  365. if (!school.campuses.Select(x => x.id).Contains(campusId))
  366. school.campuses.Add(new Campus { name = school.name, id = campusId });
  367. }
  368. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  369. return Ok(new { state = RespondCode.Ok, school });
  370. }
  371. /// <summary>
  372. /// 学校信息中间件查询和修复接口
  373. /// </summary>
  374. /// <param name="jsonElement"></param>
  375. /// <returns></returns>
  376. [ProducesDefaultResponseType]
  377. [HttpPost("set-allscinfos")]
  378. public async Task<IActionResult> SetAllScInfos(JsonElement jsonElement)
  379. {
  380. try
  381. {
  382. jsonElement.TryGetProperty("scId", out JsonElement scId);
  383. var cosmosClient = _azureCosmos.GetCosmosClient();
  384. List<string> scIds = new();
  385. if (!string.IsNullOrEmpty($"{scId}"))
  386. scIds.Add($"{scId}");
  387. else
  388. scIds = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
  389. List<BIRelation> scInfos = new();
  390. foreach (var itemId in scIds)
  391. {
  392. BIRelation bIRelation = null;
  393. var resRel = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("BIRel"));
  394. if (resRel.Status == 200)
  395. {
  396. continue;
  397. //using var fileJson = await JsonDocument.ParseAsync(resRel.ContentStream);
  398. //bIRelation = fileJson.ToObject<BIRelation>();
  399. }
  400. else
  401. {
  402. ScBaseInfo scBaseInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ScBaseInfo>(itemId, new PartitionKey("Base"));
  403. string areaName = null;
  404. if (!string.IsNullOrEmpty(scBaseInfo.areaId))
  405. {
  406. await foreach (var itemName in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<string>(queryText: $"select value(c.name) from c where c.pk='Area' and c.id='{scBaseInfo.areaId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
  407. {
  408. areaName = itemName;
  409. }
  410. }
  411. bIRelation = new BIRelation()
  412. {
  413. id = scBaseInfo.id,
  414. name = scBaseInfo.name,
  415. picture = scBaseInfo.picture,
  416. size = scBaseInfo.size,
  417. createDate = scBaseInfo.createTime,
  418. region = scBaseInfo.region,
  419. province = scBaseInfo.province,
  420. city = scBaseInfo.city,
  421. dist = scBaseInfo.dist,
  422. address = scBaseInfo.address,
  423. areaId = scBaseInfo.areaId,
  424. scale = scBaseInfo.scale,
  425. areaName = areaName,
  426. upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  427. };
  428. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("ProductSum"));
  429. if (response.Status == 200)
  430. {
  431. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  432. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  433. {
  434. List<string> serials = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  435. bIRelation.serial = serials;
  436. }
  437. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  438. {
  439. List<string> services = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  440. bIRelation.service = services;
  441. }
  442. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  443. {
  444. List<string> hards = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  445. bIRelation.hard = hards;
  446. }
  447. }
  448. bIRelation = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<BIRelation>(bIRelation, new PartitionKey("BIRel"));
  449. }
  450. scInfos.Add(bIRelation);
  451. }
  452. return Ok(new { state = RespondCode.Ok, cnt = scIds.Count, scInfos });
  453. }
  454. catch (Exception ex)
  455. {
  456. await _dingDing.SendBotMsg($"BI,{_option.Location},/sccholrep/set-allscinfos \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  457. return BadRequest();
  458. }
  459. }
  460. /// <summary>
  461. /// 修复所有学校统计数据中间件接口
  462. /// </summary>
  463. /// <param name="jsonElement"></param>
  464. /// <returns></returns>
  465. [ProducesDefaultResponseType]
  466. [HttpPost("set-allscstats")]
  467. public async Task<IActionResult> SetAllScStats(JsonElement jsonElement)
  468. {
  469. jsonElement.TryGetProperty("scId", out JsonElement scId);
  470. var cosmosClient = _azureCosmos.GetCosmosClient();
  471. List<string> scIds = new();
  472. if (!string.IsNullOrEmpty($"{scId}"))
  473. scIds.Add($"{scId}");
  474. else
  475. scIds = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
  476. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  477. List<StatsInfo> statsInfos = new();
  478. List<Task<ItemResponse<StatsInfo>>> taskStss = new();
  479. foreach (var sc in scIds)
  480. {
  481. StatsInfo statsInfo = new();
  482. var scDataStats = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{dateTime.Year}-{sc}", new PartitionKey("Statistics"));
  483. if (scDataStats.Status == 200)
  484. {
  485. using var fileJson = await JsonDocument.ParseAsync(scDataStats.ContentStream);
  486. statsInfo = fileJson.ToObject<StatsInfo>();
  487. }
  488. else
  489. {
  490. statsInfo.id = $"{dateTime.Year}-{sc}";
  491. }
  492. statsInfo = await SchoolStatsWay.GetSingleSc(cosmosClient, sc);
  493. if (scDataStats.Status == 200)
  494. taskStss.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<StatsInfo>(statsInfo, $"{dateTime.Year}-{sc}", new PartitionKey("Statistics")));
  495. else
  496. taskStss.Add(cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<StatsInfo>(statsInfo, new PartitionKey("Statistics")));
  497. //statsInfo = await StatsWay.upSingleSc(cosmosClient, sc);
  498. statsInfos.Add(statsInfo);
  499. }
  500. if (taskStss.Count < 256)
  501. await Task.WhenAll(taskStss);
  502. else
  503. {
  504. int pages = (taskStss.Count + 255) / 256;
  505. for (int i = 0; i < pages; i++)
  506. {
  507. List<Task<ItemResponse<StatsInfo>>> temTaskStss = taskStss.Skip(i * 256).Take(256).ToList();
  508. await Task.WhenAll(temTaskStss);
  509. }
  510. }
  511. return Ok(new { state = RespondCode.Ok, statsInfos });
  512. }
  513. }
  514. }