浏览代码

绑定学校教室和产品

Li 3 年之前
父节点
当前提交
7e1432d17d
共有 1 个文件被更改,包括 81 次插入0 次删除
  1. 81 0
      TEAMModelBI/Controllers/BISchool/RoomController.cs

+ 81 - 0
TEAMModelBI/Controllers/BISchool/RoomController.cs

@@ -0,0 +1,81 @@
+using Azure.Cosmos;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Options;
+using System;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelBI.Tool.Extension;
+using TEAMModelOS.Models;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Models;
+
+namespace TEAMModelBI.Controllers.BISchool
+{
+    [Route("schoolroom")]
+    [ApiController]
+    public class RoomController : ControllerBase
+    {
+        //数据容器
+        private readonly AzureCosmosFactory _azureCosmos;
+        private readonly AzureStorageFactory _azureStorage;
+        //钉钉提示信息
+        private readonly DingDing _dingDing;
+        private readonly Option _option;
+        private readonly IConfiguration _configuration;
+
+        public RoomController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration) 
+        {
+            _azureCosmos= azureCosmos;
+            _azureStorage= azureStorage;
+            _dingDing= dingDing;
+            _option= option?.Value;
+            _configuration= configuration;
+        }
+
+        [HttpPost("set-bind")]
+        public async Task<IActionResult> SetBindProduct(JsonElement jsonElement) 
+        {
+            try
+            {
+                var (_tmdId, _tmdName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
+                if (!jsonElement.TryGetProperty("roomId", out JsonElement roomId)) return BadRequest();
+                if (!jsonElement.TryGetProperty("roomCode", out JsonElement roomCode)) return BadRequest();
+                jsonElement.TryGetProperty("serial", out JsonElement serial);
+                var s = string.IsNullOrEmpty($"{serial}") ? null : $"{serial}";
+
+                Room room = new Room();
+                var cosmosClient = _azureCosmos.GetCosmosClient();
+                var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{roomId}", new PartitionKey($"{roomCode}"));
+                if (response.Status == 200)
+                {
+                    using var json = await JsonDocument.ParseAsync(response.ContentStream);
+                    Room roomInfo = json.ToObject<Room>();
+                    roomInfo.serial = string.IsNullOrEmpty($"{serial}") ? null : $"{serial}";
+
+                    room = await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(roomInfo, $"{roomId}", new PartitionKey($"{roomCode}"));
+
+                    StringBuilder stringBudeler = new StringBuilder($"{_tmdName}【{_tmdId}】将{roomInfo.id}教师和{serial}产品");
+                    if (!string.IsNullOrEmpty($"{serial}")) { stringBudeler.Append($"绑定。"); } else { stringBudeler.Append($"解绑。"); }
+
+                    //保存操作记录
+                    await _azureStorage.SaveLog("room-bindProduct", $"{_tmdName}【{_tmdId}】将{roomInfo.id}教师和{serial}产品绑定", _dingDing, httpContext: HttpContext);
+                }
+                else
+                {
+                    return Ok(new { state = 401, msg = "未找到教室" });
+                }
+
+                return Ok(new { state = 200, room });
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"BI,{_option.Location},/schoolroom/set-bind \n{ex.Message}", GroupNames.成都开发測試群組);
+                return BadRequest();
+            }
+        }
+    }
+}