Browse Source

调整数据结构

CrazyIter_Bin 4 năm trước cách đây
mục cha
commit
dcef94f20a

+ 1 - 1
TEAMModelOS.SDK/Models/Cosmos/Common/Inner/VoteRecord.cs

@@ -6,7 +6,7 @@ namespace TEAMModelOS.SDK.Models.Cosmos.Common.Inner
 {
     public class VoteRecord
     {
-        public List<string> opt { get; set; }
+        public Dictionary<string, int> opt { get; set; } = new Dictionary<string, int>();
         public long time { get; set; }
         public string userid { get; set; }
     }

+ 1 - 1
TEAMModelOS.SDK/Models/Cosmos/Common/Vote.cs

@@ -119,7 +119,7 @@ namespace TEAMModelOS.SDK.Models
         /// <summary>
         /// 得票数量
         /// </summary>
-        public int? count { get; set; } 
+        public int? count { get; set; } = 0;
     }
     
 }

+ 2 - 2
TEAMModelOS/Controllers/Common/VoteController.cs

@@ -393,7 +393,7 @@ namespace TEAMModelOS.Controllers.Learn
         /// <param name="request">
         /// !"id":"aaaa"
         /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
-        /// !"option":["A","B","B"]/["1","2","3"]
+        /// !"option":{"A":5,"B":6}/{"1":5,"2":8}
         /// </param>
         /// <returns>
         /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
@@ -417,7 +417,7 @@ namespace TEAMModelOS.Controllers.Learn
         /// <param name="request">
         /// !"id":"aaaa"
         /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
-        /// !"option":["A","B","B"]/["1","2","3"]
+        /// !"option":{"A":5,"B":6}/{"1":5,"2":8}
         /// !"userid":"15283771540"
         /// </param>
         /// <returns>

+ 1 - 0
TEAMModelOS/Controllers/Core/ImportController.cs

