lihao 4 years ago
parent
commit
a21d539586

+ 68 - 32
gx_api/GxPress/Repository/GxPress.Repository.Implement/Collection/CollectionRepository.cs

@@ -7,6 +7,7 @@ using Datory;
 using GxPress.Common.AppOptions;
 using GxPress.Common.Page;
 using GxPress.Common.Tools;
+using GxPress.EnumConst;
 using GxPress.Repository.Interface.Collection;
 using GxPress.Request.App.Collection;
 using GxPress.Result.App.Collection;
@@ -65,30 +66,72 @@ namespace GxPress.Repository.Implement.Collection
         /// <returns></returns>
         public async Task<PagedList<CollectionListPageResult>> PageListAsync(CollectionPageSearchRequest request)
         {
-            var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
-            var database = new Database(databaseType, _connectionString);
-            var connection = database.GetConnection();
-            var items = await connection
-                .QueryAsync<CollectionListPageResult, Entity.Collection, CollectionListPageResult>(GetCollectionSql(request),
-                    (collectionListPageResult, collection) =>
-                    {
-                        if (collectionListPageResult == null)
-                            throw new ArgumentNullException(nameof(collectionListPageResult));
-                        //collectionListPageResult= _mapper.Map<CollectionListPageResult>(middle);
-                        collectionListPageResult.Title = collection == null ? "" : collection.Title;
-                        collectionListPageResult.CollectionDataId = collection?.CollectionDataId ?? 0;
-                        collectionListPageResult.CollectionType = collection?.CollectionType ?? 0;
-                        collectionListPageResult.SourceUserId = collection?.SourceUserId ?? 0;
-                        collectionListPageResult.CollectionId = collection?.Id ?? 0;
-                        return collectionListPageResult;
-                    },
-                    splitOn: "MiddleId,Id");
+            // var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
+            // var database = new Database(databaseType, _connectionString);
+            // var connection = database.GetConnection();
+            // var items = await connection
+            //     .QueryAsync<CollectionListPageResult, Entity.Collection, CollectionListPageResult>(GetCollectionSql(request),
+            //         (collectionListPageResult, collection) =>
+            //         {
+            //             if (collectionListPageResult == null)
+            //                 throw new ArgumentNullException(nameof(collectionListPageResult));
+            //             //collectionListPageResult= _mapper.Map<CollectionListPageResult>(middle);
+            //             collectionListPageResult.Title = collection == null ? "" : collection.Title;
+            //             collectionListPageResult.CollectionDataId = collection?.CollectionDataId ?? 0;
+            //             collectionListPageResult.CollectionType = collection?.CollectionType ?? 0;
+            //             collectionListPageResult.SourceUserId = collection?.SourceUserId ?? 0;
+            //             collectionListPageResult.CollectionId = collection?.Id ?? 0;
+            //             return collectionListPageResult;
+            //         },
+            //         splitOn: "MiddleId,Id");
 
-            PagedList<CollectionListPageResult> result = new PagedList<CollectionListPageResult>
+            // var result = new PagedList<CollectionListPageResult>
+            // {
+            //     Total = await CountAsync(GetCollectionSqlCount(request)),
+            //     Items = items
+            // };
+            var result = new PagedList<CollectionListPageResult>();
+            var query = new SqlKata.Query().From("tede_middle");
+            query.LeftJoin("tede_collection", "tede_collection.Id", "tede_middle.MiddleId");
+            query.Where("tede_middle.UserId", request.VisitUserId > 0 ? request.VisitUserId : request.UserId);
+            query.Where("tede_middle.FolderType", GxPress.EnumConst.AllTypeConst.Collect.GetHashCode());
+            query.Where("tede_middle.IsDelete", false);
+            if (request.VisitUserId > 0)
+            {
+                var folderUserQuery = new SqlKata.Query("tede_folder_user");
+                folderUserQuery.Where("tede_folder_user.UserId", request.UserId);
+                folderUserQuery.WhereColumns("tede_folder_user.MiddleId", "=", "tede_middle.Id");
+                folderUserQuery.Select("tede_folder_user.MiddleId");
+                query.Where(n => n.WhereIn("tede_middle.RoleId", new List<int> { MiddleRoleConst.Publicity.GetHashCode(), MiddleRoleConst.Share.GetHashCode() }).OrWhereIn("tede_middle.Id", folderUserQuery));
+            }
+            if (!string.IsNullOrEmpty(request.SearchKey) && request.FolderId == 0)
+                query.Where(n => n.OrWhereLike("tede_collection.Title", $"%{request.SearchKey}%").OrWhereLike("tede_middle.FolderName", $"%{request.SearchKey}%"));
+            else
             {
-                Total = await CountAsync(GetCollectionSqlCount(request)),
-                Items = items
-            };
+                query.Where("tede_middle.ParentId", request.FolderId);
+                if (!string.IsNullOrEmpty(request.SearchKey))
+                    query.Where(n => n.OrWhereLike("tede_collection.Title", $"%{request.SearchKey}%").OrWhereLike("tede_middle.FolderName", $"%{request.SearchKey}%"));
+            }
+            result.Total = await _middleRepository.CountAsync(query);
+            query.ForPage(request.Page, request.PerPage);
+            query.OrderByDesc("tede_middle.AttributeValue");
+            query.OrderByDesc("tede_middle.IsTop");
+            query.OrderByDesc("tede_middle.Sort");
+            query.OrderByDesc("tede_middle.LastModifiedDate");
+            var enjoyUser = new SqlKata.Query("tede_user_middle");
+            enjoyUser.Where("MiddleType", AllTypeConst.Collect.GetHashCode());
+            enjoyUser.WhereColumns("DataSourceId", "=", "tede_middle.Id");
+            enjoyUser.SelectRaw("GROUP_CONCAT(SourceName Separator'、')");
+            query.Select(enjoyUser, "EnjoyUser");
+            var fileCount = new SqlKata.Query("tede_middle as a");
+            fileCount.WhereColumns("a.ParentId", "=", "tede_middle.Id");
+            fileCount.AsCount();
+            query.Select(fileCount, "FileCount");
+            query.Select(
+                "tede_middle.{Id,MiddleId,UserId,FolderName,FolderType,IsTop,ParentId,AttributeValue,RoleId}",
+                 "tede_collection.{Title,CollectionDataId,CollectionType,SourceUserId,Id as CollectionId,CreatedDate}"
+                );
+            result.Items = await _middleRepository.GetAllAsync<CollectionListPageResult>(query);
             return result;
         }
         /// <summary>
