123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- 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<Entity.Collection> _repository;
- private readonly Repository<Entity.Middle.Middle> _middleRepository;
- private readonly Repository<Entity.Analyze.Analyze> _analyzeRepository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- public CollectionRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Entity.Collection>(database);
- _middleRepository = new Repository<Entity.Middle.Middle>(database);
- _analyzeRepository = new Repository<Entity.Analyze.Analyze>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 添加收藏
- /// </summary>
- /// <param name="collection"></param>
- /// <returns></returns>
- public async Task<int> InsertAsync(Entity.Collection collection)
- {
- return await _repository.InsertAsync(collection);
- }
- /// <summary>
- /// 设置收藏置顶
- /// </summary>
- /// <param name="collection"></param>
- /// <returns></returns>
- public async Task<bool> SetTopAsync(int id)
- {
- var collction = await _repository.GetAsync(id);
- collction.IsTop = !collction.IsTop;
- return await _repository.UpdateAsync(collction);
- }
- public async Task<bool> UpdateAsync(Entity.Collection collection)
- {
- return await _repository.UpdateAsync(collection);
- }
- /// <summary>
- /// 查询结果
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<CollectionListPageResult>> PageListAsync(CollectionPageSearchRequest request)
- {
- 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
- {
- 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<CollectionListPageResult>(query);
- return result;
- }
- /// <summary>
- /// 根据ID获取收藏数据
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<Entity.Collection> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<Entity.Collection> GetAsync(SqlKata.Query query)
- {
- return await _repository.GetAsync(query);
- }
- public async Task<bool> DeleteAsync(Query query)
- {
- return await _repository.DeleteAsync(query) > 0;
- }
- /// <summary>
- /// 递归删除收藏包括文件夹下面的数据
- /// </summary>
- /// <param name="middleIds"></param>
- /// <returns></returns>
- public async Task<bool> RecursionDeleteAsync(List<int> middleIds)
- {
- foreach (var item in middleIds)
- {
- await DeleteParendIdAsync(item);
- }
- return true;
- }
- public async Task<bool> 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<bool> DeleteAsync(int id)
- {
- return await _repository.DeleteAsync(id);
- }
- public async Task<IEnumerable<Entity.Collection>> GetAllAsync(Query query)
- {
- return await _repository.GetAllAsync(query);
- }
- /// <summary>
- /// 收藏无文件夹分页列表
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<CollectionNoFolderResult>> GetCollectionNoFolderPageListAsync(CollectionPageSearchRequest request)
- {
- return await GetNewCollectionNoFolderPageListAsync(request);
- }
- /// <summary>
- /// 收藏无文件夹分页列表
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<CollectionNoFolderResult>> GetNewCollectionNoFolderPageListAsync(CollectionPageSearchRequest request)
- {
- var folderNameQuery = new SqlKata.Query("tede_middle");
- folderNameQuery.WhereColumns("Id", "=", "a.ParentId");
- folderNameQuery.Select("FolderName");
- var result = new PagedList<CollectionNoFolderResult>();
- 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<CollectionNoFolderResult>(query);
- return result;
- }
- }
- }
|