Browse Source

采用队列方式计算题目数量。

CrazyIter_Bin 3 years ago
parent
commit
b9ce81b037

+ 39 - 0
TEAMModelFunction/MonitorServicesBus.cs

@@ -15,6 +15,7 @@ using TEAMModelOS.SDK;
 using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Models.Cosmos;
 using TEAMModelOS.SDK.Models.Cosmos.Common;
+using TEAMModelOS.Services.Common;
 
 namespace TEAMModelFunction
 {
@@ -387,6 +388,44 @@ namespace TEAMModelFunction
             }
         }
 
+        [FunctionName("ItemCond")]
+        public async Task ItemCond([ServiceBusTrigger("itemcond", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
+        {
+            try
+            {
+                var jsonMsg = JsonDocument.Parse(msg);
+                ItemCondDto itemCondDto = msg.ToObject<ItemCondDto>();
+                var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"{itemCondDto.key}", $"{itemCondDto.filed}");
+                ItemCond itemCond = null;
+                var client = _azureCosmos.GetCosmosClient();
+                string containerId = itemCondDto.scope.Equals("private") ? "Teacher" : "School";
+                if (value != default && !value.IsNullOrEmpty)
+                {
 
+                    itemCond = value.ToString().ToObject<ItemCond>();
+                }
+                else {
+                    try
+                    {
+                        itemCond = await client.GetContainer(Constant.TEAMModelOS, containerId).ReadItemAsync<ItemCond>(itemCondDto.filed, new PartitionKey($"{itemCondDto.key}"));
+                    }
+                    catch (CosmosException)
+                    {
+                        itemCond = new ItemCond() { id = itemCondDto.filed, code = $"{itemCondDto.key}", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
+                    };
+                }
+                ItemService.CountItemCond(itemCondDto.newItem, itemCondDto.odlItem, itemCond);
+                await client.GetContainer(Constant.TEAMModelOS, containerId).UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
+                await _azureRedis.GetRedisClient(8).HashSetAsync($"{itemCondDto.key}",$"{itemCondDto.filed}", itemCond.ToJsonString());
+            }
+            catch (CosmosException ex )
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ItemCond()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ItemCond()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
+            }
+        }
     }
 }

+ 3 - 3
TEAMModelOS.SDK/Models/Cosmos/BI/DingDingUser.cs

@@ -1,4 +1,4 @@
-using System;
+using System;
 using System.Collections.Generic;
 using System.Text;
 
@@ -25,8 +25,8 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         public string deptPositionListTitle { get; set; }
         public string hiredDate { get; set; }
         public bool active { get; set; }
-        public List<rolelist> roleList { get; set; }
-        public class rolelist
+        public List<Role> roles { get; set; }
+        public class Role
         {
             public string id { get; set; }
             public string name { get; set; }

+ 15 - 0
TEAMModelOS.SDK/Models/Dtos/ItemCondDto.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Models
+{
+    public  class ItemCondDto
+    {
+        public ItemInfo newItem { get; set; }
+        public ItemInfo odlItem { get; set; }
+        public string key { get; set; }
+        public string filed { get; set; }
+        public string scope { get; set; }
+    }
+}

+ 21 - 82
TEAMModelOS/Controllers/Item/ItemController.cs

@@ -127,32 +127,18 @@ namespace TEAMModelOS.Controllers
                     {
                         if (itemInfo.scope.Equals("private"))
                         {
+                            ItemCondDto itemCondDto = new ItemCondDto { newItem = itemInfo, key = "ItemCond", filed = itemInfo.code.Replace("Item-", ""), scope = "private" };
+                            var messageBlobItemCond = new ServiceBusMessage(itemCondDto.ToJsonString());
+                            await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlobItemCond);
                             itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
-                            string condId = itemInfo.code.Replace("Item-","");
-                            ItemCond itemCond = null;
-                            try
-                            {
-                                itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>(condId, new PartitionKey("ItemCond"));
-                            }
-                            catch (Exception)
-                            {
-                                itemCond = new ItemCond() { id = condId, code = $"ItemCond", pk = "ItemCond", ttl = -1, count = 0 };
-                            };
-                            ItemService.CountItemCond(itemInfo, null, itemCond);
-                            await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
                         }
                         else
                         {
-                            // itemInfo.periodId
+                            ItemCondDto itemCondDto = new ItemCondDto { newItem = itemInfo, key = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", filed = itemInfo.periodId, scope = "school" };
+                            var messageBlobItemCond = new ServiceBusMessage(itemCondDto.ToJsonString());
+                            await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlobItemCond);
                             itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
-                            ItemCond itemCond = null;
-                            try {
-                                itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>(itemInfo.periodId, new PartitionKey($"ItemCond-{itemInfo.code.Replace("Item-", "")}"));
-                            } catch (Exception  ) {
-                                itemCond = new ItemCond() { id = itemInfo.periodId, code = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
-                            };
-                            ItemService.CountItemCond(itemInfo, null, itemCond);
-                            await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
+                          
                         }
                     }
                 }
