李昊 před 4 roky
rodič
revize
3222d6a6eb

+ 141 - 86
gx_api/GxPress/Repository/GxPress.Repository.Implement/Note/NoteRepository.cs

@@ -157,89 +157,129 @@ namespace GxPress.Repository.Implement.Note
             var noteTypeId = request.TypeValue == 0 ? AllTypeConst.Note.GetHashCode() : AllTypeConst.TopicNote.GetHashCode();
             if (request.VisitUserId > 0)
             {
-                sql =
-                    $@"SELECT a.Id,a.MiddleId,a.RoleId,a.ParentId,a.AttributeValue,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId} and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT 
-                    COUNT(1)
-                FROM
-                    tede_analyze
-                WHERE
-                    UserId = {request.UserId} AND TypeValue ={noteTypeId}
-                        AND SourceId = a.MiddleId
-                        AND AnalyzeType = 1) AS PraiseCount,
-            (SELECT 
-                    COUNT(1)
-                FROM
-                    tede_analyze
-                WHERE
-                    UserId = {request.UserId} AND TypeValue ={noteTypeId}
-                        AND SourceId = a.MiddleId
-                        AND AnalyzeType = 1
-                LIMIT 0 , 1) AS IsPraise,
-            (SELECT 
-                    COUNT(1)
-                FROM
-                    tede_comment
-                WHERE
-                     ArticleId =  a.MiddleId and pid=0
-                        AND TypeValue = {noteTypeId}) AS CommentCount,
-            (SELECT 
-                    COUNT(1)
-                FROM
-                    tede_analyze
-                WHERE
-                    UserId = {request.UserId} AND TypeValue ={noteTypeId}
-                        AND SourceId = a.MiddleId
-                        AND AnalyzeType = 4) AS RetransmissionCount,
-            (SELECT 
-                    COUNT(1)
-                FROM
-                    tede_analyze
-                WHERE
-                    UserId = {request.UserId} AND TypeValue = {noteTypeId}
-                        AND SourceId = a.MiddleId
-                        AND AnalyzeType = 4
-                LIMIT 0 , 1) AS IsRetransmission,
-            (SELECT 
-                    COUNT(1)
-                FROM
-                    tede_analyze
-                WHERE
-                    UserId = {request.UserId} AND TypeValue = {noteTypeId}
-                        AND SourceId = a.MiddleId
-                        AND AnalyzeType = 3
-                LIMIT 0 , 1) AS IsCollect,
-            (SELECT 
-                    COUNT(1)
-                FROM
-                    tede_analyze
-                WHERE
-                    UserId = {request.UserId} AND TypeValue = {noteTypeId}
-                        AND SourceId = a.MiddleId
-                        AND AnalyzeType = 3) AS CollectCount,
-            (SELECT 
-                    COUNT(1)
-                FROM
-                    tede_middle
-        WHERE
-            ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
-    b.ChapterId,c.Name,c.AvatarUrl,c.Nick,d.Name FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id left join tede_user c on c.Id=b.UserId left join tede_department d on d.Id=c.DepartmentId
-                        WHERE a.UserId = {request.VisitUserId} AND a.FolderType ={noteTypeId} AND a.IsDelete = 0";
-                if (request.FolderId == 0)
-                    sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
+                 sql =
+                    $@"SELECT  a.Id,a.MiddleId,a.RoleId,a.ParentId,a.AttributeValue,a.UserId,
+                      a.CreatedDate,a.IsTop,a.FolderName,a.FolderType,
+                    (SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType={noteTypeId} 
+                    and DataSourceId=a.MiddleSonId) as EnjoyUser,
+                    (SELECT 
+                            COUNT(1)
+                        FROM
+                            tede_analyze
+                        WHERE
+                            UserId = {request.UserId} AND TypeValue = {noteTypeId}
+                                AND SourceId = a.MiddleId
+                                AND AnalyzeType = 1) AS PraiseCount,
+                        (SELECT 
+                                COUNT(1)
+                            FROM
+                                tede_analyze
+                            WHERE
+                                UserId = {request.UserId} AND TypeValue ={noteTypeId}
+                                    AND SourceId = a.MiddleId
+                                    AND AnalyzeType = 1
+                            LIMIT 0 , 1) AS IsPraise,
+                        (SELECT 
+                                COUNT(1)
+                            FROM
+                                tede_comment
+                            WHERE
+                                UserId = {request.UserId} AND ArticleId =  a.MiddleId
+                                    AND TypeValue = {noteTypeId}) AS CommentCount,
+                        (SELECT 
+                                COUNT(1)
+                            FROM
+                                tede_analyze
+                            WHERE
+                                UserId = {request.UserId} AND TypeValue = {noteTypeId}
+                                    AND SourceId = a.MiddleId
+                                    AND AnalyzeType = 4) AS RetransmissionCount,
+                        (SELECT 
+                                COUNT(1)
+                            FROM
+                                tede_analyze
+                            WHERE
+                                UserId = {request.UserId} AND TypeValue = {noteTypeId}
+                                    AND SourceId = a.MiddleId
+                                    AND AnalyzeType = 4
+                            LIMIT 0 , 1) AS IsRetransmission,
+                        (SELECT 
+                                COUNT(1)
+                            FROM
+                                tede_analyze
+                            WHERE
+                                UserId = {request.UserId} AND TypeValue ={noteTypeId}
+                                    AND SourceId = a.MiddleId
+                                    AND AnalyzeType = 3
+                            LIMIT 0 , 1) AS IsCollect,
+                        (SELECT 
+                                COUNT(1)
+                            FROM
+                                tede_analyze
+                            WHERE
+                                UserId = {request.UserId} AND TypeValue = {noteTypeId}
+                                    AND SourceId = a.MiddleId
+                                    AND AnalyzeType = 3) AS IsCollect,
+                        (SELECT 
+                                COUNT(1)
+                            FROM
+                                tede_middle
+                            WHERE
+                                ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,b.MediaId,b.CatalogId,
+                        b.ChapterId,c.Name,c.AvatarUrl,d.Name AS DepartmentName,
+                        (SELECT 
+                                FolderName
+                            FROM
+                                tede_middle
+                            WHERE
+                                id = (SELECT 
+                                        ParentId
+                                    FROM
+                                        tede_middle
+                                    WHERE
+                                        MiddleId = a.Id AND FolderType ={noteTypeId})) AS FolderParentName,
+                        (SELECT 
+                                Id
+                            FROM
+                                tede_middle
+                            WHERE
+                                id = (SELECT 
+                                        ParentId
+                                    FROM
+                                        tede_middle
+                                    WHERE
+                    MiddleId = a.Id AND FolderType = {noteTypeId})) AS FolderId
+                    FROM tede_middle a  LEFT JOIN tede_note b ON a.MiddleId = b.Id 
+                    left join tede_user c on c.Id=b.UserId 
+                    left join tede_department d on d.Id=c.DepartmentId 
+                      WHERE
+                      a.FolderType = {noteTypeId} AND a.userId = {request.VisitUserId}
+                        AND a.ParentId = 0
+                        AND a.IsDelete = 0
+                        AND (a.UserId IN (SELECT 
+                            Id
+                        FROM
+                            tede_user
+                        WHERE
+                            1 = a.RoleId)
+                        OR a.Id IN (SELECT 
+                            MiddleId
+                        FROM
+                            tede_folder_user
+                        WHERE
+                        UserId = {request.UserId}))";
+                if (request.TypeValue == 0)
+                    sql += "and  (a.RoleId in(2) or a.AttributeValue=1)";
                 else
-                    sql += $" and a.ParentId={request.FolderId}";
-                if (!string.IsNullOrEmpty(request.SearchKey))
-                    sql += $@"  AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
-                sql +=
-                    $@" OR a.id IN(SELECT MiddleId FROM tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType ={noteTypeId} AND a.UserId = {request.VisitUserId}";
+                    sql += "and  (a.RoleId in(1,3) or a.AttributeValue=1)";
                 if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
                     sql += $@"  AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
                 else if (!string.IsNullOrEmpty(request.SearchKey))
-                    sql += $@" and a.ParentId={request.FolderId}  AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
+                    sql += $@" and a.ParentId={request.FolderId}  AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%')";
                 else
                     sql += $" and a.ParentId={request.FolderId}";
                 sql +=
-                    $@" ORDER BY a.AttributeValue desc,a.IsTop DESC, a.LastModifiedDate DESC";
+                    $@" ORDER BY a.IsSystemDefault desc,a.AttributeValue desc,a.IsTop DESC,a.Sort DESC , a.LastModifiedDate DESC";
                 if (request.Page > 0)
                     sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
             }
@@ -371,20 +411,35 @@ namespace GxPress.Repository.Implement.Note
 
             if (request.VisitUserId > 0)
             {
-                sql =
-                    $@"SELECT  count(1) FROM tede_middle a LEFT JOIN tede_note b ON a.MiddleId = b.Id WHERE a.UserId = {request.VisitUserId} AND a.FolderType = {noteTypeId} AND a.IsDelete = 0";
-                if (request.FolderId == 0)
-                    sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
+               sql =
+                    $@"SELECT  count(1)
+                    FROM tede_middle a  LEFT JOIN tede_note b ON a.MiddleId = b.Id 
+                    left join tede_user c on c.Id=b.UserId 
+                    left join tede_department d on d.Id=c.DepartmentId 
+                    WHERE
+                      a.FolderType = {noteTypeId} AND a.userId = {request.VisitUserId}
+                        AND a.ParentId = 0
+                        AND a.IsDelete = 0
+                        AND (a.UserId IN (SELECT 
+                            Id
+                        FROM
+                            tede_user
+                        WHERE
+                            1 = a.RoleId)
+                        OR a.Id IN (SELECT 
+                            MiddleId
+                        FROM
+                            tede_folder_user
+                        WHERE
+                        UserId = {request.UserId}))";
+                if (request.TypeValue == 0)
+                    sql += "and  (a.RoleId in(2) or a.AttributeValue=1)";
                 else
-                    sql += $" and a.ParentId={request.FolderId}";
-                if (!string.IsNullOrEmpty(request.SearchKey))
-                    sql += $@" AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
-                sql +=
-                    $@" OR a.id IN(SELECT MiddleId FROM tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = {noteTypeId} AND a.UserId = {request.VisitUserId}";
+                    sql += "and  (a.RoleId in(1,3) or a.AttributeValue=1)";
                 if (request.FolderId == 0 && !string.IsNullOrEmpty(request.SearchKey))
                     sql += $@"  AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
                 else if (!string.IsNullOrEmpty(request.SearchKey))
-                    sql += $@" and a.ParentId={request.FolderId}  AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%' or b.UserId in (select Id from tede_user where name like '%{request.SearchKey}%'))";
+                    sql += $@" and a.ParentId={request.FolderId}  AND(b.Title LIKE '%{request.SearchKey}%' OR a.FolderName LIKE '%{request.SearchKey}%' or b.Content LIKE '%{request.SearchKey}%')";
                 else
                     sql += $" and a.ParentId={request.FolderId}";
             }