AreaRelevantController.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. using Microsoft.Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelBI.Filter;
  13. using TEAMModelBI.Models;
  14. using TEAMModelBI.Tool;
  15. using TEAMModelBI.Tool.Extension;
  16. using TEAMModelOS.Models;
  17. using TEAMModelOS.SDK;
  18. using TEAMModelOS.SDK.Context.BI;
  19. using TEAMModelOS.SDK.Context.Constant;
  20. using TEAMModelOS.SDK.DI;
  21. using TEAMModelOS.SDK.Extension;
  22. using TEAMModelOS.SDK.Models;
  23. using TEAMModelOS.SDK.Models.Cosmos.BI;
  24. using TEAMModelOS.SDK.Models.Service.BI;
  25. namespace TEAMModelBI.Controllers.BINormal
  26. {
  27. [Route("area")]
  28. [ApiController]
  29. public class AreaRelevantController : ControllerBase
  30. {
  31. //数据容器
  32. private readonly AzureCosmosFactory _azureCosmos;
  33. private readonly AzureStorageFactory _azureStorage;
  34. //钉钉提示信息
  35. private readonly DingDing _dingDing;
  36. private readonly Option _option;
  37. private readonly IConfiguration _configuration;
  38. private readonly CoreAPIHttpService _coreAPIHttpService;
  39. private readonly HttpTrigger _httpTrigger;
  40. public AreaRelevantController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, HttpTrigger httpTrigger)
  41. {
  42. _azureCosmos = azureCosmos;
  43. _azureStorage = azureStorage;
  44. _dingDing = dingDing;
  45. _option = option?.Value;
  46. _configuration = configuration;
  47. _coreAPIHttpService = coreAPIHttpService;
  48. _httpTrigger = httpTrigger;
  49. }
  50. /// <summary>
  51. /// 查询区域已经有的学校 已对接
  52. /// </summary>
  53. /// <param name="jsonElement"></param>
  54. /// <returns></returns>
  55. [ProducesDefaultResponseType]
  56. [HttpPost("get-areaschools")]
  57. public async Task<IActionResult> GetAreaSchools(JsonElement jsonElement)
  58. {
  59. try
  60. {
  61. jsonElement.TryGetProperty("areaId", out JsonElement _areaId);
  62. jsonElement.TryGetProperty("isDefault", out JsonElement _isDefalue);
  63. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  64. var cosmosClient = _azureCosmos.GetCosmosClient();
  65. ////分开部署,就不需要,一站多用时,取消注释
  66. //if ($"{site}".Equals(BIConst.Global))
  67. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  68. var isManyArea = false;
  69. if (!string.IsNullOrEmpty($"{_isDefalue}"))
  70. isManyArea = true;
  71. List<JoinAreaSchool> joinAreaSchools = new();
  72. string sqltxt = $"SELECT c.code,c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.areaId,c.standard,c.manyAreas FROM c WHERE c.areaId='{_areaId}'";
  73. if (isManyArea)
  74. {
  75. sqltxt = $"SELECT c.code,c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.areaId,c.standard,c.manyAreas FROM c join m in c.manyAreas WHERE (c.areaId ='{_areaId}' or m.areaId='{_areaId}')";
  76. }
  77. //IES5實體學校
  78. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIteratorSql(queryText: sqltxt, requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  79. {
  80. using var json = await JsonDocument.ParseAsync(item.Content);
  81. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  82. {
  83. foreach(var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  84. {
  85. JoinAreaSchool joinAreaSchool = new()
  86. {
  87. code = obj.GetProperty("code").GetString(),
  88. id = obj.GetProperty("id").GetString(),
  89. name = obj.GetProperty("name").GetString(),
  90. schoolCode = obj.GetProperty("schoolCode").GetString(),
  91. province = obj.GetProperty("province").GetString(),
  92. city = obj.GetProperty("city").GetString(),
  93. dist = obj.GetProperty("dist").GetString(),
  94. picture = obj.GetProperty("picture").GetString(),
  95. period = obj.GetProperty("period").ToObject<List<Period>>().Select(x => x.name).ToList(),
  96. areaId = obj.GetProperty("areaId").GetString(),
  97. standard = obj.GetProperty("standard").GetString()
  98. };
  99. try
  100. {
  101. joinAreaSchool.areas = obj.GetProperty("areas").ToObject<List<SchoolArea>>();
  102. }
  103. catch { }
  104. if (!string.IsNullOrEmpty($"{joinAreaSchool.id}"))
  105. {
  106. joinAreaSchool.assists = await CommonFind.FindSchoolRoles(cosmosClient, joinAreaSchool.id, "assist");
  107. joinAreaSchool.scAdmin = await CommonFind.FindSchoolRoles(cosmosClient, joinAreaSchool.id, "admin");
  108. }
  109. joinAreaSchools.Add(joinAreaSchool);
  110. }
  111. }
  112. }
  113. //虛擬學校
  114. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIteratorSql(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("VirtualBase") }))
  115. {
  116. using var json = await JsonDocument.ParseAsync(item.Content);
  117. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  118. {
  119. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  120. {
  121. JoinAreaSchool joinAreaSchool = new()
  122. {
  123. code = obj.GetProperty("code").GetString(),
  124. id = obj.GetProperty("id").GetString(),
  125. name = obj.GetProperty("name").GetString(),
  126. schoolCode = obj.GetProperty("schoolCode").GetString(),
  127. province = obj.GetProperty("province").GetString(),
  128. city = obj.GetProperty("city").GetString(),
  129. dist = obj.GetProperty("dist").GetString(),
  130. picture = obj.GetProperty("picture").GetString(),
  131. period = obj.GetProperty("period").ToObject<List<Period>>().Select(x => x.name).ToList(),
  132. areaId = obj.GetProperty("areaId").GetString(),
  133. standard = obj.GetProperty("standard").GetString()
  134. };
  135. joinAreaSchools.Add(joinAreaSchool);
  136. }
  137. }
  138. }
  139. return Ok(new { state = 200, joinAreaSchools });
  140. }
  141. catch (Exception ex)
  142. {
  143. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/get-areaschools \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  144. return BadRequest();
  145. }
  146. }
  147. /// <summary>
  148. /// 学校移出区域 //已对接
  149. /// </summary>
  150. /// <param name="jsonElement"></param>
  151. /// <returns></returns>
  152. [ProducesDefaultResponseType]
  153. [AuthToken(Roles = "admin,rdc")]
  154. [HttpPost("set-areashiftschool")]
  155. public async Task<IActionResult> SetAreaShiftSchool(JsonElement jsonElement)
  156. {
  157. try
  158. {
  159. if (!jsonElement.TryGetProperty("schoolId", out JsonElement schoolId)) return BadRequest();
  160. string code = (jsonElement.TryGetProperty("code", out JsonElement codeJobj)) ? codeJobj.GetString() : "Base";
  161. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  162. jsonElement.TryGetProperty("standard", out JsonElement standard);
  163. jsonElement.TryGetProperty("isDefault", out JsonElement isDefault);
  164. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  165. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  166. var cosmosClient = _azureCosmos.GetCosmosClient();
  167. var tableClient = _azureStorage.GetCloudTableClient();
  168. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  169. ////分开部署,就不需要,一站多用时,取消注释
  170. //if ($"{site}".Equals(BIConst.Global))
  171. //{
  172. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  173. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  174. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  175. //}
  176. var responseSet = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{areaId}", new PartitionKey("AreaSetting"));
  177. if (responseSet.Status == 200)
  178. {
  179. using var fileJson = await JsonDocument.ParseAsync(responseSet.Content);
  180. AreaSetting delSet = fileJson.ToObject<AreaSetting>();
  181. if (!string.IsNullOrEmpty(delSet.accessConfig))
  182. return Ok(new { state = 401, msg = "区域已经规定了,不能切换能能力" });
  183. }
  184. School tempSchool = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{schoolId}", new PartitionKey($"{code}"));
  185. if (bool.Parse($"{isDefault}") == true)
  186. {
  187. tempSchool.areaId = null;
  188. tempSchool.standard = null;
  189. List<Teacher> teachers = new();
  190. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql<Teacher>(queryText: $"SELECT value(c) FROM c join s in c.schools where s.schoolId ='{schoolId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  191. {
  192. teachers.Add(item);
  193. }
  194. foreach (var item in teachers)
  195. {
  196. var tchSchool = item.schools.Where(f => f.schoolId.Equals($"{schoolId}")).FirstOrDefault();
  197. if (tchSchool != null)
  198. {
  199. tchSchool.areaId = "";
  200. await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(item, item.id, new PartitionKey("Base"));
  201. }
  202. }
  203. }
  204. if (tempSchool.areas != null)
  205. {
  206. if (bool.Parse($"{isDefault}") == true)
  207. {
  208. tempSchool.areaId = "";
  209. tempSchool.standard = "";
  210. }
  211. if (tempSchool.areas != null)
  212. {
  213. if (!string.IsNullOrEmpty($"{areaId}"))
  214. {
  215. var temp = tempSchool.areas.Find(ma => ma.areaId == $"{areaId}");
  216. if (temp != null)
  217. tempSchool.areas.Remove(temp);
  218. }
  219. }
  220. }
  221. School school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(tempSchool, tempSchool.id, new PartitionKey($"{code}"));
  222. //创建/修改学校信息中间件
  223. //_ = _httpTrigger.RequestHttpTrigger(new { school = $"{school}" }, _option.Location, "set-sc-birelation");
  224. if (school.code.Equals("Base")) //實體學校更新BI中間件
  225. {
  226. await BIStats.SetSchoolBIRelation(cosmosClient, blobClient, tableClient, _dingDing, school);
  227. }
  228. //保存操作记录
  229. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "school-update", $"{_tmdName}【{_tmdId}】已操作学校(ID:{schoolId})移除已区域(ID:{areaId})", _dingDing, httpContext: HttpContext);
  230. return Ok(new { state = 200, school });
  231. }
  232. catch (Exception ex)
  233. {
  234. await _dingDing.SendBotMsg($"BI, {_option.Location} /area/set-areashiftschool \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  235. return BadRequest();
  236. }
  237. }
  238. /// <summary>
  239. /// 设置区级管理员 //已对接
  240. /// </summary>
  241. /// <param name="jsonElement"></param>
  242. /// <returns></returns>
  243. [ProducesDefaultResponseType]
  244. [AuthToken(Roles = "admin,rdc")]
  245. [HttpPost("set-manage")]
  246. public async Task<IActionResult> SetAreaManage(JsonElement jsonElement)
  247. {
  248. var (_tmdId, _tmdName, _, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  249. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  250. if (!jsonElement.TryGetProperty("tmdName", out JsonElement tmdName)) return BadRequest();
  251. jsonElement.TryGetProperty("tmdPic", out JsonElement tmdPic);
  252. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  253. if (!jsonElement.TryGetProperty("areaName", out JsonElement areaName)) return BadRequest();
  254. jsonElement.TryGetProperty("isSc", out JsonElement isSc);
  255. jsonElement.TryGetProperty("scInfos", out JsonElement scInfos);
  256. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  257. var cosmosClient = _azureCosmos.GetCosmosClient();
  258. var tableClient = _azureStorage.GetCloudTableClient();
  259. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  260. //if ($"{site}".Equals(BIConst.Global))
  261. //{
  262. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  263. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  264. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  265. //}
  266. SimpleInfo existsArea = null;
  267. List<SimpleInfo> existsSc = new();
  268. List<SimpleInfo> errorSc = new();
  269. StringBuilder strMsg = new($"{_tmdName}【{_tmdId}】账户将{tmdName}【{tmdId}】");
  270. Teacher teacher = new();
  271. var response = await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync($"{tmdId}", new PartitionKey($"Base"));
  272. if (response.Status == 200)
  273. {
  274. using var json = await JsonDocument.ParseAsync(response.Content);
  275. teacher = json.ToObject<Teacher>();
  276. var existArea = teacher.areas.Find(f => f.areaId.Equals($"{areaId}"));
  277. if (existArea == null)
  278. {
  279. teacher.areas.Add(new Teacher.TeacherArea() { areaId = $"{areaId}", name = $"{areaName}", status = "join" });
  280. teacher = await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  281. strMsg.Append("设置为区管理员!");
  282. }
  283. else
  284. existsArea = new() { id = $"{areaId}", name = $"{areaName}" };
  285. }
  286. else
  287. {
  288. teacher.id = $"{tmdId}";
  289. teacher.name = $"{tmdName}";
  290. teacher.picture = string.IsNullOrEmpty($"{tmdPic}") ? "" : $"{tmdPic}";
  291. teacher.pk = "Base";
  292. teacher.code = "Base";
  293. teacher.size = 1;
  294. teacher.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  295. //教师存在,在该教师信息中添加要管理的学校信息
  296. teacher.areas.Add(new Teacher.TeacherArea { areaId = $"{areaId}", name = $"{areaName}", status = "join" });
  297. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  298. strMsg.Append("设置为区管理员!");
  299. }
  300. if (existsArea == null)
  301. {
  302. if ($"{isSc}".Equals("yesm"))
  303. {
  304. List<SimpleInfo> schoolMs = scInfos.ToObject<List<SimpleInfo>>();
  305. if (schoolMs.Count > 0)
  306. {
  307. strMsg.Append($"并将改账户设置为学校管理员;修改信息:");
  308. foreach (var item in schoolMs)
  309. {
  310. var resSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{item.id}", new PartitionKey($"Base"));
  311. if (resSc.Status == 200)
  312. {
  313. SchoolTeacher schoolTeacher = null;
  314. var resScTch = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{tmdId}", new PartitionKey($"Teacher-{item.id}"));
  315. if (resScTch.Status == 200)
  316. {
  317. using var json = await JsonDocument.ParseAsync(resScTch.Content);
  318. schoolTeacher = json.ToObject<SchoolTeacher>();
  319. if (!schoolTeacher.roles.Contains("admin"))
  320. {
  321. schoolTeacher.roles.Add("admin");
  322. schoolTeacher = await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<SchoolTeacher>(schoolTeacher, schoolTeacher.id, new PartitionKey($"Teacher-{item.id}"));
  323. }
  324. else
  325. existsSc.Add(item);
  326. }
  327. else
  328. {
  329. schoolTeacher = new()
  330. {
  331. id = $"{tmdId}",
  332. code = $"Teacher-{item.id}",
  333. roles = new List<string> { "admin" },
  334. job = "管理员",
  335. name = $"{tmdName}",
  336. picture = string.IsNullOrEmpty($"{item.picture}") ? "" : $"{item.picture}",
  337. status = "join",
  338. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
  339. pk = "Teacher",
  340. ttl = -1
  341. };
  342. schoolTeacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey($"Teacher-{item.id}"));
  343. }
  344. var existArea = teacher.schools.Find(f => f.schoolId.Equals($"{item.id}"));
  345. if (existArea == null)
  346. {
  347. teacher.schools.Add(new Teacher.TeacherSchool { schoolId = $"{item.id}", name = $"{item.name}", status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), picture = string.IsNullOrEmpty($"{item.picture}") ? "" : $"{item.picture}", areaId = $"{areaId}" });
  348. }
  349. strMsg.Append($"{item.name}【{item.id}】|");
  350. }
  351. else
  352. errorSc.Add(item);
  353. }
  354. teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  355. }
  356. else
  357. existsSc = schoolMs;
  358. }
  359. }
  360. else
  361. return Ok(new { state = RespondCode.Conflict, msg = "你已经是该区管理员!" });
  362. //保存操作记录
  363. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "schoolTeacher-add", strMsg.ToString(), _dingDing, httpContext: HttpContext);
  364. if (existsArea != null || existsSc.Count > 0 || errorSc.Count > 0)
  365. return Ok(new { state = RespondCode.Created, teacher, existsArea, existsSc, errorSc });
  366. else
  367. return Ok(new { state = RespondCode.Ok, teacher });
  368. }
  369. /// <summary>
  370. /// 获取区域的管理员 //已对接
  371. /// </summary>
  372. /// <param name="jsonElement"></param>
  373. /// <returns></returns>
  374. [ProducesDefaultResponseType]
  375. [HttpPost("get-manage")]
  376. public async Task<IActionResult> GetAreaManages(JsonElement jsonElement)
  377. {
  378. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  379. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  380. var cosmosClient = _azureCosmos.GetCosmosClient();
  381. ////分开部署,就不需要,一站多用时,取消注释
  382. //if ($"{site}".Equals(BIConst.Global))
  383. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  384. List<BaseInfo> areaManages = new();
  385. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIteratorSql<BaseInfo>(queryText:$"select c.id,c.name,c.picture from c join a in c.areas where a.areaId ='{areaId}' and c.code='Base'",requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  386. {
  387. areaManages.Add(item);
  388. }
  389. return Ok(new { state = RespondCode.Ok, areaManages });
  390. }
  391. /// <summary>
  392. /// 删除区域管理员 //已对接
  393. /// </summary>
  394. /// <param name="jsonElement"></param>
  395. /// <returns></returns>
  396. [ProducesDefaultResponseType]
  397. [AuthToken(Roles = "admin,rdc")]
  398. [HttpPost("del-manage")]
  399. public async Task<IActionResult> DelAreaManage(JsonElement jsonElement)
  400. {
  401. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  402. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  403. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  404. var cosmosClient = _azureCosmos.GetCosmosClient();
  405. //if ($"{site}".Equals(BIConst.Global))
  406. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  407. Teacher teacher = null;
  408. var response = await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync($"{tmdId}", new PartitionKey($"Base"));
  409. if (response.Status == 200)
  410. {
  411. using var json = await JsonDocument.ParseAsync(response.Content);
  412. teacher = json.ToObject<Teacher>();
  413. var existArea = teacher.areas.Find(f => f.areaId.Equals($"{areaId}"));
  414. if (existArea != null)
  415. {
  416. teacher.areas.RemoveAll((stt)=>stt.areaId.Equals($"{areaId}"));
  417. teacher = await cosmosClient.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey("Base"));
  418. }
  419. else
  420. return Ok(new { state = RespondCode.NotFound, msg = "该账户不是该区的管理员" });
  421. }
  422. else
  423. return Ok(new { state = RespondCode.NotFound, msg = "未找到该教师信息" });
  424. return Ok(new { state = RespondCode.Ok, teacher });
  425. }
  426. /// <summary>
  427. /// 通过区域ID查询学校列表 //已对接
  428. /// </summary>
  429. /// <param name="jsonElement"></param>
  430. /// <returns></returns>
  431. [ProducesDefaultResponseType]
  432. [HttpPost("get-schools")]
  433. public async Task<IActionResult> GetSchools(JsonElement jsonElement)
  434. {
  435. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  436. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  437. var cosmosClient = _azureCosmos.GetCosmosClient();
  438. ////分开部署,就不需要,一站多用时,取消注释
  439. //if ($"{site}".Equals(BIConst.Global))
  440. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  441. List<AreaSchool> areaSchool = new();
  442. StringBuilder areaScSql = new($"select c.id,c.name,c.picture,c.size,c.areaId,c.scale from c");
  443. if (!string.IsNullOrEmpty($"{areaId}"))
  444. areaScSql.Append($" where c.areaId='{areaId}'");
  445. else
  446. areaScSql.Append(" where c.areaId = null or c.areaId = ''");
  447. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<AreaSchool>(queryText: areaScSql.ToString(),requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  448. {
  449. areaSchool.Add(item);
  450. }
  451. areaSchool.ForEach(async areaSc =>
  452. {
  453. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(areaSc.id, new PartitionKey("ProductSum"));
  454. if (response.Status == 200)
  455. {
  456. using var json = await JsonDocument.ParseAsync(response.Content);
  457. if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
  458. {
  459. areaSc.serial = serial.ToObject<List<SchoolProductSumData>>().Count;//.Select(x => x.prodCode).ToList();
  460. }
  461. if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
  462. {
  463. areaSc.service = service.ToObject<List<SchoolProductSumData>>().Count;//.Select(x => x.prodCode).ToList();
  464. }
  465. if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
  466. {
  467. areaSc.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Count;//.Select(x => x.prodCode).ToList();
  468. }
  469. }
  470. });
  471. return Ok(new { state = RespondCode.Ok, areaSchool });
  472. }
  473. /// <summary>
  474. /// 依据区域id查询该区域的顾问信息 //已对接
  475. /// </summary>
  476. /// <param name="jsonElement"></param>
  477. /// <returns></returns>
  478. [ProducesDefaultResponseType]
  479. [HttpPost("get-assists")]
  480. public async Task<IActionResult> GetAssists(JsonElement jsonElement)
  481. {
  482. jsonElement.TryGetProperty("areaId", out JsonElement areaId);
  483. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  484. var cosmosClient = _azureCosmos.GetCosmosClient();
  485. var tableClient = _azureStorage.GetCloudTableClient();
  486. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  487. ////分开部署,就不需要,一站多用时,取消注释
  488. //if ($"{site}".Equals(BIConst.Global))
  489. //{
  490. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  491. // tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  492. // blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  493. //}
  494. var table = tableClient.GetTableReference("BIDDUserInfo");
  495. List<string> scIds = new();
  496. HashSet<string> tchIds = new();
  497. string scIdsSql = null;
  498. if (!string.IsNullOrEmpty($"{areaId}"))
  499. scIdsSql = $"select value(c.id) from c where c.areaId='{areaId}'";
  500. else
  501. scIdsSql = "select value(c.id) from c where c.areaId = null or c.areaId = ''";
  502. scIds = await CommonFind.GetValueSingle(cosmosClient, "School", scIdsSql, "Base");
  503. //await foreach (var itemId in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<string>(queryText: scIdsSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  504. //{
  505. // scIds.Add(itemId);
  506. //}
  507. foreach (var scId in scIds)
  508. {
  509. string tchIdSql = $"select value(c.id) from c where array_contains(c.roles,'assist',true) and c.pk='Teacher'";
  510. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIteratorSql<string>(queryText: tchIdSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{scId}") }))
  511. {
  512. tchIds.Add(item);
  513. }
  514. }
  515. List<AreaAssist> areaAssists = new();
  516. List<string> noIds = new();
  517. foreach (var item in tchIds)
  518. {
  519. List<DingDingUserInfo> ddUsers = await table.QueryWhereString<DingDingUserInfo>($"PartitionKey eq '{_option.Location}' and tmdId eq '{item}'");
  520. if (ddUsers.Count > 0)
  521. {
  522. foreach (var dduser in ddUsers)
  523. {
  524. AreaAssist areaAssist = new()
  525. {
  526. userId = dduser.userId,
  527. unionId = dduser.unionId,
  528. name = dduser.name,
  529. mobile = dduser.mobile,
  530. avatar = dduser.avatar,
  531. tmdId = dduser.tmdId,
  532. tmdName = dduser.tmdName,
  533. tmdMobile = dduser.tmdMobile,
  534. picture = dduser.picture,
  535. roles = dduser.roles,
  536. schoolIds = dduser.schoolIds
  537. };
  538. areaAssists.Add(areaAssist);
  539. }
  540. }
  541. else { noIds.Add(item); }
  542. }
  543. return Ok(new { state = 200, areaAssists, noIds });
  544. }
  545. /// <summary>
  546. /// 通过区域ID查询学校前端暂时
  547. /// </summary>
  548. public record AreaSchool
  549. {
  550. public string id{ get; set; }
  551. public string name{ get; set; }
  552. public string picture { get; set; }
  553. public int size { get; set; }
  554. public string areaId { get; set; }
  555. public int scale { get; set; }
  556. public int serial { get; set; }
  557. public int service { get; set; }
  558. public int hard { get; set; }
  559. }
  560. /// <summary>
  561. /// 已加入区域的学校
  562. /// </summary>
  563. public record JoinAreaSchool
  564. {
  565. public string code { get; set; }
  566. public string id { get; set; }
  567. public string name { get; set; }
  568. public string schoolCode { get; set; }
  569. public string picture { get; set; }
  570. public string areaId{ get; set; }
  571. public string standard { get; set; }
  572. public List<string> period { get; set; }
  573. public string province { get; set; }
  574. public string city { get; set; }
  575. public string dist { get; set; }
  576. public List<SchoolArea> areas { get; set; } = new List<SchoolArea>();
  577. public List<SchoolTeacherRoles> assists { get; set; } = new List<SchoolTeacherRoles>();
  578. public List<SchoolTeacherRoles> scAdmin { get; set; } = new List<SchoolTeacherRoles>();
  579. }
  580. }
  581. }