SchoolController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.Models;
  7. using TEAMModelOS.SDK;
  8. using TEAMModelOS.SDK.DI;
  9. using System.Text.Json;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.Extension;
  12. using Azure.Cosmos;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.Extensions.Options;
  15. using System.IO;
  16. using System.Dynamic;
  17. using System.Net.Http;
  18. using System.Net;
  19. using Newtonsoft.Json;
  20. using System.Linq;
  21. using StackExchange.Redis;
  22. using static TEAMModelOS.SDK.Models.Teacher;
  23. using Microsoft.Extensions.Configuration;
  24. using TEAMModelOS.Filter;
  25. using Microsoft.AspNetCore.Authorization;
  26. using HTEXLib.COMM.Helpers;
  27. namespace TEAMModelAPI.Controllers
  28. {
  29. [ProducesResponseType(StatusCodes.Status200OK)]
  30. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  31. [ApiController]
  32. [Route("school")]
  33. public class SchoolController : ControllerBase
  34. {
  35. public AzureCosmosFactory _azureCosmos;
  36. private readonly AzureStorageFactory _azureStorage;
  37. private readonly AzureRedisFactory _azureRedis;
  38. private readonly DingDing _dingDing;
  39. private readonly Option _option;
  40. private readonly IConfiguration _configuration;
  41. public SchoolController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration)
  42. {
  43. _azureCosmos = azureCosmos;
  44. _azureStorage = azureStorage;
  45. _azureRedis = azureRedis;
  46. _dingDing = dingDing;
  47. _option = option?.Value;
  48. _configuration = configuration;
  49. }
  50. /// <summary>
  51. /// 学校基础信息
  52. /// </summary>
  53. /// <param name="request"></param>
  54. /// <returns></returns>
  55. [ProducesDefaultResponseType]
  56. [HttpGet("get-school-info")]
  57. [ApiToken(Auth = "2",Name = "学校基础信息", RW = "R", Limit =false)]
  58. public async Task<IActionResult> GetSchoolInfo()
  59. {
  60. var (id, school) = HttpContext.GetApiTokenInfo();
  61. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  62. List<dynamic> period = new List<dynamic>();
  63. data.period.ForEach(x => { period.Add(new { x.subjects ,x.grades,x.name,x.id,x.campusId,x.semesters}); });
  64. return Ok(new {
  65. id = data.id, name = data.name, data.areaId, type = data.type,
  66. data.region, data.province, data.city, data.dist,
  67. campuses=data.campuses,
  68. period
  69. });
  70. }
  71. /// <summary>
  72. /// 获取学校教师列表
  73. /// </summary>
  74. /// <param name="request"></param>
  75. /// <returns></returns>
  76. [ProducesDefaultResponseType]
  77. [HttpGet("get-teacher-list")]
  78. [ApiToken(Auth = "3", Name = "学校教师列表", RW = "R", Limit = false)]
  79. public async Task<IActionResult> GetTeacherList()
  80. {
  81. var (id, school) = HttpContext.GetApiTokenInfo();
  82. List<dynamic> teachers= new List<dynamic>();
  83. string sql = $"select c.id,c.name ,c.picture,c.job ,c.subjectIds,c.roles from c where c.status='join' ";
  84. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<dynamic>
  85. (queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Teacher-{school}") })) {
  86. teachers.Add(item);
  87. }
  88. return Ok(new
  89. {
  90. teachers
  91. });
  92. }
  93. /// <summary>
  94. /// 获取学校教师信息
  95. /// </summary>
  96. /// <returns></returns>
  97. [ProducesDefaultResponseType]
  98. [HttpPost("get-teacher-info")]
  99. [ApiToken(Auth = "4", Name = "学校教师信息", RW = "R", Limit = false)]
  100. public async Task<IActionResult> GetTeacherInfo(JsonElement json )
  101. {
  102. json.TryGetProperty("tmdid", out JsonElement _tmdid);
  103. var (id, school) = HttpContext.GetApiTokenInfo();
  104. Azure.Response responseSchoolTch =await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School")
  105. .ReadItemStreamAsync($"{_tmdid}", new PartitionKey($"Teacher-{school}"));
  106. Azure.Response responseTch = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher")
  107. .ReadItemStreamAsync($"{_tmdid}", new PartitionKey($"Base"));
  108. Teacher teacher = null;
  109. if (responseTch.Status == 200)
  110. {
  111. teacher = JsonDocument.Parse(responseTch.Content).RootElement.Deserialize<Teacher>();
  112. }
  113. else
  114. {
  115. return Ok(new { error = 3, msg = "账号未创建!" });
  116. }
  117. if (responseSchoolTch.Status == 200 && teacher!= null )
  118. {
  119. SchoolTeacher schoolTeacher= JsonDocument.Parse(responseSchoolTch.Content).RootElement.Deserialize<SchoolTeacher>();
  120. if (schoolTeacher.status.Equals("join"))
  121. {
  122. return Ok(new {teacher.id,teacher.name,teacher.picture,schoolTeacher.job,schoolTeacher.status,schoolTeacher.roles,schoolTeacher.subjectIds, school=teacher.schools?.Find(x=>x.schoolId.Equals(school)) } );
  123. }
  124. else
  125. {
  126. return Ok(new { error = 2, msg = "教师未加入学校!" });
  127. }
  128. }
  129. else {
  130. return Ok(new { error = 1, msg = "教师未就职该学校!" });
  131. }
  132. }
  133. /*
  134. "periodId":"学段(选填)"
  135. */
  136. /// <summary>
  137. /// 获取学校的行政班,教学班,教研组,研修名单
  138. /// </summary>
  139. /// <param name="request"></param>
  140. /// <returns></returns>
  141. [ProducesDefaultResponseType]
  142. [HttpPost("get-group-list")]
  143. [ApiToken(Auth = "5", Name = "学校教师列表", RW = "R", Limit = false)]
  144. public async Task<IActionResult> GetGroupList(JsonElement json)
  145. {
  146. var client = _azureCosmos.GetCosmosClient();
  147. var (id, school) = HttpContext.GetApiTokenInfo();
  148. json.TryGetProperty("periodId", out JsonElement periodId);
  149. List<GroupListGrp> groupLists = new List<GroupListGrp>();
  150. //包含,学校的行政班,教学班
  151. json.TryGetProperty("type", out JsonElement _type);
  152. List<string> types = null;
  153. if (_type.ValueKind.Equals(JsonValueKind.Array))
  154. {
  155. types = _type.ToObject<List<string>>();
  156. }
  157. else if (_type.ValueKind.Equals(JsonValueKind.String))
  158. {
  159. types = new List<string> { $"{types}" };
  160. }
  161. if (types.IsEmpty() || types.Contains("class"))
  162. {
  163. StringBuilder classsql = new StringBuilder($"SELECT c.id,c.name,c.periodId ,c.year FROM c ");
  164. if (!string.IsNullOrEmpty($"{periodId}"))
  165. {
  166. classsql.Append($" where c.periodId='{periodId}' ");
  167. }
  168. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: classsql.ToString(),
  169. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school}") }))
  170. {
  171. HashSet<string> groupNames = new HashSet<string>();
  172. string gpsql = $"SELECT distinct c.groupId,c.groupName FROM c where c.classId='{item.id}'and c.groupName <>null";
  173. await foreach (var gp in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: gpsql,
  174. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  175. {
  176. groupNames.Add(gp.groupName);
  177. }
  178. ///行政班(学生搜寻classId动态返回)class
  179. GroupListGrp group = new GroupListGrp
  180. {
  181. id = item.id,
  182. code = $"GroupList-{school}",
  183. name = item.name,
  184. periodId = item.periodId,
  185. scope = "school",
  186. school = $"{school}",
  187. type = "class",
  188. year = item.year,
  189. groupName = groupNames
  190. };
  191. groupLists.Add(group);
  192. }
  193. }
  194. if (types.IsEmpty() || types.Contains("teach"))
  195. {
  196. //教学班
  197. StringBuilder teachsql = new StringBuilder($" SELECT distinct value(c) FROM c where c.type='teach'");
  198. if (!string.IsNullOrEmpty($"{periodId}"))
  199. {
  200. teachsql.Append($" and c.periodId='{periodId}'");
  201. }
  202. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  203. GetItemQueryIterator<GroupList>(queryText: teachsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  204. {
  205. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  206. groupLists.Add(new GroupListGrp(item, groupName));
  207. }
  208. }
  209. if (types.IsEmpty() || types.Contains("research"))
  210. {
  211. //教研组
  212. StringBuilder researchsql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='research'");
  213. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  214. GetItemQueryIterator<GroupList>(queryText: researchsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  215. {
  216. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  217. groupLists.Add(new GroupListGrp(item, groupName));
  218. }
  219. }
  220. if (types.IsEmpty() || types.Contains("yxtrain"))
  221. {
  222. //研修名单
  223. StringBuilder yxtrainsql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='yxtrain'");
  224. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  225. GetItemQueryIterator<GroupList>(queryText: yxtrainsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  226. {
  227. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  228. groupLists.Add(new GroupListGrp(item, groupName));
  229. }
  230. }
  231. return Ok(new
  232. {
  233. groupLists=groupLists.Select(x=>new { x.id,x.type,x.name,x.periodId,x.school,x.scope,x.year})
  234. });
  235. }
  236. [ProducesDefaultResponseType]
  237. [HttpPost("get-group-members")]
  238. [ApiToken(Auth = "6", Name = "获取名单详细信息和成员信息", RW = "R", Limit = false)]
  239. public async Task<IActionResult> GetGroupMembers(JsonElement json)
  240. {
  241. var client = _azureCosmos.GetCosmosClient();
  242. if (!json.TryGetProperty("ids", out JsonElement ids)) return BadRequest();
  243. var (id, school) = HttpContext.GetApiTokenInfo();
  244. List<string> listids = ids.ToObject<List<string>>();
  245. (List<RMember> members, List<RGroupList> groups) = await GroupListService.GetStutmdidListids(client, _dingDing, listids, $"{school}");
  246. return Ok(new { groups = groups.Select(x => new { x.name, x.no, x.periodId, x.school, x.type, x.year, x.tcount, x.scount, x.leader, x.members, x.id }), members });
  247. }
  248. [ProducesDefaultResponseType]
  249. [HttpPost("get-course-list")]
  250. [ApiToken(Auth = "7", Name = "获取课程列表信息",RW ="R", Limit = false)]
  251. public async Task<IActionResult> GetCourseList(JsonElement json) {
  252. var client = _azureCosmos.GetCosmosClient();
  253. var (id, school) = HttpContext.GetApiTokenInfo();
  254. json.TryGetProperty("periodId", out JsonElement periodId);
  255. json.TryGetProperty("subjectId", out JsonElement subjectId);
  256. StringBuilder sql = new StringBuilder($"SELECT c.id,c.name,c.subject,c.period,c.scope,c.no,c.school FROM c where 1=1 ");
  257. if (!string.IsNullOrWhiteSpace($"{periodId}")) {
  258. sql.Append($" and c.period.id='{periodId}'");
  259. }
  260. if (!string.IsNullOrWhiteSpace($"{subjectId}"))
  261. {
  262. sql.Append($" and c.subject.id='{subjectId}'");
  263. }
  264. List<dynamic> courses = new List<dynamic>();
  265. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  266. GetItemQueryIterator<dynamic>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
  267. {
  268. courses.Add(item);
  269. }
  270. return Ok(new { courses });
  271. }
  272. [ProducesDefaultResponseType]
  273. [HttpPost("get-course-info")]
  274. [ApiToken(Auth = "8", Name = "课程详细信息", RW = "R", Limit = false)]
  275. public async Task<IActionResult> GetCourseInfo(JsonElement json)
  276. {
  277. var (id, school) = HttpContext.GetApiTokenInfo();
  278. json.TryGetProperty("courseId", out JsonElement courseId);
  279. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School")
  280. .ReadItemStreamAsync($"{courseId}", new PartitionKey($"Course-{school}"));
  281. if (response.Status == 200)
  282. {
  283. JsonDocument document= JsonDocument.Parse(response.Content);
  284. Course course= document.RootElement.Deserialize<Course>();
  285. return Ok(new { course.name,course.id, course.subject, course .period, course .scope, course.school, course .no, course .desc, course.schedule});
  286. }
  287. else {
  288. return Ok(new { error=1,msg="课程不存在!"});
  289. }
  290. }
  291. [ProducesDefaultResponseType]
  292. [HttpPost("get-classroom-list")]
  293. [ApiToken(Auth = "9", Name = "获取物理教室列表信息", RW = "R", Limit = false)]
  294. public async Task<IActionResult> GetClassroomList(JsonElement json)
  295. {
  296. var client = _azureCosmos.GetCosmosClient();
  297. var (id, school) = HttpContext.GetApiTokenInfo();
  298. json.TryGetProperty("periodId", out JsonElement periodId);
  299. json.TryGetProperty("subjectId", out JsonElement subjectId);
  300. StringBuilder sql = new StringBuilder($"SELECT c.id,c.name,c.subject,c.period,c.scope,c.no,c.school FROM c where 1=1 ");
  301. if (!string.IsNullOrWhiteSpace($"{periodId}"))
  302. {
  303. sql.Append($" and c.period.id='{periodId}'");
  304. }
  305. if (!string.IsNullOrWhiteSpace($"{subjectId}"))
  306. {
  307. sql.Append($" and c.subject.id='{subjectId}'");
  308. }
  309. List<dynamic> courses = new List<dynamic>();
  310. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  311. GetItemQueryIterator<dynamic>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
  312. {
  313. courses.Add(item);
  314. }
  315. return Ok(new { courses });
  316. }
  317. }
  318. }