RoomController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.Models;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos;
  15. namespace TEAMModelOS.Controllers
  16. {
  17. [Route("school/room")]
  18. [ApiController]
  19. public class RoomController : ControllerBase
  20. {
  21. public readonly AzureCosmosFactory _azureCosmos;
  22. private readonly Option _option;
  23. private readonly DingDing _dingDing;
  24. public RoomController(AzureCosmosFactory azureCosmos, DingDing dingDing,
  25. IOptionsSnapshot<Option> option)
  26. {
  27. _azureCosmos = azureCosmos;
  28. _dingDing = dingDing;
  29. _option = option?.Value;
  30. }
  31. /// <summary>
  32. /// {
  33. /// "id":"教室id,新增时为空","code":"学校编码hbcn","name":"教室名称","no":"编号","x":坐标x,"y":坐标y,
  34. /// "sn":"序列号","openType":"1 教室属性,普通 /专设的教室","style":"TBL IRS 类型区分","area":"教学区,第一教学楼...."
  35. /// }
  36. /// </summary>
  37. /// <param name="request"></param>
  38. /// <returns></returns>
  39. [ProducesDefaultResponseType]
  40. //[AuthToken(Roles = "Teacher")]
  41. [HttpPost("upsert")]
  42. public async ValueTask<IActionResult> Upsert(Room request) {
  43. try
  44. {
  45. var client = _azureCosmos.GetCosmosClient();
  46. request.pk = "Room";
  47. request.code = request.pk + "-" + request.code;
  48. request.ttl = -1;
  49. if (string.IsNullOrEmpty(request.id))
  50. {
  51. request.id = Guid.NewGuid().ToString();
  52. request = await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(request, new PartitionKey($"{request.code}"));
  53. }
  54. else {
  55. var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(request.id, new PartitionKey($"{request.code}"));
  56. if (response.Status == 200)
  57. {
  58. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  59. var info = json.ToObject<Room>();
  60. request = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(request, info.id, new PartitionKey($"{info.code}"));
  61. }
  62. else
  63. {
  64. request = await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(request, new PartitionKey($"{request.code}"));
  65. }
  66. }
  67. return Ok(new { room= request });
  68. }
  69. catch (Exception e)
  70. {
  71. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/save()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  72. return BadRequest();
  73. }
  74. }
  75. /// <summary>
  76. /// {"code":"hbcn学校编码"}
  77. /// </summary>
  78. /// <param name="requert"></param>
  79. /// <returns></returns>
  80. [ProducesDefaultResponseType]
  81. //[AuthToken(Roles = "Teacher")]
  82. [HttpPost("find")]
  83. public async Task<IActionResult> Find(JsonElement request)
  84. {
  85. try
  86. {
  87. if (!request.TryGetProperty("code", out JsonElement code)) { return BadRequest(); }
  88. var client = _azureCosmos.GetCosmosClient();
  89. List<Room> rooms = new List<Room>() ;
  90. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Room>(queryText: $"select value(c) from c ",
  91. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Room-{code}") })) {
  92. rooms.Add(item);
  93. }
  94. return Ok(new { rooms= rooms });
  95. }
  96. catch (Exception ex)
  97. {
  98. await _dingDing.SendBotMsg($"OS,{_option.Location},school/room/find()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  99. return BadRequest();
  100. }
  101. }
  102. /// <summary>
  103. /// {"id":"教室id","code":"hbcn学校编码"}
  104. /// </summary>
  105. /// <param name="requert"></param>
  106. /// <returns></returns>
  107. [ProducesDefaultResponseType]
  108. [HttpPost("find-id")]
  109. public async Task<IActionResult> FindById(JsonElement requert)
  110. {
  111. try
  112. {
  113. var client = _azureCosmos.GetCosmosClient();
  114. //id
  115. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  116. //
  117. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  118. Room room = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<Room>(id.GetString(), new PartitionKey($"Room-{code}"));
  119. if (room != null)
  120. {
  121. return Ok(new { room });
  122. }
  123. else
  124. {
  125. return BadRequest("id,code不存在!");
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. await _dingDing.SendBotMsg($"OS,{_option.Location},school/room/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  131. return BadRequest();
  132. }
  133. }
  134. /// <summary>
  135. /// {"id":"教室id","code":"hbcn学校编码"}
  136. /// </summary>
  137. /// <param name="requert"></param>
  138. /// <returns></returns>
  139. [ProducesDefaultResponseType]
  140. [HttpPost("delete")]
  141. public async Task<IActionResult> Delete(JsonElement requert)
  142. {
  143. try
  144. {
  145. var client = _azureCosmos.GetCosmosClient();
  146. //id
  147. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  148. //
  149. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  150. var room= await client.GetContainer("TEAMModelOS", "School").DeleteItemAsync<Room>(id.GetString(), new PartitionKey($"Room-{code}"));
  151. return Ok();
  152. }
  153. catch (Exception ex)
  154. {
  155. await _dingDing.SendBotMsg($"OS,{_option.Location},school/room/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  156. return BadRequest();
  157. }
  158. }
  159. }
  160. }