123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Entity;
- using GxPress.Repository.Interface;
- using Microsoft.Extensions.Options;
- using Datory;
- using GxPress.Request.Notice;
- using SqlKata;
- using GxPress.Result.Notice;
- using Dapper;
- using System;
- namespace GxPress.Repository.Implement
- {
- public class NoticeAddresseeRepository : INoticeAddresseeRepository
- {
- private readonly Repository<NoticeAddressee> _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<NoticeAddressee>(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<NoticeAddressee>> GetNoticeAddresseeByNoticeIdAsync(int noticeId)
- {
- return await _repository.GetAllAsync(Q.Where(nameof(NoticeAddressee.NoticeId), noticeId));
- }
- public async Task<bool> DeleteAsync(int noticeId)
- {
- return await _repository.DeleteAsync(Q.Where(nameof(NoticeAddressee.NoticeId), noticeId)) > 0;
- }
- public async Task<int> InsertAsync(NoticeAddressee noticeAddressee)
- {
- return await _repository.InsertAsync(noticeAddressee);
- }
- /// <summary>
- /// 插入收件人
- /// </summary>
- /// <param name="noticeAddressees"></param>
- /// <returns></returns>
- public async Task<bool> InsertAsync(List<NoticeAddressee> noticeAddressees)
- {
- if (noticeAddressees.Count == 0)
- return false;
- var sql = "INSERT INTO `ccpph`.`tede_notice_addressee`(`Guid`,`CreatedDate`,`LastModifiedDate`,`NoticeId`,`UserId`,`UserName`,`IsRead`,`NoticeAddresseeType`,`NoticeFolderId`,`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.NoticeId},{item.UserId},'',{isRead},{item.NoticeAddresseeType.GetHashCode()},{item.NoticeFolderId},{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.ExecuteScalarAsync<int>(sql);
- return items > 0;
- }
- /// <summary>
- /// 查询通知的未读已读
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<IEnumerable<NoticeReadListResult>> GetNoticeAddresseeByUserIdIdAsync(NoticeReadListRequest request)
- {
- var sqlValue = "";
- if (request.ReadType == 1)
- sqlValue += " and a.IsRead=1";
- else if (request.ReadType == 2)
- sqlValue += " and a.IsRead=0";
- if (!string.IsNullOrWhiteSpace(request.Key))
- sqlValue += $" and b.Name like '%{request.Key}%'";
- var sql = $"SELECT a.*,b.Name,b.AvatarUrl,c.Name FROM ccpph.tede_notice_addressee a inner join ccpph.tede_user b on a.UserId=b.Id inner join ccpph.tede_department c on c.Id=b.DepartmentId where a.NoticeId={request.NoticeId} {sqlValue} order by a.LastModifiedDate desc";
- var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var items = await connection
- .QueryAsync<NoticeReadListResult, User, Department, NoticeReadListResult>(sql,
- (noticeReadListResult, user, department) =>
- {
- noticeReadListResult.Name = user != null ? user.Name : "";
- noticeReadListResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
- noticeReadListResult.DepartmentName = department.Name;
- noticeReadListResult.LastModifiedDate = Convert.ToDateTime(noticeReadListResult.LastModifiedDate).ToString("yyyy-MM-dd HH:mm:ss");
- return noticeReadListResult;
- },
- splitOn: "Name,Name");
- return items;
- }
- /// <summary>
- /// 获取收件人
- /// </summary>
- /// <param name="noticeId"></param>
- /// <returns></returns>
- public async Task<IEnumerable<User>> GetUserByNoticeIdAsync(int noticeId)
- {
- var sql = $"SELECT b.* FROM ccpph.tede_notice_addressee a inner join ccpph.tede_user b on a.UserId=b.Id where a.NoticeId={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<NoticeAddressee>> 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);
- }
- }
- }
|