BatchSchoolController.cs 61 KB

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