SchoolRepController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. namespace TEAMModelBI.Controllers.RepairApi
  27. {
  28. [Route("sccholrep")]
  29. [ApiController]
  30. public class SchoolRepController : ControllerBase
  31. {
  32. private readonly AzureCosmosFactory _azureCosmos;
  33. private readonly DingDing _dingDing;
  34. private readonly Option _option;
  35. private readonly AzureStorageFactory _azureStorage;
  36. private readonly AzureRedisFactory _azureRedis;
  37. private readonly IConfiguration _configuration;
  38. private readonly CoreAPIHttpService _coreAPIHttpService;
  39. private readonly IWebHostEnvironment _environment; //读取文件
  40. private readonly IHttpClientFactory _httpClient;
  41. public SchoolRepController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, IHttpClientFactory httpClient, IWebHostEnvironment hostingEnvironment)
  42. {
  43. _azureCosmos = azureCosmos;
  44. _dingDing = dingDing;
  45. _azureStorage = azureStorage;
  46. _option = option?.Value;
  47. _azureRedis = azureRedis;
  48. _configuration = configuration;
  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. id = Guid.NewGuid().ToString(),
  142. name = scName,
  143. grades = new List<string>() { name },
  144. campusId = campusId,
  145. subjects = subjects,
  146. 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() } }
  147. });
  148. if (!school.campuses.Select(x => x.id).Contains(campusId))
  149. school.campuses.Add(new Campus { name = school.name, id = campusId });
  150. }
  151. }
  152. else
  153. {
  154. if (school.period.Count > 0)
  155. {
  156. var grade = school.period.Find(sg => sg.grades.Count == 0);
  157. if (grade.grades.Count == 0)
  158. grade.grades = schoolConfig.grades;
  159. var subject = school.period.Find(sg => sg.subjects.Count == 0);
  160. if (subject.subjects.Count == 0)
  161. {
  162. subject.subjects = new List<Subject>() {
  163. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  164. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  165. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  166. };
  167. }
  168. }
  169. else
  170. {
  171. if (school.campuses.Count > 0)
  172. campusId = school.campuses.Select(x => x.id).FirstOrDefault().ToString();
  173. school.period.Add(new Period
  174. {
  175. id = Guid.NewGuid().ToString(),
  176. name = scName,
  177. campusId = campusId,
  178. 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. subjects = new List<Subject>() {
  183. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  184. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  185. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  186. },
  187. grades = schoolConfig.grades,
  188. analysis = new Analysis()
  189. {
  190. type = new List<ExamSimple>() {
  191. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  192. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  193. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  194. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name }
  195. },
  196. income = schoolConfig.PresetExam[0].income,
  197. eugenics = schoolConfig.PresetExam[0].eugenics,
  198. touch = schoolConfig.PresetExam[0].touch
  199. }
  200. });
  201. if (!school.campuses.Select(x => x.id).Contains(campusId))
  202. school.campuses.Add(new Campus { name = school.name, id = campusId });
  203. }
  204. }
  205. School rSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  206. }
  207. return Ok(new { state = RespondCode.Ok });
  208. }
  209. /// <summary>
  210. /// 新增学校学段(多个)
  211. /// </summary>
  212. /// <param name="jsonElement"></param>
  213. /// <returns></returns>
  214. [ProducesDefaultResponseType]
  215. [HttpPost("set-scperiod")]
  216. public async Task<IActionResult> SetScPeriod(JsonElement jsonElement)
  217. {
  218. if (!jsonElement.TryGetProperty("scId", out JsonElement scId)) return BadRequest();
  219. if (!jsonElement.TryGetProperty("periodName", out JsonElement periodName)) return BadRequest();
  220. List<string> periodNames = $"{periodName}".ToObject<List<string>>();
  221. jsonElement.TryGetProperty("Language", out JsonElement Language);
  222. var cosmosClient = _azureCosmos.GetCosmosClient();
  223. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  224. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  225. StringBuilder stringBuilder = new();
  226. string text;
  227. while ((text = streamReader.ReadLine()) != null)
  228. {
  229. stringBuilder.Append(text.ToString());
  230. }
  231. streamReader.Close();
  232. string input = stringBuilder.ToString();
  233. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  234. SchoolConfig schoolConfig = null;
  235. if (!string.IsNullOrEmpty($"{Language}"))
  236. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  237. if (schoolConfig == null)
  238. {
  239. if ($"{Language}".Contains("en"))
  240. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  241. else
  242. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  243. }
  244. string campusId = Guid.NewGuid().ToString();
  245. School school = null;
  246. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{scId}", new PartitionKey("Base"));
  247. if (school != null)
  248. {
  249. foreach (var item in periodNames)
  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 = item,
  257. campusId = campusId,
  258. 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() },
  259. 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() } },
  260. subjects = new List<Subject>() {
  261. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  262. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  263. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  264. },
  265. grades = schoolConfig.grades,
  266. analysis = new Analysis()
  267. {
  268. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  269. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  270. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  271. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  272. income = schoolConfig.PresetExam[0].income,
  273. eugenics = schoolConfig.PresetExam[0].eugenics,
  274. touch = schoolConfig.PresetExam[0].touch
  275. }
  276. });
  277. }
  278. if (!school.campuses.Select(x => x.id).Contains(campusId))
  279. school.campuses.Add(new Campus { name = school.name, id = campusId });
  280. }
  281. school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(school, school.id, new PartitionKey("Base"));
  282. return Ok(new { state = RespondCode.Ok, school });
  283. }
  284. /// <summary>
  285. /// 学校信息中间件查询和修复接口
  286. /// </summary>
  287. /// <param name="jsonElement"></param>
  288. /// <returns></returns>
  289. [ProducesDefaultResponseType]
  290. [HttpPost("set-allscinfos")]
  291. public async Task<IActionResult> SetAllScInfos(JsonElement jsonElement)
  292. {
  293. try
  294. {
  295. var cosmosClient = _azureCosmos.GetCosmosClient();
  296. List<string> scId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c ", "Base");
  297. List<BIRelation> scInfos = new();
  298. foreach (var itemId in scId)
  299. {
  300. BIRelation bIRelation = null;
  301. var resRel = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("BIRel"));
  302. if (resRel.Status == 200)
  303. {
  304. continue;
  305. //using var fileJson = await JsonDocument.ParseAsync(resRel.ContentStream);
  306. //bIRelation = fileJson.ToObject<BIRelation>();
  307. }
  308. else
  309. {
  310. ScBaseInfo scBaseInfo = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ScBaseInfo>(itemId, new PartitionKey("Base"));
  311. string areaName = null;
  312. if (!string.IsNullOrEmpty(scBaseInfo.areaId))
  313. {
  314. 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") }))
  315. {
  316. areaName = itemName;
  317. }
  318. }
  319. bIRelation = new BIRelation()
  320. {
  321. id = scBaseInfo.id,
  322. name = scBaseInfo.name,
  323. picture = scBaseInfo.picture,
  324. size = scBaseInfo.size,
  325. createDate = scBaseInfo.createTime,
  326. region = scBaseInfo.region,
  327. province = scBaseInfo.province,
  328. city = scBaseInfo.city,
  329. dist = scBaseInfo.dist,
  330. address = scBaseInfo.address,
  331. areaId = scBaseInfo.areaId,
  332. scale = scBaseInfo.scale,
  333. areaName = areaName,
  334. upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  335. };
  336. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("ProductSum"));
  337. if (response.Status == 200)
  338. {
  339. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  340. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  341. {
  342. List<string> serials = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  343. bIRelation.serial = serials;
  344. }
  345. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  346. {
  347. List<string> services = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  348. bIRelation.service = services;
  349. }
  350. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  351. {
  352. List<string> hards = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  353. bIRelation.hard = hards;
  354. }
  355. }
  356. bIRelation = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<BIRelation>(bIRelation, new PartitionKey("BIRel"));
  357. }
  358. scInfos.Add(bIRelation);
  359. }
  360. return Ok(new { state = RespondCode.Ok, cnt = scId.Count, scInfos });
  361. }
  362. catch (Exception ex)
  363. {
  364. await _dingDing.SendBotMsg($"BI,{_option.Location},/sccholrep/set-allscinfos \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  365. return BadRequest();
  366. }
  367. }
  368. }
  369. }