@@ -166,22 +152,13 @@ namespace TEAMModelOS.Controllers
                             itemInfo.code = "Item-" + itemInfo.code;
                         }
                         string condId = itemInfo.code.Replace("Item-", "");
-                        itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
+                     
                         ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
-                        //更新题目数量
-                        ItemCond itemCond = null;
-                        try
-                        {
-
-                            itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>(condId, new PartitionKey("ItemCond"));
-                        }
-                        catch (Exception)
-                        {
-                            itemCond = new ItemCond() { id = condId, code = $"ItemCond", pk = "ItemCond", ttl = -1, count = 0};
+                        ItemCondDto itemCondDto = new ItemCondDto { newItem = itemInfo,odlItem=olditemInfo, key = "ItemCond", filed = itemInfo.code.Replace("Item-", ""), scope = "private" };
+                        var messageBlobItemCond = new ServiceBusMessage(itemCondDto.ToJsonString());
+                        await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlobItemCond);
+                        itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
 
-                        };
-                        ItemService.CountItemCond(itemInfo, olditemInfo, itemCond);
-                        await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
                     }
                     else
                     {
@@ -190,21 +167,10 @@ namespace TEAMModelOS.Controllers
                             itemInfo.code = "Item-" + itemInfo.code;
                         }
                         ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>( itemInfo.id, new PartitionKey($"{itemInfo.code}"));
+                        ItemCondDto itemCondDto = new ItemCondDto { newItem = itemInfo,odlItem=olditemInfo, key = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", filed = itemInfo.periodId, scope = "school" };
+                        var messageBlobItemCond = new ServiceBusMessage(itemCondDto.ToJsonString());
+                        await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlobItemCond);
                         itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
-                        //更新题目数量
-                        ItemCond itemCond = null;
-                        try
-                        {
-
-                            itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>(itemInfo.periodId, new PartitionKey($"ItemCond-{itemInfo.code.Replace("Item-", "")}"));
-                        }
-                        catch (Exception )
-                        {
-                            itemCond = new ItemCond() { id = itemInfo.periodId, code = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(),subjects= new List<SubjectCount>() };
-
-                        };
-                        ItemService.CountItemCond(itemInfo, olditemInfo, itemCond);
-                        await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
 
                     }
                 }
@@ -215,7 +181,6 @@ namespace TEAMModelOS.Controllers
                 await _dingDing.SendBotMsg($"OS,{_option.Location},item/upsert()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
                 return BadRequest();
             }
-
         }
         /// <summary>
         //获取题目摘要信息
@@ -330,47 +295,21 @@ namespace TEAMModelOS.Controllers
                 if (scope.ToString().Equals("school"))
                 {
                     ItemInfo itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(id.ToString(), new PartitionKey($"{code}"));
-                    //更新题目数量
-                    ItemCond itemCond = null;
-                    try
-                    {
-
-                        itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>(itemInfo.periodId, new PartitionKey($"ItemCond-{itemInfo.code.Replace("Item-", "")}"));
-                    }
-                    catch (Exception )
-                    {
-                        itemCond = new ItemCond() { id = itemInfo.periodId, code = $"ItemCond-hbcn", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
-
-                    };
-                    ItemService.CountItemCond(null, itemInfo, itemCond);
-                    await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
+                    ItemCondDto itemCondDto = new ItemCondDto {  odlItem = itemInfo, key = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", filed = itemInfo.periodId, scope = "school" };
+                    var messageBlobItemCond = new ServiceBusMessage(itemCondDto.ToJsonString());
+                    await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlobItemCond);
                     var response = await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
                     return Ok(new { code = response.Status });
                 }
                 else
                 {
-
                     ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>($"{id}", new PartitionKey($"{code}"));
-                    //更新题目数量
-                    ItemCond itemCond = null;
-                    string condId = code.ToString().Replace("Item-", "");
-                    try
-                    {
-
-                        itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>(condId, new PartitionKey("ItemCond"));
-                    }
-                    catch (Exception)
-                    {
-                        itemCond = new ItemCond() { id = condId, code = $"ItemCond", pk = "ItemCond", ttl = -1, count = 0 };
-
-                    };
-                    ItemService.CountItemCond(null, olditemInfo, itemCond);
-                    await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
+                    ItemCondDto itemCondDto = new ItemCondDto {  odlItem = olditemInfo, key = "ItemCond", filed = olditemInfo.code.Replace("Item-", ""), scope = "private" };
+                    var messageBlobItemCond = new ServiceBusMessage(itemCondDto.ToJsonString());
+                    await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlobItemCond);
                     var response = await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
                     return Ok(new { code = response.Status });
                 }
