AreaRelevantController.cs 31 KB

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