SchoolRepController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. namespace TEAMModelBI.Controllers.RepairApi
  25. {
  26. [Route("sccholrep")]
  27. [ApiController]
  28. public class SchoolRepController : ControllerBase
  29. {
  30. private readonly AzureCosmosFactory _azureCosmos;
  31. private readonly DingDing _dingDing;
  32. private readonly Option _option;
  33. private readonly AzureStorageFactory _azureStorage;
  34. private readonly AzureRedisFactory _azureRedis;
  35. private readonly IConfiguration _configuration;
  36. private readonly NotificationService _notificationService;
  37. private readonly CoreAPIHttpService _coreAPIHttpService;
  38. private readonly IWebHostEnvironment _environment; //读取文件
  39. private readonly IHttpClientFactory _httpClient;
  40. public SchoolRepController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis, IConfiguration configuration, NotificationService notificationService, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, IWebHostEnvironment hostingEnvironment)
  41. {
  42. _azureCosmos = azureCosmos;
  43. _dingDing = dingDing;
  44. _azureStorage = azureStorage;
  45. _option = option?.Value;
  46. _azureRedis = azureRedis;
  47. _configuration = configuration;
  48. _notificationService = notificationService;
  49. _coreAPIHttpService = coreAPIHttpService;
  50. _httpClient = httpClient;
  51. _environment = hostingEnvironment;
  52. }
  53. /// <summary>
  54. /// 添加新学段的学校
  55. /// </summary>
  56. /// <param name="jsonElement"></param>
  57. /// <returns></returns>
  58. [ProducesDefaultResponseType]
  59. [HttpPost("set-allscperiod")]
  60. public async Task<IActionResult> SetAllScPeriod(JsonElement jsonElement)
  61. {
  62. jsonElement.TryGetProperty("Language", out JsonElement Language);
  63. jsonElement.TryGetProperty("scId", out JsonElement scId);
  64. jsonElement.TryGetProperty("periodName", out JsonElement periodName);
  65. var cosmosClient = _azureCosmos.GetCosmosClient();
  66. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  67. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  68. StringBuilder stringBuilder = new();
  69. string text;
  70. string scName = "默认学段";
  71. while ((text = streamReader.ReadLine()) != null)
  72. {
  73. stringBuilder.Append(text.ToString());
  74. }
  75. streamReader.Close();
  76. string input = stringBuilder.ToString();
  77. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  78. SchoolConfig schoolConfig = null;
  79. if (!string.IsNullOrEmpty($"{Language}"))
  80. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  81. if (schoolConfig == null)
  82. {
  83. if ($"{Language}".Contains("en"))
  84. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  85. else
  86. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  87. }
  88. string campusId = Guid.NewGuid().ToString();
  89. StringBuilder noPeriodScSql = new("SELECT value(c) FROM c ");
  90. List<School> allSc = new();
  91. if (!string.IsNullOrEmpty($"{scId}"))
  92. noPeriodScSql.Append($" where c.id='{scId}'");
  93. else
  94. noPeriodScSql.Append($" join sp in c.period where c.code = 'Base' AND ARRAY_LENGTH(sp.grades) = 0 OR ARRAY_LENGTH(sp.subjects) = 0");
  95. if (!string.IsNullOrEmpty($"{periodName}"))
  96. scName = $"{periodName}";
  97. 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") }))
  98. {
  99. allSc.Add(item);
  100. }
  101. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText:noPeriodScSql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  102. {
  103. allSc.Add(item);
  104. }
  105. foreach (var school in allSc)
  106. {
  107. if (school.name.Contains("幼儿园"))
  108. {
  109. string name = null;
  110. List<Subject> subjects = new();
  111. switch ($"{Language}")
  112. {
  113. case "zh-CN":
  114. name = "学前";
  115. subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = "语言", type = 1 });
  116. break;
  117. case "zh-TW":
  118. name = "學前";
  119. subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = "語言", type = 1 });
  120. break;
  121. case "en-US":
  122. name = "Pre-school";
  123. subjects.Add(new Subject { id = Guid.NewGuid().ToString(), name = "language", type = 1 });
  124. break;
  125. }
  126. if (school.period.Count > 0)
  127. {
  128. var grade = school.period.Find(sg => sg.grades.Count == 0);
  129. if (grade.grades.Count == 0)
  130. grade.grades = new List<string>() { name };
  131. var tempSub = school.period.Find(sg => sg.semesters.Count == 0);
  132. if (tempSub.semesters.Count == 0)
  133. tempSub.subjects = subjects;
  134. }
  135. else
  136. {
  137. if (school.campuses.Count > 0)
  138. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  139. school.period.Add(new Period
  140. {
  141. });
  142. if (!school.campuses.Select(x => x.id).Contains(campusId))
  143. school.campuses.Add(new Campus { name = school.name, id = campusId });
  144. }
  145. }
  146. else
  147. {
  148. if (school.period.Count > 0)
  149. {
  150. var grade = school.period.Find(sg => sg.grades.Count == 0);
  151. if (grade.grades.Count == 0)
  152. grade.grades = schoolConfig.grades;
  153. var subject = school.period.Find(sg => sg.subjects.Count == 0);
  154. if (subject.subjects.Count == 0)
  155. {
  156. subject.subjects = new List<Subject>() {
  157. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  158. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  159. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  160. };
  161. }
  162. }
  163. else
  164. {
  165. if (school.campuses.Count > 0)
  166. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  167. school.period.Add(new Period
  168. {
  169. id = Guid.NewGuid().ToString(),
  170. name = scName,
  171. campusId = campusId,
  172. 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() },
  173. 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() } },
  174. subjects = new List<Subject>() {
  175. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  176. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  177. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  178. },
  179. grades = schoolConfig.grades,
  180. analysis = new Analysis()
  181. {
  182. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  183. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  184. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  185. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  186. income = schoolConfig.PresetExam[0].income,
  187. eugenics = schoolConfig.PresetExam[0].eugenics,
  188. touch = schoolConfig.PresetExam[0].touch
  189. }
  190. });
  191. if (!school.campuses.Select(x => x.id).Contains(campusId))
  192. school.campuses.Add(new Campus { name = school.name, id = campusId });
  193. }
  194. }
  195. School rSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  196. }
  197. return Ok(new { state = RespondCode.Ok });
  198. }
  199. /// <summary>
  200. /// 新增学校学段(多个)
  201. /// </summary>
  202. /// <param name="jsonElement"></param>
  203. /// <returns></returns>
  204. [ProducesDefaultResponseType]
  205. [HttpPost("set-scperiod")]
  206. public async Task<IActionResult> SetScPeriod(JsonElement jsonElement)
  207. {
  208. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  209. if (!jsonElement.TryGetProperty("periodName", out JsonElement periodName)) return BadRequest();
  210. List<string> periodNames = $"{periodName}".ToObject<List<string>>();
  211. jsonElement.TryGetProperty("Language", out JsonElement Language);
  212. var cosmosClient = _azureCosmos.GetCosmosClient();
  213. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  214. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  215. StringBuilder stringBuilder = new();
  216. string text;
  217. while ((text = streamReader.ReadLine()) != null)
  218. {
  219. stringBuilder.Append(text.ToString());
  220. }
  221. streamReader.Close();
  222. string input = stringBuilder.ToString();
  223. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  224. SchoolConfig schoolConfig = null;
  225. if (!string.IsNullOrEmpty($"{Language}"))
  226. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  227. if (schoolConfig == null)
  228. {
  229. if ($"{Language}".Contains("en"))
  230. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  231. else
  232. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  233. }
  234. string campusId = Guid.NewGuid().ToString();
  235. School school = null;
  236. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{scId}", new PartitionKey("Base"));
  237. if (school != null)
  238. {
  239. foreach (var item in periodNames)
  240. {
  241. if (school.campuses.Count > 0)
  242. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  243. school.period.Add(new Period
  244. {
  245. id = Guid.NewGuid().ToString(),
  246. name = item,
  247. campusId = campusId,
  248. 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() },
  249. 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() } },
  250. subjects = new List<Subject>() {
  251. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  252. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  253. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  254. },
  255. grades = schoolConfig.grades,
  256. analysis = new Analysis()
  257. {
  258. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  259. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  260. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  261. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  262. income = schoolConfig.PresetExam[0].income,
  263. eugenics = schoolConfig.PresetExam[0].eugenics,
  264. touch = schoolConfig.PresetExam[0].touch
  265. }
  266. });
  267. }
  268. if (!school.campuses.Select(x => x.id).Contains(campusId))
  269. school.campuses.Add(new Campus { name = school.name, id = campusId });
  270. }
  271. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  272. return Ok(new { state = RespondCode.Ok, school });
  273. }
  274. }
  275. }