李昊 4 vuotta sitten
vanhempi
commit
36301b7732

+ 79 - 29
gx_api/GxPress/Service/GxPress.Service.Implement/Reply/ReplyService.Praise.cs

@@ -6,7 +6,7 @@ using GxPress.Common.Tools;
 using GxPress.EnumConst;
 using GxPress.Request.Reply;
 using GxPress.Result.Reply;
-
+using System.Transactions;
 namespace GxPress.Service.Implement.Reply
 {
     /// <summary>
@@ -311,7 +311,7 @@ namespace GxPress.Service.Implement.Reply
                                 AND a.CommentId = 0
                                 AND a.AnalyzeType = 1";
             count += await connection.ExecuteScalarAsync<int>(sql);
-             sql = $@"  SELECT 
+            sql = $@"  SELECT 
                                 count(1)
                                 FROM
                                     tede_analyze a
@@ -340,13 +340,15 @@ namespace GxPress.Service.Implement.Reply
         /// </summary>
         /// <param name="userId"></param>
         /// <returns></returns>
-        public async Task<int> UpdateUReadPraiseAsync(int userId)
+        public async Task<bool> UpdateUReadPraiseAsync(int userId)
         {
-            var sql = $@" UPDATE tede_analyze 
-                            SET 
-                                IsRead = 1
-                                    AND Id IN (
-             SELECT 
+            try
+            {
+
+
+                using (var tran = new TransactionScope())
+                {
+                    var sql = $@"SELECT 
                        a.Id
                     FROM
                         tede_analyze a
@@ -365,13 +367,22 @@ namespace GxPress.Service.Implement.Reply
                             AND a.AnalyzeType = 1
                             AND b.UserId = {userId}
                             AND a.IsRead = 0";
-            var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
-            var connection = database.GetConnection();
-            var count = await connection.ExecuteScalarAsync<int>(sql);
-            sql = $@"  UPDATE tede_analyze 
+                    var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
+                    var connection = database.GetConnection();
+                    var intList = await connection.QueryAsync<int>(sql);
+                    var sqlAnalyzeStr = string.Empty;
+                    foreach (var item in intList)
+                        sqlAnalyzeStr += $"{item},";
+                    sqlAnalyzeStr = !string.IsNullOrEmpty(sqlAnalyzeStr) ? sqlAnalyzeStr.Remove(sqlAnalyzeStr.Length - 1, 1) : string.Empty;
+                    if (!string.IsNullOrEmpty(sqlAnalyzeStr))
+                    {
+                        sql = $@"UPDATE tede_analyze 
                             SET 
                                 IsRead = 1
-                                    AND Id IN (
+                                    WHERE Id IN ({sqlAnalyzeStr}) and id>0";
+                        await connection.ExecuteScalarAsync<int>(sql);
+                    }
+                    sql = $@"
                     SELECT 
                         b.Id
                         FROM
@@ -386,14 +397,23 @@ namespace GxPress.Service.Implement.Reply
                             a.TypeValue IN ({AllTypeConst.Topic.GetHashCode()},{AllTypeConst.Notice.GetHashCode()},{AllTypeConst.Note.GetHashCode()})
                                 AND a.UserId ={userId}
                                 AND b.AnalyzeType = 2
-                                AND b.IsRead = 0)  and id>0) and id>0";
-            count += await connection.ExecuteScalarAsync<int>(sql);
-            sql = $@" 
-             UPDATE tede_analyze 
+                                AND b.IsRead = 0";
+                    intList = await connection.QueryAsync<int>(sql);
+                    sqlAnalyzeStr = string.Empty;
+                    foreach (var item in intList)
+                        sqlAnalyzeStr += $"{item},";
+                    sqlAnalyzeStr = !string.IsNullOrEmpty(sqlAnalyzeStr) ? sqlAnalyzeStr.Remove(sqlAnalyzeStr.Length - 1, 1) : string.Empty;
+                    if (!string.IsNullOrEmpty(sqlAnalyzeStr))
+                    {
+
+                        sql = $@"  UPDATE tede_analyze 
                             SET 
-                                IsRead = 1
-                                    AND Id IN (
-                         SELECT 
+                                IsRead = 1 
+                                    WHERE Id IN ({sqlAnalyzeStr}) and id>0";
+                        await connection.ExecuteScalarAsync<int>(sql);
+                    }
+                    sql = $@"
+                    SELECT 
                             a.Id
                         FROM
                             tede_analyze a
@@ -410,14 +430,24 @@ namespace GxPress.Service.Implement.Reply
                                 AND a.TypeValue ={AllTypeConst.Notice.GetHashCode()}
                                 AND e.IsDelete = 0
                                 AND a.CommentId = 0
-                                AND a.AnalyzeType = 1) and id>0";
-            count += await connection.ExecuteScalarAsync<int>(sql);
-             sql = $@" 
-              UPDATE tede_analyze 
+                                AND a.AnalyzeType = 1";
+                    intList = await connection.QueryAsync<int>(sql);
+                    sqlAnalyzeStr = string.Empty;
+                    foreach (var item in intList)
+                        sqlAnalyzeStr += $"{item},";
+                    sqlAnalyzeStr = !string.IsNullOrEmpty(sqlAnalyzeStr) ? sqlAnalyzeStr.Remove(sqlAnalyzeStr.Length - 1, 1) : string.Empty;
+                    if (!string.IsNullOrEmpty(sqlAnalyzeStr))
+                    {
+                        sql = $@" 
+             UPDATE tede_analyze 
                             SET 
                                 IsRead = 1
-                                    AND Id IN (
-                              SELECT 
+                                    WHERE Id IN ({sqlAnalyzeStr}) and id>0";
+                        await connection.ExecuteScalarAsync<int>(sql);
+                    }
+
+                    sql = $@"
+                           SELECT 
                                 a.Id
                                 FROM
                                     tede_analyze a
@@ -436,9 +466,29 @@ namespace GxPress.Service.Implement.Reply
                                         AND a.CommentId = 0
                                         AND e.UserId = b.UserId
                                         AND a.AnalyzeType = 1
-                                        AND b.UserId ={userId}) and id>0";
-            count += await connection.ExecuteScalarAsync<int>(sql);
-            return count;
+                                        AND b.UserId ={userId}";
+                    intList = await connection.QueryAsync<int>(sql);
+                    sqlAnalyzeStr = string.Empty;
+                    foreach (var item in intList)
+                        sqlAnalyzeStr += $"{item},";
+                    sqlAnalyzeStr = !string.IsNullOrEmpty(sqlAnalyzeStr) ? sqlAnalyzeStr.Remove(sqlAnalyzeStr.Length - 1, 1) : string.Empty;
+                    if (!string.IsNullOrEmpty(sqlAnalyzeStr))
+                    {
+                        sql = $@" 
+              UPDATE tede_analyze 
+                            SET 
+                                IsRead = 1
+                                    WHERE Id IN ({sqlAnalyzeStr}) and id>0";
+                        await connection.ExecuteScalarAsync<int>(sql);
+                    }
+                    tran.Complete();
+                }
+            }
+            catch
+            {
+                return false;
+            }
+            return true;
         }
     }
 }

+ 1 - 1
gx_api/GxPress/Service/GxPress.Service.Interface/Reply/IReplyService.cs

@@ -61,6 +61,6 @@ namespace GxPress.Service.Interface.Reply
         /// </summary>
         /// <param name="userId"></param>
         /// <returns></returns>
-        Task<int> UpdateUReadPraiseAsync(int userId);
+        Task<bool> UpdateUReadPraiseAsync(int userId);
     }
 }