BatchSchoolController.cs 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. using Azure.Cosmos;
  2. using HTEXLib.COMM.Helpers;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Dynamic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.Models;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models;
  17. using TEAMModelOS.SDK.Models.Cosmos.BI;
  18. using Microsoft.AspNetCore.Hosting; //引用读取文件
  19. using TEAMModelOS.SDK.Models.Service;
  20. using System.IO;
  21. using System.Net.Http;
  22. using Microsoft.Extensions.Configuration;
  23. using System.Net.Http.Json;
  24. using System.Net;
  25. using TEAMModelBI.Filter;
  26. using TEAMModelBI.Tool.Extension;
  27. using TEAMModelBI.Tool;
  28. using TEAMModelBI.Models;
  29. using TEAMModelBI.Tool.Context;
  30. using TEAMModelBI.DI.BIAzureStorage;
  31. namespace TEAMModelBI.Controllers.BISchool
  32. {
  33. [Route("batchschool")]
  34. [ApiController]
  35. public class BatchSchoolController : 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 IWebHostEnvironment _environment; //读取文件
  42. private readonly IHttpClientFactory _http;
  43. //读取配置信息
  44. private readonly IConfiguration _configuration;
  45. public BatchSchoolController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, IHttpClientFactory http)
  46. {
  47. _azureCosmos = azureCosmos;
  48. _dingDing = dingDing;
  49. _azureStorage = azureStorage;
  50. _option = option?.Value;
  51. _environment = hostingEnvironment;
  52. _configuration = configuration;
  53. _http = http;
  54. }
  55. /// <summary>
  56. /// 获取BI权限列表
  57. /// </summary>
  58. /// <returns></returns>
  59. [ProducesDefaultResponseType]
  60. [HttpPost("get-teacher-authoritybilist")]
  61. public async Task<IActionResult> GetAuthorityBIList(JsonElement jsonElement)
  62. {
  63. jsonElement.TryGetProperty("site", out JsonElement site);
  64. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", "authority-bi" } };
  65. var table = _azureStorage.GetCloudTableClient().GetTableReference("SchoolSetting");
  66. if ($"{site}".Equals(BIConst.GlobalSite))
  67. table = _azureStorage.GetCloudTableClient(BIConst.GlobalSite).GetTableReference("SchoolSetting");
  68. List<Authority> authorityBIList = await table.FindListByDict<Authority>(dic);
  69. return Ok(new { authorityBIList });
  70. }
  71. /// <summary>
  72. /// 通过教师的id和学校的code查询权限
  73. /// </summary>
  74. /// <param name="jsonElement"></param>
  75. /// <returns></returns>
  76. [ProducesDefaultResponseType]
  77. [HttpPost("get-teacherroles")]
  78. public async Task<IActionResult> GetTeacherRoles(JsonElement jsonElement)
  79. {
  80. try
  81. {
  82. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return Ok(new { state = 1, message = "参数错误!" });
  83. if (!jsonElement.TryGetProperty("schoolcode", out JsonElement schoolcode)) return Ok(new { state = 1, message = "参数错误!" });
  84. jsonElement.TryGetProperty("site", out JsonElement site);
  85. var client = _azureCosmos.GetCosmosClient();
  86. string sqltxt = $"select * from c where c.id='{id}'";
  87. List<string> roles = new List<string>();//角色列表
  88. List<string> permissions = new List<string>();//权限列表
  89. var cosmosClient = _azureCosmos.GetCosmosClient();
  90. if ($"{site}".Equals(BIConst.GlobalSite))
  91. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  92. var schoolRoles = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{id}", new PartitionKey($"Teacher-{schoolcode}"));
  93. if (schoolRoles.Status == 200)
  94. {
  95. using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  96. if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  97. {
  98. foreach (var obj in _roles.EnumerateArray())
  99. {
  100. roles.Add(obj.GetString());
  101. }
  102. }
  103. if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  104. {
  105. foreach (var obj in _permissions.EnumerateArray())
  106. {
  107. permissions.Add(obj.GetString());
  108. }
  109. }
  110. }
  111. return Ok(new { state = 200, roles = roles, permissions = permissions });
  112. }
  113. catch (Exception ex)
  114. {
  115. await _dingDing.SendBotMsg($"BI,{_option.Location}, /batchschool/get-teacherroles \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  116. return BadRequest();
  117. }
  118. }
  119. /// <summary>
  120. /// 批量创校
  121. /// </summary>
  122. /// <param name="school"></param>
  123. /// <returns></returns>
  124. [ProducesDefaultResponseType]
  125. [AuthToken(Roles = "admin,rdc")]
  126. [HttpPost("batch-school")]
  127. public async Task<IActionResult> BatchCreateSchool(FoundSchools foundSchools)
  128. {
  129. try
  130. {
  131. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  132. List<BISchool> schools = new List<BISchool>();
  133. List<BISchool> userScs = new List<BISchool>();
  134. StringBuilder stringBuilder = new StringBuilder($"{_tmdName}【{_tmdId}】使用批量创校功能:");
  135. var cosmosClient = _azureCosmos.GetCosmosClient();
  136. var tableClient = _azureStorage.GetCloudTableClient();
  137. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  138. if (BIConst.GlobalSite.Equals($"{foundSchools.site}"))
  139. {
  140. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  141. tableClient = _azureStorage.GetCloudTableClient(BIConst.GlobalSite);
  142. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.GlobalSite);
  143. }
  144. if (foundSchools.biSchools.Count > 0)
  145. {
  146. foreach (BISchool bischool in foundSchools.biSchools)
  147. {
  148. List<string> scName = new();
  149. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: $"select value(c.name) from c where c.name='{bischool.name}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  150. {
  151. scName.Add(item);
  152. }
  153. if (scName.Count <= 0)
  154. {
  155. string tempTmdId = null;
  156. HttpClient httpClient = _http.CreateClient();
  157. string url = _configuration.GetValue<string>("HaBookAuth:CoreId:userinfo");
  158. List<string> mobile = new List<string>() { bischool.admin };
  159. HttpResponseMessage responseMessage = await httpClient.PostAsJsonAsync(url, mobile);
  160. if (responseMessage.StatusCode == HttpStatusCode.OK)
  161. {
  162. string temp = responseMessage.Content.ReadAsStringAsync().Result;
  163. List<JsonElement> json_id = temp.ToObject<List<JsonElement>>();
  164. foreach (var item in json_id)
  165. {
  166. tempTmdId = item.GetProperty("id").ToString();
  167. }
  168. }
  169. string tmdId = !string.IsNullOrEmpty(tempTmdId) ? tempTmdId : bischool.admin;
  170. if (!string.IsNullOrEmpty(tmdId))
  171. {
  172. CreateSchoolInfo createSchoolInfo = new CreateSchoolInfo()
  173. {
  174. province = bischool.province,
  175. id = "",
  176. name = bischool.name,
  177. city = bischool.city,
  178. aname = "",
  179. createCount = 0,
  180. };
  181. //生成学校ID
  182. bool tempStaus = true;
  183. do
  184. {
  185. createSchoolInfo = await SchoolCode.GenerateSchoolCode(createSchoolInfo, _dingDing, _environment);
  186. var schoolStatu = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{createSchoolInfo.id}", new PartitionKey($"Base"));
  187. if (schoolStatu.Status != 200) tempStaus = false;
  188. else createSchoolInfo.createCount = createSchoolInfo.createCount >= 3 ? createSchoolInfo.createCount = 3 : createSchoolInfo.createCount += 1;
  189. } while (tempStaus);
  190. if (createSchoolInfo.id != null)
  191. {
  192. string campusId = Guid.NewGuid().ToString();
  193. School upSchool = new School
  194. {
  195. id = createSchoolInfo.id,
  196. name = bischool.name,
  197. size = bischool.size == 0 ? 100 : bischool.size,
  198. code = "Base",
  199. campuses = new List<Campus> { new Campus { name = bischool.name, id = campusId } },
  200. region = bischool.region,
  201. province = bischool.province,
  202. city = bischool.city,
  203. dist = bischool.dist,
  204. address = bischool.address,
  205. picture = "https://teammodelstorage.blob.core.chinacloudapi.cn/0-public/school/bbf54fb3-3fc8-43ae-a358-107281c174cc.png",
  206. timeZone = new TEAMModelOS.SDK.Models.TimeZone { label = "(UTC+08:00) 北京,重庆,香港特别行政区,乌鲁木齐", value = "+08:00" },
  207. type = string.IsNullOrEmpty(bischool.type.ToString()) ? 1 : bischool.type,
  208. pk = "School",
  209. ttl = -1,
  210. areaId = string.IsNullOrEmpty(bischool.areaId) ? "" : bischool.areaId,
  211. standard = string.IsNullOrEmpty(bischool.standard) ? "" : bischool.standard,
  212. schoolCode = createSchoolInfo.id,
  213. period = PresetSchoolPeriod(bischool.period, foundSchools.lang, campusId),
  214. scale = bischool.size >= 300 ? 500 : 0,
  215. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  216. };
  217. stringBuilder.Append($"创建学校:{upSchool.name}【{upSchool.id}】");
  218. //创建学校
  219. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<School>(upSchool, new PartitionKey(upSchool.code));
  220. Teacher teacher = null;
  221. try
  222. {
  223. //查询该教师是否存在
  224. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{tmdId}", new PartitionKey("Base"));
  225. }
  226. catch
  227. {
  228. }
  229. if (teacher != null)
  230. {
  231. //教师存在,在该教师信息中添加要管理的学校信息
  232. teacher.schools.Add(new Teacher.TeacherSchool { areaId = string.IsNullOrEmpty(bischool.areaId) ? "" : bischool.areaId, schoolId = createSchoolInfo.id, name = bischool.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() });
  233. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, tmdId, new PartitionKey("Base"));
  234. SchoolTeacher schoolTeacher = new SchoolTeacher
  235. {
  236. id = tmdId,
  237. code = $"Teacher-{createSchoolInfo.id}",
  238. roles = new List<string> { "admin", "teacher" },
  239. job = "管理员",
  240. name = teacher.name,
  241. picture = teacher.picture,
  242. status = "join",
  243. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  244. pk = "Teacher",
  245. ttl = -1
  246. };
  247. stringBuilder.Append($"教师信息:{schoolTeacher.name}【{schoolTeacher.id}】,教师权限:{string.Join(",", schoolTeacher.roles)}");
  248. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey(teacher.code));
  249. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey(schoolTeacher.code));
  250. }
  251. else
  252. {
  253. //不存在 新建教师和新建要管理的学校信息
  254. Teacher addteacher = new Teacher
  255. {
  256. id = tmdId,
  257. pk = "Base",
  258. code = "Base",
  259. name = $"{bischool.name}-管理员"?.ToString(),
  260. picture = "",
  261. //创建账号并第一次登录IES5则默认赠送1G
  262. size = 1,
  263. defaultSchool = createSchoolInfo.id,
  264. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  265. schools = new List<Teacher.TeacherSchool>() { new Teacher.TeacherSchool { schoolId = createSchoolInfo.id, name = bischool.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() } }
  266. };
  267. stringBuilder.Append($"没有该教师信息创建的教师信息:{addteacher.name}【{addteacher.id}】");
  268. try
  269. {
  270. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(addteacher, new PartitionKey("Base"));
  271. }
  272. catch { }
  273. SchoolTeacher schoolTeacher = new SchoolTeacher
  274. {
  275. id = tmdId,
  276. code = $"Teacher-{createSchoolInfo.id}",
  277. roles = new List<string> { "admin", "teacher" },
  278. job = "管理员",
  279. name = $"{tmdId}-管理员",
  280. picture = "",
  281. status = "join",
  282. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  283. pk = "Teacher",
  284. ttl = -1
  285. };
  286. stringBuilder.Append($"教师权限:{string.Join(",", schoolTeacher.roles)}");
  287. try
  288. {
  289. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey(schoolTeacher.code));
  290. }
  291. catch { }
  292. }
  293. }
  294. }
  295. else userScs.Add(bischool);
  296. }
  297. else schools.Add(bischool);
  298. }
  299. }
  300. else return Ok(new { state = 1, message = "创校信息为空" });
  301. //保存操作记录
  302. //await _azureStorage.SaveBILog("school-batchAdd", stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
  303. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "school-batchAdd", stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
  304. if (schools.Count > 0 || userScs.Count > 0)
  305. return Ok(new { state = 201, message = "已有部分学校批量创建成功;学校已经重复/或者学校信息有误!请检查学校信息!", schools, userScs });
  306. else
  307. return Ok(new { state = 200, message = "批量创校已全部完成" });
  308. }
  309. catch (Exception ex)
  310. {
  311. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/batch-school \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  312. return BadRequest();
  313. }
  314. }
  315. /// <summary>
  316. /// 依据学校编号查询学校信息;若是没有传学校编号,则查询所有学校信息
  317. /// </summary>
  318. /// <param name="jsonElement"></param>
  319. /// <returns></returns>
  320. [ProducesDefaultResponseType]
  321. [HttpPost("get-schoolsinfo")]
  322. public async Task<IActionResult> GetSchoolsInfo(JsonElement jsonElement)
  323. {
  324. try
  325. {
  326. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  327. jsonElement.TryGetProperty("schoolCode", out JsonElement _schoolCode);
  328. jsonElement.TryGetProperty("site", out JsonElement site);
  329. var cosmosClient = _azureCosmos.GetCosmosClient();
  330. if ($"{site}".Equals(BIConst.GlobalSite))
  331. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  332. int? pageSize = null; //默认不指定返回大小
  333. string continuationToken = string.Empty; //返给前端分页token
  334. string pageToken = default;//接受前端的分页Tolen
  335. bool iscontinuation = false;//是否需要进行分页查询,默认不分页
  336. List<AssistSchool> schoolAssists = new(); //返回学校列表集合
  337. List<string> schoolIds = new();
  338. StringBuilder stringBuilder = new("select c.id,c.code,c.schoolCode,c.name,c.region,c.province,c.city,c.dist,c.size,c.address,c.picture,c.type,c.scale,c.areaId,c.standard from c");
  339. if (jsonElement.TryGetProperty("pageSize", out JsonElement jsonPageSize))
  340. {
  341. if (!jsonPageSize.ValueKind.Equals(JsonValueKind.Undefined) && !jsonPageSize.ValueKind.Equals(JsonValueKind.Null) && jsonPageSize.TryGetInt32(out int tempPageSize))
  342. {
  343. pageSize = tempPageSize;
  344. }
  345. }
  346. if (pageSize != null && pageSize.Value > 0)
  347. {
  348. iscontinuation = true;
  349. }
  350. if (jsonElement.TryGetProperty("contToken", out JsonElement ContToken))
  351. {
  352. pageToken = ContToken.GetString();
  353. }
  354. if (!string.IsNullOrEmpty($"{tmdId}"))
  355. {
  356. schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  357. }
  358. if (schoolIds.Count > 0)
  359. {
  360. foreach (var id in schoolIds)
  361. {
  362. string sqlTxt = $"select c.id,c.code,c.schoolCode,c.name,c.region,c.province,c.city,c.dist,c.size,c.address,c.picture,c.type,c.scale,c.areaId,c.standard from c where c.id='{id}'";
  363. await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AssistSchool>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  364. {
  365. schoolAssists.Add(itemSchool);
  366. }
  367. }
  368. }
  369. else
  370. {
  371. if (!string.IsNullOrEmpty($"{_schoolCode}"))
  372. {
  373. stringBuilder.Append($" where c.id='{_schoolCode}'");
  374. }
  375. await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), continuationToken: pageToken, requestOptions: new QueryRequestOptions() { MaxItemCount = pageSize, PartitionKey = new PartitionKey("Base") }))
  376. {
  377. using var json = await JsonDocument.ParseAsync(itemSchool.ContentStream);
  378. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  379. {
  380. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  381. {
  382. schoolAssists.Add(obj.ToObject<AssistSchool>());
  383. }
  384. if (iscontinuation)
  385. {
  386. continuationToken = itemSchool.GetContinuationToken();
  387. break;
  388. }
  389. }
  390. }
  391. }
  392. schoolAssists.ForEach(async school =>
  393. {
  394. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school.id, new PartitionKey("ProductSum"));
  395. if (response.Status == 200)
  396. {
  397. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  398. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  399. {
  400. school.serial = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  401. }
  402. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  403. {
  404. school.service = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  405. }
  406. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  407. {
  408. school.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  409. }
  410. }
  411. school.assists = await CommonFind.FindSchoolRoles(cosmosClient, school.id, "assist");
  412. school.lessonCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c ", "School", $"LessonRecord-{school.id}");
  413. });
  414. return Ok(new { state = 200, continuationToken, schoolAssists, });
  415. }
  416. catch (Exception ex)
  417. {
  418. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/get-schoolsinfo \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  419. return BadRequest();
  420. }
  421. }
  422. /// <summary>
  423. /// 使用yieId 关键字 本地效率未测出
  424. /// </summary>
  425. /// <param name="cosmosClient"></param>
  426. /// <param name="schoolAssists"></param>
  427. /// <returns></returns>
  428. private async IAsyncEnumerable<List<AssistSchool>> GetSchools(CosmosClient cosmosClient,List<AssistSchool> schoolAssists)
  429. {
  430. List<AssistSchool> tempSchoolAssists = new();
  431. foreach (var temp in schoolAssists)
  432. {
  433. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(temp.id, new PartitionKey("ProductSum"));
  434. if (response.Status == 200)
  435. {
  436. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  437. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  438. {
  439. temp.serial = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  440. }
  441. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  442. {
  443. temp.service = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  444. }
  445. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  446. {
  447. temp.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  448. }
  449. }
  450. temp.assists = await CommonFind.FindSchoolRoles(cosmosClient, temp.id, "assist");
  451. tempSchoolAssists.Add(temp);
  452. }
  453. yield return tempSchoolAssists;
  454. }
  455. /// <summary>
  456. /// 取得学校所有顾问列表
  457. /// </summary>
  458. /// <returns></returns>
  459. [ProducesDefaultResponseType]
  460. [HttpPost("get-schoolassist")]
  461. public async Task<IActionResult> GetSchoolAssist(JsonElement jsonElement)
  462. {
  463. try
  464. {
  465. jsonElement.TryGetProperty("site", out JsonElement site);
  466. string schoolId = (jsonElement.TryGetProperty("schoolId", out JsonElement _schoolId)) ? _schoolId.GetString() : string.Empty;
  467. Dictionary<string, List<Dictionary<string, string>>> haveSchoolManger = new Dictionary<string, List<Dictionary<string, string>>>();
  468. string managerWhereOption = (!string.IsNullOrWhiteSpace(schoolId)) ? $" AND c.code = 'Teacher-{schoolId}'" : string.Empty;
  469. //查询学校的顾问
  470. string managerSql = $"SELECT DISTINCT REPLACE(c.code, 'Teacher-', '') AS schoolId, c.id, c.name FROM c WHERE ARRAY_CONTAINS(c.roles, 'assist', true) AND c.pk = 'Teacher' AND c.status = 'join'{managerWhereOption}";
  471. var cosmosClient = _azureCosmos.GetCosmosClient();
  472. if ($"{site}".Equals(BIConst.GlobalSite))
  473. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  474. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: managerSql, requestOptions: new QueryRequestOptions() { }))
  475. {
  476. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  477. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  478. {
  479. string id = obj.GetProperty("id").GetString(); //管理员ID
  480. string name = obj.GetProperty("name").GetString(); //管理员姓名
  481. string tempSchoolId = obj.GetProperty("schoolId").GetString(); //学校ID
  482. Dictionary<string, string> managerDic = new Dictionary<string, string>();
  483. managerDic.Add("id", id);
  484. managerDic.Add("name", name);
  485. if (haveSchoolManger.ContainsKey(tempSchoolId))
  486. {
  487. haveSchoolManger[tempSchoolId].Add(managerDic);
  488. }
  489. else
  490. {
  491. List<Dictionary<string, string>> managerList = new List<Dictionary<string, string>>();
  492. managerList.Add(managerDic);
  493. haveSchoolManger.Add(tempSchoolId, managerList);
  494. }
  495. }
  496. }
  497. //管理模组学校
  498. List<string> baseModuleSchoos = new List<string>();
  499. string baseWhereOption = (!string.IsNullOrEmpty(schoolId)) ? $" and c.id='{schoolId}'" : string.Empty;
  500. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c join serviceProduct in c.service.product where serviceProduct.prodCode = 'IPDYZYLC' {baseWhereOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Product") }))
  501. {
  502. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  503. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  504. {
  505. string tempSchoolId = obj.GetProperty("id").GetString(); //学校ID
  506. baseModuleSchoos.Add(tempSchoolId);
  507. }
  508. }
  509. //学校信息
  510. List<object> schools = new List<object>();
  511. string schoolWhereOption = (!string.IsNullOrEmpty(schoolId)) ? $" where c.id='{schoolId}'" : string.Empty;
  512. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id,c.name,c.period,c.schoolCode,c.region,c.province,c.city,c.picture from c {schoolWhereOption}", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  513. {
  514. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  515. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  516. {
  517. dynamic schoolExpando = new ExpandoObject();
  518. schoolExpando.id = obj.GetProperty("id").GetString();
  519. schoolExpando.name = obj.GetProperty("name").GetString();
  520. //schoolExpando.period = obj.GetProperty("period");
  521. schoolExpando.schoolCode = obj.GetProperty("schoolCode").GetString();
  522. schoolExpando.region = obj.GetProperty("region").GetString();
  523. schoolExpando.province = obj.GetProperty("province").GetString();
  524. schoolExpando.city = obj.GetProperty("city").GetString();
  525. schoolExpando.picture = obj.GetProperty("picture").GetString();
  526. if (haveSchoolManger.ContainsKey(schoolExpando.id))
  527. {
  528. schoolExpando.hasMarger = true;
  529. schoolExpando.managers = haveSchoolManger[schoolExpando.id];
  530. }
  531. else
  532. {
  533. schoolExpando.hasMarger = false;
  534. schoolExpando.managers = new List<object>();
  535. }
  536. schoolExpando.hasBaseModule = (baseModuleSchoos.Contains(schoolExpando.id)) ? true : false;
  537. schools.Add(schoolExpando);
  538. }
  539. }
  540. return Ok(new { state = 200, schools });
  541. }
  542. catch (Exception ex)
  543. {
  544. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/get-schools \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  545. return BadRequest();
  546. }
  547. }
  548. /// <summary>
  549. /// 修改学校信息和顾问信息
  550. /// </summary>
  551. /// <param name="jsonElement"></param>
  552. /// <returns></returns>
  553. [ProducesDefaultResponseType]
  554. [AuthToken(Roles = "admin,rdc")]
  555. [HttpPost("upd-schoolassist")]
  556. public async Task<IActionResult> UpdSchoolAssist(JsonElement jsonElement)
  557. {
  558. try
  559. {
  560. if (!jsonElement.TryGetProperty("schoolId", out JsonElement _schoolId)) return BadRequest();
  561. if (!jsonElement.TryGetProperty("name", out JsonElement schoolName)) return BadRequest();
  562. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  563. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  564. //if (!jsonElement.TryGetProperty("period", out JsonElement period)) return BadRequest();
  565. if (!jsonElement.TryGetProperty("picture", out JsonElement picture)) return BadRequest();
  566. if (!jsonElement.TryGetProperty("size", out JsonElement size)) return BadRequest();
  567. if (!jsonElement.TryGetProperty("scale", out JsonElement scale)) return BadRequest();
  568. if (!jsonElement.TryGetProperty("assistId", out JsonElement _assistId)) return BadRequest();
  569. if (!jsonElement.TryGetProperty("type", out JsonElement _type)) return BadRequest();
  570. jsonElement.TryGetProperty("site", out JsonElement site);
  571. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  572. List<string> assistId = _assistId.ToObject<List<string>>();
  573. //List<string> periodS = period.ToObject<List<string>>();
  574. Dictionary<string, List<Dictionary<string, string>>> haveSchoolManger = new Dictionary<string, List<Dictionary<string, string>>>();
  575. var cosmosClient = _azureCosmos.GetCosmosClient();
  576. var tableClient = _azureStorage.GetCloudTableClient();
  577. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  578. if ($"{site}".Equals(BIConst.GlobalSite))
  579. {
  580. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  581. tableClient = _azureStorage.GetCloudTableClient(BIConst.GlobalSite);
  582. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.GlobalSite);
  583. }
  584. School tempShool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{_schoolId}", new PartitionKey("Base"));
  585. if (tempShool != null)
  586. {
  587. List<Period> periods = new List<Period>();
  588. string campusId = Guid.NewGuid().ToString();
  589. //periodS.ForEach(x =>
  590. //{
  591. // periods.Add(new Period { id = Guid.NewGuid().ToString(), name = x.ToString(), campusId = campusId });
  592. //});
  593. //tempShool.period = periods;
  594. tempShool.size = !string.IsNullOrEmpty($"{size}") ? int.Parse($"{size}") : tempShool.size;
  595. tempShool.scale = !string.IsNullOrEmpty($"{scale}") ? int.Parse($"{scale}") : tempShool.scale;
  596. tempShool.picture = $"{picture}";
  597. tempShool.type = int.Parse($"{_type}");
  598. tempShool.name = $"{schoolName}";
  599. tempShool.areaId = $"{areaId}";
  600. tempShool.standard = $"{standard}";
  601. //修改学校
  602. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(tempShool, tempShool.id, new PartitionKey("Base"));
  603. //修改学校教师关联的信息
  604. string sql = $"SELECT distinct value(c) FROM c join A1 in c.schools where A1.schoolId='{tempShool.id}'";
  605. List<Teacher> teachers = new List<Teacher>();
  606. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  607. {
  608. teachers.Add(item);
  609. }
  610. foreach (var item in teachers)
  611. {
  612. Teacher.TeacherSchool teacherSchool = item.schools.Find(x => x.schoolId.Equals(tempShool.id));
  613. if (teacherSchool != null)
  614. {
  615. teacherSchool.name = tempShool.name;
  616. teacherSchool.picture = tempShool.picture;
  617. teacherSchool.areaId = tempShool.areaId;
  618. }
  619. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(item, item.id, new PartitionKey($"Base"));
  620. }
  621. //修改学校顾问
  622. string sqlTxt = $"SELECT value(c) From c WHERE ARRAY_CONTAINS(c.roles,'assist',true)";
  623. List<SchoolTeacher> schoolTeachers = new List<SchoolTeacher>();
  624. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<SchoolTeacher>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{tempShool.id}") }))
  625. {
  626. if (!assistId.Contains(item.id))
  627. {
  628. if (item.roles.Contains("assist"))
  629. {
  630. item.roles.Remove("assist");
  631. if (item.roles.Count > 0)
  632. {
  633. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolTeacher>(item, item.id, new PartitionKey(item.code));
  634. }
  635. else
  636. {
  637. await cosmosClient.GetContainer("TEAMModelOS", "School").DeleteItemAsync<SchoolTeacher>(item.id, new PartitionKey(item.code));
  638. }
  639. }
  640. }
  641. }
  642. if (assistId.Count > 0)
  643. {
  644. foreach (var itemTeacher in assistId)
  645. {
  646. Teacher tempTeacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{itemTeacher}", new PartitionKey("Base"));
  647. if (tempTeacher != null)
  648. {
  649. var haveTeacher = tempTeacher.schools.Find(x => x.schoolId.Equals($"{_schoolId}"));
  650. if (haveTeacher != null)
  651. {
  652. //查询该教师是否存在该校
  653. SchoolTeacher schoolTeacher = null;
  654. try
  655. {
  656. schoolTeacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<SchoolTeacher>($"{itemTeacher}", new PartitionKey($"Teacher-{tempShool.id}"));
  657. }
  658. catch
  659. {
  660. }
  661. if (schoolTeacher != null)
  662. {
  663. if (!schoolTeacher.roles.Contains("assist"))
  664. {
  665. schoolTeacher.roles.Add("assist");
  666. //添加顾问权限
  667. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{tempShool.id}"));
  668. }
  669. }
  670. else
  671. {
  672. SchoolTeacher addSchoolTeacher = new SchoolTeacher
  673. {
  674. id = itemTeacher,
  675. code = $"Teacher-{tempShool.id}",
  676. pk = "Teacher",
  677. status = "join",
  678. roles = new List<string>() { "assist" },
  679. name = tempTeacher.name,
  680. job = $"{tempShool.name}-顾问",
  681. size = 0,
  682. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  683. };
  684. //添加学校顾问
  685. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(addSchoolTeacher, new PartitionKey($"Teacher-{tempShool.id}"));
  686. }
  687. }
  688. else
  689. {
  690. Teacher.TeacherSchool teacherSchool = new Teacher.TeacherSchool
  691. {
  692. schoolId = tempShool.id,
  693. name = tempShool.name,
  694. status = "join",
  695. time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  696. picture = tempShool.picture,
  697. areaId = tempShool.areaId
  698. };
  699. tempTeacher.schools.Add(teacherSchool);
  700. //给醍摩豆顾问添加学校
  701. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(tempTeacher, tempTeacher.id, new PartitionKey($"Base"));
  702. //不存在则在原来的基础上添加顾问角色
  703. SchoolTeacher addSchoolTeacher = new SchoolTeacher
  704. {
  705. id = itemTeacher,
  706. code = $"Teacher-{tempShool.id}",
  707. pk = "Teacher",
  708. status = "join",
  709. roles = new List<string>() { "assist" },
  710. name = tempTeacher.name,
  711. job = $"{tempShool.name}-顾问",
  712. size = 0,
  713. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  714. };
  715. //添加学校学校顾问
  716. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(addSchoolTeacher, new PartitionKey($"Teacher-{tempShool.id}"));
  717. }
  718. }
  719. }
  720. }
  721. }
  722. //保存操作记录
  723. //await _azureStorage.SaveBILog("school-update", $"{_tmdName}【{_tmdId}】修改学校功能,修改的学校:{_schoolId},{_type},{picture},{size},{string.Join("|", assistId.ToArray())}", _dingDing, httpContext: HttpContext);
  724. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "school-update", $"{_tmdName}【{_tmdId}】修改学校功能,修改的学校:{_schoolId},{_type},{picture},{size},{string.Join("|", assistId.ToArray())}", _dingDing, httpContext: HttpContext);
  725. return Ok(new { state = 200 });
  726. }
  727. catch (Exception ex)
  728. {
  729. await _dingDing.SendBotMsg($"BI, {_option.Location} /batchschool/upd-schoolassist \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  730. return BadRequest();
  731. }
  732. }
  733. /// <summary>
  734. /// 查询区域的学校
  735. /// </summary>
  736. /// <param name="jsonElement"></param>
  737. /// <returns></returns>
  738. [ProducesDefaultResponseType]
  739. [HttpPost("get-schooldist")]
  740. public async Task<IActionResult> GetSchoolDist(JsonElement jsonElement)
  741. {
  742. try
  743. {
  744. if (!jsonElement.TryGetProperty("dist", out JsonElement _dist)) return BadRequest();
  745. jsonElement.TryGetProperty("site", out JsonElement site);
  746. var cosmosClient = _azureCosmos.GetCosmosClient();
  747. if ($"{site}".Equals(BIConst.GlobalSite))
  748. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  749. string sqltxt = $"SELECT * FROM c where c.dist='{_dist}'";
  750. List<School> schools = new List<School>();
  751. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  752. {
  753. schools.Add(item);
  754. }
  755. return Ok(new { state = 200, schools });
  756. }
  757. catch (Exception ex)
  758. {
  759. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/get-schooldist \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  760. return BadRequest();
  761. }
  762. }
  763. /// <summary>
  764. /// 分页学校和顾问信息
  765. /// </summary>
  766. /// <param name="jsonElement"></param>
  767. /// <returns></returns>
  768. [HttpPost("get-schools")]
  769. public async Task<IActionResult> GetSchools(JsonElement jsonElement)
  770. {
  771. //List<School> schools = new List<School>();
  772. List<AssistSchool> schoolAssists = new List<AssistSchool>();
  773. if (!jsonElement.TryGetProperty("endPosition", out JsonElement endPosition)) return BadRequest();
  774. if (!jsonElement.TryGetProperty("pageSize", out JsonElement pageSize)) return BadRequest();
  775. jsonElement.TryGetProperty("site", out JsonElement site);
  776. var cosmosClient = _azureCosmos.GetCosmosClient();
  777. if ($"{site}".Equals(BIConst.GlobalSite))
  778. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
  779. string sqltxt = $"SELECT * FROM c order by c.id offset {endPosition} limit {pageSize}";
  780. await foreach (var itemSchool in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  781. {
  782. AssistSchool schoolAssist = new()
  783. {
  784. id = itemSchool.id,
  785. code = itemSchool.code,
  786. schoolCode = itemSchool.schoolCode,
  787. name = itemSchool.name,
  788. region = itemSchool.region,
  789. province = itemSchool.province,
  790. city = itemSchool.city,
  791. dist = itemSchool.dist,
  792. size = itemSchool.size,
  793. address = itemSchool.address,
  794. picture = itemSchool.picture,
  795. type = itemSchool.type,
  796. scale = itemSchool.scale,
  797. areaId = itemSchool.areaId,
  798. standard = itemSchool.standard
  799. };
  800. schoolAssist.assists = await CommonFind.FindSchoolRoles(cosmosClient, itemSchool.id, "assist");
  801. //List<Assist> assists = new List<Assist>();
  802. ////查询学校的顾问
  803. //string managerSql = $"SELECT DISTINCT REPLACE(c.code, 'Teacher-', '') AS schoolId, c.id, c.name FROM c WHERE ARRAY_CONTAINS(c.roles, 'assist', true) AND c.pk = 'Teacher' AND c.status = 'join' AND c.code = 'Teacher-{itemSchool.id}'";
  804. //var cosmosClent = _azureCosmos.GetCosmosClient();
  805. //await foreach (var item in cosmosClent.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: managerSql, requestOptions: new QueryRequestOptions() { }))
  806. //{
  807. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  808. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  809. // {
  810. // Assist assist = new Assist
  811. // {
  812. // tmdId = obj.GetProperty("id").GetString(),
  813. // tmdName = obj.GetProperty("name").GetString()
  814. // };
  815. // assists.Add(assist);
  816. // }
  817. //}
  818. //schoolAssist.assists = assists;
  819. schoolAssists.Add(schoolAssist);
  820. }
  821. return Ok(new { state = 200, schoolAssists });
  822. }
  823. #region 预设学校基础信息 多语言
  824. /// <summary>
  825. /// 预设学校基础信息 多语言
  826. /// </summary>
  827. /// <param name="period"></param>
  828. /// <param name="Language"></param>
  829. /// <param name="campusId"></param>
  830. /// <returns></returns>
  831. public List<Period> PresetSchoolPeriod(List<string> period, string Language, string campusId)
  832. {
  833. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  834. StreamReader streamReader = new StreamReader(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  835. StringBuilder stringBuilder = new StringBuilder();
  836. string text;
  837. while ((text = streamReader.ReadLine()) != null)
  838. {
  839. stringBuilder.Append(text.ToString());
  840. }
  841. streamReader.Close();
  842. string input = stringBuilder.ToString();
  843. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  844. SchoolConfig schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  845. if (schoolConfig == null)
  846. {
  847. if (Language.Contains("en"))
  848. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  849. else
  850. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  851. }
  852. List<Period> periods = new List<Period>();
  853. period.ForEach(x =>
  854. {
  855. periods.Add(new Period
  856. {
  857. id = Guid.NewGuid().ToString(),
  858. name = x,
  859. campusId = campusId,
  860. 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() },
  861. 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() } },
  862. subjects = new List<Subject>() {
  863. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  864. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  865. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  866. },
  867. grades = schoolConfig.grades,
  868. analysis = new Analysis()
  869. {
  870. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  871. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  872. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  873. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  874. income = schoolConfig.PresetExam[0].income,
  875. eugenics = schoolConfig.PresetExam[0].eugenics,
  876. touch = schoolConfig.PresetExam[0].touch
  877. }
  878. });
  879. });
  880. return periods;
  881. }
  882. public record SchoolConfig
  883. {
  884. public string Lang { get; set; }
  885. public List<semester> semester { get; set; }
  886. public List<string> grades { get; set; }
  887. public List<PresetSubject> PresetSubject { get; set; }
  888. public List<PresetExam> PresetExam { get; set; }
  889. }
  890. public record semester
  891. {
  892. public string id { get; set; } = Guid.NewGuid().ToString();
  893. public string term { get; set; }
  894. public int start { get; set; }
  895. public int month { get; set; }
  896. public int day { get; set; }
  897. }
  898. public record PresetSubject
  899. {
  900. public string id { get; set; } = Guid.NewGuid().ToString();
  901. public string name { get; set; }
  902. public int type { get; set; }
  903. }
  904. public record PresetExam
  905. {
  906. public List<StudyType> type { get; set; }
  907. public int income { get; set; }
  908. public int eugenics { get; set; }
  909. public int touch { get; set; }
  910. }
  911. public record StudyType
  912. {
  913. public string id { get; set; } = Guid.NewGuid().ToString();
  914. public string name { get; set; }
  915. }
  916. #endregion
  917. //public class SchoolAssist
  918. //{
  919. // public string id { get; set; }
  920. // public string code { get; set; }
  921. // public string schoolCode { get; set; }
  922. // public string name { get; set; }
  923. // public string region { get; set; }
  924. // public string province { get; set; }
  925. // public string city { get; set; }
  926. // public string dist { get; set; }
  927. // public int size { get; set; }
  928. // public string address { get; set; }
  929. // public string picture { get; set; }
  930. // public int type { get; set; }
  931. // public int scale { get; set; }
  932. // public string standard { get; set; }
  933. // public string areaId { get; set; }
  934. // public List<SchoolTeacherRoles> assists { get; set; }
  935. // public int serial { get; set; } //软体
  936. // public int service { get; set; } //服务
  937. // public int hard { get; set; } //硬体
  938. // //public List<SchoolProductSumData> serial { get; set; } //软体
  939. // //public List<SchoolProductSumData> service { get; set; } //服务
  940. // //public List<SchoolProductSumDataHard> hard { get; set; } //硬体
  941. //}
  942. /// <summary>
  943. /// 创建多个学校实体
  944. /// </summary>
  945. public record FoundSchools()
  946. {
  947. /// <summary>
  948. /// 语系
  949. /// </summary>
  950. public string lang { get; set; }
  951. /// <summary>
  952. /// 站点
  953. /// </summary>
  954. public string site { get; set; }
  955. /// <summary>
  956. /// 批量创校的数据结构
  957. /// </summary>
  958. public List<BISchool> biSchools { get; set; } = new List<BISchool>();
  959. }
  960. /// <summary>
  961. /// 批量创校的数据结构
  962. /// </summary>
  963. public record BISchool()
  964. {
  965. /// <summary>
  966. /// 学校名称
  967. /// </summary>
  968. public string name { get; set; }
  969. /// <summary>
  970. /// 学校管理员
  971. /// </summary>
  972. public string admin { get; set; }
  973. /// <summary>
  974. /// 学校的学段
  975. /// </summary>
  976. public List<string> period { get; set; }
  977. /// <summary>
  978. /// 学校空间大小
  979. /// </summary>
  980. public int size { get; set; }
  981. /// <summary>
  982. /// 地区
  983. /// </summary>
  984. public string region { get; set; }
  985. /// <summary>
  986. /// 省份
  987. /// </summary>
  988. public string province { get; set; }
  989. /// <summary>
  990. /// 城市
  991. /// </summary>
  992. public string city { get; set; }
  993. /// <summary>
  994. /// 县,区,郡
  995. /// </summary>
  996. public string dist { get; set; }
  997. /// <summary>
  998. /// 学校详细地址
  999. /// </summary>
  1000. public string address { get; set; }
  1001. /// <summary>
  1002. /// 学校类型
  1003. /// </summary>
  1004. public int type { get; set; }
  1005. /// <summary>
  1006. /// 区域ID
  1007. /// </summary>
  1008. public string areaId { get; set; }
  1009. /// <summary>
  1010. /// 区域标准
  1011. /// </summary>
  1012. public string standard { get; set; }
  1013. }
  1014. public record Exist()
  1015. {
  1016. public string ID { get; set; }
  1017. public string name { get; set; }
  1018. }
  1019. }
  1020. }