RoomController.cs 4.7 KB

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