Procházet zdrojové kódy

更新,保存对象时ttl置为空,防止误操作。&课程安排加上学期编码

CrazyIter před 4 roky
rodič
revize
d75a8bf521

+ 67 - 1
TEAMModelOS.SDK/Module/AzureCosmosDBV3/AzureCosmosDBV3Repository.cs

@@ -288,7 +288,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         {
             CosmosModelInfo container = await InitializeCollection<T>();
             list.ForEach(x => { x.ttl = ttl; });
-            list = await SaveOrUpdateAll(list);
+            list = await DeleteTTlALL(list);
             if (container.cache && RedisHelper.Instance != null)
             {
                 list.ForEach(async x => {
@@ -297,7 +297,67 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             }
             return list;
         }
+        private async Task<List<T>> DeleteTTlALL<T>(List<T> enyites) where T : ID
+        {
+            //await Task.Run(() => Parallel.ForEach(entities, new ParallelOptions { MaxDegreeOfParallelism = 2 }, (item) =>
+            //{
+            //    Task.WaitAll(Update(item));
+            //}));
 
+            int pages = (int)Math.Ceiling((double)enyites.Count / pageSize);
+            CosmosModelInfo container = await InitializeCollection<T>();
+            bool flag = false;
+            if (RedisHelper.Exists(CacheCosmosPrefix + container.container.Id))
+            {
+                flag = true;
+            }
+            string partitionKey = GetPartitionKey<T>();
+            Type type = typeof(T);
+            Stopwatch stopwatch = Stopwatch.StartNew();
+            for (int i = 0; i < pages; i++)
+            {
+                List<T> lists = enyites.Skip((i) * pageSize).Take(pageSize).ToList();
+                List<KeyValuePair<PartitionKey, Stream>> itemsToInsert = new List<KeyValuePair<PartitionKey, Stream>>();
+                lists.ForEach(async x =>
+                {
+                    x.pk = type.Name;
+                    //x.ttl = null;
+                    MemoryStream stream = new MemoryStream();
+                    await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
+                    object o = type.GetProperty(partitionKey).GetValue(x, null);
+                    KeyValuePair<PartitionKey, Stream> keyValue = new KeyValuePair<PartitionKey, Stream>(new PartitionKey(o.ToString()), stream);
+                    itemsToInsert.Add(keyValue);
+                });
+                List<Task> tasks = new List<Task>(lists.Count);
+                itemsToInsert.ForEach(item =>
+                {
+                    tasks.Add(container.container.UpsertItemStreamAsync(item.Value, item.Key)
+                        .ContinueWith((Task<ResponseMessage> task) =>
+                        {
+                            //using (ResponseMessage response = task.Result)
+                            //{
+                            //    if (!response.IsSuccessStatusCode)
+                            //    {
+                            //    }
+                            //}
+                        }
+                        ));
+                });
+                await Task.WhenAll(tasks);
+                if (container.cache && RedisHelper.Instance != null)
+                {
+                    lists.ForEach(async x => {
+                        await RedisHelper.HSetAsync(CacheCosmosPrefix + container.container.Id, x.id, x);
+                    });
+                }
+            }
+            if (container.cache && RedisHelper.Instance != null && !flag)
+            {
+                await RedisHelper.ExpireAsync(CacheCosmosPrefix + container.container.Id, timeoutSeconds);
+            }
+            stopwatch.Stop();
+            return enyites;
+        }
 
         public async Task<List<IdPk>> DeleteAll<T>(List<IdPk> ids) where T : ID
         {
@@ -750,6 +810,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             {
                 CosmosModelInfo container = await InitializeCollection<T>();
                 entity.pk = container.type.Name;
+                entity.ttl = null;
                 ItemResponse<T> response = await container.container.CreateItemAsync<T>(entity);
                 if (container.cache && RedisHelper.Instance != null)
                 {
@@ -791,6 +852,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                 lists.ForEach(async x =>
                 {
                     x.pk = type.Name;
+                    x.ttl = null;
                     MemoryStream stream = new MemoryStream();
                     await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
                     object o = type.GetProperty(partitionKey).GetValue(x, null);
@@ -831,6 +893,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
         {
             CosmosModelInfo container = await InitializeCollection<T>();
             entity.pk = container.type.Name;
+            entity.ttl = null;
             ItemResponse<T> response = await container.container.UpsertItemAsync(item: entity);
             if (container.cache && RedisHelper.Instance != null)
             {
@@ -870,6 +933,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                 lists.ForEach(async x =>
                 {
                     x.pk = type.Name;
+                    x.ttl = null;
                     MemoryStream stream = new MemoryStream();
                     await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
                      object o = type.GetProperty(partitionKey).GetValue(x, null);
@@ -912,6 +976,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
             string partitionKey = GetPartitionKey<T>();
             Type type = typeof(T);
             entity.pk = type.Name;
+            entity.ttl = null;
             object o = type.GetProperty(partitionKey).GetValue(entity, null);
             ItemResponse<T> response = await container.container.ReplaceItemAsync(entity, entity.id, new PartitionKey(o.ToString()));
             if (container.cache && RedisHelper.Instance != null)
@@ -958,6 +1023,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDBV3
                 lists.ForEach(async x =>
                 {
                     x.pk = type.Name;
+                    x.ttl = null;
                     MemoryStream stream = new MemoryStream();
                     await JsonSerializer.SerializeAsync(stream, x, new JsonSerializerOptions { IgnoreNullValues = true });
                     object o = type.GetProperty(partitionKey).GetValue(x, null);

+ 4 - 0
TEAMModelOS.Service/Models/TeacherInfo/CoursePlan.cs

@@ -38,6 +38,10 @@ namespace TEAMModelOS.Service.Models
         /// 助教
         /// </summary>
         public List<Assistant> assistant { get; set; }
+        /// <summary>
+        /// 学期代码
+        /// </summary>
+        public string semesterCode { get; set; }
     }
 
     public class Assistant {