RoomController.cs 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.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. if ($"{site}".Equals(BIConst.Global))
  61. {
  62. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  63. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  64. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  65. }
  66. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{roomId}", new PartitionKey($"{roomCode}"));
  67. if (response.Status == 200)
  68. {
  69. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  70. Room roomInfo = json.ToObject<Room>();
  71. roomInfo.serial = string.IsNullOrEmpty($"{serial}") ? null : $"{serial}";
  72. room = await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(roomInfo, $"{roomId}", new PartitionKey($"{roomCode}"));
  73. StringBuilder stringBudeler = new($"{_tmdName}【{_tmdId}】将{roomInfo.id}教师和{serial}产品");
  74. if (!string.IsNullOrEmpty($"{serial}")) { stringBudeler.Append($"绑定。"); } else { stringBudeler.Append($"解绑。"); }
  75. //保存操作记录
  76. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "room-update", $"{_tmdName}【{_tmdId}】将{roomInfo.id}教师和{serial}产品绑定", _dingDing, httpContext: HttpContext);
  77. }
  78. else
  79. return Ok(new { state = 401, msg = "未找到教室" });
  80. return Ok(new { state = 200, room });
  81. }
  82. catch (Exception ex)
  83. {
  84. await _dingDing.SendBotMsg($"BI,{_option.Location},/schoolroom/set-bind \n{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  85. return BadRequest();
  86. }
  87. }
  88. }
  89. }