SchoolRepController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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.CoreAPI;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK;
  10. using TEAMModelOS.Models;
  11. using System.Threading.Tasks;
  12. using System.Text.Json;
  13. using System.IO;
  14. using TEAMModelBI.Models;
  15. using System.Collections.Generic;
  16. using System.Text;
  17. using TEAMModelOS.SDK.Extension;
  18. using System;
  19. using TEAMModelOS.SDK.Models;
  20. using Azure.Cosmos;
  21. using System.Linq;
  22. using TEAMModelOS.SDK.Context.Constant;
  23. using Pipelines.Sockets.Unofficial.Arenas;
  24. using TEAMModelBI.Tool;
  25. using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
  26. using TEAMModelOS.SDK.Models.Cosmos.BI;
  27. namespace TEAMModelBI.Controllers.RepairApi
  28. {
  29. [Route("sccholrep")]
  30. [ApiController]
  31. public class SchoolRepController : ControllerBase
  32. {
  33. private readonly AzureCosmosFactory _azureCosmos;
  34. private readonly DingDing _dingDing;
  35. private readonly Option _option;
  36. private readonly AzureStorageFactory _azureStorage;
  37. private readonly AzureRedisFactory _azureRedis;
  38. private readonly IConfiguration _configuration;
  39. private readonly NotificationService _notificationService;
  40. private readonly CoreAPIHttpService _coreAPIHttpService;
  41. private readonly IWebHostEnvironment _environment; //读取文件
  42. private readonly IHttpClientFactory _httpClient;
  43. public SchoolRepController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis, IConfiguration configuration, NotificationService notificationService, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, IWebHostEnvironment hostingEnvironment)
  44. {
  45. _azureCosmos = azureCosmos;
  46. _dingDing = dingDing;
  47. _azureStorage = azureStorage;
  48. _option = option?.Value;
  49. _azureRedis = azureRedis;
  50. _configuration = configuration;
  51. _notificationService = notificationService;
  52. _coreAPIHttpService = coreAPIHttpService;
  53. _httpClient = httpClient;
  54. _environment = hostingEnvironment;
  55. }
  56. /// <summary>
  57. /// 添加新学段的学校
  58. /// </summary>
  59. /// <param name="jsonElement"></param>
  60. /// <returns></returns>
  61. [ProducesDefaultResponseType]
  62. [HttpPost("set-allscperiod")]
  63. public async Task<IActionResult> SetAllScPeriod(JsonElement jsonElement)
  64. {
  65. jsonElement.TryGetProperty("Language", out JsonElement Language);
  66. jsonElement.TryGetProperty("scId", out JsonElement scId);
  67. jsonElement.TryGetProperty("periodName", out JsonElement periodName);
  68. var cosmosClient = _azureCosmos.GetCosmosClient();
  69. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  70. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  71. StringBuilder stringBuilder = new();
  72. string text;
  73. string scName = "默认学段";
  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. if (!string.IsNullOrEmpty($"{Language}"))
  83. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  84. if (schoolConfig == null)
  85. {
  86. if ($"{Language}".Contains("en"))
  87. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  88. else
  89. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  90. }
  91. string campusId = Guid.NewGuid().ToString();
  92. StringBuilder noPeriodScSql = new("SELECT value(c) FROM c ");
  93. List<School> allSc = new();
  94. if (!string.IsNullOrEmpty($"{scId}"))
  95. noPeriodScSql.Append($" where c.id='{scId}'");
  96. else
  97. noPeriodScSql.Append($" join sp in c.period where c.code = 'Base' AND ARRAY_LENGTH(sp.grades) = 0 OR ARRAY_LENGTH(sp.subjects) = 0");
  98. if (!string.IsNullOrEmpty($"{periodName}"))
  99. scName = $"{periodName}";
  100. 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") }))
  101. {
  102. allSc.Add(item);
  103. }
  104. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText:noPeriodScSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  105. {
  106. allSc.Add(item);
  107. }
  108. foreach (var school in allSc)
  109. {
  110. if (school.name.Contains("幼儿园"))
  111. {
  112. string name = null;
  113. List<Subject> subjects = new();
  114. switch ($"{Language}")
  115. {
  116. case "zh-CN":
  117. name = "学前";
  118. subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = "语言", type = 1 });
  119. break;
  120. case "zh-TW":
  121. name = "學前";
  122. subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = "語言", type = 1 });
  123. break;
  124. case "en-US":
  125. name = "Pre-school";
  126. subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = "language", type = 1 });
  127. break;
  128. }
  129. if (school.period.Count > 0)
  130. {
  131. var grade = school.period.Find(sg => sg.grades.Count == 0);
  132. if (grade.grades.Count == 0)
  133. grade.grades = new List<string>() { name };
  134. var tempSub = school.period.Find(sg => sg.semesters.Count == 0);
  135. if (tempSub.semesters.Count == 0)
  136. tempSub.subjects = subjects;
  137. }
  138. else
  139. {
  140. if (school.campuses.Count > 0)
  141. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  142. school.period.Add(new Period
  143. {
  144. });
  145. if (!school.campuses.Select(x => x.id).Contains(campusId))
  146. school.campuses.Add(new Campus { name = school.name, id = campusId });
  147. }
  148. }
  149. else
  150. {
  151. if (school.period.Count > 0)
  152. {
  153. var grade = school.period.Find(sg => sg.grades.Count == 0);
  154. if (grade.grades.Count == 0)
  155. grade.grades = schoolConfig.grades;
  156. var subject = school.period.Find(sg => sg.subjects.Count == 0);
  157. if (subject.subjects.Count == 0)
  158. {
  159. subject.subjects = new List<Subject>() {
  160. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  161. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  162. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  163. };
  164. }
  165. }
  166. else
  167. {
  168. if (school.campuses.Count > 0)
  169. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  170. school.period.Add(new Period
  171. {
  172. id = Guid.NewGuid().ToString(),
  173. name = scName,
  174. campusId = campusId,
  175. 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() },
  176. 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() } },
  177. subjects = new List<Subject>() {
  178. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  179. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  180. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  181. },
  182. grades = schoolConfig.grades,
  183. analysis = new Analysis()
  184. {
  185. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  186. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  187. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  188. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  189. income = schoolConfig.PresetExam[0].income,
  190. eugenics = schoolConfig.PresetExam[0].eugenics,
  191. touch = schoolConfig.PresetExam[0].touch
  192. }
  193. });
  194. if (!school.campuses.Select(x => x.id).Contains(campusId))
  195. school.campuses.Add(new Campus { name = school.name, id = campusId });
  196. }
  197. }
  198. School rSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  199. }
  200. return Ok(new { state = RespondCode.Ok });
  201. }
  202. /// <summary>
  203. /// 新增学校学段(多个)
  204. /// </summary>
  205. /// <param name="jsonElement"></param>
  206. /// <returns></returns>
  207. [ProducesDefaultResponseType]
  208. [HttpPost("set-scperiod")]
  209. public async Task<IActionResult> SetScPeriod(JsonElement jsonElement)
  210. {
  211. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  212. if (!jsonElement.TryGetProperty("periodName", out JsonElement periodName)) return BadRequest();
  213. List<string> periodNames = $"{periodName}".ToObject<List<string>>();
  214. jsonElement.TryGetProperty("Language", out JsonElement Language);
  215. var cosmosClient = _azureCosmos.GetCosmosClient();
  216. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  217. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  218. StringBuilder stringBuilder = new();
  219. string text;
  220. while ((text = streamReader.ReadLine()) != null)
  221. {
  222. stringBuilder.Append(text.ToString());
  223. }
  224. streamReader.Close();
  225. string input = stringBuilder.ToString();
  226. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  227. SchoolConfig schoolConfig = null;
  228. if (!string.IsNullOrEmpty($"{Language}"))
  229. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  230. if (schoolConfig == null)
  231. {
  232. if ($"{Language}".Contains("en"))
  233. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  234. else
  235. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  236. }
  237. string campusId = Guid.NewGuid().ToString();
  238. School school = null;
  239. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{scId}", new PartitionKey("Base"));
  240. if (school != null)
  241. {
  242. foreach (var item in periodNames)
  243. {
  244. if (school.campuses.Count > 0)
  245. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  246. school.period.Add(new Period
  247. {
  248. id = Guid.NewGuid().ToString(),
  249. name = item,
  250. campusId = campusId,
  251. 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() },
  252. 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() } },
  253. subjects = new List<Subject>() {
  254. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  255. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  256. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  257. },
  258. grades = schoolConfig.grades,
  259. analysis = new Analysis()
  260. {
  261. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  262. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  263. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  264. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  265. income = schoolConfig.PresetExam[0].income,
  266. eugenics = schoolConfig.PresetExam[0].eugenics,
  267. touch = schoolConfig.PresetExam[0].touch
  268. }
  269. });
  270. }
  271. if (!school.campuses.Select(x => x.id).Contains(campusId))
  272. school.campuses.Add(new Campus { name = school.name, id = campusId });
  273. }
  274. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  275. return Ok(new { state = RespondCode.Ok, school });
  276. }
  277. /// <summary>
  278. /// 学校信息中间件查询和修复接口
  279. /// </summary>
  280. /// <param name="jsonElement"></param>
  281. /// <returns></returns>
  282. [ProducesDefaultResponseType]
  283. [HttpPost("set-allscinfos")]
  284. public async Task<IActionResult> SetAllScInfos(JsonElement jsonElement)
  285. {
  286. try
  287. {
  288. var cosmosClient = _azureCosmos.GetCosmosClient();
  289. List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
  290. List<BIRelation> scInfos = new();
  291. foreach (var itemId in scId)
  292. {
  293. BIRelation bIRelation = null;
  294. var resRel = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("BIRel"));
  295. if (resRel.Status == 200)
  296. {
  297. continue;
  298. //using var fileJson = await JsonDocument.ParseAsync(resRel.ContentStream);
  299. //bIRelation = fileJson.ToObject<BIRelation>();
  300. }
  301. else
  302. {
  303. ScBaseInfo scBaseInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ScBaseInfo>(itemId, new PartitionKey("Base"));
  304. string areaName = null;
  305. if (!string.IsNullOrEmpty(scBaseInfo.areaId))
  306. {
  307. 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") }))
  308. {
  309. areaName = itemName;
  310. }
  311. }
  312. bIRelation = new BIRelation()
  313. {
  314. id = scBaseInfo.id,
  315. name = scBaseInfo.name,
  316. picture = scBaseInfo.picture,
  317. size = scBaseInfo.size,
  318. createDate = scBaseInfo.createTime,
  319. region = scBaseInfo.region,
  320. province = scBaseInfo.province,
  321. city = scBaseInfo.city,
  322. dist = scBaseInfo.dist,
  323. address = scBaseInfo.address,
  324. areaId = scBaseInfo.areaId,
  325. scale = scBaseInfo.scale,
  326. areaName = areaName,
  327. upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  328. };
  329. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("ProductSum"));
  330. if (response.Status == 200)
  331. {
  332. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  333. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  334. {
  335. List<string> serials = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  336. bIRelation.serial = serials;
  337. }
  338. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  339. {
  340. List<string> services = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  341. bIRelation.service = services;
  342. }
  343. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  344. {
  345. List<string> hards = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  346. bIRelation.hard = hards;
  347. }
  348. }
  349. bIRelation = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<BIRelation>(bIRelation, new PartitionKey("BIRel"));
  350. }
  351. scInfos.Add(bIRelation);
  352. }
  353. return Ok(new { state = RespondCode.Ok, cnt = scId.Count, scInfos });
  354. }
  355. catch (Exception ex)
  356. {
  357. await _dingDing.SendBotMsg($"BI,{_option.Location},/sccholrep/set-allscinfos \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  358. return BadRequest();
  359. }
  360. }
  361. }
  362. }