RoomController.cs 4.6 KB

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