SchoolRepController.cs 31 KB

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