BatchSchoolController.cs 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  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 TEAMModelOS.SDK;
  30. using TEAMModelOS.SDK.Context.BI;
  31. using TEAMModelOS.SDK.Context.Constant;
  32. using Pipelines.Sockets.Unofficial.Arenas;
  33. using Microsoft.Identity.Client;
  34. using TEAMModelOS.SDK.Models.Dtos;
  35. using DocumentFormat.OpenXml.Spreadsheet;
  36. using DocumentFormat.OpenXml.Wordprocessing;
  37. using DocumentFormat.OpenXml.Bibliography;
  38. using HTEXLib;
  39. using TEAMModelOS.SDK.Models.Service.BI;
  40. using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
  41. using DocumentFormat.OpenXml.Vml.Office;
  42. namespace TEAMModelBI.Controllers.BISchool
  43. {
  44. [Route("batchschool")]
  45. [ApiController]
  46. public class BatchSchoolController : ControllerBase
  47. {
  48. private readonly AzureCosmosFactory _azureCosmos;
  49. private readonly DingDing _dingDing;
  50. private readonly Option _option;
  51. private readonly AzureStorageFactory _azureStorage;
  52. private readonly IWebHostEnvironment _environment; //读取文件
  53. private readonly IHttpClientFactory _http;
  54. //读取配置信息
  55. private readonly IConfiguration _configuration;
  56. private readonly CoreAPIHttpService _coreAPIHttpService;
  57. private readonly HttpTrigger _httpTrigger;
  58. public BatchSchoolController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, IOptionsSnapshot<Option> option, IWebHostEnvironment hostingEnvironment, IConfiguration configuration, IHttpClientFactory http, CoreAPIHttpService coreAPIHttpService, HttpTrigger httpTrigger)
  59. {
  60. _azureCosmos = azureCosmos;
  61. _dingDing = dingDing;
  62. _azureStorage = azureStorage;
  63. _option = option?.Value;
  64. _environment = hostingEnvironment;
  65. _configuration = configuration;
  66. _http = http;
  67. _coreAPIHttpService = coreAPIHttpService;
  68. _httpTrigger = httpTrigger;
  69. }
  70. /// <summary>
  71. /// 获取BI权限列表 //已对接
  72. /// </summary>
  73. /// <returns></returns>
  74. [ProducesDefaultResponseType]
  75. [HttpPost("get-teacher-authoritybilist")]
  76. public async Task<IActionResult> GetAuthorityBIList(JsonElement jsonElement)
  77. {
  78. //jsonElement.TryGetProperty("site", out JsonElement site);
  79. Dictionary<string, object> dic = new() { { "PartitionKey", "authority-bi" } };
  80. var table = _azureStorage.GetCloudTableClient().GetTableReference("SchoolSetting");
  81. ////分开部署,就不需要,一站多用时,取消注释
  82. //if ($"{site}".Equals(BIConst.Global))
  83. // table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("SchoolSetting");
  84. List<Authority> authorityBIList = await table.FindListByDict<Authority>(dic);
  85. return Ok(new { authorityBIList });
  86. }
  87. /// <summary>
  88. /// 生成学校Code信息
  89. /// </summary>
  90. /// <param name="jsonElement"></param>
  91. /// <returns></returns>
  92. [ProducesDefaultResponseType]
  93. [AuthToken(Roles = "admin,rdc")]
  94. [HttpPost("get-schoolcode")]
  95. public async Task<IActionResult> GetSchoolCode(JsonElement jsonElement)
  96. {
  97. if (!jsonElement.TryGetProperty("schools", out JsonElement schools)) return BadRequest();
  98. var cosmosClient = _azureCosmos.GetCosmosClient();
  99. List<CreateSchoolInfo> cSchools = schools.ToObject<List<CreateSchoolInfo>>();
  100. List<CreateSchoolInfo> createScInfo = new(); //生成好的学校Codd信息
  101. foreach (var item in cSchools)
  102. {
  103. CreateSchoolInfo createSchoolInfo = item;
  104. //生成学校ID
  105. bool tempStaus = true;
  106. do
  107. {
  108. createSchoolInfo = await SchoolCode.GenerateSchoolCode(createSchoolInfo, _dingDing, _environment);
  109. var schoolStatu = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{createSchoolInfo.id}", new PartitionKey($"Base"));
  110. if (schoolStatu.Status != 200) tempStaus = false;
  111. else createSchoolInfo.createCount = createSchoolInfo.createCount >= 3 ? createSchoolInfo.createCount = 3 : createSchoolInfo.createCount += 1;
  112. } while (tempStaus);
  113. createScInfo.Add(createSchoolInfo);
  114. }
  115. return Ok(new { state = RespondCode.Ok, createScInfo });
  116. }
  117. /// <summary>
  118. /// 批量创校 //已对接
  119. /// </summary>
  120. /// <param name="school"></param>
  121. /// <returns></returns>
  122. [ProducesDefaultResponseType]
  123. [AuthToken(Roles = "admin,rdc")]
  124. [HttpPost("batch-school")]
  125. public async Task<IActionResult> BatchCreateSchool(FoundSchools foundSchools)
  126. {
  127. try
  128. {
  129. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  130. List<BISchool> schools = new();
  131. List<BISchool> userScs = new();
  132. List<BISchool> cutArea = new();
  133. List<CreateSchoolInfo> scInfos = new();
  134. List<string> upSc = new();
  135. StringBuilder stringBuilder = new($"{_tmdName}【{_tmdId}】使用批量创校功能:");
  136. StringBuilder vsSql = new();
  137. StringBuilder noticeDD = new(); //创校信息通知
  138. var cosmosClient = _azureCosmos.GetCosmosClient();
  139. var tableClient = _azureStorage.GetCloudTableClient();
  140. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  141. //if (BIConst.Global.Equals($"{foundSchools.site}"))
  142. //{
  143. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  144. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  145. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  146. //}
  147. if (foundSchools.biSchools.Count > 0)
  148. {
  149. #region 新版直接创建
  150. foreach (BISchool bischool in foundSchools.biSchools)
  151. {
  152. Area area = null;
  153. //查询区是否存在
  154. var respAreaId = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{bischool.areaId}", new PartitionKey("Base-Area"));
  155. if (respAreaId.Status == 200)
  156. {
  157. using var areaBase = await JsonDocument.ParseAsync(respAreaId.ContentStream);
  158. area = areaBase.ToObject<Area>();
  159. }
  160. CreateSchoolInfo createSchoolInfo = new CreateSchoolInfo()
  161. {
  162. province = bischool.province,
  163. id = bischool.id,
  164. name = bischool.name,
  165. city = bischool.city,
  166. aname = "",
  167. createCount = 0,
  168. };
  169. //生成学校ID
  170. bool tempStaus = true;
  171. do
  172. {
  173. createSchoolInfo = await SchoolCode.GenerateSchoolCode(createSchoolInfo, _dingDing, _environment);
  174. var schoolStatu = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{createSchoolInfo.id}", new PartitionKey($"Base"));
  175. if (schoolStatu.Status != 200) tempStaus = false;
  176. else createSchoolInfo.createCount = createSchoolInfo.createCount >= 3 ? createSchoolInfo.createCount = 3 : createSchoolInfo.createCount += 1;
  177. } while (tempStaus);
  178. if (createSchoolInfo.id != null)
  179. {
  180. string campusId = Guid.NewGuid().ToString();
  181. School upSchool = new()
  182. {
  183. id = createSchoolInfo.id,
  184. name = bischool.name,
  185. size = bischool.size == 0 ? 100 : bischool.size,
  186. code = "Base",
  187. campuses = new List<Campus> { new Campus { name = bischool.name, id = campusId } },
  188. region = bischool.region,
  189. province = bischool.province,
  190. city = bischool.city,
  191. dist = bischool.dist,
  192. address = bischool.address,
  193. picture = "https://teammodelstorage.blob.core.chinacloudapi.cn/0-public/school/bbf54fb3-3fc8-43ae-a358-107281c174cc.png",
  194. timeZone = new TEAMModelOS.SDK.Models.TimeZone { label = "(UTC+08:00) 北京,重庆,香港特别行政区,乌鲁木齐", value = "+08:00" },
  195. type = string.IsNullOrEmpty(bischool.type.ToString()) ? 1 : bischool.type,
  196. pk = "School",
  197. ttl = -1,
  198. areaId = area == null ? "" : area.id,
  199. standard = area == null ? "" : area.standard,
  200. schoolCode = createSchoolInfo.id,
  201. period = PresetSchoolPeriod(bischool.period, foundSchools.lang, campusId),
  202. scale = bischool.size >= 300 ? 500 : 0,
  203. isinit = true,
  204. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  205. };
  206. scInfos.Add(createSchoolInfo);
  207. vsSql.Append($"{upSchool.name}【{upSchool.id}】,");
  208. stringBuilder.Append($"创建学校:{upSchool.name}【{upSchool.id}】");
  209. noticeDD.Append($"{upSchool.name}【{upSchool.id}】 \r 学校管理员信息:");
  210. upSc.Add(upSchool.id);
  211. //创建学校
  212. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<School>(upSchool, new PartitionKey(upSchool.code));
  213. //创建学校信息中间件
  214. //_ = _httpTrigger.RequestHttpTrigger(new { school = $"{upSchool}" }, _option.Location, "set-sc-birelation");
  215. await BIStats.SetSchoolBIRelation(cosmosClient, blobClient, tableClient, _dingDing, upSchool);
  216. //创建BI和统计信息
  217. await BIStats.SetScStatsInfo(cosmosClient, _dingDing,upSchool);
  218. var content = new StringContent(bischool.admin.ToArray().ToJsonString(), Encoding.UTF8, "application/json");
  219. string json = await _coreAPIHttpService.GetUserInfos(content);
  220. List<TmdUserinfo> tmdInfos = json.ToObject<List<TmdUserinfo>>();
  221. foreach (var tmdIndo in tmdInfos)
  222. {
  223. Teacher teacher = new();
  224. var resTeache = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync($"{tmdIndo.id}", new PartitionKey("Base"));
  225. if (resTeache.Status == 200)
  226. {
  227. using var tchJson = await JsonDocument.ParseAsync(resTeache.ContentStream);
  228. teacher = tchJson.ToObject<Teacher>();
  229. Teacher.TeacherSchool tchSc = null;
  230. //var tempTch = teacher.schools.Select(x => x.schoolId.Equals(upSchool.id)).ToString();
  231. tchSc = teacher.schools.Find(x => x.schoolId.Equals(upSchool.id));
  232. if (tchSc == null)
  233. {
  234. //教师存在,在该教师信息中添加要管理的学校信息
  235. 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() });
  236. }
  237. //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, coreUser.id, new PartitionKey("Base"));
  238. SchoolTeacher schoolTeacher = new()
  239. {
  240. id = tmdIndo.id,
  241. code = $"Teacher-{createSchoolInfo.id}",
  242. roles = new List<string> { "admin" },
  243. job = "管理员",
  244. name = teacher.name,
  245. picture = teacher.picture,
  246. status = "join",
  247. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  248. pk = "Teacher",
  249. ttl = -1
  250. };
  251. stringBuilder.Append($"【基础:{schoolTeacher.name}【{schoolTeacher.id}】,权限:{string.Join(",", schoolTeacher.roles)}】");
  252. noticeDD.Append($"{schoolTeacher.name}【{schoolTeacher.id}】");
  253. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey(teacher.code));
  254. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey(schoolTeacher.code));
  255. }
  256. else
  257. {
  258. //不存在 新建教师和新建要管理的学校信息
  259. Teacher addteacher = new()
  260. {
  261. id = tmdIndo.id,
  262. pk = "Base",
  263. code = "Base",
  264. name = $"{tmdIndo.name}",
  265. picture = $"{tmdIndo.picture}",
  266. //创建账号并第一次登录IES5则默认赠送1G
  267. size = 1,
  268. defaultSchool = createSchoolInfo.id,
  269. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  270. schools = new List<Teacher.TeacherSchool>() { new Teacher.TeacherSchool { schoolId = createSchoolInfo.id, name = bischool.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() } }
  271. };
  272. stringBuilder.Append($"【没有该教师基础信息,创建的教师基本信息:{addteacher.name}【{addteacher.id}】");
  273. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(addteacher, new PartitionKey("Base"));
  274. SchoolTeacher schoolTeacher = new()
  275. {
  276. id = tmdIndo.id,
  277. code = $"Teacher-{createSchoolInfo.id}",
  278. roles = new List<string> { "admin" },
  279. job = "管理员",
  280. name = $"{tmdIndo.name}",
  281. picture = "",
  282. status = "join",
  283. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  284. pk = "Teacher",
  285. ttl = -1
  286. };
  287. stringBuilder.Append($"权限:{string.Join(",", schoolTeacher.roles)}】");
  288. noticeDD.Append($"{schoolTeacher.name}【{schoolTeacher.id}】");
  289. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey(schoolTeacher.code));
  290. }
  291. }
  292. }
  293. }
  294. #endregion
  295. #region 旧版先检测 在创建
  296. //foreach (BISchool bischool in foundSchools.biSchools)
  297. //{
  298. // List<string> scName = new();
  299. // //查询学校名称是否全字段匹配
  300. // 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") }))
  301. // {
  302. // scName.Add(item);
  303. // }
  304. // Area area = null;
  305. // //查询区是否存在
  306. // var respAreaId = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{bischool.areaId}", new PartitionKey("Base-Area"));
  307. // if (respAreaId.Status == 200)
  308. // {
  309. // using var areaBase = await JsonDocument.ParseAsync(respAreaId.ContentStream);
  310. // area = areaBase.ToObject<Area>();
  311. // }
  312. // bool isSYNCArea = false;
  313. // //查询去是否同步省平台
  314. // var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{bischool.areaId}", new PartitionKey("AreaSetting"));
  315. // if (responseSet.Status == 200)
  316. // {
  317. // using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  318. // AreaSetting delSet = fileJson.ToObject<AreaSetting>();
  319. // if (!string.IsNullOrEmpty(delSet.accessConfig))
  320. // isSYNCArea = true;
  321. // }
  322. // if (scName.Count <= 0)
  323. // {
  324. // if (isSYNCArea == false)
  325. // {
  326. // var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{bischool.admin}" } }, _option.Location, _configuration);
  327. // if (coreUser != null && coreUser.id != null)
  328. // {
  329. // CreateSchoolInfo createSchoolInfo = new CreateSchoolInfo()
  330. // {
  331. // province = bischool.province,
  332. // id = "",
  333. // name = bischool.name,
  334. // city = bischool.city,
  335. // aname = "",
  336. // createCount = 0,
  337. // };
  338. // //生成学校ID
  339. // bool tempStaus = true;
  340. // do
  341. // {
  342. // createSchoolInfo = await SchoolCode.GenerateSchoolCode(createSchoolInfo, _dingDing, _environment);
  343. // var schoolStatu = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{createSchoolInfo.id}", new PartitionKey($"Base"));
  344. // if (schoolStatu.Status != 200) tempStaus = false;
  345. // else createSchoolInfo.createCount = createSchoolInfo.createCount >= 3 ? createSchoolInfo.createCount = 3 : createSchoolInfo.createCount += 1;
  346. // } while (tempStaus);
  347. // if (createSchoolInfo.id != null)
  348. // {
  349. // string campusId = Guid.NewGuid().ToString();
  350. // School upSchool = new()
  351. // {
  352. // id = createSchoolInfo.id,
  353. // name = bischool.name,
  354. // size = bischool.size == 0 ? 100 : bischool.size,
  355. // code = "Base",
  356. // campuses = new List<Campus> { new Campus { name = bischool.name, id = campusId } },
  357. // region = bischool.region,
  358. // province = bischool.province,
  359. // city = bischool.city,
  360. // dist = bischool.dist,
  361. // address = bischool.address,
  362. // picture = "https://teammodelstorage.blob.core.chinacloudapi.cn/0-public/school/bbf54fb3-3fc8-43ae-a358-107281c174cc.png",
  363. // timeZone = new TEAMModelOS.SDK.Models.TimeZone { label = "(UTC+08:00) 北京,重庆,香港特别行政区,乌鲁木齐", value = "+08:00" },
  364. // type = string.IsNullOrEmpty(bischool.type.ToString()) ? 1 : bischool.type,
  365. // pk = "School",
  366. // ttl = -1,
  367. // areaId = area == null ? "" : area.id,
  368. // standard = area == null ? "" : area.standard,
  369. // schoolCode = createSchoolInfo.id,
  370. // period = PresetSchoolPeriod(bischool.period, foundSchools.lang, campusId),
  371. // scale = bischool.size >= 300 ? 500 : 0,
  372. // createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  373. // };
  374. // stringBuilder.Append($"创建学校:{upSchool.name}【{upSchool.id}】");
  375. // noticeDD.Append($"【ID:{upSchool.id}|名称:{upSchool.name}】 ");
  376. // upSc.Add(upSchool.id);
  377. // //创建学校
  378. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<School>(upSchool, new PartitionKey(upSchool.code));
  379. // //创建学校管理员
  380. // Teacher teacher = null;
  381. // var resTeache = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync($"{coreUser.id}", new PartitionKey("Base"));
  382. // if (resTeache.Status == 200)
  383. // {
  384. // using var tchJson = await JsonDocument.ParseAsync(resTeache.ContentStream);
  385. // teacher = tchJson.ToObject<Teacher>();
  386. // //教师存在,在该教师信息中添加要管理的学校信息
  387. // 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() });
  388. // //await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, coreUser.id, new PartitionKey("Base"));
  389. // SchoolTeacher schoolTeacher = new()
  390. // {
  391. // id = coreUser.id,
  392. // code = $"Teacher-{createSchoolInfo.id}",
  393. // roles = new List<string> { "admin", "teacher" },
  394. // job = "管理员",
  395. // name = teacher.name,
  396. // picture = teacher.picture,
  397. // status = "join",
  398. // createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  399. // pk = "Teacher",
  400. // ttl = -1
  401. // };
  402. // stringBuilder.Append($"教师信息:{schoolTeacher.name}【{schoolTeacher.id}】,教师权限:{string.Join(",", schoolTeacher.roles)}");
  403. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey(teacher.code));
  404. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey(schoolTeacher.code));
  405. // }
  406. // else
  407. // {
  408. // //不存在 新建教师和新建要管理的学校信息
  409. // Teacher addteacher = new()
  410. // {
  411. // id = coreUser.id,
  412. // pk = "Base",
  413. // code = "Base",
  414. // name = $"{coreUser.name}",
  415. // picture = $"{coreUser.picture}",
  416. // //创建账号并第一次登录IES5则默认赠送1G
  417. // size = 1,
  418. // defaultSchool = createSchoolInfo.id,
  419. // createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  420. // schools = new List<Teacher.TeacherSchool>() { new Teacher.TeacherSchool { schoolId = createSchoolInfo.id, name = bischool.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() } }
  421. // };
  422. // stringBuilder.Append($"没有该教师信息创建的教师信息:{addteacher.name}【{addteacher.id}】");
  423. // await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(addteacher, new PartitionKey("Base"));
  424. // SchoolTeacher schoolTeacher = new()
  425. // {
  426. // id = coreUser.id,
  427. // code = $"Teacher-{createSchoolInfo.id}",
  428. // roles = new List<string> { "admin", "teacher" },
  429. // job = "管理员",
  430. // name = $"{coreUser.name}",
  431. // picture = "",
  432. // status = "join",
  433. // createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  434. // pk = "Teacher",
  435. // ttl = -1
  436. // };
  437. // stringBuilder.Append($"教师权限:{string.Join(",", schoolTeacher.roles)}");
  438. // await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey(schoolTeacher.code));
  439. // }
  440. // }
  441. // }
  442. // else userScs.Add(bischool);
  443. // }
  444. // else cutArea.Add(bischool);
  445. // }
  446. // else schools.Add(bischool);
  447. //}
  448. #endregion
  449. }
  450. else return Ok(new { state = 1, message = "创校信息为空" });
  451. if (upSc.Count > 1)
  452. await _dingDing.SendBotMsg($"BI,{_option.Location} \n 批量建校信息:{noticeDD}", GroupNames.成都开发測試群組);
  453. else if (upSc.Count == 1)
  454. await _dingDing.SendBotMsg($"BI,{_option.Location} \n 单个建校信息:{noticeDD}", GroupNames.成都开发測試群組);
  455. ////v2通知
  456. //Teacher targetTeacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>($"{_tmdId}", new PartitionKey($"Base"));
  457. //_coreAPIHttpService.PushNotify(new List<IdNameCode> { new IdNameCode { id = targetTeacher.id, name = targetTeacher.name, code = targetTeacher.lang } }, "create-school", Constant.NotifyType_IES5_Management, new Dictionary<string, object> { { "tmdname", $"{_tmdName}" }, { "schooName", $"{vsSql}" } }, _option.Location, _configuration, _dingDing, _environment.ContentRootPath);
  458. //保存操作记录
  459. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "school-batchAdd", stringBuilder?.ToString(), _dingDing, httpContext: HttpContext);
  460. if (schools.Count == foundSchools.biSchools.Count || userScs.Count == foundSchools.biSchools.Count)
  461. return Ok(new { state = RespondCode.CreateFailed, message = "已有部分学校批量创建成功;学校已经重复/学校信息有误!请检查学校信息!", schools, userScs });
  462. else
  463. {
  464. if (schools.Count > 0 || userScs.Count > 0 || cutArea.Count > 0)
  465. return Ok(new { state = RespondCode.Created, message = "已有部分学校批量创建成功;学校已经重复/学校信息有误/区域已在研修不能加区!请检查学校信息!", schools, userScs, cutArea });
  466. else
  467. return Ok(new { state = RespondCode.Ok, message = "批量创校已全部完成", scInfos });
  468. }
  469. }
  470. catch (Exception ex)
  471. {
  472. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/batch-school \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  473. return BadRequest();
  474. }
  475. }
  476. /// <summary>
  477. /// 依据学校编号查询学校信息;若是没有传学校编号,则查询所有学校信息 //已对接
  478. /// </summary>
  479. /// <param name="jsonElement"></param>
  480. /// <returns></returns>
  481. [ProducesDefaultResponseType]
  482. [HttpPost("get-schoolsinfo")]
  483. public async Task<IActionResult> GetSchoolsInfo(JsonElement jsonElement)
  484. {
  485. try
  486. {
  487. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  488. jsonElement.TryGetProperty("role", out JsonElement role);
  489. jsonElement.TryGetProperty("scId", out JsonElement scId);
  490. jsonElement.TryGetProperty("name", out JsonElement name);
  491. jsonElement.TryGetProperty("order", out JsonElement order);
  492. jsonElement.TryGetProperty("province", out JsonElement province);
  493. jsonElement.TryGetProperty("city", out JsonElement city);
  494. jsonElement.TryGetProperty("dist", out JsonElement dist);
  495. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  496. var cosmosClient = _azureCosmos.GetCosmosClient();
  497. ////分开部署,就不需要,一站多用时,取消注释
  498. //if ($"{site}".Equals(BIConst.Global))
  499. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  500. int scCnt = 0;
  501. int? pageSize = 100; //默认不指定返回大小
  502. string continuationToken = string.Empty; //返给前端分页token
  503. string pageToken = default;//接受前端的分页Tolen
  504. bool iscontinuation = false;//是否需要进行分页查询,默认不分页
  505. List<AssistSchool> schoolAssists = new(); //返回学校列表集合
  506. List<string> schoolIds = new();
  507. 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 ");
  508. StringBuilder scCntSql = new($"select value(count(c.id)) from c");
  509. if (jsonElement.TryGetProperty("pageSize", out JsonElement jsonPageSize))
  510. {
  511. if (!jsonPageSize.ValueKind.Equals(JsonValueKind.Undefined) && !jsonPageSize.ValueKind.Equals(JsonValueKind.Null) && jsonPageSize.TryGetInt32(out int tempPageSize))
  512. pageSize = tempPageSize;
  513. }
  514. if (pageSize != null && pageSize.Value > 0)
  515. iscontinuation = true;
  516. if (jsonElement.TryGetProperty("contToken", out JsonElement ContToken))
  517. pageToken = ContToken.GetString();
  518. if (!string.IsNullOrEmpty($"{scId}") && string.IsNullOrEmpty($"{name}"))
  519. {
  520. stringBuilder.Append($" where c.id='{scId}'");
  521. scCntSql.Append($" where c.id='{scId}'");
  522. }
  523. if (string.IsNullOrEmpty($"{scId}") && !string.IsNullOrEmpty($"{name}"))
  524. {
  525. stringBuilder.Append($" where Contains(c.name,'{name}')");
  526. scCntSql.Append($" where Contains(c.name,'{name}')");
  527. }
  528. if (!string.IsNullOrEmpty($"{province}") && string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
  529. {
  530. stringBuilder.Append($" where Contains(c.province,'{province}')");
  531. scCntSql.Append($" where Contains(c.province,'{province}')");
  532. }
  533. if (!string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
  534. {
  535. stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
  536. scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
  537. }
  538. if (!string.IsNullOrEmpty($"{dist}") && !string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{dist}"))
  539. {
  540. stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
  541. scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
  542. }
  543. if ($"{order}".Equals("desc"))
  544. stringBuilder.Append(" order by c.createTime desc");
  545. if (!string.IsNullOrEmpty($"{tmdId}"))
  546. {
  547. schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}", roles: !string.IsNullOrEmpty($"{role}") ? $"{role}" : "assist", isMany: true);
  548. string scsSql = BICommonWay.ManyScSql("c.id", schoolIds);
  549. scCntSql.Append($" where {scsSql}");
  550. scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
  551. foreach (var id in schoolIds)
  552. {
  553. StringBuilder sqlTxt = 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 where c.id='{id}'");
  554. if ($"{order}".Equals("desc"))
  555. sqlTxt.Append(" order by c.createTime desc");
  556. else
  557. sqlTxt.Append(" order by c.createTime asc");
  558. await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AssistSchool>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  559. {
  560. schoolAssists.Add(itemSchool);
  561. }
  562. }
  563. }
  564. else
  565. {
  566. scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
  567. await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), continuationToken: pageToken, requestOptions: new QueryRequestOptions() { MaxItemCount = pageSize, PartitionKey = new PartitionKey("Base") }))
  568. {
  569. using var json = await JsonDocument.ParseAsync(itemSchool.ContentStream);
  570. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  571. {
  572. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  573. {
  574. schoolAssists.Add(obj.ToObject<AssistSchool>());
  575. }
  576. if (iscontinuation)
  577. {
  578. continuationToken = itemSchool.GetContinuationToken();
  579. break;
  580. }
  581. }
  582. }
  583. }
  584. //if (schoolIds.Count > 0)
  585. //{
  586. // scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
  587. // foreach (var id in schoolIds)
  588. // {
  589. // StringBuilder sqlTxt = 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 where c.id='{id}'");
  590. // if ($"{order}".Equals("desc"))
  591. // sqlTxt.Append(" order by c.createTime desc");
  592. // await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AssistSchool>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  593. // {
  594. // schoolAssists.Add(itemSchool);
  595. // }
  596. // }
  597. //}
  598. //else
  599. //{
  600. // if (!string.IsNullOrEmpty($"{scId}") && string.IsNullOrEmpty($"{name}"))
  601. // {
  602. // stringBuilder.Append($" where c.id='{scId}'");
  603. // scCntSql.Append($" where c.id='{scId}'");
  604. // }
  605. // if (string.IsNullOrEmpty($"{scId}") && !string.IsNullOrEmpty($"{name}"))
  606. // {
  607. // stringBuilder.Append($" where Contains(c.name,'{name}')");
  608. // scCntSql.Append($" where Contains(c.name,'{name}')");
  609. // }
  610. // if (!string.IsNullOrEmpty($"{province}") && string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
  611. // {
  612. // stringBuilder.Append($" where Contains(c.province,'{province}')");
  613. // scCntSql.Append($" where Contains(c.province,'{province}')");
  614. // }
  615. // if (!string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
  616. // {
  617. // stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
  618. // scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
  619. // }
  620. // if (!string.IsNullOrEmpty($"{dist}") && !string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{dist}"))
  621. // {
  622. // stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
  623. // scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
  624. // }
  625. // if ($"{order}".Equals("desc"))
  626. // stringBuilder.Append(" order by c.createTime desc");
  627. // scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
  628. // await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), continuationToken: pageToken, requestOptions: new QueryRequestOptions() { MaxItemCount = pageSize, PartitionKey = new PartitionKey("Base") }))
  629. // {
  630. // using var json = await JsonDocument.ParseAsync(itemSchool.ContentStream);
  631. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  632. // {
  633. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  634. // {
  635. // schoolAssists.Add(obj.ToObject<AssistSchool>());
  636. // }
  637. // if (iscontinuation)
  638. // {
  639. // continuationToken = itemSchool.GetContinuationToken();
  640. // break;
  641. // }
  642. // }
  643. // }
  644. //}
  645. foreach (var item in schoolAssists)
  646. {
  647. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(item.id, new PartitionKey("ProductSum"));
  648. if (response.Status == 200)
  649. {
  650. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  651. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  652. item.serial = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  653. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  654. item.service = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  655. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  656. item.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  657. }
  658. item.assists = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "assist");
  659. item.scAdmin = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "admin");
  660. item.lessonCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c ", $"LessonRecord-{item.id}");
  661. }
  662. //schoolAssists.ForEach(async school =>
  663. //{
  664. // var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school.id, new PartitionKey("ProductSum"));
  665. // if (response.Status == 200)
  666. // {
  667. // using var json = await JsonDocument.ParseAsync(response.ContentStream);
  668. // if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  669. // {
  670. // school.serial = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  671. // }
  672. // if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  673. // {
  674. // school.service = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  675. // }
  676. // if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  677. // {
  678. // school.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  679. // }
  680. // }
  681. // school.assists = await CommonFind.FindSchoolRoles(cosmosClient, school.id, "assist");
  682. // school.lessonCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c ", $"LessonRecord-{school.id}");
  683. //});
  684. return Ok(new { state = 200, scCnt, continuationToken, schoolAssists });
  685. }
  686. catch (Exception ex)
  687. {
  688. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/get-schoolsinfo \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  689. return BadRequest();
  690. }
  691. }
  692. /// <summary>
  693. /// 所有信息
  694. /// </summary>
  695. /// <param name="jsonElement"></param>
  696. /// <returns></returns>
  697. [ProducesDefaultResponseType]
  698. [HttpPost("get-allscinfo")]
  699. public async Task<IActionResult> GetAllScsInfo(JsonElement jsonElement)
  700. {
  701. try
  702. {
  703. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  704. jsonElement.TryGetProperty("role", out JsonElement role);
  705. jsonElement.TryGetProperty("scId", out JsonElement scId);
  706. jsonElement.TryGetProperty("name", out JsonElement name);
  707. jsonElement.TryGetProperty("order", out JsonElement order);
  708. jsonElement.TryGetProperty("province", out JsonElement province);
  709. jsonElement.TryGetProperty("city", out JsonElement city);
  710. jsonElement.TryGetProperty("dist", out JsonElement dist);
  711. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  712. var cosmosClient = _azureCosmos.GetCosmosClient();
  713. ////分开部署,就不需要,一站多用时,取消注释
  714. //if ($"{site}".Equals(BIConst.Global))
  715. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  716. int scCnt = 0;
  717. List<AssistSchool> schoolAssists = new(); //返回学校列表集合
  718. List<string> schoolIds = new();
  719. 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 ");
  720. StringBuilder scCntSql = new($"select value(count(c.id)) from c");
  721. if (!string.IsNullOrEmpty($"{scId}") && string.IsNullOrEmpty($"{name}"))
  722. {
  723. stringBuilder.Append($" where c.id='{scId}'");
  724. scCntSql.Append($" where c.id='{scId}'");
  725. }
  726. if (string.IsNullOrEmpty($"{scId}") && !string.IsNullOrEmpty($"{name}"))
  727. {
  728. stringBuilder.Append($" where Contains(c.name,'{name}')");
  729. scCntSql.Append($" where Contains(c.name,'{name}')");
  730. }
  731. if (!string.IsNullOrEmpty($"{province}") && string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
  732. {
  733. stringBuilder.Append($" where Contains(c.province,'{province}')");
  734. scCntSql.Append($" where Contains(c.province,'{province}')");
  735. }
  736. if (!string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
  737. {
  738. stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
  739. scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
  740. }
  741. if (!string.IsNullOrEmpty($"{dist}") && !string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{dist}"))
  742. {
  743. stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
  744. scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
  745. }
  746. if (!string.IsNullOrEmpty($"{tmdId}"))
  747. {
  748. schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}", roles: !string.IsNullOrEmpty($"{role}") ? $"{role}" : "assist", isMany: true);
  749. string scsSql = BICommonWay.ManyScSql("c.id", schoolIds);
  750. scCntSql.Append($" where {scsSql}");
  751. scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
  752. foreach (var id in schoolIds)
  753. {
  754. StringBuilder sqlTxt = 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 where c.id='{id}'");
  755. if ($"{order}".Equals("desc"))
  756. sqlTxt.Append(" order by c.createTime desc");
  757. await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AssistSchool>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  758. {
  759. schoolAssists.Add(itemSchool);
  760. }
  761. }
  762. }
  763. else
  764. {
  765. if ($"{order}".Equals("desc"))
  766. stringBuilder.Append(" order by c.createTime desc");
  767. scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
  768. await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  769. {
  770. using var json = await JsonDocument.ParseAsync(itemSchool.ContentStream);
  771. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
  772. {
  773. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  774. {
  775. schoolAssists.Add(obj.ToObject<AssistSchool>());
  776. }
  777. }
  778. }
  779. }
  780. foreach (var item in schoolAssists)
  781. {
  782. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(item.id, new PartitionKey("ProductSum"));
  783. if (response.Status == 200)
  784. {
  785. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  786. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  787. item.serial = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  788. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  789. item.service = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  790. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  791. item.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  792. }
  793. item.assists = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "assist");
  794. item.scAdmin = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "admin");
  795. item.lessonCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c ", $"LessonRecord-{item.id}");
  796. }
  797. return Ok(new { state = 200, scCnt, schoolAssists });
  798. }
  799. catch (Exception ex)
  800. {
  801. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/get-allscinfo() \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  802. return BadRequest();
  803. }
  804. }
  805. /// <summary>
  806. /// 使用yieId 关键字 本地效率未测出
  807. /// </summary>
  808. /// <param name="cosmosClient"></param>
  809. /// <param name="schoolAssists"></param>
  810. /// <returns></returns>
  811. private async IAsyncEnumerable<List<AssistSchool>> GetSchools(CosmosClient cosmosClient, List<AssistSchool> schoolAssists)
  812. {
  813. List<AssistSchool> tempSchoolAssists = new();
  814. foreach (var temp in schoolAssists)
  815. {
  816. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(temp.id, new PartitionKey("ProductSum"));
  817. if (response.Status == 200)
  818. {
  819. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  820. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  821. {
  822. temp.serial = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  823. }
  824. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  825. {
  826. temp.service = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
  827. }
  828. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  829. {
  830. temp.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
  831. }
  832. }
  833. temp.assists = await CommonFind.FindSchoolRoles(cosmosClient, temp.id, "assist");
  834. tempSchoolAssists.Add(temp);
  835. }
  836. yield return tempSchoolAssists;
  837. }
  838. /// <summary>
  839. /// 取得学校所有顾问列表
  840. /// </summary>
  841. /// <returns></returns>
  842. [ProducesDefaultResponseType]
  843. [HttpPost("get-schoolassist")]
  844. public async Task<IActionResult> GetSchoolAssist(JsonElement jsonElement)
  845. {
  846. try
  847. {
  848. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  849. string schoolId = (jsonElement.TryGetProperty("schoolId", out JsonElement _schoolId)) ? _schoolId.GetString() : string.Empty;
  850. Dictionary<string, List<Dictionary<string, string>>> haveSchoolManger = new();
  851. string managerWhereOption = (!string.IsNullOrWhiteSpace(schoolId)) ? $" AND c.code = 'Teacher-{schoolId}'" : string.Empty;
  852. //查询学校的顾问
  853. 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}";
  854. var cosmosClient = _azureCosmos.GetCosmosClient();
  855. //if ($"{site}".Equals(BIConst.Global))
  856. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  857. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: managerSql, requestOptions: new QueryRequestOptions() { }))
  858. {
  859. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  860. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  861. {
  862. string id = obj.GetProperty("id").GetString(); //管理员ID
  863. string name = obj.GetProperty("name").GetString(); //管理员姓名
  864. string tempSchoolId = obj.GetProperty("schoolId").GetString(); //学校ID
  865. Dictionary<string, string> managerDic = new();
  866. managerDic.Add("id", id);
  867. managerDic.Add("name", name);
  868. if (haveSchoolManger.ContainsKey(tempSchoolId))
  869. {
  870. haveSchoolManger[tempSchoolId].Add(managerDic);
  871. }
  872. else
  873. {
  874. List<Dictionary<string, string>> managerList = new List<Dictionary<string, string>>();
  875. managerList.Add(managerDic);
  876. haveSchoolManger.Add(tempSchoolId, managerList);
  877. }
  878. }
  879. }
  880. //管理模组学校
  881. List<string> baseModuleSchoos = new List<string>();
  882. string baseWhereOption = (!string.IsNullOrEmpty(schoolId)) ? $" and c.id='{schoolId}'" : string.Empty;
  883. 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") }))
  884. {
  885. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  886. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  887. {
  888. string tempSchoolId = obj.GetProperty("id").GetString(); //学校ID
  889. baseModuleSchoos.Add(tempSchoolId);
  890. }
  891. }
  892. //学校信息
  893. List<object> schools = new List<object>();
  894. string schoolWhereOption = (!string.IsNullOrEmpty(schoolId)) ? $" where c.id='{schoolId}'" : string.Empty;
  895. 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") }))
  896. {
  897. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  898. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  899. {
  900. dynamic schoolExpando = new ExpandoObject();
  901. schoolExpando.id = obj.GetProperty("id").GetString();
  902. schoolExpando.name = obj.GetProperty("name").GetString();
  903. //schoolExpando.period = obj.GetProperty("period");
  904. schoolExpando.schoolCode = obj.GetProperty("schoolCode").GetString();
  905. schoolExpando.region = obj.GetProperty("region").GetString();
  906. schoolExpando.province = obj.GetProperty("province").GetString();
  907. schoolExpando.city = obj.GetProperty("city").GetString();
  908. schoolExpando.picture = obj.GetProperty("picture").GetString();
  909. if (haveSchoolManger.ContainsKey(schoolExpando.id))
  910. {
  911. schoolExpando.hasMarger = true;
  912. schoolExpando.managers = haveSchoolManger[schoolExpando.id];
  913. }
  914. else
  915. {
  916. schoolExpando.hasMarger = false;
  917. schoolExpando.managers = new List<object>();
  918. }
  919. schoolExpando.hasBaseModule = (baseModuleSchoos.Contains(schoolExpando.id)) ? true : false;
  920. schools.Add(schoolExpando);
  921. }
  922. }
  923. return Ok(new { state = 200, schools });
  924. }
  925. catch (Exception ex)
  926. {
  927. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/get-schools \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  928. return BadRequest();
  929. }
  930. }
  931. /// <summary>
  932. /// 修改学校信息和顾问信息 //已经对接
  933. /// </summary>
  934. /// <param name="jsonElement"></param>
  935. /// <returns></returns>
  936. [ProducesDefaultResponseType]
  937. [AuthToken(Roles = "admin,rdc")]
  938. [HttpPost("upd-schoolassist")]
  939. public async Task<IActionResult> UpdSchoolAssist(JsonElement jsonElement)
  940. {
  941. try
  942. {
  943. if (!jsonElement.TryGetProperty("schoolId", out JsonElement _schoolId)) return BadRequest();
  944. if (!jsonElement.TryGetProperty("name", out JsonElement schoolName)) return BadRequest();
  945. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  946. if (!jsonElement.TryGetProperty("standard", out JsonElement standard)) return BadRequest();
  947. //if (!jsonElement.TryGetProperty("period", out JsonElement period)) return BadRequest();
  948. if (!jsonElement.TryGetProperty("picture", out JsonElement picture)) return BadRequest();
  949. if (!jsonElement.TryGetProperty("size", out JsonElement size)) return BadRequest();
  950. if (!jsonElement.TryGetProperty("scale", out JsonElement scale)) return BadRequest();
  951. if (!jsonElement.TryGetProperty("type", out JsonElement _type)) return BadRequest();
  952. jsonElement.TryGetProperty("province", out JsonElement province);
  953. jsonElement.TryGetProperty("city", out JsonElement city);
  954. jsonElement.TryGetProperty("dist", out JsonElement dist);
  955. jsonElement.TryGetProperty("address", out JsonElement address);
  956. jsonElement.TryGetProperty("assist", out JsonElement assist);
  957. List<IdInfo> idInfos = assist.ToObject<List<IdInfo>>();
  958. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  959. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  960. var cosmosClient = _azureCosmos.GetCosmosClient();
  961. var tableClient = _azureStorage.GetCloudTableClient();
  962. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  963. //if ($"{site}".Equals(BIConst.Global))
  964. //{
  965. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  966. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  967. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  968. //}
  969. School tempShool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{_schoolId}", new PartitionKey("Base"));
  970. if (tempShool != null)
  971. {
  972. List<Period> periods = new();
  973. string campusId = Guid.NewGuid().ToString();
  974. //periodS.ForEach(x =>
  975. //{
  976. // periods.Add(new Period { id = Guid.NewGuid().ToString(), name = x.ToString(), campusId = campusId });
  977. //});
  978. //tempShool.period = periods;
  979. tempShool.size = !string.IsNullOrEmpty($"{size}") ? int.Parse($"{size}") : tempShool.size;
  980. tempShool.scale = !string.IsNullOrEmpty($"{scale}") ? int.Parse($"{scale}") : tempShool.scale;
  981. tempShool.picture = $"{picture}";
  982. tempShool.type = int.Parse($"{_type}");
  983. tempShool.name = $"{schoolName}";
  984. tempShool.areaId = $"{areaId}";
  985. tempShool.standard = $"{standard}";
  986. tempShool.province = string.IsNullOrEmpty($"{province}") ? tempShool.province : $"{province}";
  987. tempShool.city = string.IsNullOrEmpty($"{city}") ? tempShool.city : $"{city}";
  988. tempShool.dist = string.IsNullOrEmpty($"{dist}") ? tempShool.dist : $"{dist}";
  989. tempShool.address = string.IsNullOrEmpty($"{address}") ? tempShool.address : $"{address}";
  990. //修改学校
  991. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(tempShool, tempShool.id, new PartitionKey("Base"));
  992. //创建学校信息中间件
  993. //_ = _httpTrigger.RequestHttpTrigger(new { school = $"{tempShool}" }, _option.Location, "set-sc-birelation");
  994. //await BIStats.SetSchoolBIRelation(cosmosClient, blobClient, tableClient, _dingDing, tempShool);
  995. //修改学校教师关联的信息
  996. string sql = $"SELECT distinct value(c) FROM c join A1 in c.schools where A1.schoolId='{tempShool.id}'";
  997. List<Teacher> teachers = new();
  998. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  999. {
  1000. teachers.Add(item);
  1001. }
  1002. foreach (var item in teachers)
  1003. {
  1004. Teacher.TeacherSchool teacherSchool = item.schools.Find(x => x.schoolId.Equals(tempShool.id));
  1005. if (teacherSchool != null)
  1006. {
  1007. teacherSchool.name = tempShool.name;
  1008. teacherSchool.picture = tempShool.picture;
  1009. teacherSchool.areaId = tempShool.areaId;
  1010. }
  1011. await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(item, item.id, new PartitionKey($"Base"));
  1012. }
  1013. //需要重大修改后保留
  1014. BIRelation biRel = new();
  1015. var respRel = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(tempShool.id, new PartitionKey("BIRel"));
  1016. if (respRel.Status == 200)
  1017. {
  1018. using var fileJson = await JsonDocument.ParseAsync(respRel.ContentStream);
  1019. biRel = fileJson.ToObject<BIRelation>();
  1020. }
  1021. else
  1022. {
  1023. biRel.id = tempShool.id;
  1024. }
  1025. string aName = null;
  1026. if (!string.IsNullOrEmpty($"{tempShool.areaId}"))
  1027. {
  1028. aName = await CosmosQueryHelper.GetStr(cosmosClient, "Normal", $"select value(c.name) from c where c.id='{tempShool.areaId}'", "Base-Area");
  1029. }
  1030. biRel.name = tempShool.name;
  1031. biRel.picture = tempShool.picture;
  1032. biRel.region = tempShool.region;
  1033. biRel.province = tempShool.province;
  1034. biRel.city = tempShool.city;
  1035. biRel.dist = tempShool.dist;
  1036. biRel.address = tempShool.address;
  1037. biRel.areaId = tempShool.areaId;
  1038. biRel.size = tempShool.size;
  1039. biRel.scale = tempShool.scale;
  1040. biRel.upDate = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  1041. biRel.areaName = aName;
  1042. if (idInfos.Count > 0)
  1043. {
  1044. foreach (var item in idInfos)
  1045. {
  1046. var tempAss = biRel.assists.Find(f => f.id.Equals(item.id));
  1047. if (tempAss == null)
  1048. biRel.assists.Add(item);
  1049. }
  1050. }
  1051. else
  1052. {
  1053. biRel.assists = idInfos;
  1054. }
  1055. if (respRel.Status == 200)
  1056. {
  1057. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<BIRelation>(biRel, biRel.id, new PartitionKey("BIRel"));
  1058. }
  1059. else
  1060. {
  1061. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<BIRelation>(biRel, new PartitionKey("BIRel"));
  1062. }
  1063. }
  1064. //保存操作记录
  1065. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "school-update", $"{_tmdName}【{_tmdId}】修改学校功能,修改的学校:{_schoolId},{_type},{picture},{size},{idInfos.ToArray()}", _dingDing, httpContext: HttpContext);
  1066. return Ok(new { state = 200 });
  1067. }
  1068. catch (Exception ex)
  1069. {
  1070. await _dingDing.SendBotMsg($"BI, {_option.Location} /batchschool/upd-schoolassist \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  1071. return BadRequest();
  1072. }
  1073. }
  1074. /// <summary>
  1075. /// 查询区域的学校
  1076. /// </summary>
  1077. /// <param name="jsonElement"></param>
  1078. /// <returns></returns>
  1079. [ProducesDefaultResponseType]
  1080. [HttpPost("get-schooldist")]
  1081. public async Task<IActionResult> GetSchoolDist(JsonElement jsonElement)
  1082. {
  1083. try
  1084. {
  1085. if (!jsonElement.TryGetProperty("dist", out JsonElement _dist)) return BadRequest();
  1086. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  1087. var cosmosClient = _azureCosmos.GetCosmosClient();
  1088. //if ($"{site}".Equals(BIConst.Global))
  1089. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  1090. string sqltxt = $"SELECT * FROM c where c.dist='{_dist}'";
  1091. List<School> schools = new List<School>();
  1092. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  1093. {
  1094. schools.Add(item);
  1095. }
  1096. return Ok(new { state = 200, schools });
  1097. }
  1098. catch (Exception ex)
  1099. {
  1100. await _dingDing.SendBotMsg($"BI,{_option.Location} /batchschool/get-schooldist \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  1101. return BadRequest();
  1102. }
  1103. }
  1104. /// <summary>
  1105. /// 分页学校和顾问信息
  1106. /// </summary>
  1107. /// <param name="jsonElement"></param>
  1108. /// <returns></returns>
  1109. [ProducesDefaultResponseType]
  1110. [HttpPost("get-schools")]
  1111. public async Task<IActionResult> GetSchools(JsonElement jsonElement)
  1112. {
  1113. //List<School> schools = new List<School>();
  1114. List<AssistSchool> schoolAssists = new List<AssistSchool>();
  1115. if (!jsonElement.TryGetProperty("endPosition", out JsonElement endPosition)) return BadRequest();
  1116. if (!jsonElement.TryGetProperty("pageSize", out JsonElement pageSize)) return BadRequest();
  1117. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  1118. var cosmosClient = _azureCosmos.GetCosmosClient();
  1119. //if ($"{site}".Equals(BIConst.Global))
  1120. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  1121. string sqltxt = $"SELECT * FROM c order by c.id offset {endPosition} limit {pageSize}";
  1122. await foreach (var itemSchool in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  1123. {
  1124. AssistSchool schoolAssist = new()
  1125. {
  1126. id = itemSchool.id,
  1127. code = itemSchool.code,
  1128. schoolCode = itemSchool.schoolCode,
  1129. name = itemSchool.name,
  1130. region = itemSchool.region,
  1131. province = itemSchool.province,
  1132. city = itemSchool.city,
  1133. dist = itemSchool.dist,
  1134. size = itemSchool.size,
  1135. address = itemSchool.address,
  1136. picture = itemSchool.picture,
  1137. type = itemSchool.type,
  1138. scale = itemSchool.scale,
  1139. areaId = itemSchool.areaId,
  1140. standard = itemSchool.standard
  1141. };
  1142. schoolAssist.assists = await CommonFind.FindSchoolRoles(cosmosClient, itemSchool.id, "assist");
  1143. schoolAssists.Add(schoolAssist);
  1144. }
  1145. return Ok(new { state = 200, schoolAssists });
  1146. }
  1147. /// <summary>
  1148. /// 创校前检查数据
  1149. /// </summary>
  1150. /// <param name="jsonElement"></param>
  1151. /// <returns></returns>
  1152. [ProducesDefaultResponseType]
  1153. [HttpPost("get-checlkexist")]
  1154. public async Task<IActionResult> GetCheckExist(JsonElement jsonElement)
  1155. {
  1156. if (!jsonElement.TryGetProperty("scNames", out JsonElement _scNames)) return BadRequest();
  1157. if (!jsonElement.TryGetProperty("accounts", out JsonElement _accounts)) return BadRequest();
  1158. if (!jsonElement.TryGetProperty("areaIds", out JsonElement _areaIds)) return BadRequest();
  1159. jsonElement.TryGetProperty("schools", out JsonElement schools);
  1160. var cosmosClient = _azureCosmos.GetCosmosClient();
  1161. List<CreateSchoolInfo> cSchools = new();
  1162. if (!string.IsNullOrEmpty($"{schools}"))
  1163. cSchools = schools.ToObject<List<CreateSchoolInfo>>();
  1164. List<string> scNames = _scNames.ToObject<List<string>>();
  1165. List<string> accounts = _accounts.ToObject<List<string>>();
  1166. List<string> areaIds = _areaIds.ToObject<List<string>>();
  1167. List<string> existScNames = new(); //存在的学校
  1168. List<string> noAccounts = new(); // 不存在的账户
  1169. List<string> noAreaIds = new(); //不存在的学区
  1170. List<string> synPro = new(); //已同步省平台
  1171. List<CreateSchoolInfo> createScInfo = new(); //学校简码信息
  1172. if (scNames.Count > 0)
  1173. {
  1174. foreach (var item in scNames)
  1175. {
  1176. //查询学校名称是否全字段匹配
  1177. await foreach (var sc in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: $"select value(c.name) from c where c.name='{item}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  1178. {
  1179. existScNames.Add(item);
  1180. }
  1181. }
  1182. }
  1183. if (accounts.Count > 0)
  1184. {
  1185. var content = new StringContent(_accounts.ToString(), Encoding.UTF8, "application/json");
  1186. string json = await _coreAPIHttpService.GetUserInfos(content);
  1187. List<TmdUserinfo> tmdInfos = json.ToObject<List<TmdUserinfo>>();
  1188. accounts.ForEach(ac =>
  1189. {
  1190. TmdUserinfo noTmdInfo = null;
  1191. tmdInfos.ForEach(tmd =>
  1192. {
  1193. if (!string.IsNullOrEmpty(tmd.id))
  1194. if (tmd.id.Equals($"{ac}"))
  1195. noTmdInfo = tmd;
  1196. if (!string.IsNullOrEmpty(tmd.mobile))
  1197. if (tmd.mobile.Equals($"{ac}"))
  1198. noTmdInfo = tmd;
  1199. if (!string.IsNullOrEmpty(tmd.mail))
  1200. if (tmd.mail.Equals($"{ac}"))
  1201. noTmdInfo = tmd;
  1202. });
  1203. //noTmdInfo = tmdInfos.Find(f => f.id.Equals($"{ac}") || f.mail.Equals($"{ac}") || f.mobile.Equals($"{ac}"));
  1204. if (noTmdInfo == null)
  1205. noAccounts.Add(ac);
  1206. });
  1207. }
  1208. if (areaIds.Count > 0)
  1209. {
  1210. foreach (var item in areaIds)
  1211. {
  1212. //查询区是否存在
  1213. var respAreaId = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{item}", new PartitionKey("Base-Area"));
  1214. if (respAreaId.Status != 200)
  1215. {
  1216. noAreaIds.Add(item);
  1217. }
  1218. //查询去是否同步省平台
  1219. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{item}", new PartitionKey("AreaSetting"));
  1220. if (responseSet.Status == 200)
  1221. {
  1222. using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  1223. AreaSetting delSet = fileJson.ToObject<AreaSetting>();
  1224. if (!string.IsNullOrEmpty(delSet.accessConfig))
  1225. synPro.Add(item);
  1226. }
  1227. }
  1228. }
  1229. foreach (var item in cSchools)
  1230. {
  1231. CreateSchoolInfo createSchoolInfo = item;
  1232. //生成学校ID
  1233. bool tempStaus = true;
  1234. do
  1235. {
  1236. createSchoolInfo = await SchoolCode.GenerateSchoolCode(createSchoolInfo, _dingDing, _environment);
  1237. var schoolStatu = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{createSchoolInfo.id}", new PartitionKey($"Base"));
  1238. if (schoolStatu.Status != 200) tempStaus = false;
  1239. else createSchoolInfo.createCount = createSchoolInfo.createCount >= 3 ? createSchoolInfo.createCount = 3 : createSchoolInfo.createCount += 1;
  1240. } while (tempStaus);
  1241. createScInfo.Add(createSchoolInfo);
  1242. }
  1243. if (existScNames.Count > 0 || noAccounts.Count > 0 || noAreaIds.Count > 0 || synPro.Count > 0)
  1244. return Ok(new { state = RespondCode.Created, existScNames, noAccounts, noAreaIds, synPro, createScInfo });
  1245. else
  1246. return Ok(new { state = RespondCode.Ok, createScInfo });
  1247. }
  1248. /// <summary>
  1249. /// 批量创校前检查数据
  1250. /// </summary>
  1251. /// <param name="jsonElement"></param>
  1252. /// <returns></returns>
  1253. [ProducesDefaultResponseType]
  1254. [HttpPost("get-checkexists")]
  1255. public async Task<IActionResult> GetCheckExists(JsonElement jsonElement)
  1256. {
  1257. if (!jsonElement.TryGetProperty("checkScs", out JsonElement checkScs)) return BadRequest();
  1258. List<BISchool> bISchools = checkScs.ToObject<List<BISchool>>();
  1259. var cosmosClient = _azureCosmos.GetCosmosClient();
  1260. List<BISchool> existScNames = new(); //存在的学校
  1261. List<BISchool> noAccounts = new(); // 不存在的账户
  1262. List<BISchool> noAreaIds = new(); //不存在的学区
  1263. List<BISchool> synPro = new(); //已同步省平台
  1264. List<BISchool> createScInfo = new(); //学校简码信息
  1265. foreach (BISchool bISchool in bISchools)
  1266. {
  1267. //查询学校名称是否全字段匹配
  1268. await foreach (var sc 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") }))
  1269. {
  1270. existScNames.Add(bISchool);
  1271. }
  1272. var content = new StringContent(bISchool.admin.ToJsonString(), Encoding.UTF8, "application/json");
  1273. string json = await _coreAPIHttpService.GetUserInfos(content);
  1274. List<TmdUserinfo> tmdInfos = json.ToObject<List<TmdUserinfo>>();
  1275. bISchool.admin.ForEach(ac =>
  1276. {
  1277. TmdUserinfo noTmdInfo = null;
  1278. tmdInfos.ForEach(tmd =>
  1279. {
  1280. if (!string.IsNullOrEmpty(tmd.id))
  1281. if (tmd.id.Equals($"{ac}"))
  1282. noTmdInfo = tmd;
  1283. if (!string.IsNullOrEmpty(tmd.mobile))
  1284. if (tmd.mobile.Equals($"{ac}"))
  1285. noTmdInfo = tmd;
  1286. if (!string.IsNullOrEmpty(tmd.mail))
  1287. if (tmd.mail.Equals($"{ac}"))
  1288. noTmdInfo = tmd;
  1289. });
  1290. //noTmdInfo = tmdInfos.Find(f => f.id.Equals($"{ac}") || f.mail.Equals($"{ac}") || f.mobile.Equals($"{ac}"));
  1291. if (noTmdInfo == null)
  1292. noAccounts.Add(bISchool);
  1293. });
  1294. if (!string.IsNullOrEmpty(bISchool.areaId))
  1295. {
  1296. //查询区是否存在
  1297. var respAreaId = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{bISchool.areaId}", new PartitionKey("Base-Area"));
  1298. if (respAreaId.Status != 200)
  1299. {
  1300. noAreaIds.Add(bISchool);
  1301. }
  1302. //查询去是否同步省平台
  1303. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{bISchool.areaId}", new PartitionKey("AreaSetting"));
  1304. if (responseSet.Status == 200)
  1305. {
  1306. using var fileJson = await JsonDocument.ParseAsync(responseSet.ContentStream);
  1307. AreaSetting delSet = fileJson.ToObject<AreaSetting>();
  1308. if (!string.IsNullOrEmpty(delSet.accessConfig))
  1309. synPro.Add(bISchool);
  1310. }
  1311. }
  1312. CreateSchoolInfo createSchoolInfo = new CreateSchoolInfo()
  1313. {
  1314. province = bISchool.province,
  1315. id = "",
  1316. name = bISchool.name,
  1317. city = bISchool.city,
  1318. aname = "",
  1319. createCount = 0,
  1320. };
  1321. //生成学校ID
  1322. bool tempStaus = true;
  1323. do
  1324. {
  1325. createSchoolInfo = await SchoolCode.GenerateSchoolCode(createSchoolInfo, _dingDing, _environment);
  1326. var schoolStatu = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{createSchoolInfo.id}", new PartitionKey($"Base"));
  1327. if (schoolStatu.Status != 200) tempStaus = false;
  1328. else createSchoolInfo.createCount = createSchoolInfo.createCount >= 3 ? createSchoolInfo.createCount = 3 : createSchoolInfo.createCount += 1;
  1329. } while (tempStaus);
  1330. bISchool.id = createSchoolInfo.id;
  1331. createScInfo.Add(bISchool);
  1332. }
  1333. if (existScNames.Count > 0 || noAccounts.Count > 0 || noAreaIds.Count > 0 || synPro.Count > 0)
  1334. return Ok(new { state = RespondCode.Created, existScNames, noAccounts, noAreaIds, synPro, createScInfo });
  1335. else
  1336. return Ok(new { state = RespondCode.Ok, createScInfo });
  1337. }
  1338. /// <summary>
  1339. /// 添加多个学校添加多个管理员
  1340. /// </summary>
  1341. /// <param name="jsonElement"></param>
  1342. /// <returns></returns>
  1343. [ProducesDefaultResponseType]
  1344. [HttpPost("set-batchmanage")]
  1345. public async Task<IActionResult> SetBatchManage(JsonElement jsonElement)
  1346. {
  1347. if (!jsonElement.TryGetProperty("scIds", out JsonElement _scIds)) return BadRequest();
  1348. if (!jsonElement.TryGetProperty("manages", out JsonElement _manages)) return BadRequest();
  1349. var cosmosClient = _azureCosmos.GetCosmosClient();
  1350. List<string> scIds = _scIds.ToObject<List<string>>();
  1351. List<string> manages = _manages.ToObject<List<string>>();
  1352. List<ExistScManage> existScManages = new();
  1353. if (scIds.Count > 0 && manages.Count > 0)
  1354. {
  1355. foreach (var scId in scIds)
  1356. {
  1357. foreach (var msId in manages)
  1358. {
  1359. //不存在则在原来的基础上添加顾问角色
  1360. Teacher tempTeacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{msId}", new PartitionKey("Base"));
  1361. if (tempTeacher != null)
  1362. {
  1363. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{msId}", new PartitionKey($"Teacher-{scId}"));
  1364. if (response.Status == 200)
  1365. {
  1366. using var tchJson = await JsonDocument.ParseAsync(response.ContentStream);
  1367. SchoolTeacher tchSc = tchJson.ToObject<SchoolTeacher>();
  1368. if (tchSc.roles.Contains("admin"))
  1369. {
  1370. tchSc.roles.Add("admin");
  1371. //添加顾问权限
  1372. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolTeacher>(tchSc, tchSc.id, new PartitionKey($"Teacher-{scId}"));
  1373. }
  1374. else
  1375. existScManages.Add(new ExistScManage() { scId = scId, tmdId = msId });
  1376. }
  1377. else
  1378. {
  1379. SchoolTeacher addSchoolTeacher = new()
  1380. {
  1381. id = tempTeacher.id,
  1382. code = $"Teacher-{scId}",
  1383. pk = "Teacher",
  1384. status = "join",
  1385. roles = new List<string>() { "admin" },
  1386. name = tempTeacher.name,
  1387. job = $"管理员",
  1388. size = 0,
  1389. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  1390. };
  1391. var haveTeacher = tempTeacher.schools.Find(x => x.schoolId.Equals($"{scId}"));
  1392. if (haveTeacher == null)
  1393. {
  1394. School school = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>(scId, new PartitionKey("Base"));
  1395. //教师存在,在该教师信息中添加要管理的学校信息
  1396. tempTeacher.schools.Add(new Teacher.TeacherSchool { areaId = string.IsNullOrEmpty(school.areaId) ? "" : school.areaId, schoolId = school.id, name = school.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() });
  1397. tempTeacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(tempTeacher, msId, new PartitionKey("Base"));
  1398. }
  1399. //添加学校学校管理员
  1400. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(addSchoolTeacher, new PartitionKey($"Teacher-{scId}"));
  1401. }
  1402. }
  1403. else
  1404. {
  1405. CoreUser coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{msId}" } }, _option.Location, _configuration);
  1406. Teacher teacher = new() { id = coreUser.id, name = coreUser.name, picture = coreUser.picture, pk = "Base", code = "Base", size = 1, createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() };
  1407. var responseSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(scId, new PartitionKey("Base"));
  1408. if (responseSc.Status == 200)
  1409. {
  1410. using var tchJson = await JsonDocument.ParseAsync(responseSc.ContentStream);
  1411. School scInfo = tchJson.ToObject<School>();
  1412. teacher.schools.Add(new() { schoolId = scInfo.id, name = scInfo.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), picture = scInfo.picture, areaId = scInfo.areaId });
  1413. }
  1414. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  1415. SchoolTeacher addSchoolTeacher = new()
  1416. {
  1417. id = coreUser.id,
  1418. code = $"Teacher-{scId}",
  1419. pk = "Teacher",
  1420. status = "join",
  1421. roles = new List<string>() { "admin" },
  1422. name = coreUser.name,
  1423. job = $"管理员",
  1424. size = 0,
  1425. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  1426. };
  1427. //添加学校学校管理员
  1428. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(addSchoolTeacher, new PartitionKey($"Teacher-{scId}"));
  1429. }
  1430. }
  1431. }
  1432. }
  1433. if (existScManages.Count > 0)
  1434. return Ok(new { state = RespondCode.Created, existScManages });
  1435. else
  1436. return Ok(new { state = RespondCode.Ok });
  1437. }
  1438. /// <summary>
  1439. /// 批量设置学校集合中学校多个管理员
  1440. /// </summary>
  1441. /// <param name="jsonElement"></param>
  1442. /// <returns></returns>
  1443. [ProducesDefaultResponseType]
  1444. [AuthToken(Roles = "admin")]
  1445. [HttpPost("batch-scmanage")]
  1446. public async Task<IActionResult> BatchScManage(JsonElement jsonElement)
  1447. {
  1448. if (!jsonElement.TryGetProperty("scManages", out JsonElement _scManages)) return BadRequest();
  1449. var cosmosClient = _azureCosmos.GetCosmosClient();
  1450. List<ScManages> scManages = _scManages.ToObject<List<ScManages>>();
  1451. List<ScManages> error = new(); //异常数据
  1452. foreach (var item in scManages)
  1453. {
  1454. School school = new();
  1455. var resSc = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{item.id}", new PartitionKey("Base"));
  1456. if (resSc.Status == 200)
  1457. {
  1458. using var sJson = await JsonDocument.ParseAsync(resSc.ContentStream);
  1459. school = sJson.ToObject<School>();
  1460. //CoreUser coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"19940828843" } }, _option.Location, _configuration);
  1461. var content = new StringContent(item.mobiles.ToArray().ToJsonString(), Encoding.UTF8, "application/json");
  1462. string json = await _coreAPIHttpService.GetUserInfos(content);
  1463. List<TmdUserinfo> tmdInfos = json.ToObject<List<TmdUserinfo>>();
  1464. Teacher teacher = new();
  1465. foreach (var tmd in tmdInfos)
  1466. {
  1467. var respone = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemStreamAsync($"{tmd.id}", new PartitionKey("Base"));
  1468. if (respone.Status == 200)
  1469. {
  1470. using var tchJson = await JsonDocument.ParseAsync(respone.ContentStream);
  1471. teacher = tchJson.ToObject<Teacher>();
  1472. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{tmd.id}", new PartitionKey($"Teacher-{item.id}"));
  1473. if (response.Status == 200)
  1474. {
  1475. using var scTchJson = await JsonDocument.ParseAsync(response.ContentStream);
  1476. SchoolTeacher tchSc = scTchJson.ToObject<SchoolTeacher>();
  1477. if (tchSc.roles.Contains("admin"))
  1478. {
  1479. tchSc.roles.Add("admin");
  1480. //添加顾问权限
  1481. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<SchoolTeacher>(tchSc, tchSc.id, new PartitionKey($"Teacher-{item.id}"));
  1482. }
  1483. else
  1484. {
  1485. var exsitI = error.Find(f => f.id.Equals(item.id));
  1486. if (exsitI != null)
  1487. {
  1488. var replacts = error.Where(w => w.id.Equals(item.id)).FirstOrDefault();
  1489. replacts.mobiles.Add($"{item.id}");
  1490. }
  1491. else
  1492. error.Add(new ScManages { id = item.id, name = item.name, mobiles = new List<string>() { $"{tmd.id}" } });
  1493. }
  1494. }
  1495. else
  1496. {
  1497. SchoolTeacher addSchoolTeacher = new()
  1498. {
  1499. id = tmd.id,
  1500. code = $"Teacher-{item.id}",
  1501. pk = "Teacher",
  1502. status = "join",
  1503. roles = new List<string>() { "admin" },
  1504. name = tmd.name,
  1505. job = $"管理员",
  1506. size = 0,
  1507. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  1508. };
  1509. var haveTeacher = teacher.schools.Find(x => x.schoolId.Equals($"{item.id}"));
  1510. if (haveTeacher == null)
  1511. {
  1512. //教师存在,在该教师信息中添加要管理的学校信息
  1513. teacher.schools.Add(new Teacher.TeacherSchool { areaId = string.IsNullOrEmpty(school.areaId) ? "" : school.areaId, schoolId = school.id, name = school.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() });
  1514. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  1515. }
  1516. //添加学校学校顾问
  1517. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(addSchoolTeacher, new PartitionKey($"Teacher-{item.id}"));
  1518. }
  1519. }
  1520. else
  1521. {
  1522. teacher = new()
  1523. {
  1524. id = $"{tmd.id}",
  1525. name = $"{tmd.name}",
  1526. picture = $"{tmd.picture}",
  1527. pk = "Base",
  1528. code = "Base",
  1529. size = 1,
  1530. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  1531. schools = new List<Teacher.TeacherSchool>() { new Teacher.TeacherSchool { schoolId = $"{school.id}", name = $"{school.name}", status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), picture = school.province, areaId = school.areaId } }
  1532. };
  1533. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  1534. SchoolTeacher addSchoolTeacher = new()
  1535. {
  1536. id = tmd.id,
  1537. code = $"Teacher-{item.id}",
  1538. pk = "Teacher",
  1539. status = "join",
  1540. roles = new List<string>() { "admin" },
  1541. name = tmd.name,
  1542. job = $"管理员",
  1543. size = 0,
  1544. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  1545. };
  1546. //添加学校学校管理员
  1547. await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(addSchoolTeacher, new PartitionKey($"Teacher-{item.id}"));
  1548. }
  1549. }
  1550. }
  1551. else
  1552. error.Add(new ScManages { id = item.id, name = item.name, mobiles = new List<string>() });
  1553. }
  1554. return Ok(new { state = RespondCode.Ok, error });
  1555. }
  1556. #region 预设学校基础信息 多语言
  1557. /// <summary>
  1558. /// 预设学校基础信息 多语言
  1559. /// </summary>
  1560. /// <param name="period"></param>
  1561. /// <param name="Language"></param>
  1562. /// <param name="campusId"></param>
  1563. /// <returns></returns>
  1564. public List<Period> PresetSchoolPeriod(List<PeriodType> period, string Language, string campusId)
  1565. {
  1566. var builder = $"{_environment.ContentRootPath}/JsonFile/Preset/LangSchoolConfig.json";
  1567. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  1568. StringBuilder stringBuilder = new();
  1569. string text;
  1570. while ((text = streamReader.ReadLine()) != null)
  1571. {
  1572. stringBuilder.Append(text.ToString());
  1573. }
  1574. streamReader.Close();
  1575. string input = stringBuilder.ToString();
  1576. List<SchoolConfig> schoolConfigs = input.ToObject<List<SchoolConfig>>();
  1577. SchoolConfig schoolConfig = schoolConfigs.Find(x => x.Lang.Contains($"{Language}"));
  1578. if (schoolConfig == null)
  1579. {
  1580. if (Language.Contains("en"))
  1581. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("en-US"));
  1582. else
  1583. schoolConfig = schoolConfigs.Find(x => x.Lang.Contains("zh-CN"));
  1584. }
  1585. List<Period> periods = new();
  1586. if (period.Count > 0)
  1587. {
  1588. period.ForEach(x =>
  1589. {
  1590. periods.Add(new Period
  1591. {
  1592. id = Guid.NewGuid().ToString(),
  1593. name = x.name,
  1594. campusId = campusId,
  1595. 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() },
  1596. 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() } },
  1597. subjects = new List<Subject>() {
  1598. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  1599. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  1600. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  1601. },
  1602. grades = schoolConfig.grades,
  1603. analysis = new Analysis()
  1604. {
  1605. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  1606. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  1607. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  1608. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  1609. income = schoolConfig.PresetExam[0].income,
  1610. eugenics = schoolConfig.PresetExam[0].eugenics,
  1611. touch = schoolConfig.PresetExam[0].touch
  1612. },
  1613. periodType = x.value
  1614. });
  1615. });
  1616. }
  1617. else
  1618. {
  1619. periods.Add(new Period
  1620. {
  1621. id = Guid.NewGuid().ToString(),
  1622. name = "默认学段",
  1623. campusId = campusId,
  1624. 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() },
  1625. 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() } },
  1626. subjects = new List<Subject>() {
  1627. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[0].name,type=schoolConfig.PresetSubject[0].type },
  1628. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[1].name,type=schoolConfig.PresetSubject[1].type },
  1629. new Subject { id=Guid.NewGuid().ToString(),name=schoolConfig.PresetSubject[2].name,type=schoolConfig.PresetSubject[2].type }
  1630. },
  1631. grades = schoolConfig.grades,
  1632. analysis = new Analysis()
  1633. {
  1634. type = new List<ExamSimple>() { new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[0].name },
  1635. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[1].name },
  1636. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[2].name },
  1637. new ExamSimple { id = Guid.NewGuid().ToString(), name = schoolConfig.PresetExam[0].type[3].name } },
  1638. income = schoolConfig.PresetExam[0].income,
  1639. eugenics = schoolConfig.PresetExam[0].eugenics,
  1640. touch = schoolConfig.PresetExam[0].touch
  1641. }
  1642. });
  1643. }
  1644. return periods;
  1645. }
  1646. #endregion
  1647. //public class SchoolAssist
  1648. //{
  1649. // public string id { get; set; }
  1650. // public string code { get; set; }
  1651. // public string schoolCode { get; set; }
  1652. // public string name { get; set; }
  1653. // public string region { get; set; }
  1654. // public string province { get; set; }
  1655. // public string city { get; set; }
  1656. // public string dist { get; set; }
  1657. // public int size { get; set; }
  1658. // public string address { get; set; }
  1659. // public string picture { get; set; }
  1660. // public int type { get; set; }
  1661. // public int scale { get; set; }
  1662. // public string standard { get; set; }
  1663. // public string areaId { get; set; }
  1664. // public List<SchoolTeacherRoles> assists { get; set; }
  1665. // public int serial { get; set; } //软体
  1666. // public int service { get; set; } //服务
  1667. // public int hard { get; set; } //硬体
  1668. // //public List<SchoolProductSumData> serial { get; set; } //软体
  1669. // //public List<SchoolProductSumData> service { get; set; } //服务
  1670. // //public List<SchoolProductSumDataHard> hard { get; set; } //硬体
  1671. //}
  1672. /// <summary>
  1673. /// 创建多个学校实体
  1674. /// </summary>
  1675. public record FoundSchools()
  1676. {
  1677. /// <summary>
  1678. /// 语系
  1679. /// </summary>
  1680. public string lang { get; set; }
  1681. /// <summary>
  1682. /// 站点
  1683. /// </summary>
  1684. public string site { get; set; }
  1685. /// <summary>
  1686. /// 批量创校的数据结构
  1687. /// </summary>
  1688. public List<BISchool> biSchools { get; set; } = new List<BISchool>();
  1689. }
  1690. /// <summary>
  1691. /// 批量创校的数据结构
  1692. /// </summary>
  1693. public record BISchool()
  1694. {
  1695. public string id { get; set; }
  1696. /// <summary>
  1697. /// 学校名称
  1698. /// </summary>
  1699. public string name { get; set; }
  1700. /// <summary>
  1701. /// 学校管理员
  1702. /// </summary>
  1703. public List<string> admin { get; set; }
  1704. /// <summary>
  1705. /// 学校的学段
  1706. /// </summary>
  1707. public List<PeriodType> period { get; set; }
  1708. /// <summary>
  1709. /// 学校空间大小
  1710. /// </summary>
  1711. public int size { get; set; }
  1712. /// <summary>
  1713. /// 地区
  1714. /// </summary>
  1715. public string region { get; set; }
  1716. /// <summary>
  1717. /// 省份
  1718. /// </summary>
  1719. public string province { get; set; }
  1720. /// <summary>
  1721. /// 城市
  1722. /// </summary>
  1723. public string city { get; set; }
  1724. /// <summary>
  1725. /// 县,区,郡
  1726. /// </summary>
  1727. public string dist { get; set; }
  1728. /// <summary>
  1729. /// 学校详细地址
  1730. /// </summary>
  1731. public string address { get; set; }
  1732. /// <summary>
  1733. /// 学校类型
  1734. /// </summary>
  1735. public int type { get; set; }
  1736. /// <summary>
  1737. /// 区域ID
  1738. /// </summary>
  1739. public string areaId { get; set; }
  1740. /// <summary>
  1741. /// 区域标准
  1742. /// </summary>
  1743. public string standard { get; set; }
  1744. }
  1745. /// <summary>
  1746. /// 学段类型和学段名称
  1747. /// </summary>
  1748. public record PeriodType
  1749. {
  1750. public string name { get; set; }
  1751. public string value { get; set; }
  1752. }
  1753. /// <summary>
  1754. /// 存在的学校
  1755. /// </summary>
  1756. public record ExistScManage
  1757. {
  1758. public string scId { get; set; }
  1759. public string tmdId { get; set; }
  1760. }
  1761. /// <summary>
  1762. /// 管理员
  1763. /// </summary>
  1764. public record ScManages
  1765. {
  1766. public string id { get; set; }
  1767. public string name { get; set; }
  1768. public List<string> mobiles { get; set; }
  1769. }
  1770. }
  1771. }