AreaRelevantController.cs 31 KB

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