RoomController.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelBI.Tool.Extension;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models;
  15. namespace TEAMModelBI.Controllers.BISchool
  16. {
  17. [Route("schoolroom")]
  18. [ApiController]
  19. public class RoomController : ControllerBase
  20. {
  21. //数据容器
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. private readonly AzureStorageFactory _azureStorage;
  24. //钉钉提示信息
  25. private readonly DingDing _dingDing;
  26. private readonly Option _option;
  27. private readonly IConfiguration _configuration;
  28. public RoomController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration)
  29. {
  30. _azureCosmos= azureCosmos;
  31. _azureStorage= azureStorage;
  32. _dingDing= dingDing;
  33. _option= option?.Value;
  34. _configuration= configuration;
  35. }
  36. /// <summary>
  37. /// 产品序号和教室进行绑定
  38. /// </summary>
  39. /// <param name="jsonElement"></param>
  40. /// <returns></returns>
  41. [HttpPost("set-bind")]
  42. public async Task<IActionResult> SetBindProduct(JsonElement jsonElement)
  43. {
  44. try
  45. {
  46. var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  47. if (!jsonElement.TryGetProperty("roomId", out JsonElement roomId)) return BadRequest();
  48. if (!jsonElement.TryGetProperty("roomCode", out JsonElement roomCode)) return BadRequest();
  49. jsonElement.TryGetProperty("serial", out JsonElement serial);
  50. var s = string.IsNullOrEmpty($"{serial}") ? null : $"{serial}";
  51. Room room = new Room();
  52. var cosmosClient = _azureCosmos.GetCosmosClient();
  53. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{roomId}", new PartitionKey($"{roomCode}"));
  54. if (response.Status == 200)
  55. {
  56. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  57. Room roomInfo = json.ToObject<Room>();
  58. roomInfo.serial = string.IsNullOrEmpty($"{serial}") ? null : $"{serial}";
  59. room = await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(roomInfo, $"{roomId}", new PartitionKey($"{roomCode}"));
  60. StringBuilder stringBudeler = new StringBuilder($"{_tmdName}【{_tmdId}】将{roomInfo.id}教师和{serial}产品");
  61. if (!string.IsNullOrEmpty($"{serial}")) { stringBudeler.Append($"绑定。"); } else { stringBudeler.Append($"解绑。"); }
  62. //保存操作记录
  63. await _azureStorage.SaveBILog("room-bindProduct", $"{_tmdName}【{_tmdId}】将{roomInfo.id}教师和{serial}产品绑定", _dingDing, httpContext: HttpContext);
  64. }
  65. else
  66. {
  67. return Ok(new { state = 401, msg = "未找到教室" });
  68. }
  69. return Ok(new { state = 200, room });
  70. }
  71. catch (Exception ex)
  72. {
  73. await _dingDing.SendBotMsg($"BI,{_option.Location},/schoolroom/set-bind \n{ex.Message}", GroupNames.成都开发測試群組);
  74. return BadRequest();
  75. }
  76. }
  77. }
  78. }