BatchSchoolController.cs 112 KB

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