RoomController.cs 3.8 KB

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