123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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.Entity;
- using GxPress.Repository.Interface.Note;
- using GxPress.Request.App.Note;
- using GxPress.Result.App.Note;
- using Microsoft.Extensions.Options;
- namespace GxPress.Repository.Implement.Note
- {
- public class NoteRepository : INoteRepository
- {
- private readonly Repository<Entity.Note.Note> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- public NoteRepository(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.Note.Note>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<Entity.Note.Note> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<bool> DeleteAsync(SqlKata.Query query)
- {
- return await _repository.DeleteAsync(query) > 0;
- }
- public async Task<int> InsertAsync(Entity.Note.Note note)
- {
- return await _repository.InsertAsync(note);
- }
- public async Task<bool> UpdateAsync(Entity.Note.Note note)
- {
- return await _repository.UpdateAsync(note);
- }
- public async Task<bool> UpdateAsync(SqlKata.Query query)
- {
- return await _repository.UpdateAsync(query) > 0;
- }
- /// <summary>
- /// 获取笔记分页
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<NotePageListRequest>> NotePageListAsync(NoteSearchPageListRequest request)
- {
- var pagedList = new PagedList<NotePageListRequest>();
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var result =
- await connection
- .QueryAsync<NotePageListRequest, Entity.Note.Note, User, Department, NotePageListRequest>(GetCollectionSql(request),
- (notePageListRequest, note, user, department) =>
- {
- notePageListRequest.Title = note == null ? "" : note.Title;
- notePageListRequest.Content = note == null ? "" : note.Content;
- notePageListRequest.Name = user == null ? "" : user.Name;
- notePageListRequest.AvatarUrl = user == null ? "" : StringUtils.AddDomainMin(user.AvatarUrl);
- notePageListRequest.DepartmentName = department == null ? "" : department.Name;
- notePageListRequest.ReadCount = note == null ? 0 : note.ReadCount;
- notePageListRequest.NickName = user == null ? "" : user.Nick;
- return notePageListRequest;
- }, splitOn: "Title,Name,Name");
- pagedList.Items = result;
- pagedList.Total = await NotePageListCountAsync(GetCollectionSqlCount(request));
- return pagedList;
- }
- /// <summary>
- /// 集合
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public string GetCollectionSql(NoteSearchPageListRequest request)
- {
- string sql = "";
- if (request.VisitUserId > 0)
- {
- sql =
- $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=4 and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 1) AS PraiseCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 1
- LIMIT 0 , 1) AS IsPraise,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_comment
- WHERE
- ArticleId = a.MiddleId and pid=0
- AND TypeValue = 3) AS CommentCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4) AS RetransmissionCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4
- LIMIT 0 , 1) AS IsRetransmission,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4
- LIMIT 0 , 1) AS IsCollect,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4) AS CollectCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_middle
- WHERE
- ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,c.Name,c.AvatarUrl,c.Nick,d.Name FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id left join ccpph.tede_user c on c.Id=b.UserId left join ccpph.tede_department d on d.Id=c.DepartmentId
- WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 4 AND a.IsDelete = 0";
- if (request.FolderId == 0)
- sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
- 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 ccpph.tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 4 AND a.UserId = {request.VisitUserId}";
- 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}%'))";
- else
- sql += $" and a.ParentId={request.FolderId}";
- sql +=
- $@" ORDER BY a.IsTop DESC,a.AttributeValue desc, a.LastModifiedDate DESC";
- if (request.Page > 0)
- sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
- }
- else
- {
- sql =
- $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=4 and DataSourceId=a.MiddleSonId) as EnjoyUser,(SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 1) AS PraiseCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 1
- LIMIT 0 , 1) AS IsPraise,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_comment
- WHERE
- UserId = {request.UserId} AND ArticleId = a.MiddleId
- AND TypeValue = 3) AS CommentCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4) AS RetransmissionCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4
- LIMIT 0 , 1) AS IsRetransmission,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4
- LIMIT 0 , 1) AS IsCollect,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 3
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4) AS IsCollect,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_middle
- WHERE
- ParentId = a.Id and IsDelete=0) FileCount, b.Title, b.Content,b.ReadCount,c.Name,c.AvatarUrl,d.Name FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id left join ccpph.tede_user c on c.Id=b.UserId left join ccpph.tede_department d on d.Id=c.DepartmentId WHERE a.UserId = {request.UserId} AND a.FolderType = 4 AND a.IsDelete = 0";
- 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}%')";
- else
- sql += $" and a.ParentId={request.FolderId}";
- sql +=
- $@" ORDER BY a.IsTop DESC,a.Sort DESC ,a.AttributeValue desc , a.LastModifiedDate DESC";
- if (request.Page > 0)
- sql += $@" limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
- }
- return sql;
- }
- /// <summary>
- /// 条数
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public string GetCollectionSqlCount(NoteSearchPageListRequest request)
- {
- string sql = "";
- if (request.VisitUserId > 0)
- {
- sql =
- $@"SELECT count(1) FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id WHERE a.UserId = {request.VisitUserId} AND a.FolderType = 4 AND a.IsDelete = 0";
- if (request.FolderId == 0)
- sql += @" AND(a.RoleId = 1 OR a.RoleId = 3) AND a.ParentId = 0";
- 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 ccpph.tede_folder_user WHERE UserId = {request.UserId}) AND a.FolderType = 4 AND a.UserId = {request.VisitUserId}";
- 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}%'))";
- else
- sql += $" and a.ParentId={request.FolderId}";
- }
- else
- {
- sql =
- $@"SELECT count(1) FROM ccpph.tede_middle a LEFT JOIN ccpph.tede_note b ON a.MiddleId = b.Id left join ccpph.tede_user c on c.Id=b.UserId left join ccpph.tede_department d on d.Id=c.DepartmentId WHERE a.UserId = {request.UserId} AND a.FolderType = 4 AND a.IsDelete = 0";
- 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}%'))";
- else
- sql += $" and a.ParentId={request.FolderId}";
- }
- return sql;
- }
- public async Task<int> NotePageListCountAsync(string sql)
- {
- var pagedList = new PagedList<NotePageListRequest>();
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var result =
- await connection
- .ExecuteScalarAsync<int>(sql);
- return result;
- }
- /// <summary>
- /// 删除笔记
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(int id) => await _repository.DeleteAsync(id);
- }
- }
|