123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Transactions;
- using AutoMapper;
- using Dapper;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Exceptions;
- using GxPress.Common.Page;
- using GxPress.Common.Tools;
- using GxPress.Entity;
- using GxPress.Entity.Middle;
- using GxPress.EnumConst;
- using GxPress.Repository.Interface;
- using GxPress.Request.Notice;
- using GxPress.Result.Notice;
- using Microsoft.Extensions.Options;
- using Datory;
- using SqlKata;
- using GxPress.Result.Job;
- using System.Linq;
- namespace GxPress.Repository.Implement
- {
- public class NoticeRepository : INoticeRepository
- {
- private readonly Repository<Notice> _repository;
- private readonly Repository<NoticeAddressee> _noticeAddresseeRepository;
- private readonly Repository<NoticeCc> _noticeCcRepository;
- private readonly Repository<User> _userRepository;
- private readonly Repository<NoticeAnalyze> _noticeAnalyzeRepository;
- private readonly Repository<Middle> _middleRepository;
- private readonly Repository<Entity.AdminVerify> _adminVerifyRepository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypeStr;
- public NoticeRepository(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<Notice>(database);
- _noticeAddresseeRepository = new Repository<NoticeAddressee>(database);
- _noticeCcRepository = new Repository<NoticeCc>(database);
- _userRepository = new Repository<User>(database);
- _noticeAnalyzeRepository = new Repository<NoticeAnalyze>(database);
- _middleRepository = new Repository<Middle>(database);
- _adminVerifyRepository = new Repository<Entity.AdminVerify>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<Notice> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<Notice> GetAsync(SqlKata.Query query)
- {
- return await _repository.GetAsync(query);
- }
- /// <summary>
- /// 添加通知
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<int> InsertAsync(NoticeInRequest request)
- {
- var noticeId = 0;
- try
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- var userEntity = await _userRepository.GetAsync(request.UserId);
- if (userEntity == null)
- throw new BusinessException("用户不存在");
- //通知
- var notice = new Notice
- {
- NoticeType = request.NoticeType.GetHashCode() == 0 ? NoticeTypeConst.Send : request.NoticeType,
- UserId = request.UserId,
- Content = request.Content,
- Title = request.Title,
- UserName = userEntity.Name,
- HtmlContent = request.HtmlContent,
- SourceId = request.SoureId,
- NoticeFolderId = request.FolderId,
- IsDelete = false,
- IsTop = true
- };
- noticeId = await _repository.InsertAsync(notice);
- transactionScope.Complete();
- }
- }
- catch (Exception e)
- {
- throw new BusinessException(e.Message);
- }
- return noticeId;
- }
- /// <summary>
- /// 根据用户ID查询
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<Notice>> GetNoticeByUserIdAsync(NoticePageSearchRequest request)
- {
- var query = Q.NewQuery();
- if (request.UserId > 0)
- {
- query.Where(nameof(NoticeAddressee.UserId), request.UserId);
- }
- if (request.SearchType == 0)
- {
- query.Where(nameof(NoticeAddressee.UserId), request.UserId);
- }
- if (request.SearchType == 1)
- {
- query.Where(nameof(NoticeAddressee.IsAdmin), true);
- }
- if (request.SearchType == 2)
- {
- query.Where(nameof(NoticeAddressee.NoticeAddresseeType), NoticeTypeConst.Draft);
- }
- if (request.SearchType == 3)
- {
- query.Where(nameof(NoticeAddressee.IsDelete), true);
- }
- if (!string.IsNullOrEmpty(request.Keyword))
- {
- var like = $"%{request.Keyword}%";
- query.WhereLike(nameof(Notice.Title), like);
- }
- var pagedList = new PagedList<Notice>
- {
- Total = await _repository.CountAsync(query)
- };
- var list = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
- pagedList.Items = list;
- return pagedList;
- }
- /// <summary>
- /// admin根据用户ID查询
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<NoticeListPageResult>> GetAdminNoticeByUserIdAsync(NoticePageSearchRequest request)
- {
- //用户收件人
- var noticeQuery = Q.NewQuery();
- if (request.SearchType == 2)
- {
- noticeQuery.Where(nameof(Notice.NoticeType), NoticeTypeConst.Draft);
- }
- noticeQuery.Where(nameof(Notice.IsDelete), request.SearchType == 3);
- if (!string.IsNullOrEmpty(request.Keyword))
- {
- noticeQuery.WhereLike(nameof(Notice.Title), $"%{request.Keyword}%");
- }
- var noticeList = await _repository.GetAllAsync(noticeQuery);
- var result = new PagedList<NoticeListPageResult>();
- result.Total = await _repository.CountAsync(noticeQuery);
- var notices = await _repository.GetAllAsync(noticeQuery.OrderByDesc(nameof(Notice.CreatedDate)).ForPage(request.Page, request.PerPage));
- var noticeListPageResults = new List<NoticeListPageResult>();
- foreach (var notice in notices)
- {
- var noticeListPageResultEntity = _mapper.Map<NoticeListPageResult>(notice);
- var user = await _userRepository.GetAsync(notice.UserId);
- if (user != null)
- {
- noticeListPageResultEntity.AvatarUrl = StringUtils.AddDomain(user.AvatarUrl);
- }
- //阅读数量
- noticeListPageResultEntity.ReadCount = await _noticeAddresseeRepository.CountAsync(
- Q.Where(nameof(NoticeAddressee.NoticeId), notice.Id)
- .Where(nameof(NoticeAddressee.IsRead), true).Where(nameof(NoticeAddressee.IsAdmin), false));
- //收件人数量
- noticeListPageResultEntity.AllCount = await _noticeAddresseeRepository.CountAsync(
- Q.Where(nameof(NoticeAddressee.NoticeId), notice.Id)
- .Where(nameof(NoticeAddressee.IsAdmin), false));
- noticeListPageResults.Add(noticeListPageResultEntity);
- }
- result.Items = noticeListPageResults;
- return result;
- }
- /// <summary>
- /// 管理员删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<bool> AdminDeleteAsync(int id)
- {
- try
- {
- var notice = await _repository.GetAsync(id);
- if (notice == null)
- throw new BusinessException("通知不存在");
- using (TransactionScope transactionScope = new TransactionScope())
- {
- //通知
- await _repository.DeleteAsync(id);
- //抄送
- await _noticeCcRepository.DeleteAsync(Q.Where(nameof(NoticeCc.NoticeId), id));
- //收件人
- await _noticeAddresseeRepository.DeleteAsync(Q.Where(nameof(NoticeAddressee.NoticeId), id));
- transactionScope.Complete();
- }
- }
- catch (Exception e)
- {
- throw new BusinessException(e.Message);
- }
- return true;
- }
- /// <summary>
- /// 自己删除
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> DeleteAsync(NoticeDeRequest request)
- {
- try
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- //var notice = await _repository.GetAsync(request.NoticeId);
- //if (notice == null)
- // throw new BusinessException("通知不存在");
- //notice.IsDelete = true;
- //await _repository.UpdateAsync(notice);
- //
- await _noticeAddresseeRepository.UpdateAsync(Q.Set(nameof(NoticeAddressee.IsDelete), true)
- .WhereIn(nameof(NoticeAddressee.Id), request.MiddleIds));
- transactionScope.Complete();
- }
- }
- catch (Exception e)
- {
- throw new BusinessException(e.Message);
- }
- return true;
- }
- /// <summary>
- /// 恢复删除通知
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> RecoverDeleteAsync(NoticeDeRequest request)
- {
- try
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- //
- await _noticeAddresseeRepository.UpdateAsync(Q.Set(nameof(NoticeAddressee.IsDelete), false)
- .Where(nameof(NoticeAddressee.Id), request.MiddleIds));
- transactionScope.Complete();
- }
- }
- catch (Exception e)
- {
- throw new BusinessException(e.Message);
- }
- return true;
- }
- /// <summary>
- /// 通知修改
- /// </summary>
- /// <param name="id"></param>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<bool> UpdateNoticeAsync(Notice notice)
- {
- return await _repository.UpdateAsync(notice);
- }
- /// <summary>
- /// 通知修改
- /// </summary>
- /// <param name="id"></param>
- /// <param name="request"></param>
- /// <returns></returns>
- 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<NoticeListPageResult>> GetNoticeListPageAsync(NoticePageSearchRequest request)
- {
- //用户收件人
- string sb = "";
- if (request.SearchType == 1)
- sb += " AND a.IsAdmin = 1 and a.NoticeAddresseeType=1 AND a.IsDelete = 0";
- else if (request.SearchType == 2)
- sb += " and a.NoticeAddresseeType=2 AND a.AttributeValue=1 AND a.IsDelete = 0";
- else if (request.SearchType == 3)
- sb += " AND a.IsDelete = 1 AND a.AttributeValue=1";
- else
- sb += " AND a.IsDelete = 0 AND a.NoticeAddresseeType != 2";
- if (!string.IsNullOrEmpty(request.Keyword))
- {
- if (request.NoticeFolderId > 0)
- sb += $@" AND a.ParentId ={request.NoticeFolderId} AND a.AttributeValue != 2 and (b.Title LIKE '%{request.Keyword}%'
- OR b.Content LIKE '%{request.Keyword}%'
- OR c.Name LIKE '%{request.Keyword}%'
- OR d.UserName LIKE '%{request.Keyword}%'
- OR a.CreatedDate LIKE '%{request.Keyword}%' or a.MiddleId in (SELECT
- g.NoticeId
- FROM
- tede_notice_addressee g
- INNER JOIN
- tede_user h ON g.UserId = h.Id
- WHERE
- g.NoticeId =a.MiddleId AND h.Name LIKE '%{request.Keyword}%'))";
- else
- sb += $@" AND a.AttributeValue != 2 AND (b.Title LIKE '%{request.Keyword}%'
- OR b.Content LIKE '%{request.Keyword}%'
- OR c.Name LIKE '%{request.Keyword}%'
- OR d.UserName LIKE '%{request.Keyword}%'
- OR a.CreatedDate LIKE '%{request.Keyword}%' or a.MiddleId in (SELECT
- g.NoticeId
- FROM
- tede_notice_addressee g
- INNER JOIN
- tede_user h ON g.UserId = h.Id
- WHERE
- g.NoticeId =a.MiddleId AND h.Name LIKE '%{request.Keyword}%'))";
- }
- else
- {
- var searchList = new List<int> { 1, 2, 3 };
- if (!searchList.Contains(request.SearchType))
- sb += " AND a.ParentId = " + request.NoticeFolderId;
- }
- var sql = $@"SELECT a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=10 and DataSourceId=a.MiddleId) as EnjoyUser,(SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- TypeValue = 2
- AND SourceId = a.MiddleId
- AND AnalyzeType = 1) AS PraiseCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 2
- AND SourceId = a.MiddleId
- AND AnalyzeType = 1
- LIMIT 0 , 1) AS IsLaud,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_comment
- WHERE
- ArticleId = a.MiddleId and pid=0
- AND TypeValue = 2) AS CommentCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 2
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4) AS RetransmissionCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_analyze
- WHERE
- UserId = {request.UserId} AND TypeValue = 2
- 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 = 2
- 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 = 2
- AND SourceId = a.MiddleId
- AND AnalyzeType = 4) AS CollectCount,
- (SELECT
- COUNT(1)
- FROM
- ccpph.tede_middle
- WHERE
- ParentId = a.Id and IsDelete=0 and NoticeAddresseeType<>2) as FileCount,(SELECT
- COUNT(1)
- FROM
- ccpph.tede_notice_addressee
- WHERE
- NoticeId = a.MiddleId AND IsRead = 1) as ReadCount,(SELECT
- COUNT(1)
- FROM
- ccpph.tede_notice_addressee
- WHERE
- NoticeId = a.MiddleId ) as AllCount,(SELECT
- COUNT(1)
- FROM
- ccpph.tede_notice_addressee
- WHERE
- NoticeId = a.MiddleId AND IsRead = 1 and UserId={request.UserId} limit 0,1) as IsRead, b.*, c.Name, c.AvatarUrl, d.*
- FROM
- ccpph.tede_middle a
- left JOIN
- ccpph.tede_notice b ON a.MiddleId = b.Id
- left JOIN
- ccpph.tede_user c ON b.UserId = c.Id
- left JOIN
- ccpph.tede_notice_addressee d ON d.Id = a.MiddleSonId
- WHERE
- a.UserId = {request.UserId} and a.FolderType=1
- {sb}
- ORDER BY a.IsTop desc,a.LastModifiedDate desc,a.AttributeValue desc
- LIMIT {(request.Page - 1) * request.PerPage},{request.PerPage}";
- var countSql = string.Format(@"SELECT
- count(1)
- FROM
- ccpph.tede_middle a
- left JOIN
- ccpph.tede_notice b ON a.MiddleId = b.Id
- left JOIN
- ccpph.tede_user c ON b.UserId = c.Id
- left JOIN
- ccpph.tede_notice_addressee d ON d.Id = a.MiddleSonId
- WHERE
- a.UserId = {1} and a.FolderType=1
- {0}", sb, request.UserId);
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var items = await connection
- .QueryAsync<NoticeListPageResult, Notice, User, NoticeAddressee, NoticeListPageResult>(sql,
- (noticeListPageResult, notice, user, noticeAddressee) =>
- {
- noticeListPageResult.Title = notice != null ? notice.Title : "";
- noticeListPageResult.Content = notice != null ? notice.Content : "[]";
- noticeListPageResult.UserName = user != null ? user.Name : "";
- noticeListPageResult.SourceId = notice != null ? notice.SourceId : 0;
- noticeListPageResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
- noticeListPageResult.NoticeAddresseeId = noticeAddressee?.Id ?? 0;
- noticeListPageResult.NoticeId = noticeAddressee?.NoticeId ?? 0;
- return noticeListPageResult;
- },
- splitOn: "Id,Name,Id");
- foreach (var item in items)
- {
- if (item.AttributeValue == 3)
- item.UReadCount = await _adminVerifyRepository.CountAsync(Q.Where(nameof(Entity.AdminVerify.AdminId), request.UserId).Where(nameof(Entity.AdminVerify.DisposeType), AdminVerifyTypeConst.UDispose.GetHashCode()));
- }
- PagedList<NoticeListPageResult> result = new PagedList<NoticeListPageResult>
- {
- Total = await CountAsync(countSql),
- Items = items
- };
- return result;
- }
- public async Task<int> CountAsync(string sql)
- {
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- return await connection.ExecuteScalarAsync<int>(sql);
- }
- public async Task<IEnumerable<Notice>> GetAllAsync()
- {
- return await _repository.GetAllAsync();
- }
- public async Task<bool> DeleteAsync(Query query)
- {
- return await _repository.DeleteAsync(query) > 0;
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public async Task<List<JobNoticeResult>> ElasticSearchNotice()
- {
- var sql = "select a.Id,a.UserId,a.GuId,a.CreatedDate,b.Id,b.Title,b.Content,b.CreatedDate,c.Name,c.AvatarUrl from ccpph.tede_middle a inner join ccpph.tede_notice b on a.MiddleId=b.Id inner join ccpph.tede_user c on c.Id=b.UserId where a.FolderType=1 and a.IsUpload=0 order by a.CreatedDate desc limit 0,100";
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var items = await connection
- .QueryAsync<JobNoticeResult, Notice, User, JobNoticeResult>(sql,
- (jobNoticeResult, notice, user) =>
- {
- jobNoticeResult.MiddleId = jobNoticeResult.Id;
- jobNoticeResult.Id = notice.Id;
- jobNoticeResult.Title = notice != null ? notice.Title : "";
- jobNoticeResult.Content = notice != null ? notice.Content : "";
- jobNoticeResult.AvatarUrl = user != null ? user.AvatarUrl : "";
- jobNoticeResult.Name = user != null ? user.Name : "";
- return jobNoticeResult;
- },
- splitOn: "Id,Id,Name");
- return items.ToList();
- }
- }
- }
|