using System; using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Dapper; 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; using Microsoft.Extensions.Options; using SqlKata; namespace GxPress.Repository.Implement.Collection { public class CollectionRepository : ICollectionRepository { private readonly Repository _repository; private readonly Repository _middleRepository; private readonly Repository _analyzeRepository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypestr; public CollectionRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _middleRepository = new Repository(database); _analyzeRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; /// /// 添加收藏 /// /// /// public async Task InsertAsync(Entity.Collection collection) { return await _repository.InsertAsync(collection); } /// /// 设置收藏置顶 /// /// /// public async Task SetTopAsync(int id) { var collction = await _repository.GetAsync(id); collction.IsTop = !collction.IsTop; return await _repository.UpdateAsync(collction); } public async Task UpdateAsync(Entity.Collection collection) { return await _repository.UpdateAsync(collection); } /// /// 查询结果 /// /// /// public async Task> PageListAsync(CollectionPageSearchRequest request) { var result = new PagedList(); 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 { 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 { 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,ResultJson}" ); result.Items = await _middleRepository.GetAllAsync(query); return result; } /// /// 根据ID获取收藏数据 /// /// /// public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task GetAsync(SqlKata.Query query) { return await _repository.GetAsync(query); } public async Task DeleteAsync(Query query) { return await _repository.DeleteAsync(query) > 0; } /// /// 递归删除收藏包括文件夹下面的数据 /// /// /// public async Task RecursionDeleteAsync(List middleIds) { foreach (var item in middleIds) { await DeleteParendIdAsync(item); } return true; } public async Task DeleteParendIdAsync(int parentId) { var middle = await _middleRepository.GetAsync(parentId); if (middle == null) return true; //删除middle await _middleRepository.DeleteAsync(middle.Id); //删除coll await _repository.DeleteAsync(middle.MiddleId); //删除收藏 var query = new SqlKata.Query(); query.Where(nameof(Entity.Analyze.Analyze.SourceId), middle.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId); await _analyzeRepository.DeleteAsync(query); var middles = await _middleRepository.GetAllAsync(Q.Where(nameof(Entity.Middle.Middle.ParentId), parentId)); if (middles.Count == 0) return true; foreach (var item in middles) { //删除middle await _middleRepository.DeleteAsync(item.Id); //删除coll await _repository.DeleteAsync(item.MiddleId); //删除收藏 query = new SqlKata.Query(); query.Where(nameof(Entity.Analyze.Analyze.SourceId), item.MiddleId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.UserId), middle.UserId); await _analyzeRepository.DeleteAsync(query); await DeleteParendIdAsync(item.Id); } return true; } public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } public async Task> GetAllAsync(Query query) { return await _repository.GetAllAsync(query); } /// /// 收藏无文件夹分页列表 /// /// /// public async Task> GetCollectionNoFolderPageListAsync(CollectionPageSearchRequest request) { return await GetNewCollectionNoFolderPageListAsync(request); } /// /// 收藏无文件夹分页列表 /// /// /// public async Task> GetNewCollectionNoFolderPageListAsync(CollectionPageSearchRequest request) { var folderNameQuery = new SqlKata.Query("tede_middle"); folderNameQuery.WhereColumns("Id", "=", "a.ParentId"); folderNameQuery.Select("FolderName"); var result = new PagedList(); var collectionContsValue = GxPress.EnumConst.AllTypeConst.Collect.GetHashCode(); var 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); query.OrderByDesc("a.IsTop"); query.OrderByDesc("a.LastModifiedDate"); query.ForPage(request.Page, request.PerPage); query.Select( "tede_collection.{UserId,Title,CollectionType,CollectionDataId,SourceUserId,ResultJson}", "a.{IsTop,Id,MiddleId,ParentId as FolderId}" ); query.Select(folderNameQuery, "FolderName"); result.Items = await _repository.GetAllAsync(query); return result; } } }