-              
-               
             }
             catch (Exception e)
             {

+ 4 - 0
TEAMModelOS/Controllers/School/CourseController.cs

@@ -97,6 +97,10 @@ namespace TEAMModelOS.Controllers
                             CourseChange courseChange = new CourseChange { id = course.id, code = course.code, name = course.name, scope = course.scope, school = course.school, creatorId = course.creatorId };
                             courseChange.addClass = course.schedule.Select(x => x.classId).ToList();
                             courseChange.addList = course.schedule.Select(x => x.stulist).ToList();
+                            var messageBlob = new ServiceBusMessage(courseChange.ToJsonString()); ;
+                            messageBlob.ApplicationProperties.Add("name", "Course");
+                            var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
+                            await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
                             course = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(course, new PartitionKey($"Course-{code}"));
                         }
                     }

+ 19 - 4
TEAMModelOS/Controllers/XTest/TestController.cs

@@ -1,6 +1,8 @@
 using Azure.Cosmos;
+using Azure.Messaging.ServiceBus;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.Extensions.Configuration;
 using Newtonsoft.Json;
 using StackExchange.Redis;
 using System;
@@ -30,14 +32,27 @@ namespace TEAMModelOS.Controllers
         private readonly AzureRedisFactory _azureRedis;
         private readonly AzureCosmosFactory _azureCosmos;
         private readonly DingDing _dingDing;
-       
-
-        public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing) {
+        private readonly AzureServiceBusFactory _serviceBus;
+        public IConfiguration _configuration { get; set; }
+        public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IConfiguration configuration, AzureServiceBusFactory serviceBus, DingDing dingDing) {
             _azureCosmos = azureCosmos;
             _azureRedis = azureRedis;
             _azureStorage = azureStorage;
-            _dingDing = dingDing;
+            _dingDing = dingDing; 
+            _serviceBus = serviceBus; _configuration = configuration;
+        }
+        /// <summary>
+        /// 测试blob多线程写入同一个文件
+        /// </summary>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("servicebus")]
+        public async Task<IActionResult> Servicebus(ItemCondDto jsonMsg) {
+            var messageBlob = new ServiceBusMessage(jsonMsg.ToJsonString()); 
+            await _serviceBus.GetServiceBusClient().SendMessageAsync("itemcond", messageBlob);
+            return Ok();
         }
+
         /// <summary>
         /// 测试blob多线程写入同一个文件
         /// </summary>

+ 3 - 3
TEAMModelOS/TEAMModelOS.csproj

@@ -37,9 +37,9 @@
     <SpaRoot>ClientApp\</SpaRoot>
     <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
     <UserSecretsId>078b5d89-7d90-4f6a-88fc-7d96025990a8</UserSecretsId>
-    <Version>5.2110.8</Version>
-    <AssemblyVersion>5.2110.8.1</AssemblyVersion>
-    <FileVersion>5.2110.8.1</FileVersion>
+    <Version>5.2110.11</Version>
+    <AssemblyVersion>5.2110.11.1</AssemblyVersion>
+    <FileVersion>5.2110.11.1</FileVersion>
     <Description>TEAMModelOS(IES5)</Description>
     <PackageReleaseNotes>版本说明</PackageReleaseNotes>
   </PropertyGroup>