123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Entity;
- using Microsoft.Extensions.Options;
- using Datory;
- using GxPress.Request.Notice;
- using SqlKata;
- using GxPress.Result.Notice;
- using Dapper;
- using System;
- using GxPress.Repository.Interface;
- using GxPress.Result.App.Missive;
- using GxPress.Request.App.Missive;
- namespace GxPress.Repository.Implement
- {
- public class NoticeAddresseeRepository : IAddresseeRepository
- {
- private readonly Repository<Addressee> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypeStr;
- public NoticeAddresseeRepository(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<Addressee>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 根据通知ID获取收件人
- /// </summary>
- /// <param name="noticeId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<Addressee>> GetNoticeAddresseeByNoticeIdAsync(int noticeId)
- {
- return await _repository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), noticeId));
- }
- public async Task<bool> DeleteAsync(int noticeId, int sourceType, int isAdmin)
- {
- var query = new SqlKata.Query();
- if (isAdmin == 0)
- query.Where(nameof(Addressee.IsAdmin), false);
- if (isAdmin == 1)
- query.Where(nameof(Addressee.IsAdmin), true);
- query.Where(nameof(Addressee.SourceId), noticeId);
- query.Where(nameof(Addressee.SourceType), sourceType);
- return await _repository.DeleteAsync(query) > 0;
- }
- public async Task<int> InsertAsync(Addressee noticeAddressee)
- {
- return await _repository.InsertAsync(noticeAddressee);
- }
- /// <summary>
- /// 插入收件人
- /// </summary>
- /// <param name="noticeAddressees"></param>
- /// <returns></returns>
- public async Task<bool> InsertAsync(List<Addressee> noticeAddressees)
- {
- if (noticeAddressees.Count == 0)
- return false;
- var sql = "INSERT INTO `tede_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`SourceId`,`SourceType`,`UserId`,`UserName`,`IsRead`,`NoticeAddresseeType`,`IsTop`,`IsAdmin`,`IsDelete`)VALUES";
- foreach (var item in noticeAddressees)
- {
- var guId = Guid.NewGuid().ToString();
- var createdDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- var isRead = item.IsRead == true ? 1 : 0;
- var isTop = item.IsTop == true ? 1 : 0;
- var isAdmin = item.IsAdmin == true ? 1 : 0;
- var isDelete = item.IsDelete == true ? 1 : 0;
- sql += $"('{guId}','{createdDate}','{createdDate}',{item.SourceId},{item.SourceType},{item.UserId},'',{isRead},{item.NoticeAddresseeType.GetHashCode()},{isTop},{isAdmin},{isDelete}),";
- }
- sql = sql.Remove(sql.Length - 1, 1);
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var items = await connection.ExecuteAsync(sql);
- return items > 0;
- }
- /// <summary>
- /// 查询通知的未读已读
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<IEnumerable<NoticeReadListResult>> GetNoticeAddresseeByUserIdIdAsync(NoticeReadListRequest request)
- {
- var query = new SqlKata.Query("tede_addressee");
- query.Join("tede_user as b", "b.Id", "tede_addressee.UserId");
- query.Where("tede_addressee.SourceId", request.NoticeId);
- if (!string.IsNullOrEmpty(request.Key))
- query.WhereLike("b.Name", $"%{request.Key}%");
- if (request.ReadType > 0)
- query.Where("tede_addressee.IsRead", request.ReadType == 1);
- query.OrderByDesc("tede_addressee.LastModifiedDate");
- var departmentNameQuery = new SqlKata.Query("tede_department");
- var departmentIdsQuery = new SqlKata.Query("tede_department_user");
- departmentIdsQuery.WhereColumns("tede_department_user.userId", "=", "tede_addressee.UserId");
- departmentIdsQuery.Select("DepartmentId");
- departmentNameQuery.WhereIn(nameof(Entity.Department.Id), departmentIdsQuery);
- departmentNameQuery.Limit(1);
- departmentNameQuery.Select("Name");
- query.Select(departmentNameQuery, "DepartmentName");
- query.Select("tede_addressee.{SourceId as NoticeId,LastModifiedDate,UserId}");
- query.Select("b.{AvatarUrl,Name}");
- var result = await _repository.GetAllAsync<NoticeReadListResult>(query);
- foreach (var item in result)
- item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
- return result;
- }
- /// <summary>
- /// 获取收件人
- /// </summary>
- /// <param name="noticeId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<User>> GetUserByNoticeIdAsync(int noticeId)
- {
- var sql = $"SELECT b.* FROM tede_addressee a inner join tede_user b on a.UserId=b.Id where a.SourceId={noticeId} and a.IsAdmin=0;";
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var items = await connection
- .QueryAsync<User>(sql);
- return items;
- }
- public async Task<int> CountAsync(Query query)
- {
- return await _repository.CountAsync(query);
- }
- public async Task<IEnumerable<Addressee>> GetAllAsync(Query query)
- {
- return await _repository.GetAllAsync(query);
- }
- public async Task<bool> UpdateAsync(Query query)
- {
- return await _repository.UpdateAsync(query) > 0;
- }
- public async Task<bool> ExistsAsync(Query query)
- {
- return await _repository.ExistsAsync(query);
- }
- /// <summary>
- /// 站内信未读
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<IEnumerable<MissiveReadListResult>> GetMissiveReadListAsync(MissiveReadListRequest request)
- {
- var query = new SqlKata.Query("tede_addressee");
- query.Join("tede_user as b", "b.Id", "tede_addressee.UserId");
- query.Where("tede_addressee.SourceId", request.MissiveId);
- if (!string.IsNullOrEmpty(request.Key))
- query.WhereLike("b.Name", $"%{request.Key}%");
- if (request.ReadType > 0)
- query.Where("tede_addressee.IsRead", request.ReadType == 1);
- query.OrderByDesc("tede_addressee.LastModifiedDate");
- var departmentNameQuery = new SqlKata.Query("tede_department");
- var departmentIdsQuery = new SqlKata.Query("tede_department_user");
- departmentIdsQuery.WhereColumns("tede_department_user.userId", "=", "tede_addressee.UserId");
- departmentIdsQuery.Select("DepartmentId");
- departmentNameQuery.WhereIn(nameof(Entity.Department.Id), departmentIdsQuery);
- departmentNameQuery.Limit(1);
- departmentNameQuery.Select("Name");
- query.Select(departmentNameQuery, "DepartmentName");
- query.Select("tede_addressee.{SourceId as MissiveId,LastModifiedDate,UserId}");
- query.Select("b.{AvatarUrl,Name}");
- var result = await _repository.GetAllAsync<MissiveReadListResult>(query);
- foreach (var item in result)
- item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
- return result;
- }
- }
- }
|