@@ -38,6 +38,7 @@ namespace TEAMModelOS.Controllers
     [ApiController]
     public class ImportController : ControllerBase
     {
+
         public PPTX2HTEXTranslator _PPTX2HTEXTranslator { get; set; }
         public DOXC2HTMLTranslator _DOXC2HTMLTranslator { get; set; }
 

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

@@ -13,12 +13,29 @@ namespace TEAMModelOS.Controllers.XTest
     [ApiController]
     public class TestController :ControllerBase
     {
+        private readonly AzureStorageFactory _azureStorage;
         private readonly AzureRedisFactory _azureRedis;
         private readonly AzureCosmosFactory _azureCosmos;
-        public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis) {
+        public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage) {
             _azureCosmos = azureCosmos;
             _azureRedis = azureRedis;
+            _azureStorage = azureStorage;
         }
+        /// <summary>
+        /// 测试blob多线程写入同一个文件
+        /// </summary>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpGet("multiple-blob")]
+        public async Task<IActionResult> MultipleBlob() {
+            List<Task<string>> tasks = new List<Task<string>>();
+            for (int i = 0; i < 100; i++) {
+                tasks.Add(_azureStorage.UploadFileByContainer("hbcn", $"{i}", "test", $"index.json"));
+            }
+            await Task.WhenAll(tasks);
+            return Ok();
+        }
+
 
         /// <summary>
         /// 测试redis通配符
@@ -27,7 +44,7 @@ namespace TEAMModelOS.Controllers.XTest
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [HttpGet("test-redis")]
-        public async Task<IActionResult> DictNull( ) {
+        public async Task<IActionResult> TestRedis( ) {
 
            var db  = _azureRedis.GetRedisClient(8);
             //var keys = server.Keys(pattern: "Vote*", database: db.Database);

+ 45 - 17
TEAMModelOS/Services/Common/ActivityStudentService.cs

@@ -38,10 +38,10 @@ namespace TEAMModelOS.Services.Common
             if (!request.TryGetProperty("code", out JsonElement code)) {
                 return msgid;
             }
-            List<string> option = new List<string>();
+            Dictionary<string, int> option = new Dictionary<string, int>();
             if (request.TryGetProperty("option", out JsonElement joption))
             {
-                option = joption.ToObject<List<string>>();
+                option = joption.ToObject<Dictionary<string, int>>();
                 if (option.IsEmpty())
                 {
                     msgid = 6;
@@ -127,15 +127,33 @@ namespace TEAMModelOS.Services.Common
             }
             return msgid;
         }
-        public static async Task<byte> VoteIng(Vote vote, RedisValue value, byte msgid, List<string> option, string Field, long curr, AzureRedisFactory _azureRedis,string userid)
+        public static async Task<byte> VoteIng(Vote vote, RedisValue value, byte msgid, Dictionary<string, int> option, string Field, long curr, AzureRedisFactory _azureRedis,string userid)
         {
             if (!value.IsNullOrEmpty)
             {
                 VoteRecord record=value.ToString().ToObject<VoteRecord>();
+                int addCount = 0;
+                foreach (var op in option) {
+                    addCount +=op.Value;
+                }
+                int crdCount = 0;
+                foreach (var op in record.opt)
+                {
+                    crdCount += op.Value;
+                }
                 //处理记录投票+当前设置的投票是否小于等于周期内最大投票数
-                if (record.opt.Count + option.Count <= vote.voteNum)
+                if (addCount + crdCount <= vote.voteNum)
                 {
-                    record.opt.AddRange(option);
+                    foreach (var op in option)
+                    {
+                        if (record.opt.ContainsKey(op.Key))
+                        {
+                            record.opt[op.Key] = record.opt[op.Key] + op.Value;
+                        }
+                        else {
+                            record.opt.Add(op.Key, op.Value);
+                        }
+                    }
                     record.time = curr;
                     record.userid = userid;
                     //保存投票记录
@@ -143,9 +161,9 @@ namespace TEAMModelOS.Services.Common
                     //单独保存每个人方便查询的记录
                     bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}_{vote.code}:{userid}", Field, record.ToJsonString());
                     //当前投票分组计数存入活动的Redis
-                    var group_opt= option.GroupBy(x => x);
-                    foreach (var opt in group_opt) {
-                        await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}_{vote.code}", opt.Key, opt.Count());
+                    foreach (var opt in option)
+                    {
+                        await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}_{vote.code}", opt.Key, opt.Value);
                     }
                     msgid = 1;
                 }
@@ -164,10 +182,9 @@ namespace TEAMModelOS.Services.Common
                     //单独保存每个人方便查询的记录
                     bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}_{vote.code}:{userid}", Field, record.ToJsonString());
                     //当前投票分组计数存入活动的Redis
-                    var group_opt = option.GroupBy(x => x);
-                    foreach (var opt in group_opt)
+                    foreach (var opt in option)
                     {
-                        await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}_{vote.code}", opt.Key, opt.Count());
+                        await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}_{vote.code}", opt.Key, opt.Value);
                     }
                     if (status)
                     {
@@ -501,16 +518,24 @@ namespace TEAMModelOS.Services.Common
                                     var value= azureRedis.GetRedisClient(8).HashGet($"Survey:Record:{survey.id}_{survey.code}",index);
                                     if (value != default && !value.IsNullOrEmpty)
                                     {
-                                        Dictionary<string, int> dt = value.ToString().ToObject<Dictionary<string, int>>();
+                                        RdsRecord dt = value.ToString().ToObject<RdsRecord>();
                                         foreach (var kp in dict)
-                                        {
-                                            if (dt.ContainsKey(kp.Key))
+                                        {   //不建议放在reids
+                                            if (dt.srecord.ContainsKey(kp.Key))
+                                            {
+                                                dt.srecord[kp.Key] = dt.srecord[kp.Key] + kp.Value;
+                                            }
+                                            else
                                             {
-                                                dt[kp.Key] = dt[kp.Key] + kp.Value;
+                                                dt.srecord.Add(kp.Key, kp.Value);
+                                            }
+                                            if (dt.urecord.ContainsKey(userid))
+                                            {
+                                                dt.urecord[userid] = recs[index].ToArray();
                                             }
                                             else
                                             {
-                                                dt.Add(kp.Key, kp.Value);
+                                                dt.urecord.Add(userid, recs[index].ToArray());
                                             }
                                         }
                                         await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}_{survey.code}", index, dt.ToJsonString());
@@ -545,7 +570,10 @@ namespace TEAMModelOS.Services.Common
             }
             return msgid;
         }
-
+        public class RdsRecord {
+            public Dictionary<string, int> srecord { get; set; } = new Dictionary<string, int>();
+            public Dictionary<string, string[]> urecord { get; set; } = new Dictionary<string, string[]>();
+        }
         private async static Task<dynamic> DoVoteTips(ActivityData commonData , AzureCosmosFactory _azureCosmos,string userid,AzureRedisFactory _azureRedis)
         {
             Vote vote=null;