@@ -102,7 +145,6 @@ namespace GxPress.Repository.Implement.Collection
 
             if (request.VisitUserId > 0)
             {
-
                 sql =
                     $@"SELECT  a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=3 and DataSourceId=a.MiddleSonId) as EnjoyUser,(select count(1) from tede_middle 
                     where ParentId=a.Id) as FileCount, b.* FROM tede_middle a 
@@ -295,7 +337,7 @@ namespace GxPress.Repository.Implement.Collection
         public async Task<PagedList<CollectionNoFolderResult>> GetNewCollectionNoFolderPageListAsync(CollectionPageSearchRequest request)
         {
             var folderNameQuery = new SqlKata.Query("tede_middle");
-            folderNameQuery.WhereColumns("Id","=","a.ParentId");
+            folderNameQuery.WhereColumns("Id", "=", "a.ParentId");
             folderNameQuery.Select("FolderName");
             var result = new PagedList<CollectionNoFolderResult>();
             var collectionContsValue = GxPress.EnumConst.AllTypeConst.Collect.GetHashCode();
@@ -306,6 +348,8 @@ namespace GxPress.Repository.Implement.Collection
             query.Where("a.FolderType", collectionContsValue);
             query.Where("tede_collection.UserId", request.UserId);
             query.Where("a.IsDelete", false);
+            //条数
+            result.Total = await _repository.CountAsync(query);
             query.OrderByDesc("a.IsTop");
             query.OrderByDesc("a.LastModifiedDate");
             query.ForPage(request.Page, request.PerPage);
@@ -315,14 +359,6 @@ namespace GxPress.Repository.Implement.Collection
             );
             query.Select(folderNameQuery, "FolderName");
             result.Items = await _repository.GetAllAsync<CollectionNoFolderResult>(query);
-            query = new SqlKata.Query("tede_collection");
-            query.Join("tede_middle as a", "a.MiddleId", "tede_collection.Id");
-            if (!string.IsNullOrEmpty(request.SearchKey))
-                query.WhereLike("tede_collection.Title", $"%{request.SearchKey}%");
-            query.Where("a.FolderType", collectionContsValue);
-            query.Where("tede_collection.UserId", request.UserId);
-            query.Where("a.IsDelete", false);
-            result.Total = await _repository.CountAsync(query);
             return result;
         }
     }