BusinessController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using Microsoft.Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Newtonsoft.Json;
  5. using StackExchange.Redis;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Net.Http;
  12. using System.Net.Http.Json;
  13. using System.Text;
  14. using System.Text.Json;
  15. using System.Threading.Tasks;
  16. using TEAMModelOS.SDK.DI;
  17. using TEAMModelOS.SDK.Extension;
  18. using TEAMModelOS.SDK.Models;
  19. using TEAMModelOS.SDK.Models.Cosmos;
  20. using TEAMModelOS.SDK.Models.Cosmos.Common;
  21. using Azure.Storage.Blobs.Models;
  22. using Microsoft.AspNetCore.Authorization;
  23. namespace TEAMModelOS.Controllers.XTest
  24. {
  25. [Route("business-test")]
  26. [ApiController]
  27. public class BusinessController : ControllerBase
  28. {
  29. private readonly AzureCosmosFactory _azureCosmos;
  30. private readonly AzureStorageFactory _azureStorage;
  31. public BusinessController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing)
  32. {
  33. _azureCosmos = azureCosmos;
  34. _azureStorage = azureStorage;
  35. }
  36. /// <summary>
  37. ///
  38. /// </summary>
  39. /// <param name="request"></param>
  40. /// <returns></returns>
  41. //[ProducesDefaultResponseType]
  42. //[HttpPost("get-schools")]
  43. //public async Task<IActionResult> GetSchools(JsonElement request) {
  44. // var client = _azureCosmos.GetCosmosClient();
  45. // string sql = $"select c.id as code,c.name,c.picture from c ";
  46. // List<dynamic> dynamics = new List<dynamic>();
  47. // await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  48. // {
  49. // dynamics.Add(item);
  50. // }
  51. // return Ok(new { schools= dynamics,status=200});
  52. //}
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. /// <param name="request"></param>
  57. /// <returns></returns>
  58. [ProducesDefaultResponseType]
  59. [HttpPost("get-school-info")]
  60. public async Task<IActionResult> GetSchoolInfo(JsonElement request)
  61. {
  62. if (!request.TryGetProperty("id", out JsonElement _code))
  63. {
  64. return BadRequest();
  65. }
  66. var client = _azureCosmos.GetCosmosClient();
  67. try
  68. {
  69. // string sql = $"select c.id ,c.code ,c.name from c ";
  70. School school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{_code}", partitionKey: new PartitionKey("Base"));
  71. //List<dynamic> knowledges = new List<dynamic>();
  72. //var sbjs= school.period.SelectMany(x => x.subjects);
  73. //List<Subject> subjects = new List<Subject>();
  74. //if (sbjs != null) {
  75. // subjects.AddRange(sbjs);
  76. //}
  77. //foreach (var sub in subjects) {
  78. // await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Knowledge-{school.id}-{sub.id}") }))
  79. // {
  80. // knowledges.Add(item);
  81. // }
  82. //}
  83. //List<dynamic> exams = new List<dynamic>();
  84. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Exam-{school.id}") }))
  85. //{
  86. // exams.Add(item);
  87. //}
  88. //List<dynamic> votes = new List<dynamic>();
  89. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Vote-{school.id}") }))
  90. //{
  91. // votes.Add(item);
  92. //}
  93. //List<dynamic> surveys = new List<dynamic>();
  94. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Survey-{school.id}") }))
  95. //{
  96. // surveys.Add(item);
  97. //}
  98. //List<dynamic> courses = new List<dynamic>();
  99. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Course-{school.id}") }))
  100. //{
  101. // courses.Add(item);
  102. //}
  103. //List<dynamic> classes = new List<dynamic>();
  104. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Class-{school.id}") }))
  105. //{
  106. // classes.Add(item);
  107. //}
  108. //List<dynamic> stulist = new List<dynamic>();
  109. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList-{school.id}") }))
  110. //{
  111. // stulist.Add(item);
  112. //}
  113. //List<dynamic> rooms = new List<dynamic>();
  114. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Room-{school.id}") }))
  115. //{
  116. // rooms.Add(item);
  117. //}
  118. //List<dynamic> volumes = new List<dynamic>();
  119. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>( requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Volume-{school.id}") }))
  120. //{
  121. // volumes.Add(item);
  122. //}
  123. return Ok(new {id= school.id,name=school.name, picture=school.picture });
  124. }
  125. catch (CosmosException ex)
  126. {
  127. return Ok(new { status = ex.StatusCode });
  128. }
  129. catch (Exception _) {
  130. return Ok(new { status = 500 });
  131. }
  132. }
  133. [ProducesDefaultResponseType]
  134. [HttpPost("get-student-info")]
  135. public async Task<IActionResult> GetStudentInfo(JsonElement request) {
  136. if (!request.TryGetProperty("id", out JsonElement _id))
  137. {
  138. return BadRequest();
  139. }
  140. if (!request.TryGetProperty("code", out JsonElement _code))
  141. {
  142. return BadRequest();
  143. }
  144. var client = _azureCosmos.GetCosmosClient();
  145. try
  146. {
  147. Student student = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<Student>($"{_id}", partitionKey: new PartitionKey($"Base-{_code}"));
  148. return Ok(new { id = student.id, name = student.name, picture = student.picture });
  149. }
  150. catch (CosmosException ex)
  151. {
  152. return Ok(new { status = ex.StatusCode });
  153. }
  154. catch (Exception _)
  155. {
  156. return Ok(new { status = 500 });
  157. }
  158. }
  159. /// <summary>
  160. ///
  161. /// </summary>
  162. /// <param name="request"></param>
  163. /// <returns></returns>
  164. [ProducesDefaultResponseType]
  165. [HttpPost("get-teacher-info")]
  166. public async Task<IActionResult> GetTeacherInfo(JsonElement request)
  167. {
  168. if (!request.TryGetProperty("id", out JsonElement _code))
  169. {
  170. return BadRequest();
  171. }
  172. var client = _azureCosmos.GetCosmosClient();
  173. try
  174. {
  175. //string sql = $"select c.id ,c.code ,c.name from c ";
  176. Teacher teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{_code}", partitionKey: new PartitionKey("Base"));
  177. //List<dynamic> exams = new List<dynamic>();
  178. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Exam-{teacher.id}") }))
  179. //{
  180. // exams.Add(item);
  181. //}
  182. //List<dynamic> votes = new List<dynamic>();
  183. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Vote-{teacher.id}") }))
  184. //{
  185. // votes.Add(item);
  186. //}
  187. //List<dynamic> surveys = new List<dynamic>();
  188. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Survey-{teacher.id}") }))
  189. //{
  190. // surveys.Add(item);
  191. //}
  192. //List<dynamic> courses = new List<dynamic>();
  193. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Course-{teacher.id}") }))
  194. //{
  195. // courses.Add(item);
  196. //}
  197. //List<dynamic> stulist = new List<dynamic>();
  198. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"StuList-{teacher.id}") }))
  199. //{
  200. // stulist.Add(item);
  201. //}
  202. //List<dynamic> volumes = new List<dynamic>();
  203. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIteratorSql<dynamic>(requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Volume-{teacher.id}") }))
  204. //{
  205. // volumes.Add(item);
  206. //}
  207. return Ok(new { id = teacher.id, name = teacher.name, picture = teacher.picture });
  208. }
  209. catch (CosmosException ex)
  210. {
  211. return Ok(new { status = ex.StatusCode });
  212. }
  213. catch (Exception _)
  214. {
  215. return Ok(new { status = 500 });
  216. }
  217. }
  218. //取的特定Blob容器的使用量
  219. [HttpPost("get-blob-list-size")]
  220. [Authorize(Roles = "IES")]
  221. public async Task<IActionResult> GetBlobListSize(JsonElement json)
  222. {
  223. if (!json.TryGetProperty("container", out JsonElement containerName))
  224. {
  225. return Ok(new { msg = "参数错误" });
  226. }
  227. //文件夹(关联id)导向的文件夹 exam homework art records survey vote item
  228. string[] prefixDirId = new string[] { "exam", "homework", "art", "records", "survey", "vote", "item" };
  229. HashSet<string> ids = new HashSet<string>();
  230. Dictionary<string, List<KeyValuePair<string, long?>>> recordUrls = new Dictionary<string, List<KeyValuePair<string, long?>>>(); //單位:bytes
  231. foreach (string prefix in prefixDirId)
  232. {
  233. await foreach (BlobItem blobItem in _azureStorage.GetBlobContainerClient(containerName.ToString()).GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: prefix))
  234. {
  235. var path = blobItem.Name.Split("/");
  236. if (path.Length > 2)
  237. {
  238. string id = path[0];
  239. ids.Add(id);
  240. if (recordUrls.ContainsKey(id))
  241. {
  242. recordUrls[id].Add(new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength));
  243. }
  244. else
  245. {
  246. recordUrls[id] = new List<KeyValuePair<string, long?>> { new KeyValuePair<string, long?>(blobItem.Name, blobItem.Properties.ContentLength) };
  247. }
  248. }
  249. }
  250. }
  251. //資料整理
  252. Dictionary<string, long> result = new Dictionary<string, long>();
  253. foreach (KeyValuePair<string, List<KeyValuePair<string, long?>>> rec in recordUrls)
  254. {
  255. foreach (KeyValuePair<string, long?> recVal in rec.Value)
  256. {
  257. if (result.ContainsKey(rec.Key))
  258. {
  259. result[rec.Key] += (long)Convert.ToDouble(recVal.Value);
  260. }
  261. else
  262. {
  263. result.Add(rec.Key, (long)Convert.ToDouble(recVal.Value));
  264. }
  265. }
  266. }
  267. return Ok(new { result });
  268. }
  269. }
  270. }